-
Notifications
You must be signed in to change notification settings - Fork 16
/
related_word_fetcher.py
161 lines (135 loc) · 4.28 KB
/
related_word_fetcher.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
from bs4 import BeautifulSoup
from urllib import request, parse
import re
import logging
import pickle
import os
from outputs.output import Log
"""
This python module is used to fill in missing keys from the
word vector model. This script attempts to find the closest
similar word to the one given. The end goal will enable every
word to be vectorized.
"""
logger = logging.getLogger('fact_clustering')
verbeRegex = re.compile("(?<=Conjugaison du verbe )\S+")
"""
Cache is a dictionary which stores keys<words not found>
and maps them to values<words found>
"""
cache = {}
__script_dir = os.path.abspath(__file__ + r"/../")
__rel_path = r'cache.pickle'
cachePickleFilePath = os.path.join(__script_dir, __rel_path)
# load pickle if it exists
if os.path.isfile(cachePickleFilePath):
Log.write('Loading cached Pickle of words.')
cacheFile = open(cachePickleFilePath, "rb")
cache = pickle.load(cacheFile)
def _find_synonym(soup):
"""
Finds a synonym to a word
:param soup: BeautifulSoup
:return: String if word found else None
"""
node = soup.find(id="Synonymes")
if node is None:
return None
synonyms = node.parent.next_sibling.next_sibling.text.split('\n')
synonyms = [word for word in synonyms if word != '']
return synonyms[0]
def _find_plural(soup):
"""
Finds plural of given word
:param soup: BeautifulSoup
:return: String if word found else None
"""
node = soup.find('i', string="Pluriel de")
if node is None:
return None
return node.next_sibling.next_sibling.text
def _find_fem_plural(soup):
"""
Finds feminine plural of word
:param soup: BeautifulSoup
:return: String if word found else None
"""
node = soup.find('i', string="Féminin pluriel de")
if node is None:
return None
return node.next_sibling.next_sibling.text
def _find_feminin(soup):
"""
Finds feminine of word
:param soup: BeautifulSoup
:return: String if word found else None
"""
node = soup.find('i', string="Féminin de")
if node is None:
return None
return node.next_sibling.next_sibling.text
def _find_conjugation(soup):
"""
Finds conjugation of verb
:param soup: BeautifulSoup
:return: String if word found else None
"""
verbText = verbeRegex.search(soup.text)
if verbText is None:
return None
return verbeRegex.search(soup.text).group()
def find_related(queryWord):
"""
Returns a related word to the given word, using French
Wikitionary. Returns the infinitif of verbs, the
masculin singulier of nouns, or synonym.
queryWord: word to lookup in wikitionary
returns: a string containing the similar word, or None
if none is found.
"""
if queryWord in cache:
return cache[queryWord]
requestURL = u"https://fr.wiktionary.org/wiki/" + \
parse.quote(queryWord)
try:
logger.info("Querying Wikitionary: " + requestURL)
response = request.urlopen(requestURL)
except BaseException:
logger.info(requestURL + " not found.")
cache[queryWord] = None
return None
soup = BeautifulSoup(response, 'html.parser')
relatedWord = _find_synonym(soup)
if relatedWord is not None:
cache[queryWord] = relatedWord
return relatedWord
relatedWord = _find_plural(soup)
if relatedWord is not None:
cache[queryWord] = relatedWord
return relatedWord
relatedWord = _find_fem_plural(soup)
if relatedWord is not None:
cache[queryWord] = relatedWord
return relatedWord
relatedWord = _find_feminin(soup)
if relatedWord is not None:
cache[queryWord] = relatedWord
return relatedWord
relatedWord = _find_conjugation(soup)
if relatedWord is not None:
cache[queryWord] = relatedWord
return relatedWord
cache[queryWord] = None
return None
def save_cache():
"""
Save cache of replaced words. This speeds up the process
whenever a previously found word needs to be found again.
:return: None
"""
__script_dir = os.path.abspath(__file__ + r"/../")
__rel_path = r'cache.pickle'
path = os.path.join(__script_dir, __rel_path)
Log.write('Saving Pickle of cached words.')
file = open(path, 'wb')
pickle.dump(cache, file)