-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_retrieval.py
121 lines (104 loc) · 4.35 KB
/
vector_retrieval.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
import json_controller
import upload_controller
from data_controller import fasttext_vocab as ft_vocab
from data_controller import fasttext_vectors as ft_vecs
from data_controller import glove_vocab as gv_vocab
from data_controller import glove_vectors as gv_vecs
from data_controller import cbow_vocab as cb_vocab
from data_controller import cbow_vectors as cb_vecs
from upload_controller import uploaded_vocab as up_vocab
from upload_controller import uploaded_vecs as up_vecs
def retrieve_vector_single(vocab, vectors, target_word):
found = {}
not_found = []
if target_word in vocab:
found[target_word] = vectors[vocab[target_word]]
else:
not_found.append(target_word)
return found, not_found
def retrieve_vector_multiple(vocab, vectors, word_list):
not_found = []
found = {}
for word in word_list:
if word in vocab:
found[word] = vectors[vocab[word]]
else:
not_found.append(word)
return found, not_found
def retrieve_uploaded_vector_single(space, target):
found = {}
not_found = []
if space == upload_controller.uploaded_filename:
if target in upload_controller.uploaded_space:
found[target] = upload_controller.uploaded_space[target]
else:
not_found.append(target)
return found, not_found
def retrieve_uploaded_vector_multiple(space, word_list):
not_found = []
found = {}
if upload_controller.uploaded_binary == 'false':
if space == upload_controller.uploaded_filename:
for word in word_list:
if word in upload_controller.uploaded_space:
found[word] = upload_controller.uploaded_space[word]
else:
not_found.append(word)
if upload_controller.uploaded_binary == 'true':
found, not_found = retrieve_vector_multiple(upload_controller.uploaded_vocab, upload_controller.uploaded_vecs,
word_list)
return found, not_found
def retrieve_vector(number, content, bar):
vectors = {}
not_found = []
if 'space' not in bar:
if 'uploaded' not in bar:
return 'BAD REQUEST - NO SPACE SELECTED', 400
space = 'fasttext'
uploaded = 'false'
lower = 'false'
if 'space' in bar:
space = bar['space']
if 'uploaded' in bar:
uploaded = bar['uploaded']
if 'lower' in bar:
lower = bar['lower']
if number == 'single':
if 'word' not in bar:
return 'BAD REQUEST - NO WORD INPUT', 401
target = bar['word']
if lower == 'true':
target = target.lower()
if uploaded == 'true':
if upload_controller.uploaded_binary == 'false':
vectors, not_found = retrieve_uploaded_vector_single(space, target)
if upload_controller.uploaded_binary == 'true':
vectors, not_found = retrieve_vector_single(up_vocab, up_vecs, target)
if space == 'fasttext':
vectors, not_found = retrieve_vector_single(ft_vocab, ft_vecs, target)
if space == 'glove':
vectors, not_found = retrieve_vector_single(gv_vocab, gv_vecs, target)
if space == 'cbow':
vectors, not_found = retrieve_vector_single(cb_vocab, cb_vecs, target)
if number == 'multiple':
if content is None:
return 'BAD REQUEST - NO WORD LIST RECEIVED', 400
target = content['Words'].split(' ')
if lower == 'true':
target = [t.lower() for t in target]
if uploaded == 'true':
if upload_controller.uploaded_binary == 'false':
vectors, not_found = retrieve_uploaded_vector_multiple(space, target)
if upload_controller.uploaded_binary == 'true':
vectors, not_found = retrieve_vector_multiple(up_vocab, up_vecs, target)
if space == 'fasttext':
vectors, not_found = retrieve_vector_multiple(ft_vocab, ft_vecs, target)
if space == 'glove':
vectors, not_found = retrieve_vector_multiple(gv_vocab, gv_vecs, target)
if space == 'cbow':
vectors, not_found = retrieve_vector_multiple(cb_vocab, cb_vecs, target)
if vectors and not_found is not None:
response = json_controller.json_vector_retrieval(vectors, not_found)
return response, 200
else:
return "NOT FOUND", 404