-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtfidf.py
53 lines (40 loc) · 1.37 KB
/
tfidf.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
#!/usr/bin/python
import os
import pickle
import re
import sys
import numpy as np
np.set_printoptions(threshold=np.nan)
sys.path.append( "../tools/" )
from parse_out_email_text import parseOutText
def tfidFreq(all_text_prepro):
word_data = all_text_prepro
#print word_data
### in Part 4, do TfIdf vectorization here
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(word_data)
#print tfidf_matrix
feature_names = vectorizer.get_feature_names()
score_dic = {}
doc = 0
for doc in range(len(word_data)):
feature_index = tfidf_matrix[doc,:].nonzero()[1]
tfidf_scores = zip(feature_index, [tfidf_matrix[doc, x] for x in feature_index])
for w, s in [(feature_names[i], s) for (i, s) in tfidf_scores]:
score_dic.update({w: s})
doc = doc + 1
#print score_dic
return score_dic
def tfidf_arr(mails, arrK) : #mails is list of mails(string), arrk is array of dict of every mail
mailNo=0
listO = []
dict = tfidFreq(mails)
for mail in mails :
dict1 = arrK[mailNo]
for key in dict1.keys() :
iden = dict1[key]
list1 = [mailNo, iden, dict[key.lower()]]
listO.append(list1)
mailNo = mailNo+1
return listO