Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,45 +14,45 @@ class UnifiedTranslator:
|
|
14 |
self.phrase_mapping = phrase_mapping
|
15 |
|
16 |
def is_name(self, word):
|
17 |
-
# A
|
|
|
18 |
return word[0].isupper()
|
19 |
|
20 |
def translate(self, text):
|
21 |
-
# Normalize text
|
22 |
-
|
23 |
-
|
24 |
# Debugging output
|
25 |
-
print(f"Input text: {
|
26 |
|
27 |
# Check if the text matches any pattern in the phrase_mapping (English to Swahili)
|
28 |
for pattern, translation in self.phrase_mapping.items():
|
29 |
try:
|
30 |
# Use regex to match the pattern with placeholders
|
31 |
pattern_regex = re.compile(
|
32 |
-
re.escape(pattern).replace(r"\{name\}", r"([\w'-]+)").strip(),
|
33 |
re.IGNORECASE
|
34 |
)
|
35 |
print(f"Checking pattern: {pattern}")
|
36 |
-
match = pattern_regex.fullmatch(
|
37 |
if match:
|
38 |
print(f"Match found: {match.group(0)}")
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
# If the matched word isn't a name, use it directly in the translation
|
46 |
-
return translation.replace('{name}', name)
|
47 |
else:
|
48 |
-
|
|
|
49 |
except re.error as e:
|
50 |
print(f"Regex error with pattern {pattern}: {e}")
|
51 |
|
52 |
# Fallback to model translation if no pattern matches
|
53 |
try:
|
54 |
-
print(f"Fallback to model translation for text: {
|
55 |
-
translation = self.model(
|
56 |
return translation['translation_text']
|
57 |
except Exception as e:
|
58 |
print(f"Model translation error: {e}")
|
|
|
14 |
self.phrase_mapping = phrase_mapping
|
15 |
|
16 |
def is_name(self, word):
|
17 |
+
# A heuristic: a word is considered a name if it starts with an uppercase letter
|
18 |
+
# and is not one of the common lowercase words
|
19 |
return word[0].isupper()
|
20 |
|
21 |
def translate(self, text):
|
22 |
+
# Normalize text and strip extra spaces
|
23 |
+
text_clean = text.strip()
|
24 |
+
|
25 |
# Debugging output
|
26 |
+
print(f"Input text: {text_clean}")
|
27 |
|
28 |
# Check if the text matches any pattern in the phrase_mapping (English to Swahili)
|
29 |
for pattern, translation in self.phrase_mapping.items():
|
30 |
try:
|
31 |
# Use regex to match the pattern with placeholders
|
32 |
pattern_regex = re.compile(
|
33 |
+
re.escape(pattern).replace(r"\{name\}", r"([\w'-]+)").strip(),
|
34 |
re.IGNORECASE
|
35 |
)
|
36 |
print(f"Checking pattern: {pattern}")
|
37 |
+
match = pattern_regex.fullmatch(text_clean)
|
38 |
if match:
|
39 |
print(f"Match found: {match.group(0)}")
|
40 |
+
matched_word = match.group(1)
|
41 |
+
|
42 |
+
# Check if the matched word is a name
|
43 |
+
if '{name}' in pattern and self.is_name(matched_word):
|
44 |
+
# If it's a name, replace the placeholder with the actual value
|
45 |
+
return translation.format(name=matched_word)
|
|
|
|
|
46 |
else:
|
47 |
+
# If it's not a name, replace the placeholder with the matched word directly
|
48 |
+
return translation.replace('{name}', matched_word)
|
49 |
except re.error as e:
|
50 |
print(f"Regex error with pattern {pattern}: {e}")
|
51 |
|
52 |
# Fallback to model translation if no pattern matches
|
53 |
try:
|
54 |
+
print(f"Fallback to model translation for text: {text_clean}")
|
55 |
+
translation = self.model(text_clean)[0]
|
56 |
return translation['translation_text']
|
57 |
except Exception as e:
|
58 |
print(f"Model translation error: {e}")
|