-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
436 lines (367 loc) · 13 KB
/
utils.py
File metadata and controls
436 lines (367 loc) · 13 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
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
# -*- coding: utf-8 -*-
import operator
import random
from collections import Counter, defaultdict
from multiprocessing.pool import Pool
import treetaggerwrapper
import config
# dictionaries of type {'word': []}
nouns_matrix_chron = defaultdict(lambda: [])
nouns_matrix_dechron = defaultdict(lambda: [])
adj_matrix_chron = defaultdict(lambda: [])
adj_matrix_dechron = defaultdict(lambda: [])
verbs_matrix_chron = defaultdict(lambda: [])
verbs_matrix_dechron = defaultdict(lambda: [])
pour_matrix_chron = defaultdict(lambda: [])
pour_matrix_dechron = defaultdict(lambda: [])
contre_matrix_chron = defaultdict(lambda: [])
contre_matrix_dechron = defaultdict(lambda: [])
loop_count = 0
def list_the_file(path):
"""Reads a file and puts each line in
a list of strings. Removes the '\n'
characters
"""
text = open(path, 'r')
res = text.read().splitlines()
text.close()
return res
def lowercase_list(lst):
return [item.lower() for item in lst]
def get_most_frequent_words_tuples(dico):
return sorted(dico.items(), key=lambda x: x[1][-1], reverse=True)
def sort_list_of_tuples(lst, total):
"""Takes a list of words and returns
a sorted list of tuples of its words
and count
"""
tmp = dict(Counter(lst))
for k, v in tmp.items():
tmp[k] = v/total
return sorted(tmp.items(), key=operator.itemgetter(1), reverse=True)
def get_list_predictions(lst):
""""From an ordered list of tuples, returns an ordered list of
tuples as (word, [freq, pred])
"""
tmp = {}
for tpl in lst:
try:
a = tpl[1][-2]
except IndexError:
a = 0
b = tpl[1][-1]
tmp[tpl[0]] = [b, get_prediction(a, b)]
# pred = "{0:.5f}".format(random.random())
# ans[tpl[0]] = ["{0:.5f}".format(tpl[1]), pred]
return get_most_frequent_words_tuples(tmp)
def get_opinion_prediction(mtrx):
res = []
for k, v in mtrx.items():
res.append(k)
try:
a = v[-2]
except IndexError:
a = 0
b = v[-1]
res.append([v[-1], get_prediction(a, b)])
return res
def feed_matrix(mtrx, lst):
"""takes an ordered list of tuples
and add them in the specified matrix
"""
for tpl in lst:
mtrx[tpl[0]].append(tpl[1])
for key, value in mtrx.items():
if len(value) < loop_count:
mtrx[key].insert(0, 0)
def merge_tuple_list(lst1, lst2):
counter = Counter(dict(lst1)) + Counter(dict(lst2))
return sorted(counter.items(), key=operator.itemgetter(1), reverse=True)
def extract_top_words(tokenizer, tagger, partial_list):
# print("extract top words function started...")
# Structures of interest
title_tokens = []
content_tokens = []
nouns = []
adjectives = []
verbs = []
adverbs = []
preps = []
conj = []
others = []
top_nouns = []
top_adjectives = []
top_verbs = []
for_count = 0
against_count = 0
# Seperate in words each initiatives of the list
for initiative in partial_list:
# words list from titles
title_tokens += tokenizer.tokenize(initiative.title)
# words list from contents
content_tokens += tokenizer.tokenize(initiative.content)
if initiative.opinion == 'pour':
for_count += 1
else:
against_count += 1
text_tokens = lowercase_list(title_tokens + content_tokens)
# date of last initiative from the list (last one added)
current_date = partial_list[-1].date
# pos tags list from words
text_tags = treetaggerwrapper.make_tags(tagger.tag_text(text_tokens), True)
for tag in text_tags:
if tag.pos == 'NOM':
nouns.append(tag.word)
elif tag.pos == 'ADJ':
adjectives.append(tag.word)
elif tag.pos[0:3] == 'VER':
if tag.lemma == "c":
pass
else:
verbs.append(tag.lemma)
elif tag.pos == 'ADV':
adverbs.append(tag.word)
elif tag.pos[0:3] == 'PRP':
preps.append(tag.word)
elif tag.pos == "KON":
conj.append(tag.word)
else:
others.append(tag.word)
total_word_count = len(text_tokens)
# # ordered list of tuples (word, frequency), by frequencies
# words_frequency = sort_list_of_tuples(text_tokens)
# ordered list of tuples (nouns, freq)
for n in wrong_nouns:
nouns = list(filter((n).__ne__, nouns))
ordered_nouns = sort_list_of_tuples(nouns, total_word_count)
# ordered list of tuples (adjectives, freq)
for a in wrong_adj:
adjectives = list(filter((a).__ne__, adjectives))
ordered_adjectives = sort_list_of_tuples(adjectives, total_word_count)
# ordered list of tuples (verbs, freq)
for v in wrong_verbs:
verbs = list(filter((v).__ne__, verbs))
ordered_verbs = sort_list_of_tuples(verbs, total_word_count)
# make a list of words of interest (nouns, adjectives, verbs)
selected_words = nouns + adjectives + verbs
# ordered list of tuples ()
ordered_words = sort_list_of_tuples(selected_words, total_word_count)
result = {}
result["words"] = ordered_words
result["nouns"] = ordered_nouns
result["adjectives"] = ordered_adjectives
result["verbs"] = ordered_verbs
result["pour"] = [('pour', for_count/total_word_count)]
result["contre"] = [('contre', against_count/total_word_count)]
return result
def reduce_too_high_frequencies(mx, dic):
"""reduce some too frequent words frequencies
"""
for k, v in mx.items():
if k in dic.keys():
mx[k] = list(map(lambda x: x*dic[k], mx[k]))
return mx
def treat_current_dataset(tokenizer, tagger, partial_chron, partial_dechron):
"""Handles the growing list of initiatives chronologically and
dechronologically, performs analyses and predictions
"""
global loop_count
loop_count += 1
# print("\nLoop number {}".format(loop_count))
current_initiative_chron = partial_chron[-1]
current_initiative_dechron = partial_dechron[-1]
global nouns_matrix_chron
global nouns_matrix_dechron
global adj_matrix_chron
global adj_matrix_dechron
global verbs_matrix_chron
global verbs_matrix_dechron
# WTF DOES IT NOT WORK ???
# with Pool() as pool:
# result_chron = pool.apply_async(extract_top_words, args=(tokenizer, tagger, partial_chron,))
# result_dechron = pool.apply_async(extract_top_words, args=(tokenizer, tagger, partial_dechron,))
# values_chron = result_chron.get()
# values_dechron = result_dechron.get()
values_chron = extract_top_words(tokenizer, tagger, partial_chron)
values_dechron = extract_top_words(tokenizer, tagger, partial_dechron)
# update the nouns matrix
feed_matrix(nouns_matrix_chron, values_chron['nouns'])
feed_matrix(nouns_matrix_dechron, values_dechron['nouns'])
feed_matrix(adj_matrix_chron, values_chron['adjectives'])
feed_matrix(adj_matrix_dechron, values_dechron['adjectives'])
feed_matrix(verbs_matrix_chron, values_chron['verbs'])
feed_matrix(verbs_matrix_dechron, values_dechron['verbs'])
feed_matrix(pour_matrix_chron, values_chron['pour'])
feed_matrix(pour_matrix_dechron, values_dechron['pour'])
feed_matrix(contre_matrix_chron, values_chron['contre'])
feed_matrix(contre_matrix_dechron, values_dechron['contre'])
# data for chronological screen
date_chron = current_initiative_chron.date
# reduce some too frequent words frequencies
mx_n = reduce_too_high_frequencies(nouns_matrix_chron, coeff_nouns)
# with open("logs.txt", "a") as text_file:
# print("\nnouns matrix = {}".format(nouns_matrix_chron), file=text_file)
# print("\nmn matrix = {}".format(mx_n), file=text_file)
top_20_nouns_chron = get_most_frequent_words_tuples(mx_n)[:20]
mx_a = reduce_too_high_frequencies(adj_matrix_chron, coeff_adj)
config.top_20_adj_chron = get_most_frequent_words_tuples(mx_a)[:20]
config.bottom_20_adj_chron = get_most_frequent_words_tuples(mx_a)[::-1][:20]
# warning, here we take the 20 least used verbs
config.bottom_20_verbs_chron = get_most_frequent_words_tuples(verbs_matrix_chron)[::-1][:20]
# data for dechronological screen
date_dechron = current_initiative_dechron.date
# reduce some too frequent word frequencies
mx_n2 = reduce_too_high_frequencies(nouns_matrix_dechron, coeff_nouns)
top_20_nouns_dechron = get_most_frequent_words_tuples(mx_n2)[:20]
mx_a2 = reduce_too_high_frequencies(adj_matrix_dechron, coeff_adj)
config.top_20_adj_dechron = get_most_frequent_words_tuples(mx_a2)[:20]
config.bottom_20_adj_dechron = get_most_frequent_words_tuples(mx_a2)[::-1][:20]
# warning, here we take the 20 least used verbs
config.bottom_20_verbs_dechron = get_most_frequent_words_tuples(verbs_matrix_dechron)[::-1][:20]
# predictions
# global nouns_chron_prediction_list
config.nouns_chron_predictions_list = get_list_predictions(top_20_nouns_chron)
config.adj_chron_predictions_list = get_list_predictions(config.top_20_adj_chron)
config.verbs_chron_predictions_list = get_list_predictions(config.bottom_20_verbs_chron)
config.pour_chron_predictions_list = get_opinion_prediction(pour_matrix_chron)
config.contre_chron_predictions_list = get_opinion_prediction(contre_matrix_chron)
config.nouns_dechron_predictions_list = get_list_predictions(top_20_nouns_dechron)
config.adj_dechron_predictions_list = get_list_predictions(config.top_20_adj_dechron)
config.verbs_dechron_predictions_list = get_list_predictions(config.bottom_20_verbs_dechron)
config.pour_dechron_predictions_list = get_opinion_prediction(pour_matrix_dechron)
config.contre_dechron_predictions_list = get_opinion_prediction(contre_matrix_dechron)
# print("\nNouns chron predictions = ")
# print(nouns_chron_prediction_list)
# Reads all words from dataset to get all possible POS
def get_full_pos_set(dataset_path):
tagger = treetaggerwrapper.TreeTagger(TAGLANG='fr',
TAGDIR='/Users/lweingart/Applications/tree-tagger')
textfile = open(dataset_path, 'r')
text = textfile.read()
textfile.close()
tags = tagger.tag_text(text)
pretty_tags = treetaggerwrapper.make_tags(tags, True)
poss = [tag.pos for tag in pretty_tags]
pos_set = set(poss)
return pos_set
def get_prediction(a, b):
scale = abs(b - a)
return a + fibo_ratios[random.randrange(0, 19)] * scale
def log(filename, t, s):
with open(filename, "a") as log_file:
print('{} :: {}'.format(t, s), file=log_file)
pos_dict = {
'ABR': 'abreviation',
'ADJ': 'adjectif',
'ADV': 'adverbe',
'DET:ART': 'article',
'DET:POS': 'pronom possessif',
'KON': 'conjonction',
'NAM': 'nom propre',
'NOM': 'nom',
'NUM': 'nombre',
'PRO:DEM': 'pronom demonstratif',
'PRO:IND': 'pronom indefini',
'PRO:PER': 'pronom personnel',
'PRO:POS': 'pronom possessif',
'PRO:REL': 'pronom relatif',
'PRP': 'preposition',
'PRP:det': 'preposition plus article',
'PUN': 'ponctuation',
'PUN:cit': 'ponctuation citation',
'SENT': 'sentence tag ??',
'SYM': 'symbole',
'VER:cond': 'verbe conditionnel',
'VER:impf': 'verbe imparfait',
'VER:infi': 'verbe infinitif',
'VER:futu': 'verbe futur',
'VER:pper': 'verbe participe passe',
'VER:ppre': 'vebre participe present',
'VER:pres': 'verbe present',
'VER:simp': 'verbe passe simple',
'VER:subi': 'verbe subjonctif imparfait',
'VER:subp': 'verbe subjonctif present'
}
fibo_ratios = {
0: 0,
1: 0.236,
2: 0.382,
3: 0.5,
4: 0.618,
5: 0.764,
6: 1,
7: 1.236,
8: 1.382,
9: 1.5,
10: 1.618,
11: 1.764,
12: 2,
13: 2.236,
14: 2.382,
15: 2.5,
16: 2.618,
17: 2.764,
18: 3
}
coeff_nouns = {
'confédération': 0.1,
'cantons': 0.05,
'droit': 0.3,
'mesures': 0.2,
'personnes': 0.8,
'impôt': 0.05,
'cas': 0.1,
'protection': 0.8,
'loi': 0.5,
'assurance': 0.3,
'constitution': 0.1,
'législation': 0.1,
'disposition': 0.05,
'prix': 0.1,
'conseil': 0.1,
'dispositions': 0.05,
'impôts': 0.1,
'droits': 0.1,
'suisse': 0.1,
'article': 0.1,
'personnes': 0.1,
'citoyens': 0.05,
'pays': 0.1,
'service': 0.1,
'peuple': 0.1,
'travail': 0.1,
'ans': 0.1,
'revenu': 0.1,
'membres': 0.1,
'durée': 0.1,
'protection': 0.1,
'autorisation': 0.1,
'voie': 0.1,
'taux': 0.1,
'base': 0.1,
'environnement': 0.1,
'population': 0.1,
'prestations': 0.05,
'salaires': 0.1,
'salaires': 0.1,
'population': 0.1,
'territoire': 0.1,
"l'environnement": 0.05,
"l'impôt": 0.1
}
coeff_adj = {
'fédéral': 0.1,
'fédérale': 0.1,
'suisse': 0.1,
'autres': 0.1,
'remplacé': 0.1,
'nationale': 0.5,
'générale': 0.1,
'obligatoire': 0.5,
'public': 0.8,
'nécessaires': 0.7,
"national": 0.1
}
wrong_adj = ['s’applique', 'l’entretien']
wrong_nouns = ['d’abattage']
wrong_verbs = ['payer|payer', 'd’exploitation']