-
Notifications
You must be signed in to change notification settings - Fork 0
/
Email.py
325 lines (226 loc) · 9.6 KB
/
Email.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
import corpus_generator
import os
import re
from nltk.corpus import names
from nltk.tokenize import sent_tokenize
from nltk.stem.porter import *
class Email:
def __init__(self, name, head, type, who, topic, dates, time, place, duration, host, poster, abstract):
self.name = name
self.head = head
self.type = type
self.who = who
self.topic = topic
self.dates = dates
self.time = time
self.place = place
self.duration = duration
self.host = host
self.poster = poster
self.abstract = abstract
self.tags = []
self.head_tags = []
self.pos = []
self.tokens = self.get_tokens()
def __str__(self):
s = self.head + \
'\nType:' + self.type
if self.who is not None:
s += '\nWho:' + self.who
s += '\nTopic:' + self.topic + \
'\nDates:' + self.dates + \
'\nTime:' + self.time
if self.place is not None:
s += '\nPlace:' + self.place
if self.duration is not None:
s += '\nDuration:' + self.duration
if self.host is not None:
s += '\nHost:' + self.host
s += '\nPostedBy:' + self.poster + \
'\nAbstract:' + self.abstract
return s
def get_frequencies(self, common):
stemmer = PorterStemmer()
freq = {}
for word in self.pos:
string = stemmer.stem(word[0].lower())
if string not in common:
if string not in freq:
freq[string] = 1
else:
freq[string] += 1
sorted_dict = sorted(freq.items(), key=lambda kv: kv[1], reverse=True)
return sorted_dict
def get_tokens(self):
tokens = corpus_generator.generate_tokens_single(self.abstract)
return tokens
def get_all_tags(self):
tags = self.tags + self.head_tags
return tags
def process(self, tagger):
self.pos = tagger.tag(self.tokens)
# tag paragraphs and sentences first to not mess with other tags when outputting
self.tag_paragraphs() # and sentences
self.tag_times()
speaker = self.tag_speakers()
location = self.tag_location()
return speaker, location
def tag_location(self):
if self.place is not None:
# tag location in header
whitespace = re.match(r'\A\s+', self.place).group(0)
place = re.sub(r'\A\s+', '', self.place)
# add to tags
self.head_tags.append((place, "location"))
s = whitespace + '<location>' + place + '</location'
self.place = s
# tag location in body
regex = r'' + re.escape(place) + r''
matches = re.findall(regex, self.abstract)
for match in matches:
self.tags.append((match, "location"))
return place
return None
def tag_location_list(self, locations):
for value in locations:
regex = r'' + re.escape(value) + r''
results = re.findall(regex, self.abstract)
for result in results:
self.tags.append((result, "location"))
# TODO extra location getting
matches = re.findall(r'(?<= in )[a-zA-Z0-9 ]*(?= at \d)', self.abstract)
for match in matches:
self.tags.append((match, "location"))
def tag_speakers(self):
if self.who is not None:
# tag location in header
# strip leading whitespace
stripped = re.sub(r'\A\s+', '', self.who)
speaker = re.match(r'^.*?(?= *[,\/])|^.*', stripped).group(0)
# add to tags
self.head_tags.append((speaker, "speaker"))
regex = r'' + re.escape(speaker) + r''
string = "<speaker>" + speaker + "</speaker>"
s = re.sub(regex, string, self.who)
self.who = s
# tag location in body
regex = r'' + re.escape(speaker) + r''
matches = re.findall(regex, self.abstract)
for match in matches:
self.tags.append((match, "speaker"))
return speaker
return None
def tag_speakers_list(self, speakers):
matched_speakers = 0
for value in speakers:
regex = r'' + re.escape(value) + r''
results = re.findall(regex, self.abstract)
for result in results:
self.tags.append((result, "speaker"))
matched_speakers += 1
#todo improve this
if matched_speakers == 0:
tagged = self.pos
l = len(tagged)
for index, word in enumerate(tagged):
if index < (l - 1):
#todo make this better and use regex
if word[1] == u'NP' and tagged[index + 1][1] == u'NP':
self.tags.append(((word[0] + " " + tagged[index + 1][0]), "speaker"))
elif word[1] == u'NP' and tagged[index + 1][1] == "unseen":
if tagged[index + 1][0] in names.words():
self.tags.append(((word[0] + " " + tagged[index + 1][0]), "speaker"))
elif word[1] == "unseen" and tagged[index + 1][1] == u'NP':
if word[0] in names.words():
self.tags.append(((word[0] + " " + tagged[index + 1][0]), "speaker"))
elif word[1] == "unseen" and tagged[index + 1][1] == "unseen":
if word[0] in names.words() and tagged[index + 1][0] in names.words():
self.tags.append(((word[0] + " " + tagged[index + 1][0]), "speaker"))
def tag_sentences(self, paragraph):
sentences = sent_tokenize(paragraph)
for value in sentences:
self.tags.append((value, "sentence"))
def tag_paragraphs(self):
# this one is ok
# (?<=(?:\n\n))[^ ]{2,}.*?(?=(?:\n\n))
# this one is better
# (?<=(?:\n\n))(?:[^-]).*?(?=\n\n)
regex = re.findall(r'(?<=(?:\n\n))(?:[^-]).*?(?=\n\n)', self.abstract, re.DOTALL)
for match in regex:
self.tags.append((match, "paragraph"))
# second loop as sentences need to be tagged after paragraphs
for match in regex:
self.tag_sentences(match)
def tag_times(self):
# tag header
# tag time
if self.time is not None:
header_times = []
# get leading whitespace
s = re.match(r'\A\s+', self.time).group(0)
# remove leading whitespace
string = re.sub(r'\A\s+', '', self.time)
# find times from text
times = re.findall(r'\d{1,2}:\d{2} AM|\d{1,2}:\d{2} PM|\d{1,2}:\d{2}', string)
if len(times) > 1:
# add to tags
self.head_tags.append((times[0], "stime"))
self.head_tags.append((times[1], "etime"))
# save for body times
header_times.append(times[0])
header_times.append(times[1])
stime = '<stime>' + times[0] + '</stime'
stime_regex = r'\b' + times[0] + r'\b'
tagged = re.sub(stime_regex, stime, string)
etime = '<etime>' + times[1] + '</etime'
etime_regex = r'\b' + times[1] + r'\b'
tagged_two = re.sub(etime_regex, etime, tagged)
s += tagged_two
else:
# add to tags
self.head_tags.append((times[0], "stime"))
header_times.append(times[0])
s += '<stime>' + times[0] + '</stime'
self.time = s
# tag abstract using data from header
times = re.findall(r'\d{1,2}(?::\d{2})?.?(?i)(?:P|A)(?:\.M\.?|M)(?![a-zA-Z])|\d{1,2}:\d{2}', self.abstract)
temp_header_times = []
# strip header formatting
for value in header_times:
temp_header_times.append(re.sub(r'[ ]', "", value))
header_times = temp_header_times
for value in times:
# strip formatting
value_formatted = value.upper()
# remove whitespace
value_formatted = re.sub(r'\s', "", value_formatted)
# remove dots
value_formatted = re.sub(r'\.', "", value_formatted)
# add hours
if re.match(r'^\d{1,2}(?:P|A)(?:\.M\.?|M)$', value_formatted):
int = re.match(r'\d{1,2}', value_formatted).group(0)
regex = r'' + int + r''
value_formatted = re.sub(regex, int + ":00", value_formatted)
if len(header_times) > 1:
if value_formatted == header_times[0]:
self.tags.append((value, "stime"))
elif value_formatted == header_times[1]:
self.tags.append((value, "etime"))
elif len(header_times) == 1:
if value_formatted == header_times[0]:
self.tags.append((value, "stime"))
def export(self):
self.write_tags()
if not os.path.exists("output/"):
os.makedirs("output/")
f = open("output/" + self.name, "w+")
f.write(self.__str__())
f.close()
def write_tags(self):
# TODO fix for multiple of the same tag double tagging
tagset = set(self.tags)
for value in tagset:
regex = r'' + re.escape(value[0]) + r''
tagged_sent = "<" + value[1] + ">" + value[0] + "</" + value[1] + ">"
temp = re.sub(regex, tagged_sent, self.abstract)
self.abstract = temp