-
Notifications
You must be signed in to change notification settings - Fork 1
/
volume.py
231 lines (189 loc) · 6.14 KB
/
volume.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
from typing import Mapping
import logging
import random
import math
import re
from queries import Query, QueryStateDict
from tree import Result
from . import AnswerTuple, LatLonTuple
TOPIC_LEMMAS = [
"hækka",
"lækka",
"þögn",
"þagna",
"hljóð",
"hljóðstyrkur",
]
_NUMBER_WORDS: Mapping[str, float] = {
"núll": 0,
"einn": 1,
"eitt": 1,
"tveir": 2,
"tvö": 2,
"þrír": 3,
"þrjú": 3,
"þremur": 3,
"fjórir": 4,
"fjögur": 4,
"fimm": 5,
"sex": 6,
"sjö": 7,
"átta": 8,
"níu": 9,
"tíu": 10,
"ellefu": 11,
"tólf": 12,
"þrettán": 13,
"fjórtán": 14,
"fimmtán": 15,
"sextán": 16,
"sautján": 17,
"átján": 18,
"nítján": 19,
"tuttugu": 20,
"þrjátíu": 30,
"fjörutíu": 40,
"fimmtíu": 50,
"sextíu": 60,
"sjötíu": 70,
"áttatíu": 80,
"níutíu": 90,
"hundrað": 100,
"hundruð": 100,
}
def help_text(lemma: str) -> str:
"""Help text to return when query.py is unable to parse a query but
one of the above lemmas is found in it"""
return "Ég get svarað ef þú spyrð til dæmis: {0}?".format(
random.choice(
(
"Hækka hljóðið",
"Lækka hljóðstyrkinn",
"Hækka",
"Þagna",
"Þögn!",
"hljóð fimmtíu prósent",
)
)
)
# This module wants to handle parse trees for queries
HANDLE_TREE = True
# The grammar nonterminals this module wants to handle
QUERY_NONTERMINALS = {"QVolume"}
# The context-free grammar for the queries recognized by this plug-in module
GRAMMAR = """
Query →
QVolume
QVolume →
QVolumePercQuery '?'?
| QVolumeAbsQuery '?'?
QVolumePercQuery →
QVolumeUpQuery QVolumeVolume? # [Hækkaðu] [hljóð]?
| QVolumeDownQuery QVolumeVolume? # [Lækkaðu] [hljóð]?
| QVolumeVolume QVolumePercent # [Hljóð] [50 prósent]
| QVolumeUp QVolumeVolume? QVolumePPUp? QVolumePercent # [Hækkaðu] [(hljóð)] [(í) 50 prósent]
| QVolumeDown QVolumeVolume? QVolumePPDown? QVolumePercent # [Lækkaðu] [(hljóð)] [(í) 50 prósent]
| QVolumeMuteQuery # [Lækkaðu í botn]
QVolumeAbsQuery →
QVolumeVolume QVolumeAbs # [Hljóð] [12]
| QVolumeUp QVolumeVolume? QVolumePPUp? QVolumeAbs # [Hækkaðu] [(hljóð)] [(í) 12]
| QVolumeDown QVolumeVolume? QVolumePPDown? QVolumeAbs # [Lækkaðu] [(hljóð)] [(í) 12]
QVolumeAbs → töl | tala | to
QVolumeUpVP → QVolumeUpQuery QVolumePPUp?
QVolumeDownVP → QVolumeDownQuery QVolumePPDown?
QVolumeUpQuery → QVolumeUp
QVolumeUp →
"hækka" | "hækka" "þú" | "hækkaðu" | "hækkað"
| "fækka"
QVolumeDownQuery → QVolumeDown
QVolumeDown →
"lækka" | "lækkað" | "lækka" "þú" | "lækkaðu"
QVolumeVolume →
"hljóð" | "hljóðið" | "hljóðstyrk" | "hljóðstyrkinn"
| "hljóðstyrkur"
| "ljóð"
QVolumePPUp → "upp" "í" | "í" | "alveg" "upp" "í"
QVolumePPDown → "niður" "í" | "í" | "alveg" "niður" "í"
QVolumePercent → Prósenta
QVolumeMuteQuery → QVolumeDownQuery QVolumeMute
QVolumeMute → "í" "botn" | "alveg" "niður"?
"""
def QVolumeAbs(node, params, result):
result.qtype = "VolumeSet"
result._canonical = result._text
n = result._text.split()
print(result._canonical)
print("N:", n, type(n))
if n[0].isdecimal():
print(n[0], "is decimal")
result["command"] = int(n[0]) # 8
elif n[0] in _NUMBER_WORDS:
print(n[0], "is not decimal")
result["command"] = _NUMBER_WORDS[n[0]] # átta
else:
print("tjah, hvað er í gangi hér?")
def QVolumePercent(node, params, result):
result.qtype = "VolumePercentage"
result._canonical = result._text
n = result._text.split()
print(result._canonical)
print("N:", n, type(n))
# skoda rett control flow
# hvad gaeti fokkad thessu upp?
n[0] = n[0].rstrip("%")
if n[0].isdecimal():
print(n[0], "is decimal")
result["command"] = int(n[0]) # 8
elif n[0] in _NUMBER_WORDS:
print(n[0], "is not decimal")
result["command"] = _NUMBER_WORDS[n[0]] # átta
else:
print("tjah, hvað er í gangi hér?")
def QVolumeUpQuery(node, params, result):
result.qtype = "Volume"
result["command"] = "+"
def QVolumeDownQuery(node, params, result):
result.qtype = "Volume"
result["command"] = "-"
def QVolumeMuteQuery(node, params, result):
result["command"] = "MUTE"
def parse_num(num_str: str) -> float:
# Parse Icelandic number string to float or int
num = None
try:
# Pi
if num_str == "pí":
num = math.pi
# Handle numbers w. Icelandic decimal places ("17,2")
elif re.search(r"^\d+,\d+$", num_str):
num = float(num_str.replace(",", "."))
# Handle digits ("17")
else:
num = float(num_str)
except ValueError:
# Handle number words ("sautján")
if num_str in _NUMBER_WORDS:
num = _NUMBER_WORDS[num_str]
# Ordinal number strings ("17.")
elif re.search(r"^\d+\.$", num_str):
num = int(num_str[:-1])
else:
num = 0
except Exception as e:
logging.warning("Unexpected exception: {0}".format(e))
raise
return num
def sentence(state: QueryStateDict, result: Result) -> None:
"""Called when sentence processing is complete."""
q: Query = state["query"]
if "qtype" in result and result["qtype"] == "Volume" or "VolumePercentage":
try:
print("))============>", result["qtype"], "<============((")
q.set_qtype(result["qtype"])
q.set_answer("", result["command"], "")
return
except Exception as e:
logging.warning("Exception generating answer from Volume: {0}".format(e))
q.set_error("E_EXCEPTION: {0}".format(e))
else:
q.set_error("E_QUERY_NOT_UNDERSTOOD")