-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_index.py
156 lines (134 loc) · 4.39 KB
/
search_index.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
import nltk
import json
from elasticsearch import Elasticsearch
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
installed_packages = 0
try:
wordnet_dir = nltk.find('corpora/wordnet')
print('Wordnet is already installed in ' + str(wordnet_dir))
installed_packages += 1
punkt_dir = nltk.find('tokenizers/punkt')
print('Punkt is already installed in ' + str(punkt_dir))
installed_packages += 1
omw_dir = nltk.find('corpora/omw-1.4')
print('Open Multilingual Wordnet(OMW) is already installed in ' + str(omw_dir))
installed_packages += 1
print()
except:
print('One of these corporas and tokenizers were not installed:\n \t- corpora/wordnet\n \t- corpora/omw-1.4\n \t- tokenizers/punkt')
print()
print('Installing required packages...')
if installed_packages == 0:
print('Installing corpora/wordnet...')
nltk.download('wordnet')
print('corpora/wordnet installed successfully')
print()
print('Installing tokenizers/punkt...')
nltk.download('punkt')
print('tokenizers/punkt installed successfully')
print()
print('Installing corpora/omw-1.4...')
nltk.download('omw-1.4')
print('corpora/omw-1.4 installed successfully')
elif installed_packages == 1:
print('Installing tokenizers/punkt...')
nltk.download('punkt')
print('tokenizers/punkt installed successfully')
print()
print('Installing corpora/omw-1.4...')
nltk.download('omw-1.4')
print('corpora/omw-1.4 installed successfully')
else:
print('Installing corpora/omw-1.4...')
nltk.download('omw-1.4')
print('corpora/omw-1.4 installed successfully')
elastic_host = {
"host": "localhost",
"port": 9200
}
index = 'facts-index'
es = Elasticsearch(hosts=[elastic_host])
def expand(query):
query = query.lower()
tokens = word_tokenize(query)
query_string = ""
num_tokens = 0
for token in tokens:
synonyms = []
synset_array = wordnet.synsets(token)
for syn in synset_array:
for l in syn.lemmas():
if l.name() not in synonyms:
synonyms.append(l.name())
if(len(synset_array) == 0):
synonyms.append(token)
query_string += '(' + ' OR '.join(synonyms) + ')'
num_tokens += 1
if (num_tokens < len(tokens)):
query_string = query_string + ' AND '
print(query, '\n*******************\n', query_string)
return query_string
def search(query):
result = es.search(
index=index,
body={
"query": {
"query_string": {
"fields": ["title", "url", "date_published", "rating", "author_name", "category", "tags", "claim", "content"],
"query": query
}
}
}
)
return result
def write(path, query, result):
if(not path.endswith("/")):
path+="/"
path += query + ".txt"
print(path)
dst_file = open(path, 'w')
dst_file.write(result)
dst_file.close()
def query(simple_query):
simple_search_results = search(simple_query)
path = './result/simple_queries/'
write(path,
simple_query,
json.dumps(
{
"max_score": simple_search_results['hits']['max_score'],
"hits": simple_search_results['hits']['hits']
},
indent=1
)
)
expanded_query = expand(simple_query)
expanded_search_result = search(expanded_query)
path = './result/expanded_queries/'
write(
path,
simple_query,
json.dumps(
{
"max_score": expanded_search_result['hits']['max_score'],
"hits": expanded_search_result['hits']['hits']
},
indent=1
)
)
if __name__ == '__main__':
path = './simple_queries.txt'
simple_queries_file = open(path, 'r')
lines = simple_queries_file.readlines()
simple_queries_file.close()
simple_queries = []
for line in lines:
line = line.replace('\n', '')
line = line.replace('"', '')
q = line.split('=')[1]
q = q[1: len(q)]
simple_queries.append(q)
for q in simple_queries:
query(q)