forked from mergeos-bounties/Loru
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.py
More file actions
44 lines (38 loc) · 1.33 KB
/
Copy pathstream.py
File metadata and controls
44 lines (38 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Continuous multi-gloss stream stub (phrase building)."""
from __future__ import annotations
from loru.infer.text import gloss_to_sentence, multi_gloss_to_sentence
from loru.models.vocab import DEFAULT_GLOSS
def stream_glosses(glosses: list[str]) -> dict:
"""
Accept a sequence of gloss tokens and emit progressive sentence states.
This is a scaffold for a future continuous recognizer that emits partial
glosses over time (e.g. webcam frames). Offline and deterministic.
"""
cleaned: list[str] = []
for g in glosses:
key = str(g).strip().lower().replace(" ", "_")
if not key:
continue
if key not in DEFAULT_GLOSS:
# allow unknown tokens as raw words for streaming demos
cleaned.append(key)
else:
cleaned.append(key)
partials: list[dict] = []
acc: list[str] = []
for g in cleaned:
acc.append(g)
partials.append(
{
"glosses": list(acc),
"sentence": multi_gloss_to_sentence(acc),
"last_gloss_sentence": gloss_to_sentence(g),
}
)
return {
"ok": True,
"n": len(cleaned),
"final_sentence": multi_gloss_to_sentence(cleaned) if cleaned else "",
"stream": partials,
"mode": "continuous-gloss-stub",
}