forked from gooofy/zamia-speech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_sentences.py
executable file
·321 lines (246 loc) · 10.1 KB
/
speech_sentences.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright 2018 Marc Puels
# Copyright 2013, 2014, 2016, 2017, 2018, 2019 Guenter Bartsch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# Generate training sentences for language models
#
# Let text_corpus be the argument given on the command line.
# Then the corpus text_corpus is tokenized and each sentence is written on a
# separate line into `data/dst/text-corpora/<text_corpus>.txt`. All
# punctuation marks are stripped.
#
import codecs
import json
import logging
import os
import sys
from optparse import OptionParser
from nltools import misc
from nltools.tokenizer import tokenize
import parole
from parole import load_punkt_tokenizer
from speech_transcripts import Transcripts
PROC_TITLE = 'speech_sentences'
SENTENCES_STATS = 1000
DEBUG_LIMIT = 0
DEBUG_SGM_LIMIT_PAROLE = 0
TEXT_CORPORA_DIR = 'data/dst/text-corpora'
TEXT_CORPORA = {
"cornell_movie_dialogs":
lambda corpus_path: proc_cornell_movie_dialogs(corpus_path, tokenize),
"europarl_de":
lambda corpus_path: proc_europarl_de(corpus_path, tokenize),
"europarl_en":
lambda corpus_path: proc_corpus_with_one_sentence_perline(corpus_path, tokenize, 'en'),
"europarl_fr":
lambda corpus_path: proc_corpus_with_one_sentence_perline(corpus_path, tokenize, 'fr'),
"est_republicain":
lambda corpus_path: proc_corpus_with_one_sentence_perline(corpus_path, tokenize, 'fr'),
"parole_de":
None,
"web_questions":
lambda corpus_path: proc_web_questions(corpus_path, tokenize),
"yahoo_answers":
lambda corpus_path: proc_yahoo_answers(corpus_path, tokenize),
}
SPEECH_CORPORA = {
"cv_corpus_v1":
lambda: proc_transcripts("cv_corpus_v1"),
"cv_de":
lambda: proc_transcripts("cv_de"),
"cv_fr":
lambda: proc_transcripts("cv_fr"),
"forschergeist":
lambda: proc_transcripts("forschergeist"),
"gspv2":
lambda: proc_transcripts("gspv2"),
"librispeech":
lambda: proc_transcripts("librispeech"),
"ljspeech":
lambda: proc_transcripts("ljspeech"),
"m_ailabs_de":
lambda: proc_transcripts("m_ailabs_de"),
"m_ailabs_en":
lambda: proc_transcripts("m_ailabs_en"),
"m_ailabs_fr":
lambda: proc_transcripts("m_ailabs_fr"),
"tedlium3":
lambda: proc_transcripts("tedlium3"),
"voxforge_de":
lambda: proc_transcripts("voxforge_de"),
"voxforge_en":
lambda: proc_transcripts("voxforge_en"),
"voxforge_fr":
lambda: proc_transcripts("voxforge_fr"),
"zamia_de":
lambda: proc_transcripts("zamia_de"),
"zamia_en":
lambda: proc_transcripts("zamia_en"),
}
CORPORA = {}
CORPORA.update(TEXT_CORPORA)
CORPORA.update(SPEECH_CORPORA)
def proc_cornell_movie_dialogs(corpus_path, tokenize):
num_sentences = 0
with codecs.open('%s/movie_lines.txt' % corpus_path, 'r',
'latin1') as inf:
for line in inf:
parts = line.split('+++$+++')
if not len(parts) == 5:
logging.warn('movie dialogs: skipping line %s' % line)
continue
sentence = u' '.join(tokenize(parts[4], lang='en'))
if not sentence:
logging.warn('movie dialogs: skipping null sentence %s' % line)
continue
yield u'%s' % sentence
num_sentences += 1
if num_sentences % SENTENCES_STATS == 0:
logging.info('movie dialogs: %8d sentences.' % num_sentences)
if DEBUG_LIMIT and num_sentences >= DEBUG_LIMIT:
logging.warn('movie dialogs: debug limit reached, stopping.')
break
def proc_europarl_de(corpus_path, tokenize):
logging.info("adding sentences from europarl...")
num_sentences = 0
with codecs.open(corpus_path, 'r', 'utf8') as inf:
for line in inf:
yield u'%s' % ' '.join(tokenize(line))
num_sentences += 1
if num_sentences % SENTENCES_STATS == 0:
logging.info ('%8d sentences.' % num_sentences)
def proc_corpus_with_one_sentence_perline(corpus_path, tokenize, lang):
logging.info("adding sentences from %s..." % corpus_path)
num_sentences = 0
with codecs.open(corpus_path, 'r', 'utf8') as inf:
for line in inf:
sentence = u' '.join(tokenize(line, lang=lang))
if not sentence:
logging.warn('%s: skipping null sentence.' % corpus_path)
continue
yield u'%s' % sentence
num_sentences += 1
if num_sentences % SENTENCES_STATS == 0:
logging.info('%s: %8d sentences.' % (corpus_path, num_sentences))
if DEBUG_LIMIT and num_sentences >= DEBUG_LIMIT:
logging.warn('%s: debug limit reached, stopping.' % corpus_path)
break
def proc_parole_de(corpus_path, load_punkt_tokenizer, outf):
punkt_tokenizer = load_punkt_tokenizer()
apply_punkt_wrapper = parole.ApplyPunktWrapper(punkt_tokenizer, outf)
parole.parole_crawl(corpus_path, apply_punkt_wrapper.apply_punkt,
DEBUG_SGM_LIMIT_PAROLE)
def proc_web_questions(corpus_path, tokenize):
num_sentences = 0
for infn in ['webquestions.examples.test.json',
'webquestions.examples.train.json']:
with open('%s/%s' % (corpus_path, infn), 'r') as inf:
data = json.loads(inf.read())
for a in data:
sentence = u' '.join(tokenize(a['utterance'], lang='en'))
if not sentence:
logging.warn(
'web questions: skipping null sentence')
continue
yield u'%s' % sentence
num_sentences += 1
if num_sentences % SENTENCES_STATS == 0:
logging.info(
'web questions: %8d sentences.' % num_sentences)
if DEBUG_LIMIT and num_sentences >= DEBUG_LIMIT:
logging.warn(
'web questions: debug limit reached, stopping.')
break
def proc_yahoo_answers(corpus_path, tokenize):
num_sentences = 0
for infn in os.listdir('%s/text' % corpus_path):
logging.debug('yahoo answers: reading file %s' % infn)
with codecs.open('%s/text/%s' % (corpus_path, infn), 'r',
'latin1') as inf:
for line in inf:
sentence = u' '.join(tokenize(line, lang='en'))
if not sentence:
continue
yield u'%s' % sentence
num_sentences += 1
if num_sentences % SENTENCES_STATS == 0:
logging.info(
'yahoo answers: %8d sentences.' % num_sentences)
if DEBUG_LIMIT and num_sentences >= DEBUG_LIMIT:
logging.warn(
'yahoo answers: debug limit reached, stopping.')
break
if DEBUG_LIMIT and num_sentences >= DEBUG_LIMIT:
logging.warn('yahoo answers: debug limit reached, stopping.')
break
def proc_transcripts(corpus_name):
global use_prompts, lang
transcripts = Transcripts(corpus_name=corpus_name)
if use_prompts:
transcripts_set = set((u' '.join(tokenize(transcripts[key]["prompt"], lang))) for key in transcripts)
else:
transcripts_set = set( (u' '.join(tokenize(transcripts[key]["ts"], lang))) for key in transcripts )
for ts in transcripts_set:
yield ts
if __name__ == "__main__":
misc.init_app(PROC_TITLE)
#
# config
#
config = misc.load_config('.speechrc')
#
# commandline
#
parser = OptionParser("usage: %%prog [options] <corpus>")
parser.add_option ("-l", "--lang", dest="lang", type = "str", default='de',
help="language (default: de)")
parser.add_option ("-p", "--prompts", action="store_true", dest="use_prompts",
help="extract original prompts instead of transcripts")
parser.add_option ("-v", "--verbose", action="store_true", dest="verbose",
help="verbose output")
(options, args) = parser.parse_args()
if options.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
lang = options.lang
use_prompts = options.use_prompts
if len(args) != 1:
logging.error("Exactly one corpus (text or speech) must be provided.")
parser.print_help()
sys.exit(1)
corpus = args[0]
misc.mkdirs(TEXT_CORPORA_DIR)
out_file = '%s/%s.txt' % (TEXT_CORPORA_DIR, corpus)
with codecs.open(out_file, "w", "utf-8") as outf:
# I haven't figured out how to refactor the processing algorithms of the
# parole corpus to implement a generator.
if corpus == "parole_de":
corpus_path = config.get("speech", corpus)
proc_parole_de(corpus_path, load_punkt_tokenizer, outf)
elif corpus in TEXT_CORPORA:
corpus_path = config.get("speech", corpus)
for sentence in TEXT_CORPORA[corpus](corpus_path):
outf.write(sentence + "\n")
elif corpus in SPEECH_CORPORA:
for sentence in SPEECH_CORPORA[corpus]():
outf.write(sentence + "\n")
else:
raise Exception("This shouldn't happen.")
logging.info('%s written.' % out_file)