-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
86 lines (68 loc) · 2.53 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
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
import nltk
import pickle
import gzip
import re
import numpy as np
nltk.download('stopwords')
from nltk.corpus import stopwords
# Paths for all resources for the bot.
RESOURCE_PATH = {
'INTENT_RECOGNIZER': 'intent_recognizer.pkl',
'TAG_CLASSIFIER': 'tag_classifier.pkl',
'TFIDF_VECTORIZER': 'tfidf_vectorizer.pkl',
'THREAD_EMBEDDINGS_FOLDER': 'thread_embeddings_by_tags',
'WORD_EMBEDDINGS': 'word_embeddings.tsv.gz',
}
def text_prepare(text):
"""Performs tokenization and simple preprocessing."""
replace_by_space_re = re.compile('[/(){}\[\]\|@,;]')
bad_symbols_re = re.compile('[^0-9a-z #+_]')
stopwords_set = set(stopwords.words('english'))
text = text.lower()
text = replace_by_space_re.sub(' ', text)
text = bad_symbols_re.sub('', text)
text = ' '.join([x for x in text.split() if x and x not in stopwords_set])
return text.strip()
def load_embeddings(embeddings_path):
"""Loads pre-trained word embeddings from tsv file.
Args:
embeddings_path - path to the embeddings file.
Returns:
embeddings - dict mapping words to vectors;
embeddings_dim - dimension of the vectors.
"""
# Hint: you have already implemented a similar routine in the 3rd assignment.
# Note that here you also need to know the dimension of the loaded embeddings.
# When you load the embeddings, use numpy.float32 type as dtype
########################
#### YOUR CODE HERE ####
########################
embeddings = dict()
text = gzip.open(embeddings_path,'rb').read().split("\n")
for t in text:
if len(t) < 1 : continue
w = t.split("\t")
embeddings[w[0]] = list(map(float,w[1:]))
return embeddings, len(w)-1
def question_to_vec(question, embeddings, dim=100):
"""
question: a string
embeddings: dict where the key is a word and a value is its' embedding
dim: size of the representation
result: vector representation for the question
"""
######################################
######### YOUR CODE HERE #############
######################################
result = np.zeros((dim),dtype=np.float32)
res_list = []
for token in question.split():
if token in embeddings:
res_list.append(embeddings[token])
if len(res_list) > 0 :
result = np.mean(np.array(res_list), axis=0)
return result
def unpickle_file(filename):
"""Returns the result of unpickling the file content."""
with open(filename, 'rb') as f:
return pickle.load(f)