-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnltk_email_scan.py
45 lines (37 loc) · 1.21 KB
/
nltk_email_scan.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
"""
Code to intersect the emails with a list of interesting topics, then flag each with
a "message of interest : True value/ Takes about 20 minutes to run on the whole dataset
"""
import pymongo
import nltk
from pymongo import MongoClient
import datetime
import email
# connect
cn = MongoClient("localhost")
db = cn.enron_mail_2
counter = 1
# open line by line words of interest
word_lines = set(line.strip() for line in open("words_of_interest.txt"))
# loop through all mail items
for document in db.mail.find():
mail = document["text"]
# remove all text after the "To:" string, hopefully this removes forwarded emails and old emails
#
mail = mail.split("To:")[0]
counter += 1
# tokenize mail to hopefully split out words
doc_words = set(nltk.word_tokenize(mail))
# count and print every 100 emails
if counter % 100 == 0:
print(datetime.datetime.utcnow(), counter)
message_of_interest = (len(doc_words.intersection(word_lines)) > 0)
db.mail.update_one({
"_id": document["_id"]
}, {
"$set": {
"message_of_interest": message_of_interest
}
}, upsert=False)
# print(doc_words.intersection(word_lines))
print("Finished updating:")