forked from mergeos-bounties/Loru
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.py
More file actions
111 lines (100 loc) · 3.02 KB
/
Copy pathtext.py
File metadata and controls
111 lines (100 loc) · 3.02 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from __future__ import annotations
from pathlib import Path
from loru.data.loader import list_sample_files, load_sequence
from loru.models.toy import ToySignClassifier
TEMPLATES = {
"hello": "Hello!",
"thanks": "Thank you.",
"yes": "Yes.",
"no": "No.",
"help": "I need help.",
"please": "Please.",
"love": "I love this.",
"name": "What is your name?",
"water": "I want water.",
"good": "That is good.",
"goodbye": "Goodbye!",
"sorry": "I am sorry.",
"stop": "Please stop.",
"want": "I want that.",
"need": "I need this.",
"happy": "I am happy.",
"sad": "I feel sad.",
"mother": "Mother.",
"father": "Father.",
"friend": "This is my friend.",
"eat_food": "I want to eat.",
"drink": "I want a drink.",
"home": "I am going home.",
"school": "I am at school.",
"go": "Let's go.",
"come": "Please come here.",
"see": "I see it.",
"know": "I know.",
"big": "It is big.",
"small": "It is small.",
"welcome": "Welcome!",
"maybe": "Maybe.",
"wait": "Please wait.",
"today": "Today.",
"understand": "I understand.",
"again": "Again.",
"more": "I want more.",
"finish": "I am finished.",
"what": "What?",
"where": "Where?",
"how": "How?",
"why": "Why?",
"fingerspell_z": "Z.",
"fingerspell_y": "Y.",
"fingerspell_x": "X.",
"fingerspell_w": "W.",
"fingerspell_v": "V.",
"fingerspell_u": "U.",
"fingerspell_t": "T.",
"fingerspell_s": "S.",
"fingerspell_r": "R.",
"fingerspell_q": "Q.",
"fingerspell_p": "P.",
"fingerspell_o": "O.",
"fingerspell_n": "N.",
"fingerspell_m": "M.",
"fingerspell_l": "L.",
"fingerspell_k": "K.",
"fingerspell_j": "J.",
"fingerspell_i": "I.",
"fingerspell_h": "H.",
"fingerspell_g": "G.",
"fingerspell_f": "F.",
"fingerspell_e": "E.",
"fingerspell_d": "D.",
"fingerspell_c": "C.",
"fingerspell_b": "B.",
"fingerspell_a": "A.",
}
def build_demo_classifier() -> ToySignClassifier:
return ToySignClassifier.from_samples(list_sample_files())
def sign_to_text(sequence_path: Path, classifier: ToySignClassifier | None = None) -> dict:
model = classifier or build_demo_classifier()
gloss_true, frames = load_sequence(sequence_path)
pred, confidence = model.predict(frames)
text = gloss_to_sentence(pred)
return {
"true_gloss": gloss_true,
"predicted_gloss": pred,
"confidence": round(confidence, 4),
"text": text,
"source": str(sequence_path),
}
def gloss_to_sentence(gloss: str) -> str:
key = gloss.lower().strip()
if key in TEMPLATES:
return TEMPLATES[key]
return key.replace("_", " ").strip().capitalize() + "."
def multi_gloss_to_sentence(glosses: list[str]) -> str:
parts = [gloss_to_sentence(g).rstrip(".") for g in glosses if g.strip()]
if not parts:
return ""
if len(parts) == 1:
return parts[0] + ("." if not parts[0].endswith("!") else "")
return " ".join(parts) + "."