-
Notifications
You must be signed in to change notification settings - Fork 1
/
SnippetGeneration_ExtraCredits.py
161 lines (142 loc) · 4.6 KB
/
SnippetGeneration_ExtraCredits.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
import re
import os
import glob
InputQueries = []
results = {}
rawHtmlFolder = os.getcwd() + '/corpus/'
docId2DocNumFile = 'DocumentIndexMapping_CACM.txt'
queryResultFile = os.getcwd() + '/7thRun_BM25_Stopping/BM25_Stopping_QueryResults.txt'
queryFile = os.getcwd() + '\\cacm.query'
pTagRegex = '<pre>(.*?)</pre>'
minFreq = 3
maxFreq = 8
def queries():
queries = open(queryFile, 'r').read()
pattern = re.compile(r'</DOCNO>(.*?)</DOC>')
lst = re.findall(pattern, queries.replace('\n',' '))
for query in lst:
InputQueries.append(query)
def prefixZero(suffix):
i = 0
prefix = ''
while i < 4 - len(suffix):
prefix += '%d' % i
i+=1
return prefix + suffix
def getParagraphs(body):
pattern = re.compile(pTagRegex)
return re.findall(pattern, body)
def rankingResult():
with open(queryResultFile, 'r') as f:
for res in f:
items = res.split(' ')
queryID = items[0]
documentID = items[2]
documentID = 'CACM-' + prefixZero(documentID) + '.html'
if queryID in results:
if len(results[queryID]) < 10:
results[queryID].append(documentID)
else:
results[queryID] = [documentID]
f.close()
def getTextTif(text):
wordFreq = {}
for unigram in text.split(' '):
word = unigram.strip("'").strip()
if word == '':
continue
if word in wordFreq:
wordFreq[word] += 1
else:
wordFreq[word] = 1
return wordFreq
def getSnippet(docDict, text):
words = text.split(' ')
snippet = ''
snippets = []
for word in words:
if word in docDict:
if docDict[word] > minFreq and docDict[word] < maxFreq:
snippet += word + ' '
maxNSWordCount = 0
else:
if snippet != '':
if maxNSWordCount < 4:
maxNSWordCount += 1
snippet += word + ' '
else:
snippets.append(snippet.strip())
snippet = ''
return snippets
def removePunctuation(text):
if ',' in text:
text = text.replace(',',' ')
if '.' in text:
text = text.replace('.',' ')
if '/' in text:
text = text.replace('/',' ')
if '?' in text:
text = text.replace('?',' ')
if '!' in text:
text = text.replace('!',' ')
if '"' in text:
text = text.replace('"',' ')
if '~' in text:
text = text.replace('~',' ')
if '@' in text:
text = text.replace('@',' ')
if '#' in text:
text = text.replace('#',' ')
if '(' in text:
text = text.replace('(',' ')
if ')' in text:
text = text.replace(')',' ')
if '^' in text:
text = text.replace('^',' ')
if '[' in text:
text = text.replace('[',' ')
if ']' in text:
text = text.replace(']',' ')
if ':' in text:
text = text.replace(':',' ')
if ';' in text:
text = text.replace(';',' ')
if '&' in text:
text = text.replace('&',' ')
if ' ' in text:
text = text.replace(' ',' ')
if text != '' and (text[-1] == '.' or text[-1] == ','):
return text[:-1]
return text.lower()
def highlight(snippets, query):
newSnippet = ''
query = removePunctuation(query)
for snippet in snippets:
words = snippet.split(' ')
for word in words:
if ' ' + word.lower() + ' ' in query:
newSnippet += '<HL>'+ word + '</HL> '
else:
newSnippet += word + ' '
newSnippet +='.... '
return newSnippet
def printSnippet(queryId):
query = InputQueries[i]
qid = '%d' % (queryId + 1)
snippet = ''
for doc in results[qid]:
with open (rawHtmlFolder + doc, 'r') as htmlFile:
text = getParagraphs(htmlFile.read().replace('\n',' ').replace('\t',' ').replace(' ',' ').replace(' ', ' '))
docDict = getTextTif(text[0])
snippet += '"'+ doc + '":\n'
snippet += highlight(getSnippet(docDict, text[0]), query) + '\n\n'
return snippet
queries()
rankingResult()
with open (os.getcwd() + '/snippets.txt', 'a+') as sFile:
i = 0
for i in range(len(InputQueries)):
sFile.write('query %d: %s\n' % (i+1, InputQueries[i]))
sFile.write('%s' % printSnippet(i))
sFile.write('----------------------------------------------------------------------------------\n\n')
sFile.close()