-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.py
272 lines (211 loc) · 7.17 KB
/
tokenizer.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
##############################################################################
# tokenizer.py
# -------------
# Parse and pre-process sonnet text data into tokens for model training.
#
##############################################################################
import collections
# SONNET FORMAT:
SONNET_LINES = 14
NUM_QUATRAINS = 3
QUATRAIN_LINES = 4
COUPLET_LINES = 2
# Shakespeare sonnets 99, 126, 145 removed due to format deviations
EXCLUDED_SONNETS = ['99', '126', '145']
PUNCTUATION = [',', ':', '.', ';', '?', '!', '(', ')', "'", '"']
RAW_DATA_FILES = ["data/shakespeare.txt", "data/spenser.txt"]
def load_data(data_files=RAW_DATA_FILES):
"""Read in raw text of sonnets as training data.
Parameters
----------
data__files : list
List of file paths to sonnets training data files.
Returns
-------
data : list
Sonnet data (lines) read from file.
"""
data = []
for filename in data_files:
with open(filename, 'r') as f:
for line in f:
if line is not None and line != '\n':
data.append(line)
return data
def tokenize_lpunc(line):
"""Parse sonnet lines and tokenize on words.
Tokenization rules:
- For standardization, all lines are in lowercase.
- Newline characters are removed.
- Hyphenated words are kept as single words
(to prevent random words to be hyphenated together).
- Ending punctuation is attached to the word to its left
(this can help the model learn the positioning
of words with punctuation).
Parameters
----------
line : str
A single sonnet line.
Returns
-------
line : str
Formatted sonnet line with punctuation attached to word on the left
and newline characters removed.
"""
line = line.lower().lstrip().rstrip()
line = line.split(' ')
return line
def tokenize_nopunc(line):
"""Parse sonnet lines and tokenize on words.
Tokenization rules:
- For standardization, all lines are in lowercase.
- Newline characters are removed.
- Hyphenated words are kept as single words
(to prevent random words to be hyphenated together).
- All punctuation is removed.
Parameters
----------
line : str
A single sonnet line.
Returns
-------
line : str
Formatted sonnet line with punctuation attached to word on the left
and newline characters removed.
"""
line = line.lower().lstrip().rstrip()
for punc in PUNCTUATION:
line = line.replace(punc, '')
line = line.split(' ')
return line
# TRAINING SEQUENCES
def sequence_each_line(tokenize, data):
"""Parse and format sonnets into training sequences on a per-line basis.
Parameters
----------
tokenizer : function
Function that parses sonnet lines into tokens.
data : list
Sonnet data (lines) read from file.
Returns
-------
sequences : list
List of training sequences, where each sequence is
a tokenized sonnet line.
"""
sequences = []
for line in data:
parsed_line = tokenize(line)
# Skip first line (which is just the sonnet number)
if len(parsed_line) > 1:
sequences.append(parsed_line)
return sequences
def sequence_quatrains_couplets(tokenize, data):
"""Parse and format sonnets into training sequences as sets of quatrains and couplets.
Sonnets are split into quatrains and couplets, and then quatrains and
couplets are split on a per-line basis.
Parameters
----------
tokenizer : function
Function that parses sonnet lines into tokens.
data : list
Sonnet data (lines) read from file.
Returns
-------
quatrains : list
Training seqeunces of all lines from quatrains.
couplets : list
Training sequences of all lines from couplets.
"""
quatrains = []
couplets = []
lines = iter(data)
line = next(lines, None)
while line is not None:
sonnet_line = tokenize(line) # First line is the sonnet number
if len(sonnet_line) == 1 and sonnet_line not in EXCLUDED_SONNETS:
for i in range(NUM_QUATRAINS * QUATRAIN_LINES):
line = next(lines, None)
if line: quatrains.append(tokenize(line))
for i in range(COUPLET_LINES):
line = next(lines, None)
if line: couplets.append(tokenize(line))
line = next(lines, None)
return quatrains, couplets
def sequence_full_sonnet(tokenize, data):
"""Parse and format each sonnet (full text) into a training sequence.
Parameters
----------
tokenizer : function
Function that parses sonnet lines into tokens.
data : list
Sonnet data (lines) read from file.
Returns
-------
sequences : list
List of training sequences, where each sequence is
a pre-processed sonnet string.
"""
sonnets = collections.defaultdict(list)
count = 0
for line in data:
sequence = tokenize(line)
if len(sequence) == 1:
count += 1
else:
sonnets[count].append(sequence)
for i in sonnets:
sonnets[i] = [' '.join(line) for line in sonnets[i]]
return [' '.join(sonnets[line]) for line in sonnets]
# MISCELLANEOUS SONNET PROCESSING
def process_rhymes(data):
"""Compile lists of rhyming pairs from the sonnets text.
Parameters
----------
data : list
Sonnet data (lines) read from file.
Returns
-------
quatrain_rhymes : list
Pairs of rhyming lines (in tuples) from all the quatrains.
couplet_rhymes : list
Pairs of rhyming lines (in tuples) from all the couplets.
"""
quatrain_rhymes = []
couplet_rhymes = []
lines = iter(data)
line = next(lines, None)
while line is not None:
sonnet_line = tokenize_nopunc(line) # First line is the sonnet number
if len(sonnet_line) == 1 and sonnet_line not in EXCLUDED_SONNETS:
for i in range(NUM_QUATRAINS):
quatrain = []
for j in range(QUATRAIN_LINES):
line = next(lines, None)
if line: quatrain.append(tokenize_nopunc(line))
quatrain_rhymes.append((quatrain[0][-1], quatrain[2][-1]))
quatrain_rhymes.append((quatrain[1][-1], quatrain[3][-1]))
couplet = []
for i in range(COUPLET_LINES):
line = next(lines, None)
if line: couplet.append(tokenize_nopunc(line))
couplet_rhymes.append((couplet[-1], couplet[-1]))
line = next(lines, None)
return quatrain_rhymes, couplet_rhymes
def process_word_frequency(data):
"""Count frequency of words in all sonnets.
Parameters
----------
data : list
Sonnet data (lines) read from file.
Returns
-------
word_count : dict
Mapping of words to frequency count.
"""
word_count = collections.Counter()
for line in data:
words = tokenize_nopunc(line)
if len(words) > 1:
word_count.update(words)
return dict(word_count)