-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
51 lines (35 loc) · 1.07 KB
/
utils.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
# imports
import configs
import logging
from six.moves import cPickle as pickle
import gensim
# classes
class SimsModel(object):
def __init__(self):
self.model = self.load_sims_model()
def load_sims_model(self):
logging.debug('loading sims model')
return gensim.models.KeyedVectors.load(configs.SIMS_FILE_PATH, mmap='r')
def get_similarity(self, word1, word2):
try:
return self.model.similarity(word1, word2)
except Exception as e:
return -float('inf')
# functions
def load_pkl_file(file_path):
logging.debug('loading pickle file at: {}'.format(file_path))
with open(file_path, 'rb') as pkl_file:
return pickle.load(pkl_file)
def save_pkl_file(data, file_path):
logging.debug('saving pickle file at: {}'.format(file_path))
with open(file_path, 'wb') as pkl_file:
pkl_file.flush()
pickle.dump(data, pkl_file)
def load_word2vec_model():
logging.debug('loading word2vec model')
return gensim.models.KeyedVectors.load_word2vec_format(configs.WORD2VEC_MODEL_PATH,
binary=True)
def main():
pass
if __name__ == '__main__':
main()