This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_vocab.py
executable file
·65 lines (44 loc) · 1.59 KB
/
build_vocab.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
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
def words(line):
"""Splits a line of text into tokens."""
return line.strip().split()
def create_vocabulary(lines, max_vocab, min_count=5):
"""Reads text lines and generates a vocabulary."""
lines.seek(0, os.SEEK_END)
nbytes = lines.tell()
lines.seek(0, os.SEEK_SET)
vocab = {}
for lineno, line in enumerate(lines, start=1):
for word in words(line):
vocab.setdefault(word, 0)
vocab[word] += 1
if lineno % 100000 == 0:
pos = lines.tell()
sys.stdout.write('\rComputing vocabulary: %0.1f%% (%d/%d)...' %
(100.0 * pos / nbytes, pos, nbytes))
sys.stdout.flush()
sys.stdout.write('\n')
vocab = [(tok, n) for tok, n in vocab.iteritems() if n >= min_count]
vocab.sort(key=lambda kv: (-kv[1], kv[0]))
num_words = min(len(vocab), max_vocab)
if not num_words:
raise Exception('empty vocabulary')
print('vocabulary contains %d tokens' % num_words)
vocab = vocab[:num_words]
return [tok for tok, n in vocab]
def main(data_path, max_vocab=40000):
assert os.path.exists(data_path)
vocab_path = os.path.join(os.path.dirname(data_path), 'vocab-cased.txt')
with open(data_path) as fi:
vocab = create_vocabulary(fi, max_vocab)
with open(vocab_path, 'w') as fo:
for w in vocab:
fo.write(w + '\n')
return vocab
if __name__ == '__main__':
vocab = main(sys.argv[1])
print('done!')