-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryExpansion.py
More file actions
69 lines (55 loc) · 1.51 KB
/
QueryExpansion.py
File metadata and controls
69 lines (55 loc) · 1.51 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
import pandas as pd
import time
from collections import Counter
def FindW(query, queryset):
count = 0
for term in query:
count += queryset[term]
return count
def FindWeights():
print 'Fetching Data...'
start = time.time()
QueryData = pd.read_csv('Data/Queries.csv')
end = time.time()
print 'Data Fetched in :',end-start
frequencies = {}
termlist = []
print 'Fetching List of terms ...'
start = time.time()
for terms in QueryData['ListOfTerms']:
for term in terms.split(':'):
termlist += [term]
# print termlist
end = time.time()
print "termlist in :", end-start
print "Finding Frequency Distributuion.."
start = time.time()
FreqDist = Counter(termlist)
end = time.time()
print "FreqDist in", end-start
print "Getting frequencies / weights...."
start = time.time()
for query in QueryData['ListOfTerms']:
try:
W = FindW(query,FreqDist)
for term in query.split(':'):
Wk = FreqDist[term]
value = float(float(Wk) / float(W))
frequencies[term] = value
except ZeroDivisionError:
continue
end = time.time()
print "Values calculated in :",end-start
m = max(frequencies.values())
print "Entering data into file..."
start = time.time()
freqlist = []
for key in frequencies.keys():
freqlist += [[key,float(float(frequencies[key])/float(m))]]
df = pd.DataFrame(freqlist,columns=['Term','Frequency'])
df.to_csv('Data/Frequency.csv',header=True,index=0)
end = time.time()
print "Entered into file in :",end-start
return frequencies
if __name__ == '__main__':
FindWeights()