Spaces:
Running
Running
codevision
commited on
Commit
•
7b54849
1
Parent(s):
7a439aa
Account for different forms of currency name (#33)
Browse files- requirements.txt +1 -1
- setup.py +1 -1
- tests/test_formatter.py +24 -18
- ukrainian_tts/formatter.py +40 -5
requirements.txt
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
torch
|
3 |
espnet>=202301
|
4 |
typeguard<3 # typeguard 3.0.0 is incompatible with espnet
|
5 |
-
num2words
|
6 |
ukrainian-word-stress==1.0.2
|
7 |
git+https://github.com/egorsmkv/ukrainian-accentor.git@5b7971c4e135e3ff3283336962e63fc0b1c80f4c
|
8 |
gradio # 3.34
|
|
|
2 |
torch
|
3 |
espnet>=202301
|
4 |
typeguard<3 # typeguard 3.0.0 is incompatible with espnet
|
5 |
+
git+https://github.com/kant2002/num2words.git@kant/add-cases
|
6 |
ukrainian-word-stress==1.0.2
|
7 |
git+https://github.com/egorsmkv/ukrainian-accentor.git@5b7971c4e135e3ff3283336962e63fc0b1c80f4c
|
8 |
gradio # 3.34
|
setup.py
CHANGED
@@ -14,7 +14,7 @@ setup(
|
|
14 |
install_requires=[
|
15 |
"espnet>=202301",
|
16 |
"typeguard<3",
|
17 |
-
"num2words
|
18 |
"ukrainian-word-stress==1.0.1",
|
19 |
"ukrainian_accentor @ git+https://github.com/egorsmkv/ukrainian-accentor.git@5b7971c4e135e3ff3283336962e63fc0b1c80f4c",
|
20 |
],
|
|
|
14 |
install_requires=[
|
15 |
"espnet>=202301",
|
16 |
"typeguard<3",
|
17 |
+
"num2words @ git+https://github.com/kant2002/num2words.git@kant/add-cases",
|
18 |
"ukrainian-word-stress==1.0.1",
|
19 |
"ukrainian_accentor @ git+https://github.com/egorsmkv/ukrainian-accentor.git@5b7971c4e135e3ff3283336962e63fc0b1c80f4c",
|
20 |
],
|
tests/test_formatter.py
CHANGED
@@ -1,20 +1,26 @@
|
|
1 |
from ukrainian_tts.formatter import preprocess_text
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from ukrainian_tts.formatter import preprocess_text
|
2 |
+
import pytest
|
3 |
|
4 |
+
@pytest.mark.parametrize('text,expected', [
|
5 |
+
("Quality of life update", "кваліті оф ліфе юпдате"),
|
6 |
+
("Він украв 20000000 $", "він украв двадцять мільйонів доларів"),
|
7 |
+
("Він украв 20000000", "він украв двадцять мільйонів"),
|
8 |
+
("Він украв 1 $", "він украв один долар"),
|
9 |
+
("Він украв 2 $", "він украв два долари"),
|
10 |
+
("Він украв 2 ₴", "він украв дві гривні"),
|
11 |
+
(
|
12 |
+
"111 000 000 000 доларів державного боргу.",
|
13 |
+
"сто одинадцять мільярдів доларів державного боргу.",
|
14 |
+
),
|
15 |
+
(
|
16 |
+
"11100000001 доларів державного боргу.",
|
17 |
+
"одинадцять мільярдів сто мільйонів один доларів державного боргу.",
|
18 |
+
),
|
19 |
+
# this is wrong case, should be "це дев'ятнадцяти-річне вино."
|
20 |
+
# Implementing this, require to have proper parsing of words into the token stream
|
21 |
+
# which reqiure reworking of current approach.
|
22 |
+
("це 19-річне вино.", "це дев'ятнадцять-річне вино."),
|
23 |
+
("10-30-40-50-5-9-5", "десять-тридцять-сорок-п'ятдесят-п'ять-дев'ять-п'ять"),
|
24 |
+
])
|
25 |
+
def test_formatter(text, expected):
|
26 |
+
assert preprocess_text(text) == expected
|
ukrainian_tts/formatter.py
CHANGED
@@ -1,13 +1,37 @@
|
|
1 |
-
import num2words
|
2 |
import re
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def preprocess_text(text):
|
6 |
text = text.lower()
|
7 |
# currencies
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# replace apostrophe
|
12 |
text = text.replace("`", "'")
|
13 |
text = text.replace("ʼ", "'")
|
@@ -32,12 +56,15 @@ def preprocess_text(text):
|
|
32 |
def detect_num_and_convert(word):
|
33 |
numbers = "0123456789,."
|
34 |
result = []
|
|
|
35 |
parts = word.split("-") # for handling complex words
|
36 |
for part in parts:
|
37 |
is_number = all(map(lambda x: x in numbers, part))
|
38 |
if is_number:
|
39 |
try:
|
40 |
-
|
|
|
|
|
41 |
except:
|
42 |
result.append(part)
|
43 |
else:
|
@@ -46,6 +73,14 @@ def preprocess_text(text):
|
|
46 |
|
47 |
# print([detect_num_and_convert(word) for word in text.split(" ")])
|
48 |
text = " ".join([detect_num_and_convert(word) for word in text.split(" ")])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# fallback numbers
|
51 |
text = text.replace("1", "один ")
|
|
|
1 |
+
from num2words import num2words
|
2 |
import re
|
3 |
|
4 |
+
def number_form(number):
|
5 |
+
if number[-1] == "1":
|
6 |
+
return 0
|
7 |
+
elif number[-1] in ("2", "3", "4"):
|
8 |
+
return 1
|
9 |
+
else:
|
10 |
+
return 2
|
11 |
+
|
12 |
+
CURRENCY = {
|
13 |
+
'USD': ('долар', 'долари', 'доларів'),
|
14 |
+
'UAH': ('гривня', 'гривні', 'гривень'),
|
15 |
+
'EUR': ('євро', 'євро', 'євро'),
|
16 |
+
}
|
17 |
|
18 |
def preprocess_text(text):
|
19 |
text = text.lower()
|
20 |
# currencies
|
21 |
+
if "$" in text:
|
22 |
+
currency = "USD"
|
23 |
+
gender = 'masculine'
|
24 |
+
elif "₴" in text:
|
25 |
+
currency = "UAH"
|
26 |
+
gender = 'feminine'
|
27 |
+
elif "€" in text:
|
28 |
+
currency = "EUR"
|
29 |
+
gender = 'masculine'
|
30 |
+
else:
|
31 |
+
currency = ""
|
32 |
+
gender = 'masculine'
|
33 |
+
|
34 |
+
num_form = 0
|
35 |
# replace apostrophe
|
36 |
text = text.replace("`", "'")
|
37 |
text = text.replace("ʼ", "'")
|
|
|
56 |
def detect_num_and_convert(word):
|
57 |
numbers = "0123456789,."
|
58 |
result = []
|
59 |
+
nonlocal num_form
|
60 |
parts = word.split("-") # for handling complex words
|
61 |
for part in parts:
|
62 |
is_number = all(map(lambda x: x in numbers, part))
|
63 |
if is_number:
|
64 |
try:
|
65 |
+
num_form = number_form(part)
|
66 |
+
print("-" + part + "-" + str(num_form))
|
67 |
+
result.append(num2words(part, lang="uk", gender=gender))
|
68 |
except:
|
69 |
result.append(part)
|
70 |
else:
|
|
|
73 |
|
74 |
# print([detect_num_and_convert(word) for word in text.split(" ")])
|
75 |
text = " ".join([detect_num_and_convert(word) for word in text.split(" ")])
|
76 |
+
if (currency == 'USD'):
|
77 |
+
text = text.replace("$", CURRENCY[currency][num_form])
|
78 |
+
|
79 |
+
if (currency == 'UAH'):
|
80 |
+
text = text.replace("₴", CURRENCY[currency][num_form])
|
81 |
+
|
82 |
+
if (currency == 'EUR'):
|
83 |
+
text = text.replace("€", CURRENCY[currency][num_form])
|
84 |
|
85 |
# fallback numbers
|
86 |
text = text.replace("1", "один ")
|