-
Notifications
You must be signed in to change notification settings - Fork 1
/
embedding.py
69 lines (57 loc) · 1.9 KB
/
embedding.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
import numpy as np
def ngrams(sentence, n):
"""
Returns:
list: a list of lists of words corresponding to the ngrams in the sentence.
"""
return [sentence[i:i+n] for i in range(len(sentence)-n+1)]
class PretrainEmbedding(object):
def __init__(self, data_file, dim=300):
self.data_file = data_file
self.dim = dim
self.emb = dict()
def read_emb(self):
print 'reading data from %s'%(self.data_file)
with open(self.data_file) as f:
for line_ in f:
line = line_.strip().split(' ')
if line[0] not in self.emb:
self.emb[line[0]] = [float(x) for x in line[1:]]
def lookup(self, w):
return self.emb[w] if w in self.emb else [0. for x in range(self.dim)]
class GloveEmbedding(PretrainEmbedding):
"""docstring for GloveEmbedding"""
def __init__(self, data_file=None, dim=0):
super(GloveEmbedding, self).__init__(data_file, dim)
self.data_file = './data/embedding/glove.840B.300d.txt'
self.dim = 300
self.read_emb()
class KazumaCharEmbedding(PretrainEmbedding):
"""docstring for GloveEmbedding"""
def __init__(self, data_file=None, dim=0):
super(KazumaCharEmbedding, self).__init__(data_file, dim)
self.data_file = './data/embedding/jmt_pre-trained_embeddings/charNgram.txt'
self.dim = 100
self.read_emb()
def lookup(self, w):
w = ['#BEGIN#'] + list(w) + ['#END#']
embs = np.zeros(self.dim, dtype=np.float32)
match = {}
for i in [2, 3, 4]:
grams = ngrams(w, i)
for g in grams:
g = '%sgram-%s'%(i, ''.join(g))
if g in self.emb:
match[g] = np.array(self.emb[g], np.float32)
if match:
embs = sum(match.values()) / len(match)
return embs.tolist()
class ComposedEmbedding():
def __init__(self):
self.glove = GloveEmbedding()
self.kazuma = KazumaCharEmbedding()
def lookup(self, w):
e = []
e = e + self.glove.lookup(w)
e = e + self.kazuma.lookup(w)
return e