-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_wordnet.py
39 lines (32 loc) · 1.21 KB
/
module_wordnet.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
import nltk
from nltk.corpus import wordnet
class WordnetModule:
def __init__(self, model_dir):
self.model_dir = model_dir
def query_expanded(self, query_as_list):
"""
expand the query with similar words to the terms in query
:param query_as_list: list of query's terms
:return: the query with more similar terms in it
"""
if len(query_as_list) == 0:
return query_as_list
synonyms = list(query_as_list)
for term in query_as_list[:2]:
self.add_synonyms(synonyms, term, 1)
return synonyms
def add_synonyms(self, synonyms, term, amount=1):
"""
adding similar words to term
:param synonyms: list of query terms
:param term: the term to find similar words to
:param amount: amount of words to add to query list with this term
"""
current = 0
for syn in wordnet.synsets(term):
for lemma in syn.lemmas():
if lemma.name() not in synonyms:
synonyms.append(lemma.name())
current += 1
if current == amount:
return