forked from alumae/kiirkirjutaja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
words2numbers.py
297 lines (241 loc) · 9.81 KB
/
words2numbers.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import os
import argparse
import unicodedata
import itertools
import string
import pdb
from pynini import *
from pynini.lib import byte
import pytest
input_chars = list(u" 0123456789-+/_.:'~,?!.")
input_chars.extend(list(u"abcdefghijklmnoprsštuvwõõäöüxyzž"))
input_chars.extend(u"ćçčĉø")
input_chars.extend([c.upper() for c in input_chars])
sigma_star = union(*input_chars).closure().optimize()
sigma_star = closure(byte.BYTE).optimize()
digits_2_to_9 = {
"kaks" : "2",
"kolm" : "3",
"neli" : "4",
"viis" : "5",
"kuus" : "6",
"seitse" : "7",
"kaheksa" : "8",
"üheksa" : "9"}.items()
numbers_10_to_19_nom = {
"kümme" : "10",
"üksteist" : "11",
"kaksteist" : "12",
"kolmteist" : "13",
"neliteist" : "14",
"viisteist" : "15",
"kuusteist" : "16",
"seitseteist" : "17",
"kaheksateist" : "18",
"üheksateist" : "19"
}.items()
# Genitive
digits_2_to_9_gen = {
"kahe" : "2",
"kolme" : "3",
"nelja" : "4",
"viie" : "5",
"kuue" : "6",
"seitsme" : "7",
"kaheksa" : "8",
"üheksa" : "9"}.items()
numbers_10_to_19_gen = {
"kümne" : "10",
"üheteistkümne" : "11",
"kaheteistkümne" : "12",
"kolmeteistkümne" : "13",
"neljateistkümne" : "14",
"viieteistkümne" : "15",
"kuueteistkümne" : "16",
"seitsmeteistkümne" : "17",
"kaheksateistkümne" : "18",
"üheksateistkümne" : "19"
}.items()
digits_2_to_9_ord = {
"teine" : "2",
"kolmas" : "3",
"neljas" : "4",
"viies" : "5",
"kuues" : "6",
"seitsmes" : "7",
"kaheksas" : "8",
"üheksas" : "9"}.items()
nom_to_gen = {
"üks" : "ühe",
"kaks" : "kahe",
"kolm" : "kolme",
"neli" : "nelja",
"viis" : "viie",
"kuus" : "kuue",
"seitse" : "seitsme",
"kaheksa" : "kaheksa",
"üheksa" : "üheksa",
"kümme" : "kümne",
"kümmend" : "kümne",
"üksteist" : "üheteistkümne",
"kaksteist" : "kaheteistkümne",
"kolmteist" : "kolmeteistkümne",
"neliteist" : "neljateistkümne",
"viisteist" : "viieteistkümne",
"kuusteist" : "kuueteistkümne",
"seitseteist" : "sitsmeteistkümne",
"kaheksateist" : "kaheksateistkümne",
"üheksateist" : "üheksateistkümne",
"sada" : "saja",
"tuhat" : "tuhande"
}.items()
gen_to_ord_specials = {
"ühe" : "esimene",
"kahe" : "teine",
"kolme" : "kolmas",
}.items()
gen_to_ord_gen_specials = {
"ühe" : "esimese",
"kahe" : "teise",
"kolme" : "kolmanda",
}.items()
def words2num_fst():
left_break = accep("[BOS]") | accep(" ")
right_break = accep("[EOS]") | accep(" ")
t_10_to_19 = string_map(numbers_10_to_19_nom, ).optimize()
t_2_to_9 = string_map(digits_2_to_9, ).optimize()
t_1_to_9 = t_2_to_9 | cross("üks", "1", )
t_tens_cont = t_2_to_9 + cross("kümmend", "")
t_tens = t_tens_cont + cross("", "0")
t_20_to_99 = t_tens | (t_tens_cont + cross(" ", "") + t_1_to_9)
t_10_to_99 = t_10_to_19 | t_20_to_99
t_100_to_999 = (cross("üks", "1", ) | cross("", accep("1", weight=1)) | t_2_to_9 ) + cross("sada", "") + \
(
cross("", "00") |
(cross(" ", accep("0", weight=-1)) + (t_1_to_9)) |
(cross(" ", accep("", weight=-2)) + t_10_to_99)
)
t_2_to_999 = t_2_to_9 | t_10_to_99 | t_100_to_999
t_1000_to_9999 = (cross("üks ", "1", ) | cross("", accep("1", weight=1)) | (t_2_to_999 + cross(" ", ""))) + cross("tuhat", "") + \
(
(cross(" ", "", ) + t_100_to_999) |
(cross(" ", "0") + t_10_to_99) |
(cross(" ", "00") + t_1_to_9) |
cross("", "000")
)
t_10_to_9999 = (t_10_to_19 | t_20_to_99 | t_100_to_999 | t_1000_to_9999).optimize()
t_0_to_9 = cross("null", "0") | t_1_to_9
t_0_to_99 = t_0_to_9 | t_10_to_19 | t_20_to_99
t_0_to_9999 = t_0_to_9 | t_10_to_9999
t_fractional_part = cross(" koma ", ",") + \
(
t_0_to_9999 |
(cross("null", accep("0", weight=-1)) + (cross(" ", accep("", weight=-1)) + t_0_to_9).plus)
)
t_fraction = t_0_to_9999 + t_fractional_part
# Genitive
t_gen_to_nom = cdrewrite(string_map(nom_to_gen).invert(), "", "", sigma_star)
t_10_to_9999_gen = t_gen_to_nom @ t_10_to_9999
t_fraction_gen = t_gen_to_nom @ t_fraction
# Other inflections
t_10_to_9999_infl = t_10_to_9999_gen + (cross("", "-") + union(u"le", u"l", u"ni", u"ga", u"lt", u"st", u"na", u"sse", u"ks"))
# Ordinal nominative
t_ord_to_gen = string_map(gen_to_ord_specials, ).invert() | cross("s", "")
t_10_to_9999_ord = (sigma_star + t_ord_to_gen) @ t_10_to_9999_gen + cross("", ".")
# Ordinal genitive
t_ord_gen_to_gen = string_map(gen_to_ord_gen_specials, ).invert() | cross("nda", "")
t_10_to_9999_ord_gen = (sigma_star + t_ord_gen_to_gen) @ t_10_to_9999_gen + cross("", ".")
# Ordinal other inflections
t_10_to_9999_ord_infl = t_10_to_9999_ord_gen + (cross("l", "") | (cross("", "-") + union(u"le", u"ni", u"ga", u"lt", u"st", u"na", u"sse", u"ks", u"te", u"tele", u"teni", u"tega", u"telt", u"test", u"tesse", u"teks")))
transforms = [
cdrewrite(t_fraction, left_break, right_break, sigma_star),
cdrewrite(t_10_to_9999, left_break, right_break, sigma_star),
cdrewrite(t_10_to_9999_infl, left_break, right_break, sigma_star),
cdrewrite(t_fraction_gen, left_break, right_break, sigma_star),
cdrewrite(t_10_to_9999_ord, left_break, right_break, sigma_star),
cdrewrite(t_10_to_9999_ord_gen, left_break, right_break, sigma_star),
cdrewrite(t_10_to_9999_ord_infl, left_break, right_break, sigma_star),
cdrewrite(t_10_to_9999_gen, left_break, right_break, sigma_star),
]
result = sigma_star
for transform in transforms:
result = result @ transform
result = result.optimize()
return result
def words2num(fst, text):
#text_fsa = acceptor(text, )
result = text @ fst
result = shortestpath(result)
try:
return result.string()
except:
print("Failed to convert sentence: %s" % text, file=sys.stderr)
return text
@pytest.fixture(scope="module", autouse=True)
def model():
print ('INITIALIZATION')
yield words2num_fst()
print ('TEAR DOWN')
def test_simple(model):
#model = words2num_fst()
assert words2num(model, "see on üheksateist protsenti suurem") == "see on 19 protsenti suurem"
assert words2num(model, "see on kakskümmend protsenti suurem") == "see on 20 protsenti suurem"
assert words2num(model, "see on kakskümmend üks protsenti suurem") == "see on 21 protsenti suurem"
assert words2num(model, "see on sada protsenti") == "see on 100 protsenti"
assert words2num(model, "see on sada kümme protsenti") == "see on 110 protsenti"
assert words2num(model, "see on sada üks protsenti") == "see on 101 protsenti"
assert words2num(model, "see on tuhat korda") == "see on 1000 korda"
assert words2num(model, "aastal tuhat üheksasada kaheksakümmend") == "aastal 1980"
assert words2num(model, "kolm koma neliteist protsenti") == "3,14 protsenti"
assert words2num(model, "kolm koma null üks protsenti") == "3,01 protsenti"
assert words2num(model, "viiskümmend koma null null kaheksa kaks protsenti") == "50,0082 protsenti"
assert words2num(model, "kaks tuhat kuusteist") == "2016"
assert words2num(model, "kahe tuhande kolmekümne viiendaks aastaks") == "2035.-ks aastaks"
assert words2num(model, "kakssada tuhat eurot") == "200000 eurot"
assert words2num(model, "kakssada üheksakümmend kuus tuhat eurot") == "296000 eurot"
def test_inflections(model):
# inflections
#model = words2num_fst()
assert words2num(model, "see on kahe või isegi kaheteistkümne protsendi võrra suurem") == "see on kahe või isegi 12 protsendi võrra suurem"
assert words2num(model, "see on kolmeteistkümne võrra suurem") == "see on 13 võrra suurem"
assert words2num(model, "see on saja võrra suurem") == "see on 100 võrra suurem"
assert words2num(model, "see on saja ühe võrra suurem") == "see on 101 võrra suurem"
assert words2num(model, "see on kahekümne protsendi võrra suurem") == "see on 20 protsendi võrra suurem"
assert words2num(model, "see on kolme koma kahe protsendi võrra suurem") == "see on 3,2 protsendi võrra suurem"
assert words2num(model, "see on kolme koma null kahe protsendi võrra suurem") == "see on 3,02 protsendi võrra suurem"
def test_ordinals(model):
#model = words2num_fst()
# ordinals
assert words2num(model, "see on kolmeteistkümnes kord") == "see on 13. kord"
assert words2num(model, "Tallinna neljakümne esimene kool") == "Tallinna 41. kool"
assert words2num(model, "see juhtus kahe tuhandenda aasta suvel") == "see juhtus 2000. aasta suvel"
assert words2num(model, "see juhtus kahe tuhande esimesel aastal") == "see juhtus 2001. aastal"
assert words2num(model, "see juhtus kaheksakümnendate aastate paiku") == "see juhtus 80.-te aastate paiku"
def test_complex_inflections(model):
#model = words2num_fst()
# more complex inflections
assert words2num(model, "see kasvas kaheteistkümnele protsendile") == "see kasvas 12-le protsendile"
assert words2num(model, "see kasvas sajale protsendile") == "see kasvas 100-le protsendile"
assert words2num(model, "see kasvas sajandi protsendile") == "see kasvas sajandi protsendile"
assert words2num(model, "see kasvas saja ühele protsendile") == "see kasvas 101-le protsendile"
assert words2num(model, "kuuele tuhandele ") == "6000-le"
assert words2num(model, "kahesaja üheksakümne kuuele tuhandele ") == "296000-le"
def test_invalid_chars(model):
#model = words2num_fst()
assert words2num(model, "€¨€½|") == "€¨€½|"
class Words2Numbers():
def __init__(self):
self.words2num_fst_ = words2num_fst()
def post_process(self, text):
return words2num(self.words2num_fst_, text)
if __name__ == '__main__':
words2num_fst = words2num_fst()
while 1:
l = sys.stdin.readline()
if not l: break
sys.stdout.write(words2num(words2num_fst, l.strip()) + "\n")
sys.stdout.flush()