forked from muskaancontlo/Pdf-Search-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
290 lines (207 loc) · 5.9 KB
/
query.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Import Statements
import time
import json
import nltk
from nltk.tokenize import word_tokenize
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
nltk.download('stopwords')
nltk.download('words')
from nltk.corpus import words
from nltk.tokenize import MWETokenizer
from wildcard import wildcardProcessing
start_time = time.time()
# Text file the query
file = open('query.txt')
read = file.read()
file.seek(0)
read
# Calc No of line
line = 1
for word in read:
if word == '\n':
line += 1
# print("Number of lines in file is: ", line)
#list to store each line as an element of list
# array = []
# for i in range(line):
# array.append(file.readline())
# print(array)
# Taking care of Punctuations
punc = '''!()-[]{};:'"\, <>./?@#$%^&_~'''
for ele in read:
if ele in punc:
read = read.replace(ele, " ")
# print(read)
# Making case insensitive
read=read.lower()
# print(read)
# print(read)
# tokenizer = MWETokenizer([('*')])
# # Tokenization
# for i in range(1):
# # this will convert
# # the word into tokens
# text_tokens = tokenizer.tokenize(word_tokenize(read))
# print(text_tokens)
text_tokens = read.split(' ')
# Wildcard characters
wildcards = []
# Checking for permute term
for tok in text_tokens:
if '*' in tok:
wildcards.extend(wildcardProcessing(tok))
text_tokens.remove(tok)
# for wc in wildcards:
# if wc not in text_tokens:
# text_tokens.append(wc)
# Spell Correction
correct_words = words.words()
correct_tokens = []
changed_words = {}
for word in text_tokens:
temp = [(jaccard_distance(set(ngrams(word, 2)),
set(ngrams(w, 2))),w)
for w in correct_words if w[0]==word[0]]
correct_word = sorted(temp, key = lambda val:val[0])[0][1]
correct_tokens.append(correct_word)
if(word != correct_word):
changed_words[word] = correct_word
# Taking care of stop words
tokens_without_sw = [
word for word in correct_tokens if not word in stopwords.words()]
# print(tokens_without_sw)
# For Performming Stemming (Porter)
# ps = PorterStemmer()
# tokens_final = []
# for tok in tokens_without_sw:
# tok_lem = ps.stem(tok)
# if tok_lem not in tokens_final:
# tokens_final.append(tok_lem)
tokens_final = tokens_without_sw
# file1 = open('token_try.txt','w')
# for tok in tokens_final:
# file1.write(tok +" ")
# file1.close()
# Dictionary for storing the line numbers of tokens (Inverted Tavle Index)
# dict = {}
# for i in range(line):
# check = array[i].lower()
# for item in tokens_final:
# if item in check:
# if item not in dict:
# dict[item] = []
# if item in dict:
# if ((i+1) not in dict[item]):
# dict[item].append(i+1)
# print(dict)
# file1 = open('dict.txt','w')
# for tok in dict:
# file1.write(tok +"\n\n")
# file1.close()
# print(dict)
# Printing ITI
# for i in dict:
# print (i)
# json = json.dumps(dict)
# f = open("query.json", 'w')
# f.write(json)
# f.close()
# Loading all Json files
f = open('start.json')
start = json.load(f)
f.close()
f = open('end.json')
end = json.load(f)
f.close()
f = open('dict.json')
data = json.load(f)
f.close()
f = open('document.json')
doc = json.load(f)
f.close()
# print(start)
# print(end)
# print(data)
# Function to find paragraph containing all tokens
# def common_numbers(d):
# # Find the set of common numbers for all keys
# common_set = set(d[next(iter(d))])
# for lst in d.values():
# common_set &= set(lst)
# # Convert the set of common numbers to a list of lists
# common_list = [list(x) for x in common_set]
# return common_list
def common_para(search):
res = search[list(search.keys())[0]]
for key in search:
res = list(set(res).intersection(set(search[key])))
return res
# Function to check which document the line belongs
def check_doc(line_no):
for d in doc:
if line_no>=doc[d][0] and line_no<=doc[d][1]:
return d
list_tokens = []
# Wild Card handling
for wc in wildcards:
temp = []
temp.extend(tokens_final)
temp.append(wc)
list_tokens.append(temp)
tot_search = []
# Searching the line for the search token keywords
for lis in list_tokens:
search = {}
for item in lis:
if item in data:
for idx in data[item]:
for i in range(len(start)):
if(idx>=start[i] and idx<=end[i]):
if item in search:
search[item].append((start[i], end[i]))
else:
search[item] = [(start[i], end[i])]
# print(item + " : " + str(start[i]) + " -> " + str(end[i]))
if(search):
tot_search.append(search)
# print(search)
tot_common_list =[]
flag = 0
for search in tot_search:
if len(search)!=0:
common_list = common_para(search)
flag = 1
if(len(common_list)!=0):
tot_common_list.extend(common_list)
print('\n\n')
res_count = 1
if flag:
for lis in tot_common_list:
with open(r"final-final.txt", errors="ignore") as fp:
x = fp.readlines()[lis[0]:lis[1]]
if(lis[0] == lis[1]) :
continue
print("RESULT " + str(res_count))
print("\n")
print("Document Name : " + str(check_doc(lis[0])))
print("\n")
print(''.join(x))
print("\n\n--------------------------------------------------------------------------")
res_count += 1
if (res_count == 1):
print("\n\nNO OUTPUT FOR SAME PARAGRAPH\n\n")
end_time = time.time()
print("Time Taken : " + str(end_time-start_time))
print("\nTokens :")
print(tokens_final)
print("\nWildcards :")
print(wildcards)
# print(search)
# print(common_list)
if(len(changed_words) != 0):
print("\nDid You Mean?")
for key in changed_words:
print(key + " -> " + changed_words[key])