-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdata.py
63 lines (51 loc) · 1.88 KB
/
data.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
import os
import torch
import pickle
class Dictionary(object):
def __init__(self):
self.word2idx = {'<unk>': 0}
self.idx2word = ['<unk>']
self.wordcnt = {'<unk>': 1}
def add_word(self, word):
if word not in self.word2idx:
self.idx2word.append(word)
self.word2idx[word] = len(self.idx2word) - 1
self.wordcnt[word] = 1
else:
self.wordcnt[word] = self.wordcnt[word] + 1
return self.word2idx[word]
def getid(self, word, thresh=10):
if (word not in self.word2idx) or (self.wordcnt[word] < thresh):
return self.word2idx['<unk>']
return self.word2idx[word]
def __len__(self):
return len(self.idx2word)
class Corpus(object):
def __init__(self, path):
self.dictionary = Dictionary()
self.train = self.tokenize(os.path.join(path, 'train.txt'))
self.valid = self.tokenize(os.path.join(path, 'valid.txt'))
self.test = self.tokenize(os.path.join(path, 'test.txt'))
with open(os.path.join(path, 'dict.pkl'), 'wb') as f:
pickle.dump(self.dictionary, f)
def tokenize(self, path):
"""Tokenizes a text file."""
assert os.path.exists(path)
# Add words to the dictionary
with open(path, 'r') as f:
tokens = 0
for line in f:
words = ['<sos>'] + line.split() + ['<eos>']
tokens += len(words)
for word in words:
self.dictionary.add_word(word)
# Tokenize file content
with open(path, 'r') as f:
ids = torch.LongTensor(tokens)
token = 0
for line in f:
words = ['<sos>'] + line.split() + ['<eos>']
for word in words:
ids[token] = self.dictionary.getid(word)
token += 1
return ids