-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorize_text.py
More file actions
93 lines (65 loc) · 2.79 KB
/
vectorize_text.py
File metadata and controls
93 lines (65 loc) · 2.79 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
#!/usr/bin/python
import os
import pickle
import re
import sys
sys.path.append( "../tools/" )
from parse_out_email_text import parseOutText
"""
Starter code to process the emails from Sara and Chris to extract
the features and get the documents ready for classification.
The list of all the emails from Sara are in the from_sara list
likewise for emails from Chris (from_chris)
The actual documents are in the Enron email dataset, which
you downloaded/unpacked in Part 0 of the first mini-project. If you have
not obtained the Enron email corpus, run startup.py in the tools folder.
The data is stored in lists and packed away in pickle files at the end.
"""
from_sara = open("from_sara.txt", "r")
from_chris = open("from_chris.txt", "r")
from_data = []
word_data = []
### temp_counter is a way to speed up the development--there are
### thousands of emails from Sara and Chris, so running over all of them
### can take a long time
### temp_counter helps you only look at the first 200 emails in the list so you
### can iterate your modifications quicker
replace_words = ["sara", "shackleton", "chris", "germani"]
for name, from_person in [("sara", from_sara), ("chris", from_chris)]:
### temp_counter = 0
for path in from_person:
### only look at first 200 emails when developing
### once everything is working, remove this line to run over full dataset
### temp_counter += 1
### if temp_counter < 200:
path = os.path.join('..', path[:-1])
print path
email = open(path, "r")
### use parseOutText to extract the text from the opened email
text = parseOutText ( email )
### use str.replace() to remove any instances of the words
### ["sara", "shackleton", "chris", "germani"]
for i in replace_words:
if ( i in text ):
text = text.replace ( i, "" )
### append the text to word_data
word_data.append(text)
### append a 0 to from_data if email is from Sara, and 1 if email is from Chris
if ( name == "sara" ):
from_data.append(0)
if ( name == "chris" ):
from_data.append(1)
email.close()
print "emails processed"
from_sara.close()
from_chris.close()
print word_data[152]
pickle.dump( word_data, open("your_word_data.pkl", "w") )
pickle.dump( from_data, open("your_email_authors.pkl", "w") )
### in Part 4, do TfIdf vectorization here
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer ( stop_words="english" )
vectorizer.fit_transform ( word_data )
print len ( vectorizer.get_feature_names () )
vectorizer_words = vectorizer.get_feature_names ()
print vectorizer_words [34597]