-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.py
278 lines (166 loc) · 7.1 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
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
269
270
271
272
273
274
275
276
277
278
from terms import common_terms, particles, adjectives, token_to_word
from patterns import pattern_list
from exceptions import *
import log
next_token = ""
all_tokens = []
curr_rule = "NO RULE"
def check(token):
global next_token, all_tokens, curr_rule
if next_token == 'OVER' : raise EndOfSentenceException([token,curr_rule])
if (next_token == token) :
log.explain.append(next_token)
print("CORRECT!", token, "@", curr_rule)
else :
print("INCORRET! Expecting:", token, "- Found:", next_token, "@", curr_rule)
raise GrammarException([token,next_token,curr_rule])
if (len(all_tokens) != 0) : next_token = all_tokens.pop(0)
else : next_token = 'OVER' ; print("NO TOKENS LEFT TO CRY")
def PHRASE():
global curr_rule
curr_rule = "PHRASE"
test = NOMINAL() # Tests for a NOMINAL
early_test = False
if test : # The phrase starts with a NOMINAL
curr_rule = "TOPIC"
has_topic = False # For now, no TOPIC has been identified
if (next_token == 'WA') :
check('WA')
if (next_token == 'AUX_VERB') : check('NOMINAL') # Breaks
has_topic = True # Where's a WA, there's a TOPIC
log.found_topic()
if (not has_topic) :
if (next_token in particles) : # If there's no TOPIC and the NEXT TOKEN is a PARTICLE
curr_rule = "INFO"
check(next_token) # Check whatever PARTICLE that is
early_test = True
if (next_token == 'VERB') :
check('PARTICLE')
test = INFO() # Tests for INFO
if test or early_test : test = VERBAL() # ... and the INFO test succeeds, it should be an ACTION PHRASE
else : test = COPULA() # ... and the INFO test fails, it should be a DESCRIPTIVE PHRASE
else : test = VERBAL()
if next_token == 'KA' : curr_rule = 'QUESTION' ; check('KA') ; log.question = True
return test
def INFO():
global curr_rule
curr_rule = "INFO"
# It returns TRUE if it reached a VERB, possibly acknowledging a chain of (NOMINAL PARTICLE) structures
# It returns FALSE if the chain was broken by a PARTICLE-less NOMINAL
# It will create a GrammarException if a NOMINAL-less PARTICLE is found
if (next_token == 'VERB') : return True # It reached the end of the structure
nominal_test = NOMINAL() # Tests for a NOMINAL
particle_test = False
if (next_token in particles) : # If the NEXT TOKEN is a PARTICLE
check(next_token) # Check whatever PARTICLE that is
particle_test = True # and tell 'em there's a PARTICLE
if nominal_test :
if particle_test : return INFO() # If a (NOMINAL PARTICLE) block was checked, eat recursions
else : return False # Else, it's a chain break
else :
if particle_test : check('NOMINAL') # Breaks
return False
def VERBAL():
global curr_rule
curr_rule = "VERBAL"
check('VERB')
log.set_sentence_type("VERBAL")
return True
def COPULA():
global curr_rule
curr_rule = "COPULA"
check('AUX_VERB')
log.set_sentence_type("DESCRIPTIVE")
return True
def NOMINAL():
adverb_test = False
adnom_test = False
if (next_token == 'ADVERB') : check('ADVERB') ; adverb_test = True
elif (next_token == 'ADNOM_ADJ') : check('ADNOM_ADJ') ; adnom_test = True
adjective_test = ADJECTIVE()
noun_test = NOUN()
if adnom_test and (not noun_test) : check('NOUN') ; return False
if adjective_test and (not noun_test) :
if (next_token != 'AUX_VERB') : check('AUX_VERB') ; return False # it's gonna explode
else : return True
return noun_test
def NOUN():
if (next_token == 'NOUN') : check('NOUN')
else : return False
if (next_token == 'NO') :
check('NO')
if (next_token == 'NOUN') : check('NOUN')
return True
def ADJECTIVE():
if (next_token == 'ADJ_I') : check('ADJ_I') ; return True
elif (next_token == 'ADJ_NOUN') : check('ADJ_NOUN') ; return True
elif (next_token == 'ADJ_NA') :
check('ADJ_NA')
if (next_token != 'NOUN') : check('NOUN') ; return False # Breaks
return True
elif (next_token == 'ADJ_TE') :
check('ADJ_TE')
if (next_token not in adjectives) : check('ADJECTIVE') ; return False # Breaks
return ADJECTIVE()
elif (next_token == 'ADJ_DE') :
check('ADJ_DE')
if (next_token not in adjectives) : check('ADJECTIVE') ; return False # Breaks
return ADJECTIVE()
return False
def token_wo_issho_ni_suru(token_list,word_list):
global next_token, all_tokens
aux_token_list = token_list
aux_word_list = word_list
old_list = []
while (True) :
old_list = aux_token_list
for p in pattern_list :
(aux_token_list,aux_word_list) = pattern_matcher(aux_token_list,aux_word_list,p)
if (aux_token_list == old_list) : break
all_tokens = aux_token_list
log.set_phrase(aux_word_list)
print("Tokens - After : ", all_tokens)
next_token = all_tokens.pop(0)
try :
PHRASE()
except GrammarException as e :
log.error = "Foi encontrado um termo do tipo %s, mas era esperado um termo do tipo %s" % (token_to_word(e.info[1]), token_to_word(e.info[0]))
except EndOfSentenceException as e :
log.error = "Fim de frase, esperava-se um termo do tipo %s" % token_to_word(e.info[0])
def pattern_matcher(token_list, word_list, pattern):
# print("Token list: ", list)
# print("Pattern: ", pattern)
if len(pattern) == 0 : return
new_token_list, token_buffer = [], []
new_word_list, word_buffer = [], []
i, j = 0, 0
pattern_break = False
while (i < len(token_list)) :
if pattern_break :
new_token_list.extend(token_buffer) ; new_word_list.extend(word_buffer)
token_buffer = [] ; word_buffer = []
j = 0
pattern_break = False
token_buffer.append(token_list[i])
word_buffer.append(word_list[i])
# print("Buffer: ", buffer)
if pattern == tuple(token_buffer) :
# print("Pattern found: ", pattern, " ==> ", pattern_list[pattern])
new_token_list.append(pattern_list[pattern])
new_word = ""
for w in word_buffer: new_word = new_word + w
new_word_list.append(new_word)
token_buffer = [] ; word_buffer = []
j = 0
elif len(token_buffer) != 0 and pattern[j] == token_buffer[j] :
j = j + 1
else :
if j > 0 : pattern_break = True
new_token_list.extend(token_buffer) ; new_word_list.extend(word_buffer)
token_buffer = [] ; word_buffer = []
j = 0
i = i + 1
if len(token_buffer) != 0 : new_token_list.extend(token_buffer) ; new_word_list.extend(word_buffer)
print(new_token_list)
print(new_word_list)
return (new_token_list,new_word_list)