-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
375 lines (324 loc) · 11.8 KB
/
parser.py
File metadata and controls
375 lines (324 loc) · 11.8 KB
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
import sys
import re
from elasticsearch import Elasticsearch
from yandex_translate import YandexTranslate
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
class BilingualPraser():
def __init__(self, file1, file2, lang1, lang2):
self.es = Elasticsearch()
self.translate = YandexTranslate('trnsl.1.1.20161120T092429Z.26536300f1fab524.bb5fcc81494303125cb46f52681dfdcf652477e0')
self.file1 = file1
self.file2 = file2
self.file1_lang = lang1
self.file2_lang = lang2
self.paragraphLinks = []
self.withoutWhiteChars = re.compile(r'[\n\t \r]+')
self.nonAlphaNumeric = re.compile(r'^\W+$')
self.endOfSetnence = re.compile(r'([\.\?\!:])')
self.sentences_pair = []
self.getNameWords = re.compile(r'([A-ZА-Я]\w+)')
self.onlyLetters = re.compile(r'[^a-zA-Zа-яА-Я ]+')
self.MIN_RATE = 75
self.synced_sentences = []
self.nonsynced_sentences = {'first': [], 'second': [], 'keywords': []}
def parse(self):
par1 = parSplit(self.file1.readlines())
par2 = parSplit(self.file2.readlines())
self.firstPhase(par1, par2)
self.secondPhase()
self.splitSentences()
return self.synced_sentences
def firstPhase(self, pars1, pars2):
for i in range(0, len(pars1)):
pars1[i] = self.withoutWhiteChars.sub(' ', pars1[i])
self.paragraphLinks.append({'first': pars1[i]})
for i in range(0, len(pars2)):
if i == len(self.paragraphLinks):
pars2[i] = self.withoutWhiteChars.sub(' ', pars2[i])
self.paragraphLinks.append({'second': pars2[i]})
else:
pars2[i] = self.withoutWhiteChars.sub(' ', pars2[i])
self.paragraphLinks[i]['second'] = pars2[i]
def parSplit(self, file_lines):
paragraphs = []
curr_par = 0
par_whitespace = False
for i in range(0, len(file_lines)):
if file_lines[i][0] == ' ' \
or file_lines[i][0] == '\t' \
or file_lines[i][0] == '\n':
file_lines[i] = file_lines[i].strip()
if len(file_lines[i]) == 0:
par_whitespace = True
else:
if self.nonAlphaNumeric.match(file_lines[i]) == None:
paragraphs.append(file_lines[i])
curr_par = len(paragraphs)
par_whitespace = False
else:
par_whitespace = True
continue
if par_whitespace:
paragraphs.append('')
curr_par = len(paragraphs)
par1_whitespace = False
paragraphs[curr_par - 1] += ' ' + file_lines[i]
return paragraphs
# def secondPhase():
def getSentence(self, par):
for pars in self.paragraphLinks:
if par in pars.keys():
# print('/-----------------------')
sentences = self.endOfSetnence.split(pars[par])
sentences_parsed = []
newSentence = True
curr_len = 0
additional = ''
brace_opened = False
# print('/-----------------------')
# print(sentences)
for sentence in sentences:
if self.nonAlphaNumeric.match(sentence) != None:
if newSentence:
if sentence == '\"':
brace_opened = True
additional = sentence
newSentence = False
else:
if curr_len > 0:
if sentence == '\"':
if brace_opened:
brace_opened = False
sentences_parsed[curr_len - 1] += sentence
else:
brace_opened = True
additional = sentence
else:
sentences_parsed[curr_len - 1] += sentence
else:
additional += sentence
else:
if len(sentence) == 0:
continue
sentences_parsed.append(additional + sentence)
additional = ''
curr_len = len(sentences_parsed)
newSentence = False
pars[par + '_sentences'] = sentences_parsed
# for pars in paragraphLinks:
# if 'first' in pars.keys():
# print('/--------------------------')
# print(pars['first'])
# if 'second' in pars.keys():
# print('/--------------------------')
# print(pars['second'])
def secondPhase(self):
self.getSentence('first')
self.getSentence('second')
sentence_count = []
for par in self.paragraphLinks:
if 'first_sentences' in par.keys():
sentence_count.append(len(par['first_sentences']))
for sent in par['first_sentences']:
self.sentences_pair.append({'first': sent})
offset = 0
secondOffset = 0
paragraphId = 0
maxOffset = sentence_count[0]
for par in self.paragraphLinks:
if 'second_sentences' in par.keys():
for sent in par['second_sentences']:
if len(self.sentences_pair) > offset:
self.sentences_pair[offset]['second'] = sent
curr_par = self.paragraphLinks[paragraphId]
if not 'second_phase' in curr_par.keys():
curr_par['second_phase'] = []
curr_par[
'second_phase'].append(self.sentences_pair[offset])
else:
self.sentences_pair.append({'second': sent})
offset += 1
secondOffset += 1
if maxOffset == secondOffset:
if (len(sentence_count) - 1) <= paragraphId:
maxOffset = len(par['second_sentences'])
else:
secondOffset = 0
paragraphId += 1
maxOffset = sentence_count[paragraphId]
def updateTranslateElastica(self, data_id, lang, word):
insert_body = {'doc': {}}
insert_body['doc']['word_' + lang] = word
resp = self.es.update(index='languages', doc_type='translates', id=data_id, body=insert_body)
if resp['result'] != 'updated':
print(resp)
def addWordToElasticaOnlyIfTranslateExists(self, word_not_translated, word_translated, lang1, lang2):
body = {"query": {'bool': {'must': [{'match': {}}]}}}
body['query']['bool']['must'][0]['match']['word_' + lang2] = word_translated
print(body)
res = self.es.search(index="languages", doc_type="translates", body=body)
if res['hits']['total'] == 0:
insert_body = {}
insert_body['word_' + lang1] = word_not_translated
insert_body['word_' + lang2] = word_translated
resp = es.index(index='languages', doc_type='translates', body=insert_body)
if resp['result'] != 'created':
print('Word ' + word_translated + ' was not inserted')
else:
data_id = res['hits']['hits'][0]['_id']
self.updateTranslateElastica(data_id, lang1, word_not_translated)
def getTranslate(self,word, lang1, lang2):
body = {"query": {'bool': {'must': [{'match': {}}]}}}
body['query']['bool']['must'][0]['match']['word_' + lang1] = word
res = self.es.search(index="languages", doc_type="translates", body=body)
if res['hits']['total'] == 0:
translate_res = self.translate.translate(word, lang1 + '-' + lang2)
if translate_res['code'] == 200:
self.addWordToElasticaOnlyIfTranslateExists(word, translate_res['text'][0], lang1, lang2)
# print(translate_res['text'])
return translate_res['text'][0]
else:
print(translate_res)
return None
else:
if not 'word_' + lang2 in res['hits']['hits'][0]['_source'].keys():
data_id = res['hits']['hits'][0]['_id']
translate_res = self.translate.translate(word, lang1 + '-' + lang2)
if translate_res['code'] == 200:
self.updateTranslateElastica(data_id, lang2, translate_res['text'][0])
return translate_res['text'][0]
else:
print(translate_res)
return None
else:
# print(res)
return res['hits']['hits'][0]['_source']['word_' + lang2]
def getSentenceRate(self, keyWords, sentence):
# sentence = ' '.join(sentences)
cleared_sentence = self.onlyLetters.sub('', sentence)
word_array = cleared_sentence.split(' ')
rate = 0.0
# print('getSentenceRate')
# print('//------------')
# print(keyWords)
# print('//------------')
# print(sentence)
for word in keyWords:
res = process.extractOne(word, word_array)
if res != None:
if res[1] > MIN_RATE:
rate += res[1]
word_array.remove(res[0])
rate = rate / len(keyWords)
return rate
def getSentenceSize(self, sentence):
cleared_sentence = self.onlyLetters.sub('', sentence)
word_array = cleared_sentence.split(' ')
# word_array = list(filter(lambda x: len(x) > 2, word_array))
return len(word_array)
def getSentenceSub(self, sentence1, sentence2):
# sentence1 = ' '.join(sentences1)
# sentence2 = ' '.join(sentences2)
# print(sentence1, sentence2)
cleared_sentence1 = self.onlyLetters.sub('', sentence1)
cleared_sentence2 = self.onlyLetters.sub('', sentence2)
word_array1 = cleared_sentence1.split(' ')
word_array2 = cleared_sentence2.split(' ')
# word_array1 = list(filter(lambda x: len(x) > 2, word_array1))
# word_array2 = list(filter(lambda x: len(x) > 2, word_array2))
# print(word_array1)
# print(word_array2)
sub = abs(len(word_array1) - len(word_array2))
# print(sub)
maxWords = max(len(word_array1), len(word_array2))
rate = 100 / float(maxWords) * (maxWords - sub)
# print(rate)
return rate
def splitSentences(self):
for par in self.sentences_pair:
par['keywords1'] = []
if 'first' in par.keys():
cleared_sentence = self.onlyLetters.sub('', par['first'])
word_array = cleared_sentence.split(' ')
keyWords = self.getNameWords.findall(par['first'])
keyWords = list(filter(lambda x: len(x) > 2, keyWords))
keyWords.append(word_array[-1])
# print(keyWords)
if len(keyWords) > 0:
for i in range(0, len(keyWords)):
res = self.getTranslate(keyWords[i], file1_lang, self.file2_lang)
# print(res)
# print('^')
if res != None:
par['keywords1'].append(res)
j = 0
i = 0
sync = True
offset = 0
reverse = False
offsetoffset = 0
nonsynced_count = 0
ns_sentence1_size = 0
ns_sentence2_size = 0
j_changed = True
i_changed = True
while i < len(self.sentences_pair) and j < len(self.sentences_pair):
if 'first' in self.sentences_pair[i].keys() and 'second' in self.sentences_pair[j].keys():
if i_changed:
self.nonsynced_sentences['first'].append(self.sentences_pair[i]['first'])
self.nonsynced_sentences['keywords'].extend(self.sentences_pair[i]['keywords1'])
ns_sentence1_size += self.getSentenceSize(self.sentences_pair[i]['first'])
if j_changed:
self.nonsynced_sentences['second'].append(self.sentences_pair[j]['second'])
ns_sentence2_size += self.getSentenceSize(self.sentences_pair[j]['second'])
i_changed = False
j_changed = False
rate = 0.0
rate += self.getSentenceSub(self.sentences_pair[i]['first'], self.sentences_pair[j]['second'])
print('//---------------------------')
print('Length 1: ', ns_sentence1_size)
print('Length 2: ', ns_sentence2_size)
print('Rate 1:', rate)
if len(self.sentences_pair[i]['keywords1']) > 0:
rate1 = getSentenceRate(self.sentences_pair[i]['keywords1'], self.sentences_pair[j]['second'])
print('Rate 2:', rate1)
rate += rate1
rate /= 2
print(self.sentences_pair[i]['first'], self.sentences_pair[j]['second'])
print('Rate: ', rate)
print('//-----------------------------')
if rate < self.MIN_RATE:
sync = False
if ns_sentence1_size <= ns_sentence2_size:
i_changed = True
i += 1
else:
j_changed = True
j += 1
continue
else:
self.synced_sentences.append({'first': ' '.join(self.nonsynced_sentences['first']), 'second': ' '.join(self.nonsynced_sentences['second'])})
self.nonsynced_sentences['first'] = []
self.nonsynced_sentences['second'] = []
self.nonsynced_sentences['keywords'] = []
ns_sentence1_size = 0
ns_sentence2_size = 0
j += 1
i += 1
i_changed = True
j_changed = True
else:
while i < len(self.sentences_pair) and 'first' in self.sentences_pair[i].keys():
self.nonsynced_sentences['first'].append(self.sentences_pair[i]['first'])
i += 1
while j < len(self.sentences_pair) and 'second' in self.sentences_pair[j].keys():
self.nonsynced_sentences['second'].append(self.sentences_pair[j]['second'])
j += 1
break
if len(self.nonsynced_sentences['first']) > 0:
self.synced_sentences.append({'first': ''.join(self.nonsynced_sentences['first']), 'second': ''.join(self.nonsynced_sentences['second'])})
# for par in synced_sentences:
# print('/-----------------------------------')
# print(par['first'])
# print(par['second'])