This repository has been archived by the owner on Aug 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wordnet_fetcher.py
187 lines (134 loc) · 6.05 KB
/
wordnet_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# coding: utf-8
import json
import sys
import pickle
import pymysql.cursors
import os
from nltk import word_tokenize
from nltk.corpus import stopwords
from collections import defaultdict
from nltk.stem.porter import *
import hashlib
# Number of words in the wordnet dictionary
# Also all ids from 1 to NUMBER_OF_WORDS_IN_DICT are present in the database
# We use this attribute to deterministically select x words randomly (with seed)
NUMBER_OF_WORDS = 147306
NUMBER_OF_DEFINITIONS = 117659
NUMBER_OF_SENSES = 206941
# Database connection information
DATABASE_HOST = 'localhost'
DATABASE_USER = 'root'
DATABASE_PASS = ''
DATABASE_DB = 'wordnet30'
class WordnetFetcher:
"""
Creates a nice dataset from lemmas and their definitions
We also store definitions in pickle files so we do not
need to refetch every time
"""
def mysql_connection(self):
"""
Creates a MySQL database connection
"""
return pymysql.connect(host=DATABASE_HOST,
user=DATABASE_USER,
password=DATABASE_PASS,
db=DATABASE_DB,
charset='utf8mb4',
cursorclass=pymysql.cursors.SSCursor)
def fetch_definitions(self, stem=False, remove_stop_words=False, definition_limit=None):
pickle_name = hashlib.md5(
json.dumps(
('fetch_definitions', stem, remove_stop_words, definition_limit, __file__),
sort_keys=True
).encode('utf-8')
).hexdigest()
pickle_file = os.path.dirname(os.path.realpath(__file__)) + '/../../out/%s.pkl' % (pickle_name,)
if os.path.isfile(pickle_file):
print('Loading %s…' % (pickle_file,))
return pickle.load(open(pickle_file, 'r+b'))
connection = self.mysql_connection()
definitions = dict()
definition_vocab = defaultdict(int)
stemmer = PorterStemmer()
stopwords_english = stopwords.words('english')
punctiation_words = ['.', ',', ';', ':', '(', ')', '`']
punctiation_chars = ['`']
try:
with connection.cursor() as cursor:
sql = "SELECT `synset`.`synsetid`, `synset`.`definition` FROM `synset` " \
"WHERE EXISTS " \
"(SELECT 1 " \
"FROM `sense` " \
"WHERE `sense`.`synsetid` = `synset`.`synsetid`)"
# Read records
cursor.execute(sql)
for i, (identifier, definition) in enumerate(cursor):
if definition_limit is not None and i > definition_limit:
break
if i % 1000 == 0:
sys.stdout.write("\rReading definitions… %6.2f%%" % ((100 * i) / float(NUMBER_OF_DEFINITIONS),))
tokens = word_tokenize(definition)
if remove_stop_words:
tokens = [t for t in tokens if t not in stopwords_english]
# remove punctuation
tokens = [t for t in tokens if t not in punctiation_words]
# remove punctuation chars
tokens = [''.join(c for c in t if c not in punctiation_chars) for t in tokens]
# to lower case
tokens = [t.lower() for t in tokens]
if stem:
tokens = [stemmer.stem(t) for t in tokens]
if len(tokens) > 0:
definitions[int(identifier)] = tokens
for token in tokens:
definition_vocab[token] += 1
finally:
connection.close()
print("\rDefinitions read")
result = (definitions, definition_vocab)
print('Storing %s' % (pickle_file,))
pickle.dump(result, open(pickle_file, 'w+b'), protocol=4)
return result
def fetch_lemmas(self, definitions, stem=False, multi_word_lemmas=False):
pickle_name = hashlib.md5(
json.dumps(
('fetch_lemma', definitions, stem, multi_word_lemmas, __file__),
sort_keys=True
).encode('utf-8')
).hexdigest()
pickle_file = os.path.dirname(os.path.realpath(__file__)) + '/../../out/%s.pkl' % (pickle_name,)
if os.path.isfile(pickle_file):
print('Loading %s…' % (pickle_file,))
return pickle.load(open(pickle_file, 'r+b'))
connection = self.mysql_connection()
stemmer = PorterStemmer()
lemma_per_definition = defaultdict(list)
lemma_vocab = defaultdict(int)
try:
with connection.cursor() as cursor:
sql = "SELECT `sense`.`synsetid`, `word`.`lemma` FROM `sense` " \
"INNER JOIN `word` ON `sense`.`wordid` = `word`.`wordid` " \
"WHERE `sense`.`synsetid` IN (" + ','.join([str(x) for x in definitions.keys()]) + ")"
cursor.execute(sql)
for i, (identifier, lemma) in enumerate(cursor):
if i % 1000 == 0:
sys.stdout.write("\rReading words… %6.2f%%" % ((100 * i) / float(NUMBER_OF_SENSES),))
tokens = word_tokenize(lemma)
if stem:
tokens = [stemmer.stem(t) for t in tokens]
# to lower case
tokens = [t.lower() for t in tokens]
if len(tokens) > 0:
if multi_word_lemmas or len(tokens) == 1:
lemma_per_definition[int(identifier)].append(tokens)
if multi_word_lemmas or len(tokens) == 1:
for token in tokens:
lemma_vocab[token] += 1
finally:
connection.close()
print("\rWords read")
result = (lemma_per_definition, lemma_vocab)
print('Storing %s' % (pickle_file,))
pickle.dump(result, open(pickle_file, 'w+b'), protocol=4)
return result