-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlm.py
164 lines (132 loc) · 4.69 KB
/
lm.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from __future__ import division, print_function
import numpy as np
import dynet as dy
from collections import defaultdict
import pickle
class LanguageModel(object):
def p_next(self, sent):
pass
def init(self):
pass
def p_next_expr(self, sent):
return dy.inputTensor(self.p_next(sent))
def fit(self, corpus):
pass
def save(self, filename):
pass
def load(self, filename):
pass
class UniformLanguageModel(LanguageModel):
def __init__(self, w2id):
self.n = len(w2id)
def p_next(self, sent):
return np.ones(self.n) / self.n
class UnigramLanguageModel(LanguageModel):
def __init__(self, w2id, eps=0):
self.w2id = w2id
self.eps = eps
self.unigrams = np.ones(len(self.w2id)) / len(self.w2id)
def init(self):
self.u_e = dy.inputTensor(self.unigrams)
def p_next(self, sent):
return self.unigrams
def p_next_expr(self, sent):
return self.u_e
def fit(self, corpus):
self.unigrams = np.zeros(len(self.w2id)) + self.eps
for sent in corpus:
for w in sent:
self.unigrams[w] += 1
self.unigrams /= self.unigrams.sum()
def save(self, filename):
np.save(filename, self.unigrams)
def load(self, filename):
if not filename.endswith('.npy'):
filename += '.npy'
self.unigrams = np.load(filename)
def zero():
return 0.0
def dd():
return defaultdict(zero)
class BigramLanguageModel(LanguageModel):
def __init__(self, w2id, alpha=0.0, eps=0):
self.w2id = w2id
self.eps = eps
self.alpha = alpha
self.unigrams = np.ones(len(self.w2id)) / len(self.w2id)
self.bigrams = defaultdict(dd)
def init(self):
self.u_e = dy.inputTensor(self.unigrams)
def p_next(self, sent):
pw = sent#[s[-1] for s in sent]
b_p = np.zeros((len(self.w2id), len(pw)))
for i, w in enumerate(pw):
for k, v in self.bigrams[w].items():
b_p[k, i] = v
return b_p
def p_next_expr(self, sent):
return dy.inputTensor(self.p_next(sent), batched=True)
def fit(self, corpus):
# Learn unigrams
self.unigrams = np.zeros(len(self.w2id)) + self.eps
for sent in corpus:
for w in sent:
self.unigrams[w] += 1
self.unigrams /= self.unigrams.sum()
# Learn bigrams
for sent in corpus:
for w, w_next in zip(sent[:-1], sent[1:]):
self.bigrams[w][w_next] += 1
for k, v in self.bigrams.items():
s = sum(map(lambda x: x[1], v.items()))
for w in v.keys():
self.bigrams[k][w] /= s
def save(self, filename):
np.save(filename + '_unigrams', self.unigrams)
with open(filename + '_bigrams', 'wb+') as f:
pickle.dump(self.bigrams, f)
def load(self, filename):
self.unigrams = np.load(filename + '_unigrams')
with open(filename + '_bigrams', 'rb') as f:
self.bigrams = pickle.load(f)
def get_language_model(lm_type, w2id, test=False):
if lm_type is None:
return None
if lm_type == 'uniform':
return None
elif lm_type == 'unigram':
return lm.UnigramLanguageModel(w2id)
elif lm_type == 'bigram':
return lm.BigramLanguageModel(w2id)
else:
print('Unknown language model %s, using unigram language model' % lm_type)
return lm.UnigramLanguageModel(w2id)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Train a simple LM',
)
parser.add_argument('train_corpus', type=str, help='a path to the training corpus')
parser.add_argument('out', type=str, help='Output file')
parser.add_argument('--lm_type', '-lm', type=str, default=None,
help='Type of language model (for instance unigram or bigram)')
parser.add_argument('--dic', '-d', type=str, default=None,
help='Path to a pickled dictionnary (maps words to ints). '
'If not provided, will learn from training data')
parser.add_argument("--verbose", '-v',
help="increase output verbosity",
action="store_true")
opt = parser.parse_args()
# Load/create dic
if opt.dic is None:
dic = data.read_dic(opt.train_corpus)
else:
dic = data.load_dic(opt.dic)
# Create LM object
lm = get_language_model(opt.lm_type, dic)
if lm is not None:
# Load data
train_data = data.read_corpus(opt.train_corpus, dic)
# Train lm
lm.fit(train_data)
# Save lm
lm.save(opt.out)