Yurii Paniv commited on
Commit
e5a3778
1 Parent(s): f99f689

Improve number handling in complex words

Browse files
Files changed (1) hide show
  1. formatter.py +19 -12
formatter.py CHANGED
@@ -15,15 +15,20 @@ def preprocess_text(text, autostress=False):
15
 
16
  def detect_num_and_convert(word):
17
  numbers = "0123456789,."
18
- is_number = all(map(lambda x: x in numbers, word))
19
- if is_number:
20
- try:
21
- return num2words.num2words(word, lang="uk")
22
- except:
23
- return word
24
- else:
25
- return word
 
 
 
 
26
 
 
27
  text = " ".join([detect_num_and_convert(word) for word in text.split(" ")])
28
 
29
  # fallback numbers
@@ -78,7 +83,9 @@ def preprocess_text(text, autostress=False):
78
 
79
 
80
  if __name__ == "__main__":
81
- print(preprocess_text("Quality of life update"))
82
- print(preprocess_text("Він украв 20000000 $"))
83
- print(preprocess_text("111 000 000 000 доларів державного боргу."))
84
- print(preprocess_text("11100000001 доларів державного боргу."))
 
 
15
 
16
  def detect_num_and_convert(word):
17
  numbers = "0123456789,."
18
+ result = []
19
+ parts = word.split("-") # for handling complex words
20
+ for part in parts:
21
+ is_number = all(map(lambda x: x in numbers, part))
22
+ if is_number:
23
+ try:
24
+ result.append(num2words.num2words(part, lang="uk"))
25
+ except:
26
+ result.append(part)
27
+ else:
28
+ result.append(part)
29
+ return "-".join(result)
30
 
31
+ #print([detect_num_and_convert(word) for word in text.split(" ")])
32
  text = " ".join([detect_num_and_convert(word) for word in text.split(" ")])
33
 
34
  # fallback numbers
83
 
84
 
85
  if __name__ == "__main__":
86
+ assert preprocess_text("Quality of life update") == "КВюаліті оф ліфе юпдате"
87
+ assert preprocess_text("Він украв 20000000 $") == "Він украв двадцять мільйонів долар"
88
+ assert preprocess_text("111 000 000 000 доларів державного боргу.") == "сто одинадцять мільярдів доларів державного боргу."
89
+ assert preprocess_text("11100000001 доларів державного боргу.") == "одинадцять мільярдів сто мільйонів одна доларів державного боргу."
90
+ assert preprocess_text("це 19-річне вино.") == "це дев'ятнадцять-річне вино."
91
+ assert preprocess_text("10-30-40-50-5-9-5") == "десять-тридцять-сорок-п'ятдесят-п'ять-дев'ять-п'ять"