-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfidf.py
executable file
·97 lines (78 loc) · 1.9 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
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
import sys
import ast
from collections import Counter
from os import listdir
import simplejson
import math
import numpy as np
import csv
import string
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
###Term Frequency###
def term_frequency(documents,vocabulary_list):
TF=[]
for document in documents:
tf_per_document=[]
for word in vocabulary_list:
fij=document.count(word)
if fij>0:
tf=1+math.log(fij,2)
else:
tf=0
tf_per_document.append(tf)
TF.append(tf_per_document)
return TF
###Inverse Document Frequency###
def inverse_document_frequency(documents,vocabulary_list):
IDF=[]
N=len(documents)
for word in vocabulary_list:
count=0
for document in documents:
if word in document:
count+=1
if count == 0:
idf=0
else:
idf=math.log(N/count,2)
IDF.append(idf)
return IDF
###Term Frequency Inverse Document Frequency###
def term_frequency_inverse_document_frequency(TF,IDF):
IDF=np.array(IDF)
TFIDF=[]
for tf in TF:
tf=np.array(tf)
tfidf=tf*IDF
TFIDF.append(tfidf.tolist())
return TFIDF
###Accessing the Vocabulary###
file_path='vocabulary.txt'
f1 = open(file_path, 'r')
vocabulary_list=f1.read()
vocabulary_list = ast.literal_eval(vocabulary_list)
f1.close()
###Accessing the Documents###
file_path='documents.txt'
f1 = open(file_path, 'r')
documents=f1.read()
documents = ast.literal_eval(documents)
f1.close()
###Performing TFIDF###
TF=term_frequency(documents,vocabulary_list)
IDF=inverse_document_frequency(documents,vocabulary_list)
TFIDF=term_frequency_inverse_document_frequency(TF,IDF)
file_path='idf.txt'
f = open(file_path, 'w')
simplejson.dump(IDF, f)
f.close()
###Generating CSV###
file_path='tfidf.csv'
with open(file_path, 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(vocabulary_list)
for tfidf in TFIDF:
wr.writerow(tfidf)
#wr.writerow(TFIDF_query)