-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrepeat.py
More file actions
executable file
·101 lines (75 loc) · 2.97 KB
/
repeat.py
File metadata and controls
executable file
·101 lines (75 loc) · 2.97 KB
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
"""
Greynir: Natural language processing for Icelandic
Repeat-after-me query response module
Copyright (C) 2023 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 where the user requests that a certain
text segment be repeated back to him/her.
"""
# TODO: Migrate repeat functionality in builtin to this module
# TODO: Hvað spurði ég þig um síðast? Hvert var síðasta svar þitt? Endurtaktu þetta, o.s.frv.
from datetime import datetime, timedelta, timezone
from queries import Query
from queries.util import gen_answer
from utility import icequote
_REPEAT_QTYPE = "Parrot" # 'Repeat' is already taken for repeating the last answer
_REPEAT_PREFIXES = tuple(
(
"segðu eftirfarandi orð",
"segðu orðið",
"segðu orðin",
"segðu setninguna",
"segðu eftirfarandi setningu",
"segðu eftirfarandi",
"farðu með setninguna",
"endurtaktu eftirfarandi setningu",
"endurtaktu eftirfarandi orð",
"endurtaktu eftirfarandi",
"endurtaktu setninguna",
"endurtaktu eftir mér",
"endurtaktu orðið",
"endurtaktu orðin",
"endurtaktu",
"hermdu eftir mér",
# "segðu",
)
)
# _PREFIX_BLACKLIST = frozenset(
# ("segðu mér", "segðu okkur", "segðu eitthvað", "segðu frá")
# )
def gen_repeat_answ(text: str, cmd_prefix: str, q: Query) -> None:
atxt = text.strip()
atxt = atxt[:1].upper() + atxt[1:] # Capitalize first character
q.set_answer(*gen_answer(atxt))
q.set_qtype(_REPEAT_QTYPE)
q.set_key(atxt)
q.set_expires(datetime.now(timezone.utc) + timedelta(hours=24))
q.set_context(dict(subject=text))
# Beautify query by placing text to repeat within quotation marks
q.set_beautified_query(
"{0}{1}".format(cmd_prefix.capitalize(), icequote(atxt.rstrip(".") + "."))
)
def handle_plain_text(q: Query) -> bool:
"""Handles a plain text query."""
ql = q.query_lower.rstrip("?")
# for blw in _PREFIX_BLACKLIST:
# if ql.startswith(blw):
# return False
qlen = len(ql)
for r in _REPEAT_PREFIXES:
pfx = r + " "
plen = len(pfx)
if ql.startswith(pfx) and qlen > plen:
rtxt = q.query[plen:]
gen_repeat_answ(rtxt, pfx, q)
return True
return False