-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpic.py
More file actions
executable file
·144 lines (105 loc) · 4.29 KB
/
pic.py
File metadata and controls
executable file
·144 lines (105 loc) · 4.29 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
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
"""
Greynir: Natural language processing for Icelandic
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 picture-related queries.
"""
# TODO: "Sýndu mér X"
from typing import Optional
import random
import logging
from urllib.parse import urlparse
from reynir import NounPhrase
from queries import Query, QueryStateDict
from utility import icequote
from queries.util import gen_answer, read_grammar_file
from tree import Result, Node
from images import get_image_url, Img
_PIC_QTYPE = "Picture"
TOPIC_LEMMAS = ["mynd", "ljósmynd"]
def help_text(lemma: str) -> str:
"""Help text to return when query processor is unable to parse a query but
one of the above lemmas is found in it"""
return "Ég get svarað ef þú segir til dæmis: {0}?".format(
random.choice(
(
"Sýndu mér mynd af Halldóri Laxness",
"Sýndu ljósmynd af Kára Stefánssyni",
"Sýndu mynd af Hörpunni",
)
)
)
# This module wants to handle parse trees for queries
HANDLE_TREE = True
# The grammar nonterminals this module wants to handle
QUERY_NONTERMINALS = {"QPic"}
# The context-free grammar for the queries recognized by this plug-in module
GRAMMAR = read_grammar_file("pic")
def QPicQuery(node: Node, params: QueryStateDict, result: Result) -> None:
# Set the query type
result.qtype = _PIC_QTYPE
def _preprocess(s: str) -> str:
return s
def QPicSubject(node: Node, params: QueryStateDict, result: Result) -> None:
n = _preprocess(result._text)
nom = NounPhrase(n).nominative or n
result.subject = nom
result.subject_þgf = result._text
def QPicWrongPictureQuery(node: Node, params: QueryStateDict, result: Result) -> None:
result.wrong = True
def _gen_pic_answer(result: Result, q: Query):
"""Generate answer to query asking for a picture of something."""
subj = result["subject"]
subj_þgf = result["subject_þgf"]
# Look up picture using Google Images API
img: Optional[Img] = None
try:
img = get_image_url(subj)
if img and img.src:
# We found an image of the subject
answ = f"Hér er mynd sem ég fann af {icequote(subj_þgf)}"
q.set_answer(*gen_answer(answ))
q.set_image(img.src)
src: str = "Google Images"
o = urlparse(img.src)
if o and o.hostname:
src = o.hostname
q.set_source(src)
# q.set_expires(datetime.now(timezone.utc) + timedelta(hours=1))
else:
# No picture found
q.set_answer(*gen_answer(f"Engin mynd fannst af {icequote(subj_þgf)}"))
except Exception:
q.set_answer(*gen_answer(f"Ekki tókst að leita í myndagrunni"))
q.set_key(subj)
q.set_qtype(result.qtype)
_WRONG_PIC_ANSWER = "Ég biðst afsökunar á því. Enginn er fullkominn."
def _gen_wrong_pic_answer(result: Result, q: Query):
"""Query states that previous picture was wrong."""
q.set_qtype(result.qtype)
q.set_answer(*gen_answer(_WRONG_PIC_ANSWER))
def sentence(state: QueryStateDict, result: Result) -> None:
"""Called when sentence processing is complete."""
q: Query = state["query"]
if "qtype" in result:
# Successfully matched a query type
try:
if "wrong" in result:
_gen_wrong_pic_answer(result, q)
else:
_gen_pic_answer(result, q)
except Exception as e:
logging.warning(f"Exception answering picture query: {e}")
q.set_error(f"E_EXCEPTION: {e}")
return
else:
q.set_error("E_QUERY_NOT_UNDERSTOOD")