-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_functions.py
43 lines (32 loc) · 981 Bytes
/
training_functions.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
from nltk import word_tokenize
from gensim.models import FastText
import pickle
import pymorphy2
import numpy as np
def get_vector(sentence):
FT_model = FastText.load(r'models\fasttext.model')
sentence = word_tokenize(sentence)
vector = []
morph = pymorphy2.MorphAnalyzer()
for word in sentence:
word = morph.parse(word)[0].normal_form
try:
if vector:
vector += FT_model[word]
continue
vector = FT_model[word]
except(Exception):
pass
return vector
def get_bow_vector(sentence):
morph = pymorphy2.MorphAnalyzer()
sentence = word_tokenize(sentence)
with open(r'models\bag_of_words.pickle', 'rb') as f:
bow = pickle.load(f)
for word in sentence:
word = morph.parse(word)[0].normal_form
try:
bow[word] += 1
except(Exception):
pass
return(np.array(list(bow.values())).astype(float))