Skip to content

Commit da6af59

Browse files
committed
Add imikolov.py and unittest for book chapter word2vec
1 parent c6bfb71 commit da6af59

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
imikolov's simple dataset: http://www.fit.vutbr.cz/~imikolov/rnnlm/
3+
"""
4+
import paddle.v2.dataset.common
5+
import tarfile
6+
import collections
7+
8+
__all__ = ['train', 'test']
9+
10+
URL = 'http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz'
11+
MD5 = '30177ea32e27c525793142b6bf2c8e2d'
12+
13+
14+
def add(a_dict, ele):
15+
if ele in a_dict:
16+
a_dict[ele] += 1
17+
else:
18+
a_dict[ele] = 1
19+
20+
21+
def word_count(f, word_freq=None):
22+
if word_freq == None:
23+
word_freq = {}
24+
25+
for l in f:
26+
for w in l.strip().split():
27+
add(word_freq, w)
28+
add(word_freq, '<s>')
29+
add(word_freq, '<e>')
30+
31+
return word_freq
32+
33+
34+
def build_dict(train_filename, test_filename):
35+
with tarfile.open(
36+
paddle.v2.dataset.common.download(
37+
paddle.v2.dataset.imikolov.URL, 'imikolov',
38+
paddle.v2.dataset.imikolov.MD5)) as tf:
39+
trainf = tf.extractfile(train_filename)
40+
testf = tf.extractfile(test_filename)
41+
word_freq = word_count(testf, word_count(trainf))
42+
43+
STOPWORD_FREQ = 3000
44+
TYPO_FREQ = 50
45+
word_freq = filter(lambda x: x[1] > TYPO_FREQ and x[1] < STOPWORD_FREQ,
46+
word_freq.items())
47+
48+
dictionary = sorted(word_freq, key=lambda x: (-x[1], x[0]))
49+
words, _ = list(zip(*dictionary))
50+
word_idx = dict(zip(words, xrange(len(words))))
51+
word_idx['<any>'] = len(words)
52+
53+
return word_idx
54+
55+
56+
word_idx = {}
57+
58+
59+
def reader_creator(filename, n):
60+
global word_idx
61+
if len(word_idx) == 0:
62+
word_idx = build_dict('./simple-examples/data/ptb.train.txt',
63+
'./simple-examples/data/ptb.valid.txt')
64+
65+
def reader():
66+
with tarfile.open(
67+
paddle.v2.dataset.common.download(
68+
paddle.v2.dataset.imikolov.URL, 'imikolov',
69+
paddle.v2.dataset.imikolov.MD5)) as tf:
70+
f = tf.extractfile(filename)
71+
72+
ANY = word_idx['<any>']
73+
for l in f:
74+
l = ['<s>'] + l.strip().split() + ['<e>']
75+
if len(l) >= n:
76+
l = [word_idx.get(w, ANY) for w in l]
77+
for i in range(n, len(l) + 1):
78+
yield l[i - n:i]
79+
80+
return reader
81+
82+
83+
def train(n):
84+
return reader_creator('./simple-examples/data/ptb.train.txt', n)
85+
86+
87+
def test(n):
88+
return reader_creator('./simple-examples/data/ptb.valid.txt', n)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import paddle.v2.dataset.imikolov
2+
import unittest
3+
4+
5+
class TestMikolov(unittest.TestCase):
6+
def check_reader(self, reader, n):
7+
for l in reader():
8+
self.assertEqual(len(l), n)
9+
10+
def test_train(self):
11+
n = 5
12+
self.check_reader(paddle.v2.dataset.imikolov.train(n), n)
13+
14+
def test_test(self):
15+
n = 5
16+
self.check_reader(paddle.v2.dataset.imikolov.test(n), n)
17+
18+
19+
if __name__ == '__main__':
20+
unittest.main()

0 commit comments

Comments
 (0)