-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathturbo_demo.py
executable file
·220 lines (195 loc) · 9.63 KB
/
turbo_demo.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
from flask import Flask, request, render_template
from flask.ext.wtf import Form
from wtforms import Form, BooleanField, TextField, TextAreaField, PasswordField, RadioField, SelectField, validators
import os
import sys
sys.path.append(os.getcwd())
import nlp_pipeline
pipeline = nlp_pipeline.NLPPipeline()
import threading
#import time
import json
app = Flask(__name__)
lock = threading.Lock()
class TurboDemoForm(Form):
sentence = TextAreaField('Write the sentence here:',
[validators.Length(min=1, max=100000)])
language = SelectField('Language:',
choices=[#('PT', 'Portuguese'),
#('ES', 'Spanish'),
#('EN', 'English'),
#('EN-Nonprojective', 'English-Nonprojective'),
#('PT-BR-Universal', 'Brazilian Portuguese-Universal'),
#('DE-Universal', 'German-Universal'),
('EN-Universal', 'English-Universal'),
('PT-Universal', 'Portuguese-Universal'),
('ES-Universal', 'Spanish-Universal'),
('FR-Universal', 'French-Universal'),
('IT-Universal', 'Italian-Universal')])
entity_tagged_sentence = ''
parsed_sentence = ''
parsed_sentence_json = '{}'
semantic_parsed_sentence = ''
semantic_parsed_sentence_json = '{}'
coref_document = ''
decorated_coref_document = ''
coref_document_json = '{}'
@app.route('/turbo_demo', methods=['GET', 'POST'])
def turbo_demo():
form = TurboDemoForm(request.form)
if request.method == 'POST' and form.validate():
#text = form.sentence.data.encode('utf-8')
text = form.sentence.data # Problems in Spanish with 'A~nos. E'
language = form.language.data
print "Lock acquire"
lock.acquire()
#import pdb
#pdb.set_trace()
sentences = pipeline.split_sentences(text, language)
sentences = [s.encode('utf-8') for s in sentences]
entity_tagged_sentence = ''
parsed_sentence = ''
semantic_parsed_sentence = ''
coref_document = ''
decorated_coref_document = ''
if 'parse' in request.form:
# Only process the first sentence.
sentence = sentences[0]
#parsed_sentence = pipeline.parse_conll(sentence, language)
tokenized_sentence = pipeline.tokenize(sentence, language)
tags, lemmas, feats = pipeline.tag(tokenized_sentence, language)
if pipeline.has_entity_recognizer(language):
entity_tags = pipeline.recognize_entities(tokenized_sentence,
tags, language)
entity_tagged_sentence = ''
prefixes = [tag[:1] for tag in entity_tags]
entities = [tag[2:] for tag in entity_tags]
for i, token in enumerate(tokenized_sentence):
if prefixes[i] == 'B':
entity_tagged_sentence += '[' + entities[i] + ' '
entity_tagged_sentence += token + ' '
if prefixes[i] != 'O' and \
(i == len(tokenized_sentence)-1 or prefixes[i+1] != 'I'):
entity_tagged_sentence += '] '
print entity_tagged_sentence
else:
entity_tagged_sentence = ''
# Currently, discard the lemmas when parsing.
# TODO: train models with predicted lemmas and predicted morph tags.
fake_lemmas = ['_' for lemma in lemmas]
heads, deprels = pipeline.parse(tokenized_sentence, tags, \
fake_lemmas, language)
parsed_sentence = ''
for i, token in enumerate(tokenized_sentence):
parsed_sentence += str(i+1) + '\t' + token + '\t' + lemmas[i] + \
'\t' + tags[i] + '\t' + tags[i] + '\t' + feats[i] + '\t' + str(heads[i]+1) + \
'\t' + deprels[i] + '\n'
parsed_sentence += '\n'
if pipeline.has_semantic_parser(language):
predicates, argument_lists = \
pipeline.parse_semantic_dependencies(tokenized_sentence, tags,
lemmas, heads, deprels,
language)
semantic_parsed_sentence = ''
for i, token in enumerate(tokenized_sentence):
semantic_output = predicates[i] + '\t' + '\t'.join(argument_lists[i])
#if len(argument_lists[i]) > 0:
# semantic_output += '\t' + '\t'.join(argument_lists[i])
semantic_parsed_sentence += str(i+1) + '\t_\t_\t_\t_\t' + token \
+ '\t' + lemmas[i] + '\t' + tags[i] + '\t' + str(heads[i]+1) \
+ '\t' + deprels[i] + '\t' + semantic_output + '\n'
semantic_parsed_sentence += '\n'
else:
semantic_parsed_sentence = ''
elif 'resolve_coreferences' in request.form:
if pipeline.has_coreference_resolver(language):
all_tokenized_sentences = []
all_tags = []
all_lemmas = []
all_heads = []
all_deprels = []
all_entity_tags = []
# Process all sentences.
for sentence in sentences:
tokenized_sentence = pipeline.tokenize(sentence, language)
tags, lemmas, feats = pipeline.tag(tokenized_sentence, language)
heads, deprels = pipeline.parse(tokenized_sentence, tags, lemmas, language)
# TODO(atm): replace this by actual entity tags.
entity_tags = ['*' for i in xrange(len(tags))]
all_tokenized_sentences.append(tokenized_sentence)
all_tags.append(tags)
all_lemmas.append(lemmas)
all_heads.append(heads)
all_deprels.append(deprels)
all_entity_tags.append(entity_tags)
all_coref_info = pipeline.resolve_coreferences(all_tokenized_sentences,
all_tags,
all_lemmas,
all_heads,
all_deprels,
all_entity_tags,
language)
coref_document = ''
for j, tokenized_sentence in enumerate(all_tokenized_sentences):
tags = all_tags[j]
lemmas = all_lemmas[j]
heads = all_heads[j]
deprels = all_deprels[j]
entity_tags = all_entity_tags[j]
coref_info = all_coref_info[j]
for i, token in enumerate(tokenized_sentence):
tag = tags[i]
lemma = lemmas[i]
head = heads[i]
deprel = deprels[i]
entity_tag = entity_tags[i]
coref = coref_info[i]
coref_document += '\t'.join(['_', '0', str(i+1), token, tag,
'*', str(head+1), deprel,
'-', '-', '-', '-', entity_tag,
coref])
coref_document += '\n'
coref_document += '\n'
#print coref_document
else:
assert False
#time.sleep(5)
print "Lock release"
lock.release()
form.entity_tagged_sentence = entity_tagged_sentence.decode('utf-8')
form.parsed_sentence = parsed_sentence.decode('utf-8')
form.semantic_parsed_sentence = semantic_parsed_sentence.decode('utf-8')
form.coref_document = coref_document.decode('utf-8')
import conll_to_json as ctj
lines = form.parsed_sentence.split('\n')
conll_string = ''
for line in lines:
line = line.rstrip('\n')
if line == '':
conll_string += '\n'
else:
conll_string += line + '\t_\t_\n'
json_string = ctj.encode_conll(conll_string)
form.parsed_sentence_json = json.dumps(json_string)
print form.parsed_sentence_json
lines = form.semantic_parsed_sentence.split('\n')
conll2008_string = ''
for line in lines:
line = line.rstrip('\n')
if line == '':
conll2008_string += '\n'
else:
conll2008_string += line + '\n'
json_string = ctj.encode_conll2008(conll2008_string)
form.semantic_parsed_sentence_json = json.dumps(json_string)
print form.semantic_parsed_sentence_json
import pdb
import decorate_coref as dc
#pdb.set_trace()
decorated_coref_document = dc.decorate_coref(form.coref_document)
#print decorated_coref_document
form.decorated_coref_document = decorated_coref_document
return render_template('turbo_demo.html', form=form)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')
#app.run(host='0.0.0.0')