-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
295 lines (237 loc) · 8.79 KB
/
main.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import re
import string
import sys
import nltk
from nltk.stem.snowball import SnowballStemmer
from pprint import pprint
from functools import reduce
__author__="Shail Shah"
#Earley Parser code
#--------------------------------------------------------------------------------------------------
def predictor(rule, state):
return [{
"lhs": rule["rhs"][rule["dot"]],
"rhs": rhs,
"dot": 0,
"state": state,
"op": "Predictor",
"completer": []
} for rhs in grammar[rule["rhs"][rule["dot"]]]] if rule["rhs"][rule["dot"]].isupper() else []
def scanner(rule, next_input):
return [{
"lhs": rule["rhs"][rule["dot"]],
"rhs": [next_input],
"dot": 1,
"state": rule["state"],
"op": "Scanner",
"completer": []
}] if rule["rhs"][rule["dot"]].islower() and next_input in grammar[rule["rhs"][rule["dot"]]] else []
def completer(rule, charts):
return list(map(
lambda filter_rule: {
"lhs": filter_rule["lhs"],
"rhs": filter_rule["rhs"],
"dot": filter_rule["dot"] + 1,
"state": filter_rule["state"],
"op": "Completer",
"completer": [rule] + filter_rule["completer"]
},
filter(
lambda p_rule: p_rule["dot"] < len(p_rule["rhs"]) and rule["lhs"] == p_rule["rhs"][p_rule["dot"]],
charts[rule["state"]]
)
)) if rule["dot"] == len(rule["rhs"]) else []
#Earley Parser End
#------------------------------------------------------------------------------------------------------------
#Function to print chart
#------------------------------------------------------------------------------------------------------------
def printCharts(charts, inp):
i=0
for chart_no, chart in zip(range(len(charts)), charts):
print("Chart "+str(chart_no)+"\t\n".join(map(
lambda x: "\t {0:>10} -> {1:<19} {2:^20} {3:^25}".format(
x["lhs"],
" ".join(x["rhs"][:x["dot"]] + ["*"] + x["rhs"][x["dot"]:]),
"[" + str(x["state"]) + "," + str(chart_no) + "]",
x["op"]
),
chart
)))
#Print chart function end
#--------------------------------------------------------------------------------------------------------------
#Stemmer Start
#--------------------------------------------------------------------------------------------------------------
def stemKeywords(word,stemmer):
stemWord=stemmer.stem(word)
return stemWord;
def checkInputType(token):
patternOP = re.compile("[.:|;=]")
patternString=re.compile("[a-zA-Z]+")
patternInt=re.compile("[0-9]+")
patternDouble=re.compile("\d[\.]\d")
if re.search(patternOP,token):
return token+" OP";
elif re.search(patternDouble, token):
return token+" DOUBLE";
elif re.search(patternInt, token):
return token+" INT";
elif re.search(patternString, token):
return token+" STRING";
return "";
#Stemmer End
#---------------------------------------------------------------------------------------------------------------
#File reading, input and initial processing code
#--------------------------------------------------------------------------------------------------------------
#Initial processing to read a file and take the input:
#Taking the input from dat file to a temporary text file which will be processed line by line and processed further
#-------------------------------------------------------------------------------------------------------------
f=open("temp.txt","w+")
for line in sys.stdin:
f.write(line.rstrip()+"\n")
f.close()
with open("temp.txt") as f:
content = f.readlines()
#Read file complete
#-----------------------------------------
#Tokenize the input and replace a few special characters
#--------------------------------------------------------------------
for n,i in enumerate(content):
s=i.replace(":"," : ")
s=s.replace("|"," | ")
s=s.replace("."," .")
content[n]=s.replace(";"," ; ")
#Printing the input file content
#---------------------------------------------------------------------
print("\n\nInput:")
print("{0:-^50}".format(""))
content = [x.split() for x in content]
for i in range(0,len(content)):
print(' '.join(content[i]))
#---------------------------------------------------------------------
#STEMMER-Initializing, conditional check, stemming and printing
#Using snowball stemmer here from NLTK
#--------------------------------------------------------------------
stemmer = SnowballStemmer("english")
print("\n\nStemmer:")
print("{0:-^30}".format(""))
k=1
flag=0
for i in range(0,len(content)):
for j in range(0,len(content[i])):
#print(content[i][j])
output=checkInputType(content[i][j])
#if("->" in content[i] or "-->" in content[i] or "$" in content[i] or "@" in content[i] or "^" in content[i]):
if("#" in content[i]):
flag=1
continue
if("W" in content[i] and "=" in content[i] and content[i][j]!="."):
exclude=set(string.punctuation)
parse_string=''.join(ch for ch in content[i][j] if ch not in exclude).lower()
output+=" "+stemKeywords(content[i][j],stemmer)
if(output==""):
print("\n\n\n\t Invalid Input \n\n\n")
sys.exit()
output+=" "+str(k)
print("\t"+output)
if(flag==0):
k+=1
flag=0
print("\tENDFILE")
#End of stemmer
#----------------------------------------------------------------------
#Earley - Parser begins
#----------------------------------------------------------------------
print("\n\nParsed Chart:")
parse_string=""
#Extract grammar from the input file and store it as a dictionary into "Grammar" variable for parser
#------------------------------------------------------------------------------------------------------
grammar={}
with open("temp.txt") as f:
line=f.readline()
tempSplit=str(line)
content=[]
while line:
if(tempSplit.strip().startswith("W")):
#print('Input found'+ tempSplit)
parse_string+=tempSplit[tempSplit.index("=")+1:].strip()
exclude=set(string.punctuation)
parse_string=''.join(ch for ch in parse_string if ch not in exclude).lower()
#print("Temp "+parse_string)
#tempSplit=str(line)
if("#" in tempSplit):
tempSplit=f.readline()
continue;
if(";" in tempSplit):
tempSplit=tempSplit.replace(";"," ")
temp=tempSplit.split(":")
hello=str(temp[1])
key=""
if(any(x.isupper() for x in hello)):
content=hello.split("|")
content=[x.strip() for x in content]
content=[x.split() for x in content]
for i in content:
for j in i:
#Splitting terminals and non terminals here
if('prep' in j.lower() or 'aux' in j.lower() or 'det' in j.lower() or 'noun' in j.lower() or'verb' in j.lower() or 'proper-noun' in j.lower() or 'pronoun' in j.lower()):
index_i=content.index(i)
content[index_i][content[index_i].index(j)]=j.lower()
if(j.upper()=='NOMINAL'):
index_i=content.index(i)
content[index_i][content[index_i].index(j)]=j.upper()
key=temp[0].strip().upper()
grammar[key]=content
else:
content=hello.split("|")
content=[x.strip() for x in content]
s={}
s=set(content)
key=temp[0].lower().strip()
grammar[key]=content
#print(content)
content=[]
tempSplit=""
tempSplit=f.readline()
continue;
line=f.readline()
tempSplit+=str(line)
charts = [[{
"lhs": "Root",
"rhs": ["S"],
"dot": 0,
"state": 0,
"op": "Dummy Start State",
"completer": []
}]]
#Stem the given input ready for parser:
temp=[]
for words in parse_string.split():
temp.append(stemKeywords(words,stemmer))
parse_string=' '.join(temp)
print("Stemmed string to be parsed: "+parse_string)
print("{0:-^94}".format(""))
#----------------------------------------------------------------------------------------------
#Parse the given input string
#-----------------------------------------------------------------------------------------------
input_arr =parse_string.split()+[""]
for curr_state in range(len(input_arr)):
curr_chart = charts[curr_state]
next_chart = []
for curr_rule in curr_chart:
if curr_rule["dot"] < len(curr_rule["rhs"]):
curr_chart += [i for i in predictor(curr_rule, curr_state) if i not in curr_chart]
next_chart += [i for i in scanner(curr_rule, input_arr[curr_state]) if i not in next_chart]
else:
curr_chart += [i for i in completer(curr_rule, charts) if i not in curr_chart]
charts.append(next_chart)
printCharts(charts[:-1], input_arr)
print("{0:-^94}".format(""))
#Parser End
#----------------------------------------------------------------------------------------
#Truncate temp file to be ready for next program run
#----------------------------------------------------------------------------------------
f = open('temp.txt', 'r+')
f.truncate()
f.close()
#End of program
#---------------------------------------------------------------------------------------------