-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathword2vec_server.py
executable file
·464 lines (411 loc) · 15.3 KB
/
word2vec_server.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env python3
# coding: utf-8
import socket
import datetime
import threading
import sys
import gensim
import logging
import json
import configparser
import csv
from smart_open import open
from numpyencoder import NumpyEncoder
import config_path
class WebVectorsThread(threading.Thread):
def __init__(self, connect, address):
threading.Thread.__init__(self)
self.connect = connect
self.address = address
def run(self):
clientthread(self.connect, self.address)
def clientthread(connect, address):
# Sending message to connected client
connect.send(bytes(b"word2vec model server"))
# infinite loop so that function do not terminate and thread do not end.
while True:
# Receiving from client
data = connect.recv(1024)
if not data:
break
query = json.loads(data.decode("utf-8"))
output = operations[query["operation"]](query)
now = datetime.datetime.now()
print(
f"{now.strftime('%Y-%m-%d %H:%M')}\t{address[0]}:{str(address[1])}\t"
f"{data.decode('utf-8')}",
file=sys.stderr,
)
reply = json.dumps(output, ensure_ascii=False, cls=NumpyEncoder)
connect.sendall(reply.encode("utf-8"))
break
# came out of loop
connect.close()
def create_model_graph(model_identifier, tmodelfile, ffile):
graph = tf.Graph()
with graph.as_default() as current_graph:
with current_graph.name_scope(model_identifier) as scope:
tmodel = ElmoModel()
tmodel.load(tmodelfile)
freqdic = {}
for line in open(ffile, "r"):
if "\t" not in line:
freqdic["corpus_size"] = int(line.strip())
else:
(external_word, corp_frequency) = line.strip().split("\t")
freqdic[external_word] = int(corp_frequency)
return tmodel, freqdic, current_graph
config = configparser.RawConfigParser()
config.read(config_path.CONFIG)
root = config.get("Files and directories", "root")
HOST = config.get("Sockets", "host") # Symbolic name meaning all available interfaces
PORT = config.getint("Sockets", "port") # Arbitrary non-privileged port
tags = config.getboolean("Tags", "use_tags")
# Loading models
logging.basicConfig(
format="%(asctime)s : %(levelname)s : %(message)s", level=logging.INFO
)
# Contextualized models:
contextualized = config.getboolean("Token", "use_contextualized")
if contextualized:
import tensorflow as tf
from simple_elmo import ElmoModel
contextual_models = {}
with open(root + config.get("Files and directories", "contextualized_models"), "r") as csvfile:
reader = csv.DictReader(csvfile, delimiter="\t")
for row in reader:
contextual_models[row["identifier"]] = {}
contextual_models[row["identifier"]]["type_path"] = row["type_path"]
contextual_models[row["identifier"]]["token_path"] = row["token_path"]
contextual_models[row["identifier"]]["freq_path"] = row["freq_path"]
contextual_models[row["identifier"]]["default"] = row["default"]
contextual_models[row["identifier"]]["algo"] = row["algo"]
contextual_models[row["identifier"]]["ref_static"] = row["ref_static"]
contextual_models[row["identifier"]]["string"] = row["string"]
contextual_models_dic = {}
for m in contextual_models:
token_model_file = contextual_models[m]["token_path"]
type_model_file = contextual_models[m]["type_path"]
frequency_file = contextual_models[m]["freq_path"]
type_model = gensim.models.KeyedVectors.load_word2vec_format(type_model_file, binary=True)
token_model, elmo_frequency, g = create_model_graph(m, token_model_file, frequency_file)
contextual_models_dic[m] = (token_model, type_model, elmo_frequency, g)
our_models = {}
with open(root + config.get("Files and directories", "models"), "r") as csvfile:
reader = csv.DictReader(csvfile, delimiter="\t")
for row in reader:
our_models[row["identifier"]] = {}
our_models[row["identifier"]]["path"] = row["path"]
our_models[row["identifier"]]["default"] = row["default"]
our_models[row["identifier"]]["tags"] = row["tags"]
our_models[row["identifier"]]["algo"] = row["algo"]
our_models[row["identifier"]]["corpus_size"] = int(row["size"])
if row["default"] == "True":
defaultmodel = row["identifier"]
models_dic = {}
for m in our_models:
modelfile = our_models[m]["path"]
our_models[m]["vocabulary"] = True
if our_models[m]["algo"] == "fasttext":
models_dic[m] = gensim.models.KeyedVectors.load(modelfile)
else:
if modelfile.endswith(".bin.gz") or modelfile.endswith(".bin"):
models_dic[m] = gensim.models.KeyedVectors.load_word2vec_format(
modelfile, binary=True
)
our_models[m]["vocabulary"] = False
elif (
modelfile.endswith(".vec.gz")
or modelfile.endswith(".txt.gz")
or modelfile.endswith(".vec")
or modelfile.endswith(".txt")
):
models_dic[m] = gensim.models.KeyedVectors.load_word2vec_format(
modelfile, binary=False
)
our_models[m]["vocabulary"] = False
else:
models_dic[m] = gensim.models.KeyedVectors.load(modelfile)
print(f"Model {m} from file {modelfile} loaded successfully.", file=sys.stderr)
# Get pairs of words to create graph
def get_edges(word, model, mostsim):
edges = [{"source": word, "target": word, "value": 1}]
neighbors_list = []
for item in mostsim:
edges.append({"source": word, "target": item[0], "value": item[1]})
neighbors_list.append(item[0])
pairs = [
(neighbors_list[ab], neighbors_list[ba])
for ab in range(len(neighbors_list))
for ba in range(ab + 1, len(neighbors_list))
]
for pair in pairs:
edges.append(
{
"source": pair[0],
"target": pair[1],
"value": float(model.similarity(*pair)),
}
)
return edges
def find_variants(word, usermodel):
# Find variants of query word in the model
model = models_dic[usermodel]
results = None
candidates_set = set()
candidates_set.add(word.upper())
if tags and our_models[usermodel]["tags"] == "True":
candidates_set.add(word.split("_")[0] + "_X")
candidates_set.add(word.split("_")[0].lower() + "_" + word.split("_")[1])
candidates_set.add(word.split("_")[0].capitalize() + "_" + word.split("_")[1])
else:
candidates_set.add(word.lower())
candidates_set.add(word.capitalize())
for candidate in candidates_set:
if candidate in model.key_to_index:
results = candidate
break
return results
def frequency(word, model, external=None):
# Find word frequency tier
if external:
if word in external:
wordfreq = external[word]
else:
return 0, "low"
corpus_size = external["corpus_size"]
else:
corpus_size = our_models[model]["corpus_size"]
if word not in models_dic[model].key_to_index:
word = find_variants(word, model)
if not word:
return 0, "low"
if not our_models[model]["vocabulary"]:
return 0, "mid"
wordfreq = models_dic[model].get_vecattr(word, "count")
relative = wordfreq / corpus_size
tier = "mid"
if relative > 0.00001:
tier = "high"
elif relative < 0.0000005:
tier = "low"
return wordfreq, tier
# Vector functions
def find_synonyms(query):
q = query["query"]
pos = query["pos"]
usermodel = query["model"]
nr_neighbors = query["nr_neighbors"]
results = {"frequencies": {}, "neighbours_dist": []}
qf = q
model = models_dic[usermodel]
if qf not in model.key_to_index:
qf = find_variants(qf, usermodel)
if not qf:
if our_models[usermodel]["algo"] == "fasttext" and model.__contains__(q):
results["inferred"] = True
qf = q
else:
results[q + " is unknown to the model"] = True
results["frequencies"][q] = frequency(q, usermodel)
return results
results["frequencies"][q] = frequency(qf, usermodel)
results["neighbors"] = []
if pos == "ALL":
for i in model.most_similar(positive=qf, topn=nr_neighbors):
results["neighbors"].append(i)
else:
counter = 0
for i in model.most_similar(positive=qf, topn=30):
if counter == nr_neighbors:
break
if i[0].split("_")[-1] == pos:
results["neighbors"].append(i)
counter += 1
if len(results) == 0:
results["No results"] = True
return results
for res in results["neighbors"]:
freq, tier = frequency(res[0], usermodel)
results["frequencies"][res[0]] = (freq, tier)
raw_vector = model[qf]
results["vector"] = raw_vector.tolist()
results["edges"] = get_edges(q, model, results["neighbors"])
return results
def find_similarity(query):
q = query["query"]
usermodel = query["model"]
model = models_dic[usermodel]
results = {"similarities": [], "frequencies": {}}
for pair in q:
(q1, q2) = pair
qf1 = q1
qf2 = q2
if q1 not in model.key_to_index:
qf1 = find_variants(qf1, usermodel)
if not qf1:
if our_models[usermodel][
"algo"
] == "fasttext" and model.wv.__contains__(q1):
results["inferred"] = True
qf1 = q1
else:
results["Unknown to the model"] = q1
return results
if q2 not in model.key_to_index:
qf2 = find_variants(qf2, usermodel)
if not qf2:
if our_models[usermodel][
"algo"
] == "fasttext" and model.wv.__contains__(q2):
results["inferred"] = True
qf2 = q2
else:
results["Unknown to the model"] = q2
return results
results["frequencies"][qf1] = frequency(qf1, usermodel)
results["frequencies"][qf2] = frequency(qf2, usermodel)
pair2 = (qf1, qf2)
result = float(model.similarity(qf1, qf2))
results["similarities"].append((pair2, result))
return results
def scalculator(query):
q = query["query"]
pos = query["pos"]
usermodel = query["model"]
nr_neighbors = query["nr_neighbors"]
model = models_dic[usermodel]
results = {"neighbors": [], "frequencies": {}}
positive_list = q[0]
negative_list = q[1]
plist = []
nlist = []
for word in positive_list:
if len(word) < 2:
continue
if word in model.key_to_index:
plist.append(word)
continue
else:
q = find_variants(word, usermodel)
if not q:
if our_models[usermodel][
"algo"
] == "fasttext" and model.wv.__contains__(word):
results["inferred"] = True
plist.append(word)
else:
results["Unknown to the model"] = word
return results
else:
plist.append(q)
for word in negative_list:
if len(word) < 2:
continue
if word in model.key_to_index:
nlist.append(word)
continue
else:
q = find_variants(word, usermodel)
if not q:
if our_models[usermodel][
"algo"
] == "fasttext" and model.wv.__contains__(word):
results["inferred"] = True
nlist.append(word)
else:
results["Unknown to the model"] = word
return results
else:
nlist.append(q)
if pos == "ALL":
for w in model.most_similar(
positive=plist, negative=nlist, topn=nr_neighbors
):
results["neighbors"].append(w)
else:
for w in model.most_similar(positive=plist, negative=nlist, topn=30):
if w[0].split("_")[-1] == pos:
results["neighbors"].append(w)
if len(results["neighbors"]) == nr_neighbors:
break
if len(results["neighbors"]) == 0:
results["No results"] = True
return results
for res in results["neighbors"]:
freq, tier = frequency(res[0], usermodel)
results["frequencies"][res[0]] = (freq, tier)
return results
def contextual(query):
q = [query["query"]]
layer = query["layers"]
usermodel = query["model"]
tmodel = contextual_models_dic[usermodel][0]
tp_model = contextual_models_dic[usermodel][1]
freqdic = contextual_models_dic[usermodel][2]
graph = contextual_models_dic[usermodel][3]
results = {"frequencies": {w: 0 for w in q[0]}}
for word in q[0]:
results["frequencies"][word] = frequency(
word, defaultmodel, external=freqdic
)
with graph.as_default():
elmo_vectors = tmodel.get_elmo_vectors(q, layers=layer)
results["neighbors"] = []
for word, embedding in zip(q[0], elmo_vectors[0, :, :]):
neighbors = tp_model.similar_by_vector(embedding)
neighbors = [n for n in neighbors if n[0] != word]
for neighbor in neighbors:
results["frequencies"][neighbor[0]] = frequency(
neighbor[0], defaultmodel, external=freqdic
)
results["neighbors"].append(neighbors)
return results
def vector(query):
q = query["query"]
usermodel = query["model"]
results = {}
qf = q
results["frequencies"] = {}
results["frequencies"][q] = frequency(q, usermodel)
model = models_dic[usermodel]
if q not in model.key_to_index:
qf = find_variants(qf, usermodel)
if not qf:
if our_models[usermodel]["algo"] == "fasttext" and model.wv.__contains__(q):
results["inferred"] = True
qf = q
else:
results[q + " is unknown to the model"] = True
return results
raw_vector = model[qf]
raw_vector = raw_vector.tolist()
results["vector"] = raw_vector
return results
operations = {
"1": find_synonyms,
"2": find_similarity,
"3": scalculator,
"4": vector,
"5": contextual,
}
# Bind socket to local host and port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket created", file=sys.stderr)
try:
s.bind((HOST, PORT))
except socket.error as msg:
print(f"Bind failed. Error Code and message: {msg}", file=sys.stderr)
sys.exit()
print("Socket bind complete", file=sys.stderr)
# Start listening on socket
s.listen(100)
print(f"Socket now listening on port {PORT}", file=sys.stderr)
# now keep talking with the client
while 1:
conn, addr = s.accept()
# wait to accept a connection - blocking call
# start new thread takes 1st argument as a function name to be run,
# 2nd is the tuple of arguments to the function.
thread = WebVectorsThread(conn, addr)
thread.start()