-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculateTfidf.py
72 lines (58 loc) · 1.72 KB
/
calculateTfidf.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
from preprocessing import *
from modules import *
# Calulate word Frequency of query document
def wordFreqA(bowA, wordDictA):
for word in bowA:
wordDictA[word]+=1
# Calculate word frequency of source parsed
def wordFreqB(bowB, wordDictB):
for word in bowB:
wordDictB[word]+=1
# Compute Term Frequency
def computeTF(wordDict, bow):
tfDict = {}
bowCount = len(bow)
for word, count in wordDict.items():
try:
tfDict[word] = count/float(bowCount)
except Exception as err:
continue
return tfDict
# Compute Inverse Document Frequency
def computeIDF(docList):
import math
idfDict = {}
N = len(docList)+1
idfDict = dict.fromkeys(docList[0].keys(), 0)
for doc in docList:
for word, val in doc.items():
if val > 0:
idfDict[word] += 1
for word, val in idfDict.items():
idfDict[word] = math.log10(N / float(val))
return idfDict
# Compute Inverse Inverse Document Frequency
def computeTFIDF(tfBow, idfs):
tfidf = {}
for word, val in tfBow.items():
tfidf[word] = val*idfs[word]
return tfidf
# Calculate similarity of query and source documents
def similarity(tfBowA, tfBowB, bowA, bowB):
sum =0
totalSum=0
for commonToken in set(bowA).intersection(set(bowB)):
sum+= min(tfBowA[commonToken] *len(bowA),tfBowB[commonToken]*len(bowB))
for val in tfBowA.values():
totalSum+= val
if totalSum==0:
return 0
else:
return (sum)/(totalSum*len(bowA))
# def generateQuery(tfBowA):
# tfBowA_sorted = dict( sorted(tfBowA.items(), key=operator.itemgetter(1),reverse=True))
# query = ""
# for key,value in tfBowA_sorted.items():
# if value!=0:
# query+=key+" "
# return query