-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
167 lines (129 loc) · 5.75 KB
/
helpers.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
158
159
160
161
162
163
164
165
166
167
import pickle
from indexing import *
from query import *
from subprocess import call
from pylab import *
def load_index(dir = "/home/stathis/Projects/UVA_IR/index.pkl"):
if os.path.exists(dir):
pkl_file = open(dir, 'rb')
index = pickle.load(pkl_file)
pkl_file.close()
else:
index = {}
index['indexed_docs'] = {}
index['tokens'] = {}
index['info'] = {}
index['info']['stemmer'] = " "
index['info']['lemmatization'] = " "
index['info']['remove_stopwords'] = False
return index
def save_index(index):
output = open('/home/stathis/Projects/UVA_IR/index.pkl', 'wb')
pickle.dump(index, output)
output.close()
def print_statistics(index , token = 'of'):
total_tokens = 0
for x in index['indexed_docs']:
total_tokens += index['indexed_docs'][x]['length']
unique_tokens = len(index['tokens'])
token_counts = 0
if token in index['tokens']:
token_counts = index['tokens'][token]['total_counts']
print "Total Number of Tokens : " , total_tokens
print "Number of Unique Tokens : " , unique_tokens
print "Total Count of Token '" + token + "' : " , token_counts
def evaluate(query,qid,model='intersection'):
index = load_index()
result = run_query(query , index , model)
rank = 1
score = 1.0
runID = 1
## Write res file of the query
f = open('/home/stathis/Projects/UVA_IR/results/'+ str(qid) +".res",'wb')
string = ""
i = 0
for doc in result:
i += 1
string += str(qid) + " Q0 " + doc[0] +" "+ str(i) + " " + str(round(doc[1],2))+ " " + str(runID) + "\n"
f.write(string)
f.close()
##evaluation using terrier
call(["/home/stathis/Projects/UVA_IR/terrier/bin/trec_terrier.sh", "-e /home/stathis/Projects/UVA_IR/results/"+ str(qid) +".res"])
f = open("/home/stathis/Projects/UVA_IR/results/"+ str(qid) +".eval",'r')
evaluation = f.read()
f.close()
#return chart_results(str(qid))
return evaluation
def index(dir_or_file , stemmer = 'lancaster' , lemmatization = "wordnet" , remove_stopwords = False , stopwords = 'nltk'):
index = load_index()
if ( (index['info']['stemmer'] != stemmer ) or (index['info']['lemmatization'] != lemmatization) or (index['info']['remove_stopwords'] != remove_stopwords) ) :
index = {}
index['indexed_docs'] = {}
index['tokens'] = {}
index['info'] = {}
index['info']['stemmer'] = stemmer
index['info']['lemmatization'] = lemmatization
index['info']['remove_stopwords'] = remove_stopwords
if 'stopwords' not in index['info']:
index['info']['stopwords'] = load_stopwords(stopwords ,lemmatization , stemmer)
print "Settings :"
print "Lemmatization :" + lemmatization
print "Stemmer:" + stemmer
print "Remove Stopwords :" + str(remove_stopwords)
if os.path.isfile(dir_or_file):
print "Indexing Document '" + dir_or_file + "' ..."
index_document(dir_or_file,index , stemmer , lemmatization ,remove_stopwords , index['info']['stopwords'])
elif os.path.isdir(dir_or_file):
print "Indexing Directory '" + dir_or_file + "' ..."
index_directory(dir_or_file, index , stemmer , lemmatization ,remove_stopwords , index['info']['stopwords'])
save_index(index)
print "Done!"
return index
def chart_results(qid):
results = {}
precx = []
precy = []
pprecx = []
pprecy = []
with open("/home/stathis/Projects/UVA_IR/results/"+ str(qid) +".eval",'r') as infile:
i = 0
for row in infile:
line = nltk.word_tokenize(row)
if len(line) == 3 :
results[line[0]] = line[2];
elif len(line) == 5 :
if line[0] == 'Number':
results['NumberOfQueries'] = line[4];
else:
# results["P" + line[2]] = line[4];
precx += [line[2]]
precy += [line[4]]
elif len(line) == 6 :
# results["PP" + line[2]] = line[5];
pprecx += [line[2]]
pprecy += [line[5]]
elif len(line) == 4 :
if line[0] == 'Relevant':
results['RelevantRetrieved'] = line[3];
else:
results[line[0]] = line[3];
rcParams['figure.figsize'] = 7, 3
# Make an example plot with two subplots...
fig = figure()
ax1 = fig.add_subplot(1,2,1)
ax1.plot(precx,precy ,color="blue", linewidth=2.5, linestyle="-" ,label="Precision at")
ax1.legend(loc='upper right')
ax2 = fig.add_subplot(1,2,2)
ax2.plot(pprecx,pprecy ,color="blue", linewidth=2.5, linestyle="-" ,label="Precision at %")
ax2.legend(loc='upper right')
fig.savefig("/home/stathis/Projects/UVA_IR/results/"+str(qid)+'.png' ,transparent=True)
fig.savefig("/home/stathis/Projects/UVA_IR/irsysweb/query/static/"+str(qid)+'.png' ,transparent=True)
results["path"] ="/static/"+str(qid)+'.png'
return results
# subplot(1,2,1)
# plot(precx,precy ,color="blue", linewidth=2.5, linestyle="-" ,label="Precision at")
# legend(loc='upper right')
# subplot(1,2,2)
# plot(pprecx,pprecy ,color="blue", linewidth=2.5, linestyle="-" ,label="Precision at %")
# legend(loc='upper right')
# show()