-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery.py
executable file
·116 lines (83 loc) · 2.7 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
from nltk import *
from nltk.corpus import stopwords
from nltk.corpus import wordnet
from collections import Counter
import numpy as np
import pickle
import os
import math
from utils import *
from pathlib import Path
from preprocess import pre_process
""" Use the wordnet corpus dataset and return the text representing
the expanded query """
BASE_DIR = Path(__file__).resolve().parent
PROBS_DIRS = os.path.join(BASE_DIR, 'probs')
def expand_query(text):
text = text.lower()
tokens = word_tokenize(text)
#find synonyms to words in the given text
similar = []
for word in tokens:
for syn in wordnet.synsets(word):
if len(syn.lemmas()) > 3:
for x in range(3):
similar.append(syn.lemmas()[x].name())
else:
for x in range(len(syn.lemmas())):
similar.append(syn.lemmas()[x].name())
#add the similar words to the query
for word in similar:
if word not in text:
text += " " + word
return text
""" We can use the expanded query and return each document that is similar
we return required number of search results
We use concepts of cosine-similarity to find the following """
def query(query, no_of_docs):
query = expand_query(query)
query = pre_process(query)
#read persistant storage
pickle_in = open('serial.txt', 'rb+')
db = pickle.load(pickle_in)
pickle_in.close()
#get data that we stored in the map
words = db['words']
N = db['N']
documents_vector = db['documents_vector']
dfs = db['dfs']
#generate query vector
query_vector = []
for word in words:
#calculate document frequency
tf = 0
for term in query:
if term == word:
tf = tf + 1
df = 1 if dfs[word] == 0 else dfs[word]
idf = math.log(N/df)
tfidf = tf*idf
query_vector.append(tfidf)
#calculate cosine similarity
scores = []
for vector in documents_vector:
score = np.dot(vector, query_vector)
scores.append(score)
document_scores = []
dir = PROBS_DIRS
file_dir = os.listdir(dir)
file_dir.sort()
for x in range(len(file_dir)):
document_scores.append((scores[x], file_dir[x]))
document_scores.sort(reverse=True)
#return no_of_docs documents
result = []
if len(document_scores) < no_of_docs:
for x in range(len(document_scores)):
if(document_scores[x][0] != 0):
result.append(document_scores[x][1])
else:
for x in range(no_of_docs):
if(document_scores[x][0] != 0):
result.append(document_scores[x][1])
return result