Bildad commited on
Commit
366d921
1 Parent(s): 9747913

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -14,24 +14,32 @@ class UnifiedTranslator:
14
  self.phrase_mapping = phrase_mapping
15
 
16
  def translate(self, text):
17
- # Normalize text to lowercase for case-insensitive matching
18
- text_lower = text.lower()
19
 
20
  # Debugging output
21
  print(f"Input text: {text_lower}")
22
 
 
 
 
 
23
  # Check if the text matches any pattern in the phrase_mapping
24
  for pattern, translation in self.phrase_mapping.items():
25
  try:
26
  # Use regex to match the pattern with placeholders
 
27
  pattern_regex = re.compile(
28
  re.escape(pattern).replace(r"\{name\}", r"([\w'-]+)").strip(),
29
  re.IGNORECASE
30
  )
31
- match = pattern_regex.match(text_lower)
32
  if match:
33
- # Replace the placeholder with the actual value
34
- return translation.format(name=match.group(1))
 
 
 
35
  except re.error as e:
36
  print(f"Regex error with pattern {pattern}: {e}")
37
 
 
14
  self.phrase_mapping = phrase_mapping
15
 
16
  def translate(self, text):
17
+ # Normalize text to lowercase and strip extra spaces
18
+ text_lower = text.lower().strip()
19
 
20
  # Debugging output
21
  print(f"Input text: {text_lower}")
22
 
23
+ # Handle specific case for 'sasa' based on its position in the sentence
24
+ if text_lower.startswith("sasa"):
25
+ return self.phrase_mapping.get("Sasa", "Translation not found")
26
+
27
  # Check if the text matches any pattern in the phrase_mapping
28
  for pattern, translation in self.phrase_mapping.items():
29
  try:
30
  # Use regex to match the pattern with placeholders
31
+ # Make sure pattern is properly escaped and case insensitive
32
  pattern_regex = re.compile(
33
  re.escape(pattern).replace(r"\{name\}", r"([\w'-]+)").strip(),
34
  re.IGNORECASE
35
  )
36
+ match = pattern_regex.fullmatch(text_lower)
37
  if match:
38
+ # Replace the placeholder with the actual value if needed
39
+ if '{name}' in pattern:
40
+ return translation.format(name=match.group(1))
41
+ else:
42
+ return translation
43
  except re.error as e:
44
  print(f"Regex error with pattern {pattern}: {e}")
45