-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_manipulation.py
216 lines (168 loc) · 5.96 KB
/
data_manipulation.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# SPECIAL TOKENS
#
# Usage
# =====
# Used to identify start and end of documents, paragraphs and sentences
#
import glob
import random
import struct
import sys
from tensorflow.core.example import example_pb2
PARAGRAPH_START = '<p>'
PARAGRAPH_END = '</p>'
SENTENCE_START = '<s>'
SENTENCE_END = '</s>'
UNKNOWN_TOKEN = '<UNK>'
PAD_TOKEN = '<PAD>'
DOCUMENT_START = '<d>'
DOCUMENT_END = '</d>'
class Vocab(object):
"""Vocabulary class for mapping words and ids."""
def __init__(self, vocab_file="/home/textsum/Documents/AbstractiveTextSummarizer/data/vocab", max_size=20000):
self._word_to_id = {}
self._id_to_word = {}
self._count = 0
with open(vocab_file, 'r') as vocab_r:
for line in vocab_r:
pieces = line.split()
if len(pieces) != 2:
sys.stderr.write('Bad line: %s\n' % line)
continue
if pieces[0] in self._word_to_id:
raise ValueError('Duplicated word: %s.' % pieces[0])
self._word_to_id[pieces[0]] = self._count
self._id_to_word[self._count] = pieces[0]
self._count += 1
if self._count > max_size:
raise ValueError('Too many words: >%d.' % max_size)
def check_vocab(self, word):
if word not in self._word_to_id:
return None
return self._word_to_id[word]
def word_to_id(self, word):
if word not in self._word_to_id:
return self._word_to_id[UNKNOWN_TOKEN]
return self._word_to_id[word]
def id_to_word(self, word_id):
if word_id not in self._id_to_word:
raise ValueError('Id not found in vocab: %d.' % word_id)
return self._id_to_word[word_id]
def count_of_ids(self):
return self._count
def pad(ids, pad_id, length):
"""pad or trim list to len length.
Args:
ids: list of ints to pad
pad_id: what to pad with
length: length to pad or trim to
Returns:
ids trimmed or padded with pad_id
"""
assert pad_id is not None
assert length is not None
if len(ids) < length:
a = [pad_id] * (length - len(ids))
return ids + a
else:
return ids[:length]
def get_ids_from_words(text, vocab, pad_len=None, pad_id=None):
"""Get ids corresponding to words in text.
Assumes tokens separated by space.
Args:
text: a string
vocab: TextVocabularyFile object
pad_len: int, length to pad to
pad_id: int, word id for pad symbol
Returns:
A list of ints representing word ids.
"""
ids = []
for w in text.split():
i = vocab.word_to_id(w)
if i >= 0:
ids.append(i)
else:
ids.append(vocab.word_to_id(UNKNOWN_TOKEN))
if pad_len is not None:
return pad(ids, pad_id, pad_len)
return ids
def get_words_from_ids(ids_list, vocab):
"""Get words from ids.
Args:
ids_list: list of int32
vocab: TextVocabulary object
Returns:
List of words corresponding to ids.
"""
assert isinstance(ids_list, list), '%s is not a list' % ids_list
return [vocab.id_to_word(i) for i in ids_list]
def paragraph_to_sentences(paragraph, include_token=True):
"""
Takes tokens of a paragraph and returns list of sentences.
Args:
paragraph: string, text of paragraph
include_token: Whether include the sentence separation tokens result.
Returns:
List of sentence strings.
"""
s_gen = snippet_generator(paragraph, SENTENCE_START, SENTENCE_END, include_token)
return [s for s in s_gen]
def example_gen(data_path, num_epochs=None):
"""
Generates tf.Examples from path of data files.
Binary data format: <length><blob>. <length> represents the byte size
of <blob>. <blob> is serialized tf.Example proto. The tf.Example contains
the tokenized article text and summary.
Args:
data_path: path to tf.Example data files.
num_epochs: Number of times to go through the data. None means infinite.
Yields:
Deserialized tf.Example.
If there are multiple files specified, they accessed in a random order.
"""
epoch = 0
while True:
if num_epochs is not None and epoch >= num_epochs:
break
filelist = glob.glob(data_path)
assert filelist, 'Empty filelist.'
random.shuffle(filelist)
for f in filelist:
reader = open(f, 'rb')
while True:
len_bytes = reader.read(8)
if not len_bytes: break
str_len = struct.unpack('q', len_bytes)[0]
example_str = struct.unpack('%ds' % str_len, reader.read(str_len))[0]
yield example_pb2.Example.FromString(example_str)
epoch += 1
def snippet_generator(text, start_tok, end_tok, inclusive=True):
"""
Generates consecutive snippets between start and end tokens.
Args:
text: a string
start_tok: a string denoting the start of snippets
end_tok: a string denoting the end of snippets
inclusive: Whether include the tokens in the returned snippets.
Yields:
String snippets
Information:
Generator are iterators, but we can only iterate over them once. It's because they do not store all the
values in memory, they generate the values on the fly
Yield is a keyword that is used like return, except the function will return a generator.
"""
cur = 0
while True:
try:
start_p = text.index(start_tok, cur)
end_p = text.index(end_tok, start_p + 1)
cur = end_p + len(end_tok)
if inclusive:
yield text[start_p:cur]
else:
yield text[start_p + len(start_tok):end_p]
except ValueError as e:
raise StopIteration('no more snippets in text: %s' % e)
def get_example_feature_text(example, key):
return example.features.feature[key].bytes_list.value[0]