-
Notifications
You must be signed in to change notification settings - Fork 17
/
words.py
executable file
·268 lines (213 loc) · 8.91 KB
/
words.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
"""
Greynir: Natural language processing for Icelandic
Word properties query response module
Copyright (C) 2020 Miðeind ehf.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
This module handles queries related to words and their properties,
e.g. spelling, declension, dictionary definitions, etymology, etc.
"""
# "Hvernig orð er X", "Hvers konar orð er X"
# "Er X [tegund af orði]"
# TODO: Handle definite article in declension ("Hvernig beygist orðið 'kötturinn'?")
# TODO: Declension queries should support adjectives etc.
# TODO: Beautify query by placing word being asked about within quotation marks
# TODO: Handle numbers ("3" should be spelled as "þrír" etc.)
# TODO "Hvaða orð rímar við X"
import re
import logging
from datetime import datetime, timedelta
from queries import gen_answer
from reynir.bindb import BIN_Db
# Spell out how character names are pronounced in Icelandic
_CHAR_PRONUNCIATION = {
"a": "a",
"á": "á",
"b": "bé",
"c": "sé",
"d": "dé",
"ð": "eð",
"e": "e",
"é": "je",
"f": "eff",
"g": "gé",
"h": "há",
"i": "i",
"í": "í",
"j": "joð",
"k": "ká",
"l": "ell",
"m": "emm",
"n": "enn",
"o": "o",
"ó": "ó",
"p": "pé",
"q": "kú",
"r": "err",
"s": "ess",
"t": "té",
"u": "u",
"ú": "ú",
"v": "vaff",
"x": "ex",
"y": "ufsilon i",
"ý": "ufsilon í",
"þ": "þoddn",
"æ": "æ",
"ö": "ö",
"z": "seta",
}
_WORDTYPE_RX_NOM = "(?:orðið|nafnið|nafnorðið)"
_WORDTYPE_RX_GEN = "(?:orðsins|nafnsins|nafnorðsins)"
_SPELLING_RX = (
r"^hvernig stafsetur maður {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig stafset ég {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig stafa ég {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig stafar þú {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig stafarðu {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig skal stafsetja {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig skrifar maður {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig skrifa ég {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig stafar maður {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig er {0}?\s?(.+) stafsett$".format(_WORDTYPE_RX_NOM),
r"^hvernig er {0}?\s?(.+) skrifað$".format(_WORDTYPE_RX_NOM),
r"^hvernig er {0}?\s?(.+) stafað$".format(_WORDTYPE_RX_NOM),
r"^hvernig skal stafa {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig stafast {0}?\s?(.+)$".format(_WORDTYPE_RX_NOM),
)
_DECLENSION_RX = (
r"^hvernig beygi ég {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig fallbeygi ég {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig beygirðu {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig fallbeygirðu {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig á að beygja {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig á að fallbeygja {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig á ég að beygja {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig á ég að fallbeygja {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig á maður að beygja {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig á maður að fallbeygja {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig beygir maður {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig fallbeygir maður {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig beygist {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig fallbeygist {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig skal beygja {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig skal fallbeygja {0} (.+)$".format(_WORDTYPE_RX_NOM),
r"^hvernig er {0} (.+) beygt$".format(_WORDTYPE_RX_NOM),
r"^hvernig er {0} (.+) fallbeygt$".format(_WORDTYPE_RX_NOM),
r"^hverjar eru beygingarmyndir {0} (.+)$".format(_WORDTYPE_RX_GEN),
r"^hvað eru beygingarmyndir {0} (.+)$".format(_WORDTYPE_RX_GEN),
)
def lookup_best_word(word):
""" Look up word in BÍN, pick right one acc. to a criterion. """
with BIN_Db().get_db() as db:
def nouns_only(bin_meaning):
return bin_meaning.ordfl in ("kk", "kvk", "hk")
res = list(filter(nouns_only, db.lookup_nominative(word)))
if not res:
# Try with uppercase first char
capw = word.capitalize()
res = list(filter(nouns_only, db.lookup_nominative(capw)))
if not res:
return None
# OK, we have one or more matching nouns
if len(res) == 1:
m = res[0]
else:
# TODO: Pick best result
m = res[0] # For now
wid = m.utg
# TODO: If more than one declesion form possible (e.g. gen. björns vs. bjarnar)
# we should also list such variations
def sort_by_preference(m_list):
# Filter out words that don't have the same "utg" i.e. word ID as
# the one we successfully looked up in BÍN
mns = list(filter(lambda w: w.utg == wid, m_list))
# Discourage rarer declension forms, i.e. ÞGF2 and ÞGF3
return sorted(mns, key=lambda m: "2" in m.beyging or "3" in m.beyging)
# Look up all cases of the word in BÍN
nom = m.stofn
acc = db.cast_to_accusative(nom, meaning_filter_func=sort_by_preference)
dat = db.cast_to_dative(nom, meaning_filter_func=sort_by_preference)
gen = db.cast_to_genitive(nom, meaning_filter_func=sort_by_preference)
return nom, acc, dat, gen
_NOT_IN_BIN_MSG = "Nafnorðið '{0}' fannst ekki í Beygingarlýsingu íslensks nútímamáls."
def declension_answer_for_word(word, query):
""" Look up all morphological forms of a given word,
construct natural language response. """
# Look up in BÍN
forms = lookup_best_word(word)
if not forms:
return gen_answer(_NOT_IN_BIN_MSG.format(word))
answ = ", ".join(forms)
response = dict(answer=answ)
# TODO: Handle plural e.g. "Hér eru"
cases_desc = "Hér er {0}, um {1}, frá {2}, til {3}".format(*forms)
voice = "Orðið '{0}' beygist á eftirfarandi hátt: {1}.".format(word, cases_desc)
query.set_qtype("Declension")
query.set_key(word)
return response, answ, voice
# Time to pause after reciting each character name
_LETTER_INTERVAL = 0.3 # Seconds
def spelling_answer_for_word(word, query):
""" Spell out a word provided in a query. """
# Generate list of characters in word
chars = list(word)
# Text answer shows chars in uppercase separated by space
answ = " ".join([c.upper() for c in chars])
response = dict(answer=answ)
# Piece together SSML for speech synthesis
v = [_CHAR_PRONUNCIATION.get(c, c) for c in chars]
jfmt = '<break time="{0}s"/>'.format(_LETTER_INTERVAL)
voice = "Orðið '{0}' er stafað á eftirfarandi hátt: {1} {2}".format(
word, jfmt, jfmt.join(v)
)
query.set_qtype("Spelling")
query.set_key(word)
return response, answ, voice
def handle_plain_text(q):
""" Handle a plain text query, contained in the q parameter. """
ql = q.query_lower.rstrip("?")
matches = None
handler = None
# Spelling queries
for rx in _SPELLING_RX:
matches = re.search(rx, ql)
if matches:
handler = spelling_answer_for_word
break
# Declension queries
if handler is None:
for rx in _DECLENSION_RX:
matches = re.search(rx, ql)
if matches:
handler = declension_answer_for_word
break
# Nothing caught by regexes, bail
if handler is None:
return False
assert matches is not None
matching_word = matches.group(1)
# Generate answer
try:
answ = handler(matching_word, q)
except Exception as e:
logging.warning("Exception generating word query answer: {0}".format(e))
q.set_error("E_EXCEPTION: {0}".format(e))
answ = None
if answ:
q.set_answer(*answ)
q.set_expires(datetime.utcnow() + timedelta(hours=24))
# Beautify query by placing word being asked about within Icelandic quotation marks
# TODO: This needs to be fixed, mangles the query if asking about "maður", "orð", etc.
# bq = re.sub(r"\s({0})".format(matching_word), r" „\1“", q.beautified_query)
# q.set_beautified_query(bq)
return True
return False