-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
402 lines (314 loc) · 12.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import json
import os
# from nltk.parse import stanford
import stanza
stanza.download('en',model_dir='stanza_resources')
# stanza.install_corenlp()
# from nltk.stem import WordNetLemmatizer
# from nltk.tokenize import word_tokenize
# from nltk.tokenize import sent_tokenize
# from nltk.corpus import stopwords
from nltk.parse.stanford import StanfordParser
from nltk.tree import *
from six.moves import urllib
import zipfile
import sys
import time
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from flask import Flask,request,render_template,send_from_directory,jsonify
app =Flask(__name__,static_folder='static', static_url_path='')
import stanza
# from stanza.server import CoreNLPClient
import pprint
# These few lines are important
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
# Download zip file from https://nlp.stanford.edu/software/stanford-parser-full-2018-10-17.zip and extract in stanford-parser-full-2015-04-20 folder in higher directory
os.environ['CLASSPATH'] = os.path.join(BASE_DIR, 'stanford-parser-full-2018-10-17')
os.environ['STANFORD_MODELS'] = os.path.join(BASE_DIR,
'stanford-parser-full-2018-10-17/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz')
os.environ['NLTK_DATA'] = '/usr/local/share/nltk_data/'
# checks if jar file of stanford parser is present or not
def is_parser_jar_file_present():
stanford_parser_zip_file_path = os.environ.get('CLASSPATH') + ".jar"
return os.path.exists(stanford_parser_zip_file_path)
def reporthook(count, block_size, total_size):
global start_time
if count == 0:
start_time = time.perf_counter()
return
duration = time.perf_counter() - start_time
progress_size = int(count * block_size)
speed = int(progress_size / (1024 * duration))
percent = min(int(count*block_size*100/total_size),100)
sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" %
(percent, progress_size / (1024 * 1024), speed, duration))
sys.stdout.flush()
# downloads stanford parser
def download_parser_jar_file():
stanford_parser_zip_file_path = os.environ.get('CLASSPATH') + ".jar"
url = "https://nlp.stanford.edu/software/stanford-parser-full-2018-10-17.zip"
urllib.request.urlretrieve(url, stanford_parser_zip_file_path, reporthook)
# extracts stanford parser
def extract_parser_jar_file():
stanford_parser_zip_file_path = os.environ.get('CLASSPATH') + ".jar"
try:
with zipfile.ZipFile(stanford_parser_zip_file_path) as z:
z.extractall(path=BASE_DIR)
except Exception:
os.remove(stanford_parser_zip_file_path)
download_parser_jar_file()
extract_parser_jar_file()
# extracts models of stanford parser
def extract_models_jar_file():
stanford_models_zip_file_path = os.path.join(os.environ.get('CLASSPATH'), 'stanford-parser-3.9.2-models.jar')
stanford_models_dir = os.environ.get('CLASSPATH')
with zipfile.ZipFile(stanford_models_zip_file_path) as z:
z.extractall(path=stanford_models_dir)
# checks jar file and downloads if not present
def download_required_packages():
if not os.path.exists(os.environ.get('CLASSPATH')):
if is_parser_jar_file_present():
pass
else:
download_parser_jar_file()
extract_parser_jar_file()
if not os.path.exists(os.environ.get('STANFORD_MODELS')):
extract_models_jar_file()
# Pipeline for stanza (calls spacy for tokenizer)
en_nlp = stanza.Pipeline('en',processors={'tokenize':'spacy'})
# print(stopwords.words('english'))
# stop words that are not to be included in ISL
stop_words = set(["am","are","is","was","were","be","being","been","have","has","had",
"does","did","could","should","would","can","shall","will","may","might","must","let"]);
# sentences array
sent_list = [];
# sentences array with details provided by stanza
sent_list_detailed=[];
# word array
word_list=[];
# word array with details provided by stanza
word_list_detailed=[];
# converts to detailed list of sentences ex. {"text":"word","lemma":""}
def convert_to_sentence_list(text):
for sentence in text.sentences:
sent_list.append(sentence.text)
sent_list_detailed.append(sentence)
# converts to words array for each sentence. ex=[ ["This","is","a","test","sentence"]];
def convert_to_word_list(sentences):
temp_list=[]
temp_list_detailed=[]
for sentence in sentences:
for word in sentence.words:
temp_list.append(word.text)
temp_list_detailed.append(word)
word_list.append(temp_list.copy())
word_list_detailed.append(temp_list_detailed.copy())
temp_list.clear();
temp_list_detailed.clear();
# removes stop words
def filter_words(word_list):
temp_list=[];
final_words=[];
# removing stop words from word_list
for words in word_list:
temp_list.clear();
for word in words:
if word not in stop_words:
temp_list.append(word);
final_words.append(temp_list.copy());
# removes stop words from word_list_detailed
for words in word_list_detailed:
for i,word in enumerate(words):
if(words[i].text in stop_words):
del words[i];
break;
return final_words;
#
# removes punctutation
def remove_punct(word_list):
# removes punctutation from word_list_detailed
for words,words_detailed in zip(word_list,word_list_detailed):
for i,(word,word_detailed) in enumerate(zip(words,words_detailed)):
if(word_detailed.upos=='PUNCT'):
del words_detailed[i];
words.remove(word_detailed.text);
break;
# lemmatizes words
def lemmatize(final_word_list):
for words,final in zip(word_list_detailed,final_word_list):
for i,(word,fin) in enumerate(zip(words,final)):
if fin in word.text:
if(len(fin)==1):
final[i]=fin;
else:
final[i]=word.lemma;
for word in final_word_list:
print("final_words",word);
def label_parse_subtrees(parent_tree):
tree_traversal_flag = {}
for sub_tree in parent_tree.subtrees():
tree_traversal_flag[sub_tree.treeposition()] = 0
return tree_traversal_flag
# handles if noun is in the tree
def handle_noun_clause(i, tree_traversal_flag, modified_parse_tree, sub_tree):
# if clause is Noun clause and not traversed then insert them in new tree first
if tree_traversal_flag[sub_tree.treeposition()] == 0 and tree_traversal_flag[sub_tree.parent().treeposition()] == 0:
tree_traversal_flag[sub_tree.treeposition()] = 1
modified_parse_tree.insert(i, sub_tree)
i = i + 1
return i, modified_parse_tree
# handles if verb/proposition is in the tree followed by nouns
def handle_verb_prop_clause(i, tree_traversal_flag, modified_parse_tree, sub_tree):
# if clause is Verb clause or Proportion clause recursively check for Noun clause
for child_sub_tree in sub_tree.subtrees():
if child_sub_tree.label() == "NP" or child_sub_tree.label() == 'PRP':
if tree_traversal_flag[child_sub_tree.treeposition()] == 0 and tree_traversal_flag[child_sub_tree.parent().treeposition()] == 0:
tree_traversal_flag[child_sub_tree.treeposition()] = 1
modified_parse_tree.insert(i, child_sub_tree)
i = i + 1
return i, modified_parse_tree
# modifies the tree according to POS
def modify_tree_structure(parent_tree):
# Mark all subtrees position as 0
tree_traversal_flag = label_parse_subtrees(parent_tree)
# Initialize new parse tree
modified_parse_tree = Tree('ROOT', [])
i = 0
for sub_tree in parent_tree.subtrees():
if sub_tree.label() == "NP":
i, modified_parse_tree = handle_noun_clause(i, tree_traversal_flag, modified_parse_tree, sub_tree)
if sub_tree.label() == "VP" or sub_tree.label() == "PRP":
i, modified_parse_tree = handle_verb_prop_clause(i, tree_traversal_flag, modified_parse_tree, sub_tree)
# recursively check for omitted clauses to be inserted in tree
for sub_tree in parent_tree.subtrees():
for child_sub_tree in sub_tree.subtrees():
if len(child_sub_tree.leaves()) == 1: #check if subtree leads to some word
if tree_traversal_flag[child_sub_tree.treeposition()] == 0 and tree_traversal_flag[child_sub_tree.parent().treeposition()] == 0:
tree_traversal_flag[child_sub_tree.treeposition()] = 1
modified_parse_tree.insert(i, child_sub_tree)
i = i + 1
return modified_parse_tree
# converts the text in parse trees
def reorder_eng_to_isl(input_string):
download_required_packages();
# check if all the words entered are alphabets.
count=0
for word in input_string:
if(len(word)==1):
count+=1;
if(count==len(input_string)):
return input_string;
parser = StanfordParser()
# Generates all possible parse trees sort by probability for the sentence
possible_parse_tree_list = [tree for tree in parser.parse(input_string)]
print("i am testing this",possible_parse_tree_list)
# Get most probable parse tree
parse_tree = possible_parse_tree_list[0]
# print(parse_tree)
# Convert into tree data structure
parent_tree = ParentedTree.convert(parse_tree)
modified_parse_tree = modify_tree_structure(parent_tree)
parsed_sent = modified_parse_tree.leaves()
return parsed_sent
# final word list
final_words= [];
# final word list that is detailed(dict)
final_words_detailed=[];
# pre processing text
def pre_process(text):
remove_punct(word_list)
final_words.extend(filter_words(word_list));
lemmatize(final_words)
# checks if sigml file exists of the word if not use letters for the words
def final_output(input):
final_string=""
valid_words=open("words.txt",'r').read();
valid_words=valid_words.split('\n')
fin_words=[]
for word in input:
word=word.lower()
if(word not in valid_words):
for letter in word:
# final_string+=" "+letter
fin_words.append(letter);
else:
fin_words.append(word);
return fin_words
final_output_in_sent=[];
# converts the final list of words in a final list with letters seperated if needed
def convert_to_final():
for words in final_words:
final_output_in_sent.append(final_output(words));
# takes input from the user
def take_input(text):
test_input=text.strip().replace("\n","").replace("\t","")
test_input2=""
if(len(test_input)==1):
test_input2=test_input;
else:
for word in test_input.split("."):
test_input2+= word.capitalize()+" .";
# pass the text through stanza
some_text= en_nlp(test_input2);
convert(some_text);
def convert(some_text):
convert_to_sentence_list(some_text);
convert_to_word_list(sent_list_detailed)
# reorders the words in input
for i,words in enumerate(word_list):
word_list[i]=reorder_eng_to_isl(words)
# removes punctuation and lemmatizes words
pre_process(some_text);
convert_to_final();
remove_punct(final_output_in_sent)
print_lists();
def print_lists():
print("--------------------Word List------------------------");
pprint.pprint(word_list)
print("--------------------Final Words------------------------");
pprint.pprint(final_words);
print("---------------Final sentence with letters--------------")
pprint.pprint(final_output_in_sent)
# clears all the list after completing the work
def clear_all():
sent_list.clear();
sent_list_detailed.clear();
word_list.clear();
word_list_detailed.clear();
final_words.clear();
final_words_detailed.clear();
final_output_in_sent.clear();
final_words_dict.clear();
# dict for sending data to front end in json
final_words_dict = {};
@app.route('/',methods=['GET'])
def index():
clear_all();
return render_template('index.html')
@app.route('/',methods=['GET','POST'])
def flask_test():
clear_all();
text = request.form.get('text') #gets the text data from input field of front end
print("text is", text)
if(text==""):
return "";
take_input(text)
# fills the json
for words in final_output_in_sent:
for i,word in enumerate(words,start=1):
final_words_dict[i]=word;
print("---------------Final words dict--------------");
for key in final_words_dict.keys():
if len(final_words_dict[key])==1:
final_words_dict[key]=final_words_dict[key].upper()
print(final_words_dict)
print(final_words_dict)
return final_words_dict;
# serve sigml files for animation
@app.route('/static/<path:path>')
def serve_signfiles(path):
print("here");
return send_from_directory('static',path)
if __name__=="__main__":
app.run(host='0.0.0.0')