forked from HanqingLu/MultiscaleRNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
115 lines (87 loc) · 3.01 KB
/
Copy pathdata.py
File metadata and controls
115 lines (87 loc) · 3.01 KB
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
import os
import cPickle as pkl
import time
import torch
class TextIterator:
"""Simple Bitext iterator."""
def __init__(self, data_path, dict, batch_size=80, maxlen=100, dict_size=-1):
self.data = open(data_path, 'r')
with open(dict, 'rb') as f:
self.dict = pkl.load(f)
self.batch_size = batch_size
self.maxlen = maxlen
self.dict_size = dict_size
self.end_of_data = False
def __iter__(self):
return self
def reset(self):
self.data.seek(0)
def next(self):
if self.end_of_data:
self.end_of_data = False
self.reset()
raise StopIteration
data = []
try:
# actual work here
while True:
# read from data file and map to word index
ss = self.data.readline()
if ss == "":
raise IOError
ss = unicode(ss, 'utf8')
ss = list(ss.replace(' ', '').strip('\r\n'))
ss = [self.dict[w] if w in self.dict else 1 for w in ss]
if self.dict_size > 0:
ss = [w if w < self.dict_size else 1 for w in ss]
# read from data file and map to word index
if len(ss) > self.maxlen:
continue
data.append(ss)
if len(data) >= self.batch_size:
break
except IOError:
self.end_of_data = True
if len(data) <= 0:
self.end_of_data = False
self.reset()
raise StopIteration
return data
class Dictionary(object):
def __init__(self):
self.word2idx = {}
self.idx2word = []
def add_word(self, word):
if word not in self.word2idx:
self.idx2word.append(word)
self.word2idx[word] = len(self.idx2word) - 1
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'))
def tokenize(self, path):
"""Tokenizes a text file."""
assert os.path.exists(path)
# Add words to the dictionary
tokens = 0
with open(path, 'r') as f:
for line in f:
words = 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 = line.split() + ['<eos>']
for word in words:
ids[token] = self.dictionary.word2idx[word]
token += 1
return ids