-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_modify.py
208 lines (192 loc) · 8.02 KB
/
vector_modify.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
import gensim
import pymorphy2
import os
import re
model = gensim.models.KeyedVectors.load_word2vec_format("corpus_gensim/ruwikiruscorpora_0_300_20.bin.gz", binary=True)
model.init_sims(replace=True)
morph = pymorphy2.MorphAnalyzer()
punct = re.compile('^(.*?)([а-яА-ЯёЁ-]+)(.*?)$')
capit = re.compile('^[А-Я]+$')
pth_source = 'text_before/'
lst = os.listdir(pth_source)
print('LIST: ', lst)
pth_result = 'text_after/'
cotags = {'ADJF':'ADJ', # pymorphy2: word2vec
'ADJS' : 'ADJ',
'ADVB' : 'ADV',
'COMP' : 'ADV',
'GRND' : 'VERB',
'INFN' : 'VERB',
'NOUN' : 'NOUN',
'PRED' : 'ADV',
'PRTF' : 'ADJ',
'PRTS' : 'VERB',
'VERB' : 'VERB'
}
capit_letters = [chr(x) for x in range(1040,1072)] + ['Ё']
def search_neighbour(word, pos, gend='masc'):
word = word.replace('ё', 'е')
lex = word + '_' + cotags[pos]
if lex in model:
neighbs = model.most_similar([lex], topn=20)
for nei in neighbs:
lex_n, ps_n = nei[0].split('_')
if cotags[pos] == ps_n:
if pos == 'NOUN':
parse_result = morph.parse(lex_n)
for ana in parse_result:
if ana.normal_form == lex_n:
if ana.tag.gender == gend:
return lex_n
elif cotags[pos] == 'VERB' and word[-2:] == 'ся':
if lex_n[-2:] == 'ся':
return lex_n
elif cotags[pos] == 'VERB' and word[-2:] != 'ся':
if lex_n[-2:] != 'ся':
return lex_n
else:
return lex_n
return None
def flection(lex_neighb, tags):
tags = str(tags)
tags = re.sub(',[AGQSPMa-z-]+? ', ',', tags)
tags = tags.replace("impf,", "")
tags = re.sub('([A-Z]) (plur|masc|femn|neut|inan)', '\\1,\\2', tags)
tags = tags.replace("Impe neut", "")
tags = tags.split(',')
tags_clean = []
for t in tags:
if t:
if ' ' in t:
t1, t2 = t.split(' ')
t = t2
tags_clean.append(t)
tags = frozenset(tags_clean)
prep_for_gen = morph.parse(lex_neighb)
ana_array = []
for ana in prep_for_gen:
if ana.normal_form == lex_neighb:
ana_array.append(ana)
for ana in ana_array:
try:
flect = ana.inflect(tags)
except:
print(tags)
return None
if flect:
word_to_replace = flect.word
return word_to_replace
return None
print(model.most_similar(['холодный_ADJ'], topn=20))
cash_neighb = {}
for fl in lst:
# if not fl.endswith('_JOF.txt'):
# continue
print(fl)
i = 0
f = open(pth_source + fl, 'r', encoding='utf-8')
fw = open(pth_result + '3.0_' + fl, 'w', encoding='utf-8')
fs = open(pth_result + '3.0_Sample ' + fl, 'w', encoding='utf-8')
for line in f:
new_line = []
i += 1
line = line.strip()
words = line.split(' ')
for word in words:
struct = punct.findall(word)
if struct:
struct = struct[0]
else:
new_line.append(word)
continue
# print (struct)
wordform = struct[1]
if wordform:
if capit.search(wordform):
new_line.append(word)
continue
else:
if wordform[0] in capit_letters:
capit_flag = 1
else:
capit_flag = 0
parse_result = morph.parse(wordform)[0]
if 'Name' in parse_result.tag or 'Patr' in parse_result.tag:
new_line.append(word)
continue
if parse_result.normal_form == 'глава':
new_line.append(word)
continue
pos_flag = 0
for tg in cotags:
if tg in parse_result.tag:
pos_flag = 1
lex = parse_result.normal_form
pos_tag = parse_result.tag.POS
if (lex, pos_tag) in cash_neighb:
lex_neighb = cash_neighb[(lex, pos_tag)]
else:
if pos_tag == 'NOUN':
gen_tag = parse_result.tag.gender
lex_neighb = search_neighbour(lex, pos_tag, gend=gen_tag)
else:
lex_neighb = search_neighbour(lex, pos_tag)
cash_neighb[(lex, pos_tag)] = lex_neighb
if not lex_neighb:
new_line.append(word)
break
else:
if pos_tag == 'NOUN':
if parse_result.tag.case == 'nomn' and parse_result.tag.number == 'sing':
if capit_flag == 1:
lex_neighb = lex_neighb.capitalize()
new_line.append(struct[0] + lex_neighb + struct[2])
else:
word_to_replace = flection(lex_neighb, parse_result.tag)
if word_to_replace:
if capit_flag == 1:
word_to_replace = word_to_replace.capitalize()
new_line.append(struct[0] + word_to_replace + struct[2])
else:
new_line.append(word)
elif pos_tag == 'ADJF':
if parse_result.tag.case == 'nomn' and parse_result.tag.number == 'sing':
if capit_flag == 1:
lex_neighb = lex_neighb.capitalize()
new_line.append(struct[0] + lex_neighb + struct[2])
else:
word_to_replace = flection(lex_neighb, parse_result.tag)
if word_to_replace:
if capit_flag == 1:
word_to_replace = word_to_replace.capitalize()
new_line.append(struct[0] + word_to_replace + struct[2])
else:
new_line.append(word)
elif pos_tag == 'INFN':
if capit_flag == 1:
lex_neighb = lex_neighb.capitalize()
new_line.append(struct[0] + lex_neighb + struct[2])
elif pos_tag in ['ADVB', 'COMP', 'PRED']:
if capit_flag == 1:
lex_neighb = lex_neighb.capitalize()
new_line.append(struct[0] + lex_neighb + struct[2])
else:
word_to_replace = flection(lex_neighb, parse_result.tag)
if word_to_replace:
if capit_flag == 1:
word_to_replace = word_to_replace.capitalize()
new_line.append(struct[0] + word_to_replace + struct[2])
else:
new_line.append(word)
break
if pos_flag == 0:
new_line.append(word)
else:
new_line.append(''.join(struct))
line_replace = ' '.join(new_line)
if i < 21:
fs.write(line_replace + '\n')
fw.write(line_replace + '\n')
f.close()
fw.close()
fs.close()