forked from gooofy/zamia-speech
-
Notifications
You must be signed in to change notification settings - Fork 2
/
speech_train_punkt_tokenizer.py
executable file
·80 lines (60 loc) · 2.45 KB
/
speech_train_punkt_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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright 2018 Marc Puels
# Copyright 2013, 2014, 2016, 2017 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/>.
#
#
# scan voxforge and kitchen dirs for new audio data and transcripts
# convert to 16kHz wav, add transcripts entries
#
import logging
import pickle
import nltk
import plac
import parole
from nltools.misc import init_app, load_config
@plac.annotations(
verbose=("Enable verbose logging", "flag", "v"),
debug_sgm_limit=("Limit number of sgm files for debugging purposes",
"option", None, int))
def main(verbose=False, debug_sgm_limit=0):
"""Train the Punkt tokenizer on the German Parole corpus"""
init_app('speech_sentences')
if verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
config = load_config('.speechrc')
parole_path = config.get("speech", "parole_de")
logging.info("training punkt...")
punkt_trainer = nltk.tokenize.punkt.PunktTrainer()
train_punkt_wrapper = parole.TrainPunktWrapper(punkt_trainer)
parole.parole_crawl(parole_path, train_punkt_wrapper.train_punkt,
debug_sgm_limit)
logging.info("finalizing punkt training...")
punkt_trainer.finalize_training(verbose=True)
logging.info("punkt training done. %d text segments."
% train_punkt_wrapper.punkt_count)
params = punkt_trainer.get_params()
# print "Params: %s" % repr(params)
parole.PUNKT_PICKLEFN.parent.mkdir(parents=True, exist_ok=True)
tokenizer = nltk.tokenize.punkt.PunktSentenceTokenizer(params)
with open(str(parole.PUNKT_PICKLEFN), mode='wb') as f:
pickle.dump(tokenizer, f, protocol=pickle.HIGHEST_PROTOCOL)
logging.info('%s written.' % parole.PUNKT_PICKLEFN)
if __name__ == "__main__":
plac.call(main)