-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
161 lines (107 loc) · 4.89 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'''
Created on Sep 21, 2012
@author: johnterzis
arguments: <precision> <query>
Contains the main loop of the application
'''
import json
import sys
import bingclient
import constants
import parser
import constants
import logging
import indexer
import rocchio
import common
import math
import PorterStemmer
#only if run as standalone script (not imported module) does, __name__ attribute defaults to __main__
#assume first arg is <precision> second is <query>
if __name__ == '__main__':
logging.basicConfig(level=logging.ERROR)
#create all singleton objects
arglist = sys.argv
if len(arglist) < 3:
print "Usage: <precision> <query>"
sys.exit(1) #exit interpreter
print 'Desired precision@10: {}'.format(arglist[1])
precisionTenTarg = float(arglist[1]) #must convert string to float
#'eECeOiLBFOie0G3C03YjoHSqb1aMhEfqk8qe7Xi2YMs='
#connect to client with key arg[1] and post a query with arg[3], query
bingClient = bingclient.BingClient(constants.BING_ACCT_KEY)
indexer = indexer.Indexer()
queryOptimizer = rocchio.RocchioOptimizeQuery(arglist[2])
firstPass = 1
precisionAtK = 0.00
expandedQuery = arglist[2]
queryWeights = {}
#while precision at 10 is less than desired amt issue a query, obtain new precision metric, expand query, repeat
while (precisionAtK < precisionTenTarg):
precisionAtK = 0.00 #reset precision each round
#PROCESS A QUERY
print 'Parameters'
print '%-20s= %s' % ("Query", expandedQuery)
print '%-20s= %s' % ("Target Precision", precisionTenTarg)
indexer.clearIndex()
if firstPass == 1:
result = bingClient.webQuery(arglist[2])
else:
result = bingClient.webQuery(expandedQuery)
jsonResult = json.loads(result) #convert string to json
#put result into a list of documents
parsedResult = parser.Parser(jsonResult['d']['results'])
parsedResult.parser()
DocumentList = parsedResult.getDocList()
print 'Total number of results: %d' % len(DocumentList)
#to calc precision@10 display documents to user and ask them to categorize as Relevant or Non-Relevant
print '======================'
# Reset collections for relevant ad nonrelevant documents
relevantDocuments = []
nonrelevantDocuments = []
for i in range(len(DocumentList)):
DocumentList[i]["ID"] = i
indexer.indexDocument(DocumentList[i])
print 'Result %d' % (i+1)
print '['
print ' %-9s: %10s' % ("URL", DocumentList[i]["Url"])
print ' %-9s: %10s' % ("Title", DocumentList[i]["Title"])
print ' %-9s: %10s' % ("Summary", DocumentList[i]["Description"])
print ']'
print ''
sys.stdout.write('Relevant (Y/N)? ')
value = raw_input()
if value.upper() == 'Y':
DocumentList[i]['IsRelevant'] = 1 #1 is true , 0 is false
precisionAtK = precisionAtK + 1
relevantDocuments.append(i)
elif value.upper() == 'N':
DocumentList[i]['IsRelevant'] = 0 #1 is true , 0 is false
nonrelevantDocuments.append(i)
else:
print 'Invalid value entered!'
precisionAtK = float(precisionAtK) / 10 #final precision@10 per round
print ''
print 'Precision@10 is: {}'.format(float(precisionAtK))
print ''
#expand query here by indexing and weighting current document list
if (precisionAtK == 0):
print 'Below desired precision, but can no longer augment the query'
sys.exit()
print 'Indexing results...'
indexer.waitForIndexer() # Will block until indexer is done indexing all documents
# Print inveretd file
for term in sorted(indexer.invertedFile, key=lambda posting: len(indexer.invertedFile[posting].keys())):
logging.info("%-30s %-2s:%-3d %-2s:%-3d %-3s:%-10f" % (term, "TF", indexer.termsFrequencies[term], "DF", len(indexer.invertedFile[term]), "IDF", math.log(float(len(DocumentList)) / len(indexer.invertedFile[term].keys()),10)))
print '======================'
print 'FEEDBACK SUMMARY'
if (precisionAtK < precisionTenTarg):
print ''
print 'Still below desired precision of %f' % precisionTenTarg
queryWeights = queryOptimizer.Rocchio(indexer.invertedFile, DocumentList, relevantDocuments, nonrelevantDocuments) #optimize new query here
newTerms = common.getTopTerms(expandedQuery, queryWeights, 2)
expandedQuery = expandedQuery + " " + newTerms[0] + " " + newTerms[1]
firstPass = 0
print 'Augmenting by %s %s' % (newTerms[0], newTerms[1])
#precision@10 is > desired , return query and results to user
print 'Desired precision reached, done'