-
Notifications
You must be signed in to change notification settings - Fork 17
/
grammar.py
executable file
·103 lines (67 loc) · 2.95 KB
/
grammar.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
"""
Greynir: Natural language processing for Icelandic
Example of a grammar query processor 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 is an example of a plug-in query response module
for the Greynir query subsystem. It handles grammar queries, i.e.
queries that require grammatical parsing of the query text.
"""
import random
from queries import QueryStateDict
from tree import Node, Result
# Indicate that this module wants to handle parse trees for queries,
# as opposed to simple literal text strings
HANDLE_TREE = True
# Lemmas to look for when providing help text
TOPIC_LEMMAS = ["prufa"]
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 þú spyrð til dæmis: {0}?".format(
random.choice(("Er þetta prufa", "Gæti þetta verið prufa"))
)
# The context-free grammar for the queries recognized by this plug-in module
GRAMMAR = """
Query →
QGrammarExample '?'?
QGrammarExampleQuery →
"er" "þetta" QGrammarExampleTestOrNot
| "gæti" "þetta" "verið" QGrammarExampleTestOrNot
QGrammarExampleTestOrNot →
QGrammarExampleTest | QGrammarExampleNotTest
QGrammarExampleTest →
"prufa"
QGrammarExampleNotTest →
"ekki" "prufa"
"""
def QGrammarExampleQuery(node: Node, params: QueryStateDict, result: Result):
# Set the query type
result.qtype = "GrammarTest"
def QGrammarExampleTest(node: Node, params: QueryStateDict, result: Result):
result.qkey = "Test"
def QGrammarExampleNotTest(node: Node, params: QueryStateDict, result: Result):
result.qkey = "NotTest"
def sentence(state: QueryStateDict, result: Result) -> None:
"""Called when sentence processing is complete"""
q = state["query"]
if "qtype" in result and "qkey" in result and result.qtype == "GrammarTest":
# Successfully matched this query type, we're handling it...
q.set_qtype(result.qtype)
answ = "Já" if result.qkey == "Test" else "Nei"
voice = answ
response = dict(answer=answ)
# Set query answer
q.set_answer(response, answ, voice)
return
# This module did not understand the query
q.set_error("E_QUERY_NOT_UNDERSTOOD")