-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoser.py
209 lines (166 loc) · 6.59 KB
/
doser.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
import codecs
import datetime
import re
class Doser:
def __init__(self, talk, people, stop_words, swear_words):
self.talk = talk
self.stop_words = stop_words
self.swear_words = swear_words
self.talk_line_number = 0
self.data = {
'talk': {
'words_count': 0,
'lines': []
},
'dates': [],
'days': {
'Monday': 0,
'Tuesday': 0,
'Wednesday': 0,
'Thursday': 0,
'Friday': 0,
'Saturday': 0,
'Sunday': 0
},
'people': [],
'swear_words': [],
'words': []
}
for person in people:
pseudo = person['pseudo']
self.data['people'].append({
'pseudo': pseudo,
'pronounced_words': 0,
'pronounced_swear_words': 0
})
def parse(self):
for line in self.talk_lines():
if len(line) == 0:
continue
if self.extract_date_time(line):
continue
line_without_date_time = line[20:]
self.data['talk']['lines'].append({
'line': line_without_date_time,
'line_number': self.talk_line_number
})
self.talk_line_number += 1
for person in self.data['people']:
pseudo = person['pseudo']
# Expects that a sentence begin with a person pseudo
if line_without_date_time.startswith(pseudo):
pseudo_length = len(pseudo) + 2
line_without_pseudo = line_without_date_time[pseudo_length:]
self.extract_words(pseudo, line_without_pseudo, self.talk_line_number)
continue
self.sort_words()
self.sort_swear_words()
return self.data
def talk_lines(self):
with codecs.open(self.talk, 'r', 'utf-8') as f:
return [x.strip() for x in f.readlines()]
def tokenize(self, text):
# TODO: tokenize instead
words = re.sub('[^\w]', ' ', text).split()
return [w.lower() for w in words]
def extract_date_time(self, line):
date_time_line = re.match(r'^(\d{2}\/\d{2}\/\d{4}), (\d{2}:\d{2}) - ', line)
if not date_time_line:
return False
date_line = date_time_line.group(1)
time_line = date_time_line.group(2)
self.extract_day(date_line)
self.extract_consecutives_days()
date_line_match = next((d for d in self.data['dates'] if d['date'] == date_line), None)
if date_line_match == None:
self.data['dates'].append({
'date': date_line,
'count': 1
})
else:
date_line_match['count'] += 1
def extract_words(self, pseudo, line, talk_line_number):
# TODO: remove dd/mm/YYYY, hh:mm - pseudo: <Fichier omis>
words = self.tokenize(line)
filtered_words = []
for word in words:
if word in self.stop_words or not word.isalpha() or len(word) <= 1:
continue
self.data['talk']['words_count'] += 1
people_match = next((p for p in self.data['people'] if p['pseudo'] == pseudo), None)
if people_match == None:
print('ko')
else:
people_match['pronounced_words'] += 1
self.extract_stop_word(pseudo, word, self.talk_line_number)
self.extract_swear_word(pseudo, word)
def extract_day(self, date):
day = datetime.datetime.strptime(date, '%d/%m/%Y').strftime('%A')
self.data['days'][day] += 1
def extract_consecutives_days(self):
print('ok')
def extract_swear_word(self, pseudo, word):
if word in self.swear_words:
print('in')
swear_word_match = next((s for s in self.data['swear_words'] if s['word'] == word), None)
if swear_word_match == None:
self.data['swear_words'].append({
'word': word,
'count': 1,
'people': [
{
'pseudo': pseudo,
'count': 1
}
]
})
else:
swear_word_match['count'] += 1
people_match = next((p for p in swear_word_match['people'] if p['pseudo'] == pseudo), None)
if people_match == None:
swear_word_match['people'].append({
'pseudo': pseudo,
'count': 1
})
else:
people_match['count'] += 1
people_match = next((p for p in self.data['people'] if p['pseudo'] == pseudo), None)
if people_match == None:
print('ko')
else:
people_match['pronounced_swear_words'] += 1
def extract_stop_word(self, pseudo, word, talk_line_number):
word_match = next((l for l in self.data['words'] if l['word'] == word), None)
if word_match == None:
self.data['words'].append({
'word': word,
'count': 1,
'people': [
{
'pseudo': pseudo,
'count': 1,
'line_numbers': [talk_line_number]
}
]
})
else:
word_match['count'] += 1
# word_match['people']['line_numbers'].append(talk_line_number)
people_match = next((p for p in word_match['people'] if p['pseudo'] == pseudo), None)
if people_match == None:
word_match['people'].append({
'pseudo': pseudo,
'count': 1,
'line_numbers': [talk_line_number]
})
else:
people_match['count'] += 1
# def sort_dates(self):
# sorted_dates = [{ k: self.data['dates'][k] } for k in sorted(self.data['dates'], key = self.data['dates'].get, reverse = True)]
# self.data['dates'] = sorted_dates
def sort_words(self):
sorted_words = sorted(self.data['words'], key = lambda k: k['count'], reverse = True)
self.data['words'] = sorted_words
def sort_swear_words(self):
sorted_words = sorted(self.data['swear_words'], key = lambda k: k['count'], reverse = True)
self.data['swear_words'] = sorted_words