-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayResult.py
207 lines (148 loc) · 6.86 KB
/
DisplayResult.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import operator
import os
import re
import shutil
import traceback
import RetrievalModels
import GenerateTokenizedCorpus
import PerformanceEvaluation
OUTPUT_RUN_FILE= 'Retrieval/OutputFiles/Top_100_Query_Result_QueryLikelihoodModel_0.txt'
'''Represents the path of the folder which will contain top 10 retrieved documents for all queries with snippets'''
SNIPPETS_FOLDER_PATH="Display/Retrieved_Docments_with_snippets"
'''Represents the path of the folder containing the un-processed corpus'''
CORPUS_PATH="CASM-Files/Corpus"
'''Represents the path of the folder containing all the STOPWORDS'''
STOPWORDS_FILE_LOCATION="CASM-Files/common_words.txt"
'''A dictionary for storing all the stopwords'''
STOPWORDS={}
'''Stores all the stopwords from a file called common_words.txt and stores them into a global dictionary called STOPWORDS'''
def fetch_stopwords():
global STOPWORDS
path=os.path.join(os.getcwd(),STOPWORDS_FILE_LOCATION)
f=open(path,"r")
content=f.readlines()
for line in content:
STOPWORDS[line[:-1]]=True
def generate_snippets(query_id,query,docScore_docids_query):
DOCUMENT_SNIPPET_DICT={}
query=str(query).lower()
docScores = docScore_docids_query[0]
docIDss = docScore_docids_query[1]
docIds = [i for _, i in sorted(zip(docScores, docIDss))] # doc ids sorted in reverse order according to the scores
docScores.sort()
sorted_doc_score = docScores[::-1]
c=0
#for t in sorted_doc_score:
for doc_id in docIds:
c+=1
'''Stores frequencies of every term in the index'''
WORD_FREQUENCY_DICT={}
'''Stores significance scores for every sentence in a document'''
SENTENCE_SCORES={}
'''fetching the text content of the document from the corpus'''
current_doc_path=os.path.join(CORPUS_PATH,doc_id+".html")
f=open(current_doc_path,"r")
content=f.read()
text_content=GenerateTokenizedCorpus.parseHTML(content)
text_content=text_content.lower()
sentence_count=0
'''Calculating the frequency of each term and counting the number of sentences'''
for line in text_content.split("\n"):
if(line != ""):
sentence_count+=1
for term in line.split():
term=RetrievalModels.removePunctuation(term)
if term in WORD_FREQUENCY_DICT.keys():
WORD_FREQUENCY_DICT[term]+=1
else:
WORD_FREQUENCY_DICT[term]=1
'''Calculating the significance score for each sentence'''
for line in text_content.split("\n"):
if(line!=""):
significant_word_count=0
first_index=0
last_index=0
term_list=line.split()
for i in range(0,len(term_list)):
term=RetrievalModels.removePunctuation(term_list[i])
if(check_significant_term(term,sentence_count,str(query),WORD_FREQUENCY_DICT)):
significant_word_count+=1
if(first_index==0):
first_index=i+1
last_index=i+1
span_length=(last_index-first_index)+1
SENTENCE_SCORES[line]=float(significant_word_count**2/span_length)
sorted_sentence_score=sorted(SENTENCE_SCORES.items(),key=operator.itemgetter(1),reverse=True)
DOCUMENT_SNIPPET_DICT[doc_id]=sorted_sentence_score
if(c==10):
break
'''Generate retrived results with snippets for the currnt query'''
genarate_snippet_files(docIds,DOCUMENT_SNIPPET_DICT,query_id,query)
'''Stores the top 10 retrieved results with snippets and highlighting significant terms in a text file named as <query_id>.txt
inside a folder called 'Retrieved_Documents_with_snippets'''
def genarate_snippet_files(docIds,DOCUMENT_SNIPPET_DICT,query_id,query):
global STOPWORDS
try:
c=0
file_name="Query"+str(query_id)+".txt"
file_location=open(SNIPPETS_FOLDER_PATH+"/"+file_name,"a")
for doc_id in docIds:
c+=1
sorted_sentences=DOCUMENT_SNIPPET_DICT[doc_id]
file_location.write(doc_id+"\n")
file_location.write("----------------------------------------------------"+"\n")
for i in range(0,len(sorted_sentences)):
sentence=sorted_sentences[i][0]
sentence=re.sub(r'[^a-zA-Z0-9]'," ",sentence)
for term in sentence.split():
if(term in query and term not in STOPWORDS):
'''if the term is present in query then it is printed in uppercase to represent highlighting'''
file_location.write(term.upper()+" ")
else:
file_location.write(term+" ")
file_location.write("\n")
if(i==3):
break
file_location.write("\n\n\n")
if(c==5):
break
file_location.close()
except Exception:
print(traceback.format_exc())
'''Checks whether a particular is significant or not'''
def check_significant_term(term,sentence_count,query,WORD_FREQUENCY_DICT):
global STOPWORDS
query_term_list=query.split()
term_frequency=WORD_FREQUENCY_DICT[term]
if(term not in STOPWORDS):
if(sentence_count<25):
if(term_frequency>=(7-0.1*(25-sentence_count)) or term in query_term_list):
return True
elif(sentence_count>=25 and sentence_count<=40):
if(term_frequency>=7):
return True
elif(sentence_count>40):
if(term_frequency>=(7+0.1*(sentence_count-40))):
return True
return False
def main(docScorePerQuery):
'''Checks if the folder storing all the retrieved results with snippets exists, if exists delete and make a new one'''
if(os.path.exists(SNIPPETS_FOLDER_PATH)):
shutil.rmtree(SNIPPETS_FOLDER_PATH)
os.mkdir(SNIPPETS_FOLDER_PATH)
'''Fetching Stopwords'''
fetch_stopwords()
# fetch QueryMap
queryMap=RetrievalModels.fetchQueryMap()
print("Storing the top 10 documents with snippets for all queries")
print(" ")
print("docScorePerQuery")
print(docScorePerQuery)
print(" ")
print("queryMap")
print(queryMap)
for queryID in docScorePerQuery:
generate_snippets(queryID,queryMap[int(queryID[1:])],docScorePerQuery[queryID])
if __name__=='__main__':
docScorePerQuery = PerformanceEvaluation.fetchScoresFromDocScore()
main(docScorePerQuery)