-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangrycritic.py
439 lines (379 loc) · 18.9 KB
/
angrycritic.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import yaml
from yaml.loader import SafeLoader
import os
import pandas as pd
from pymorphy3 import MorphAnalyzer
import string
import time
from tqdm import tqdm
from nltk.corpus import stopwords
from nltk.probability import FreqDist
import jarowinkler
def main():
global skip
global types_keywords
global replace
global rustopwords
rustopwords = stopwords.words('russian')
start_time = time.time()
active_types = []
if os.path.exists('data.xlsx'):
print("Файл обнаружен. Начинаем обработку...")
df = pd.read_excel('data.xlsx', index_col=None, header=0)
new_df = df.assign(Lemma='', Type='', Type_Keywords='', Mood='', Mood_Keywords='', Index='')
arr = new_df.to_numpy()
with open('./data/skip.yaml', encoding='utf8') as f:
skip = yaml.load(f, Loader=SafeLoader)
with open('./data/types.yaml', encoding='utf8') as f:
types_keywords = yaml.load(f, Loader=SafeLoader)
# with open('./data/replace.yaml', encoding='utf8') as f:
# replace = yaml.load(f, Loader=SafeLoader)
for item in types_keywords:
# if item != 'undef' and item != 'presence_kw':
active_types.append(item)
# else:
# continue
prep = prepare(arr)
prepdf = pd.DataFrame(prep[0])
countdf = pd.DataFrame(prep[1])
prepdf.columns= ['Text', 'Lemma', 'Type', 'Type_Keywords', 'Mood', 'Mood_Keywords', 'Index']
countdf.columns= ['Слово', 'Частота']
filtred = prepdf.query("Type in ('skip', 'undef')").to_numpy()
df1 = pd.DataFrame(filtred)
frames = []
data = {}
writer = pd.ExcelWriter('data_done.xlsx', engine='xlsxwriter')
for item in active_types:
val = str(item).lower()
df2 = prepdf.query("Type == @val")
arr2 = df2.to_numpy()
new_arr = mood_define(arr2, item)
mood_df = pd.DataFrame(new_arr[0])
frames.append(mood_df)
if len(new_arr[1]) > 0:
data[item] = new_arr[1]
frames.append(df1)
result = pd.concat(frames)
result.columns= ['Text', 'Lemma', 'Type', 'Type_Keywords', 'Mood', 'Mood_Keywords', 'Index']
result.to_excel(writer, sheet_name='Total', index=False)
countdf.to_excel(writer, sheet_name='Частота по документу', index=False)
for k, v in data.items():
r = pd.DataFrame(v)
r.columns = ['Слово', 'Частота']
r.to_excel(writer, sheet_name=k, index=False)
writer.close()
end_time = time.time()
execution_time = end_time - start_time
print(f"Время выполнения программы: {execution_time} секунд")
print("Обработка выполнена! Проверьте файл data_done.xlsx")
else:
print("Файл с данными (data.xlsx) не найден. Пожалуйста проверьте их наличие в папке с программой!")
def prepare(arr):
print('Начинаем предварительную обработку...')
array = arr
morph = MorphAnalyzer()
textcount = []
for item in tqdm(array):
time.sleep(0.001)
tokens = []
keyword = []
type_arr = []
new_text = ''
char = ''
skip_status = 0
spec_chars = string.punctuation + '\xa0«»\t—…'
text = str(item[0]).lower().replace('-', ' ')
text = str(item[0]).lower().replace('/', ' ')
text = str(item[0]).lower().replace('.', ' ')
text = str(item[0]).lower().replace(',', ' ')
text = str(item[0]).lower().replace('¶', ' ')
text = "".join([ch for ch in text if ch not in spec_chars])
for word in skip:
if word in str(text):
skip_status = 1
char = 'skip'
keyword.append(word)
break
else:
skip_status = 0
continue
if skip_status == 0:
for token in text.split():
token = token.strip()
token = morph.normal_forms(token)[0]
if token and token not in rustopwords:
if not token.isdigit():
textcount.append(token)
new_text += ' ' + token
tokens.append(token)
for k, v in types_keywords.items():
for i in v:
y = 0
for word in tokens:
if word is not None and len(word) > 2:
if i.isalpha() == True: # Тональность определяется по одному слову
simular = jarowinkler.jarowinkler_similarity(i, word)
if simular >= 0.9:
type_arr.append(k)
keyword.append(i)
else:
kwrd = i.split('_')
simular = jarowinkler.jarowinkler_similarity(word, kwrd[1])
if simular >= 0.9:
if len(kwrd[0]) > 0 and len(kwrd[2]) == 0: # Только near
result = check_near(tokens, y, kwrd[0])
if result[0] == True:
type_arr.append(k)
keyword.append(
str(result[1]).replace('"', '').replace('[', '').replace(']',
'').replace(
'\\', '').replace("'", "") + ' *' + kwrd[1] + '*')
elif len(kwrd[0]) == 0 and len(kwrd[2]) > 0: # Только presence
res = check_presence(tokens, kwrd[2])
if res[0] == True:
type_arr.append(k)
keyword.append(
'*' + kwrd[1] + '*' + str(res[1]).replace('"', '').replace('[', '').replace(
']', '').replace('\\', '').replace("'", ""))
elif len(kwrd[0]) > 0 and len(kwrd[2]) > 0: # Near & Presence
result = check_near(tokens, y, kwrd[0])
if result[0] == True:
res = check_presence(tokens, kwrd[2])
if res[0] == True:
type_arr.append(k)
keyword.append(
str(result[1]).replace('"', '').replace('[', '').replace(']',
'').replace(
'\\', '').replace("'", "") + ' *' + kwrd[1] + '*' + str(
res[1]).replace('"', '').replace('[', '').replace(']',
'').replace(
'\\', '').replace("'", ""))
y += 1
if len(type_arr) > 0:
char = {i: type_arr.count(i) for i in type_arr};
char = list(char.keys())[0]
else:
char = 'undef'
item[1] = tokens
item[2] = char
item[3] = keyword
fdist = FreqDist(textcount)
print('Предварительная обработка завершена!')
return arr, fdist.most_common()
def mood_define(arr, char):
print('Начинаем процесс определения тональности - ' + char)
neg = []
pos = []
textcount = []
path = './data/mood_keywords_' + char + '.yaml'
with open(path, encoding='utf8') as f:
keywords = yaml.safe_load(f)
for key, value in keywords.items(): # собираем ключевые слова из yaml в переменные
if key == 'negative':
neg = value
elif key == 'positive':
pos = value
for item in tqdm(arr):
index = 0
lemtext = item[1]
sum_words = []
y = 0
for i in lemtext:
if len(i) > 2:
if i and i not in rustopwords:
if not i.isdigit():
textcount.append(i)
for wrd in neg:
if wrd is not None:
if wrd.isalpha() == True: # Тональность определяется по одному слову
simular = jarowinkler.jarowinkler_similarity(i, wrd)
if simular >= 0.95:
sum_words.append('*' + wrd + '*')
index -= 1
else:
kwrd = wrd.split('_')
simular = jarowinkler.jarowinkler_similarity(i, kwrd[1])
if simular >= 0.95:
if len(kwrd[0]) > 0 and len(kwrd[2]) == 0: # Только near
result = check_near(lemtext, y, kwrd[0])
if result[0]== True:
sum_words.append(str(result[1]).replace('"','').replace('[','').replace(']','').replace('\\','').replace("'","") + ' *' + kwrd[1] + '*')
index -= 1
elif len(kwrd[0]) == 0 and len(kwrd[2]) > 0: # Только presence
res = check_presence(lemtext, kwrd[2])
if res[0]== True:
sum_words.append('*' + kwrd[1] + '*' + str(res[1]).replace('"','').replace('[','').replace(']','').replace('\\','').replace("'",""))
index -= 1
elif len(kwrd[0]) > 0 and len(kwrd[2]) > 0: # Near & Presence
result = check_near(lemtext, y, kwrd[0])
if result[0]== True:
res = check_presence(lemtext, kwrd[2])
if res[0] == True:
sum_words.append(str(result[1]).replace('"','').replace('[','').replace(']','').replace('\\','').replace("'","") + ' *' + kwrd[1] + '*' + str(res[1]).replace('"','').replace('[','').replace(']','').replace('\\','').replace("'",""))
index -= 1
# else:
# sum_words.append(str(result).replace('"','').replace('[','').replace(']','').replace('\\','') + ' *' + i + '*' + str(res[1]).replace('"','').replace('[','').replace(']','').replace('\\',''))
for wrd in pos:
if wrd is not None:
if wrd.isalpha() == True: # Тональность определяется по одному слову
simular = jarowinkler.jarowinkler_similarity(i, wrd)
if simular >= 0.95:
sum_words.append('*' + wrd + '*')
index += 1
continue
else:
kwrd = wrd.split('_')
simular = jarowinkler.jarowinkler_similarity(i, kwrd[1])
if simular >= 0.95:
if len(kwrd[0]) > 0 and len(kwrd[2]) == 0: # Только near
result = check_near(lemtext, y, kwrd[0])
if result[0]== True:
sum_words.append(str(result[1]).replace('"','').replace('[','').replace(']','').replace('\\','').replace("'","") + ' *' + kwrd[1] + '*')
index += 1
elif len(kwrd[0]) == 0 and len(kwrd[2]) > 0: # Только presence
res = check_presence(lemtext, kwrd[2])
if res[0]== True:
sum_words.append('*' + kwrd[1] + '*' + str(res[1]).replace('"','').replace('[','').replace(']','').replace('\\','').replace("'",""))
index += 1
elif len(kwrd[0]) > 0 and len(kwrd[2]) > 0: # Near & Presence
result = check_near(lemtext, y, kwrd[0])
if result[0]== True:
res = check_presence(lemtext, kwrd[2])
if res[0] == True:
sum_words.append(str(result[1]).replace('"','').replace('[','').replace(']','').replace('\\','').replace("'","") + ' *' + kwrd[1] + '*' + str(res[1]).replace('"','').replace('[','').replace(']','').replace('\\','').replace("'",""))
index += 1
# else:
# sum_words.append(str(result).replace('"','').replace('[','').replace(']','').replace('\\','') + ' *' + i + '*' + str(
# res[1]).replace('"','').replace('[','').replace(']','').replace('\\',''))
y += 1
if index > 0:
mood = 'positive'
elif index < 0:
mood = 'negative'
else:
mood = 'undef'
item[4] = mood
item[5] = sum_words
item[6] = index
fdist = FreqDist(textcount)
return arr, fdist.most_common()
def check_near (lemtext, i, keywords):
text = []
status = False
if keywords.find('+') >= 0 and keywords.find('-') >= 0:
if keywords.find('+') == 0:
plus = keywords[1:keywords.find('-')].split(',')
minus = keywords[keywords.find('-') + 1:].split(',')
else:
plus = keywords[keywords.find('-') + 1:].split(',')
minus = keywords[1:keywords.find('-')].split(',')
if i == 1:
if lemtext[i - 1] in plus:
text.append('+' + lemtext[i - 1] + ' -' + str(minus))
status = True
elif i > 2:
if lemtext[i - 1] in plus:
if lemtext[i - 2] not in minus:
text.append('+' + lemtext[i - 1] + ' -' + str(minus))
status = True
elif lemtext[i - 2] in plus:
if lemtext[i - 1] not in minus:
text.append('+' + lemtext[i - 2] + ' -' + str(minus))
status = True
elif keywords.find('+') >= 0 and keywords.find('-') < 0:
plus = keywords[1:].split(',')
if i == 1:
if lemtext[i - 1] in plus:
text.append('+' + lemtext[i - 1])
status = True
elif i >= 2:
if lemtext[i - 1] in plus:
text.append('+' + lemtext[i - 1])
status = True
elif lemtext[i - 2] in plus:
text.append('+' + lemtext[i - 2])
status = True
elif keywords.find('+') < 0 and keywords.find('-') >= 0:
minus = keywords[1:].split(',')
if i == 1:
if lemtext[i - 1] not in minus:
text.append('+' + lemtext[i - 1])
status = True
elif i >= 2:
if lemtext[i - 1] not in minus and lemtext[i - 2] not in minus:
text.append('-' + str(minus))
status = True
return status, text
def check_presence (lemtext, keywords):
text = []
status = False
if keywords.find('+') >= 0 and keywords.find('-') >= 0:
if keywords.find('+') == 0:
plus = keywords[1:keywords.find('-')].split(',')
minus = keywords[keywords.find('-') + 1:].split(',')
else:
plus = keywords[keywords.find('-') + 1:].split(',')
minus = keywords[1:keywords.find('-')].split(',')
res = []
# res_min = []
for pl in plus:
for lem in lemtext:
simular = jarowinkler.jarowinkler_similarity(pl, lem)
if simular >= 0.95:
for min in minus:
for l in lemtext:
simular = jarowinkler.jarowinkler_similarity(min, l)
if simular >= 0.95:
# res_min.append('-' + min)
break
else:
res.append(' +' + pl + ' -' + str(minus))
continue
break
if len(set(res)) > 1:
unique = list(set(res))
status = True
text.append(unique)
elif len(set(res)) == 1:
status = True
text.append(res[0])
else:
# if len(set(res_min)) > 1:
# unique = list(set(res_min))
# text.append(unique)
# elif len(set(res_min)) == 1:
# text.append(res_min)
# else:
text = []
elif keywords.find('+') >= 0 and keywords.find('-') < 0:
plus = keywords[1:].split(',')
for pl in plus:
for lem in lemtext:
simular = jarowinkler.jarowinkler_similarity(pl, lem)
if simular >= 0.95:
text.append(' +' + pl)
status = True
elif keywords.find('+') < 0 and keywords.find('-') >= 0:
minus = keywords[1:].split(',')
res = []
for min in minus:
for lem in lemtext:
simular = jarowinkler.jarowinkler_similarity(min, lem)
if simular >= 0.95:
break
else:
res.append(' -' + str(minus))
continue
break
if len(set(res)) > 1:
unique = list(set(res))
status = True
text.append(unique)
elif len(set(res)) == 1:
status = True
text.append(res[0])
else:
text = []
return status, text
if __name__ == '__main__':
main()