-
Notifications
You must be signed in to change notification settings - Fork 1
/
QueryExpansion.py
44 lines (31 loc) · 980 Bytes
/
QueryExpansion.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
from PyDictionary import PyDictionary
from Stopping import isStopWord
def DictExpandQuery(q_terms, k=5):
dic = PyDictionary()
new_terms = []
for term in q_terms:
if isStopWord(term):
continue
# check if word exists in the dictionary
w_found = True
try:
dic.meaning(term)
except:
w_found = False
# get k first synonyms
if w_found:
try:
synonyms = dic.synonym(term)
except:
continue
if synonyms == None:
continue
if len(synonyms) > k:
synonyms = synonyms[:k]
new_terms.extend(synonyms)
new_query_terms = q_terms + new_terms
return new_query_terms
if __name__ == '__main__':
test_query = "global warming potential"
expanded_query = DictExpandQuery(test_query.split(' '),k=2)
print expanded_query