-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_vectors.py
50 lines (46 loc) · 1.68 KB
/
create_vectors.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
import random
import numpy as np
from sklearn.preprocessing import LabelEncoder
import gensim
import operator
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=UserWarning)
word_vector_path = "data/glove.txt"
vector_dim = 50
word_vector = gensim.models.KeyedVectors.load_word2vec_format(word_vector_path, binary=False)
def create_vector(question):
global word_vector
splitted = question.split(" ")
vector = np.zeros(vector_dim)
count = 2.0
try:
if len(splitted) == 0:
return vector
else:
vector = map(operator.add,
word_vector[splitted[0].lower()],
vector)
if len(splitted) == 1:
return np.asarray(vector)
vector = map(operator.add,
word_vector[splitted[1].lower()],
vector)
if (splitted[0].lower() == 'what' and
splitted[1].lower() == 'is'):
count = 0.0
vector = np.zeros(vector_dim)
for token in splitted:
count += 1
try:
vector = map(operator.add,
word_vector[token.lower()],
vector)
except KeyError:
count -=1
if count == 0:
return np.asarray(vector)
return np.asarray(vector) / count
return np.asarray(vector) / count
except KeyError:
return vector