Simons2017
/
Towards-Implicit-Content-Introducing-for-Generative-Short-Text-Conversation-Systems
Public
forked from yaolili/Towards-Implicit-Content-Introducing-for-Generative-Short-Text-Conversation-Systems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_iterator.py
143 lines (113 loc) · 4.53 KB
/
data_iterator.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
import numpy
import pickle as pkl
import gzip
def fopen(filename, mode='r'):
if filename.endswith('.gz'):
return gzip.open(filename, mode)
return open(filename, mode)
class TextIterator:
"""Simple Bitext iterator."""
def __init__(self, source, target, topic,
source_dict, target_dict,
batch_size=128,
maxlen=100,
n_words_source=-1,
n_words_target=-1):
self.source = fopen(source, 'r')
self.target = fopen(target, 'r')
self.topic = fopen(topic, 'r')
with open(source_dict, 'rb') as f:
self.source_dict = pkl.load(f)
with open(target_dict, 'rb') as f:
self.target_dict = pkl.load(f)
self.batch_size = batch_size
self.maxlen = maxlen
self.n_words_source = n_words_source
self.n_words_target = n_words_target
self.source_buffer = []
self.target_buffer = []
self.topic_buffer = []
self.k = batch_size * 20
self.end_of_data = False
def __iter__(self):
return self
def reset(self):
self.source.seek(0)
self.target.seek(0)
self.topic.seek(0)
def next(self):
if self.end_of_data:
self.end_of_data = False
self.reset()
raise StopIteration
source = []
target = []
topic = []
# fill buffer, if it's empty
assert len(self.source_buffer) == len(self.target_buffer) and len(self.topic_buffer) == len(self.source_buffer) , 'Buffer size mismatch!'
if len(self.source_buffer) == 0:
for k_ in range(self.k):
ss = self.source.readline()
if ss == "":
break
tt = self.target.readline()
if tt == "":
break
kw = self.topic.readline()
if kw == "":
break
self.source_buffer.append(ss.strip().split())
self.target_buffer.append(tt.strip().split())
self.topic_buffer.append(kw.strip().split())
# sort by target buffer
tlen = numpy.array([len(t) for t in self.target_buffer])
tidx = tlen.argsort()
_sbuf = [self.source_buffer[i] for i in tidx]
_tbuf = [self.target_buffer[i] for i in tidx]
_kwbuf = [self.topic_buffer[i] for i in tidx]
self.source_buffer = _sbuf
self.target_buffer = _tbuf
self.topic_buffer = _kwbuf
if len(self.source_buffer) == 0 or len(self.target_buffer) == 0 or len(self.topic_buffer) == 0:
self.end_of_data = False
self.reset()
raise StopIteration
try:
# actual work here
while True:
# read from source file and map to word index
try:
ss = self.source_buffer.pop()
except IndexError:
break
ss = [self.source_dict[w] if w in self.source_dict else 1
for w in ss]
if self.n_words_source > 0:
ss = [w if w < self.n_words_source else 1 for w in ss]
# read from source file and map to word index
tt = self.target_buffer.pop()
tt = [self.target_dict[w] if w in self.target_dict else 1
for w in tt]
if self.n_words_target > 0:
tt = [w if w < self.n_words_target else 1 for w in tt]
# read from topic file and map to word index
kw = self.topic_buffer.pop()
kw = [self.target_dict[w] if w in self.target_dict else 1
for w in kw]
if self.n_words_target > 0:
kw = [w if w < self.n_words_target else 1 for w in kw]
if len(ss) > self.maxlen and len(tt) > self.maxlen and len(kw) > self.maxlen:
continue
source.append(ss)
target.append(tt)
topic.append(kw)
if len(source) >= self.batch_size or \
len(target) >= self.batch_size:
break
except IOError:
self.end_of_data = True
if len(source) <= 0 or len(target) <= 0 or len(topic) <= 0:
self.end_of_data = False
self.reset()
raise StopIteration
return source, target, topic