forked from gooofy/zamia-speech
-
Notifications
You must be signed in to change notification settings - Fork 2
/
seqif.py
87 lines (66 loc) · 2.31 KB
/
seqif.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2014, 2018 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/>.
#
#
# sequitur g2p interface
#
import sys
import os
import traceback
import logging
import readline
from nltools.tts import TTS
from nltools.sequiturclient import sequitur_gen_ipa
from nltools.phonetics import ipa2xsampa, xsampa2ipa
import SequiturTool
from sequitur import Translator
SEQUITUR_MODEL = 'data/models/sequitur-dict-de.ipa-latest'
class SeqOptionsObject(object):
pass
def loadG2PSample(fname):
if fname == '-':
sample = loadPlainSample(fname)
else:
firstLine = gOpenIn(fname, defaultEncoding).readline()
if firstLine.startswith('<?xml'):
sample = [ (tuple(orth), tuple(phon))
for orth, phon in loadBlissLexicon(fname) ]
else:
sample = loadPlainSample(fname)
return sample
class SeqIf(object):
def __init__(self, modelfn=SEQUITUR_MODEL):
options = SeqOptionsObject()
options.resume_from_checkpoint = False
options.modelFile = modelfn
options.shouldRampUp = False
options.trainSample = None
options.shouldTranspose = False
options.newModelFile = None
options.shouldSelfTest = False
self.model = SequiturTool.procureModel(options, loadG2PSample, log=sys.stdout)
self.translator = Translator(self.model)
def g2p(self, word):
res = self.translator(word)
xs = u' '.join(res)
ipa = xsampa2ipa(word, xs)
return ipa
if __name__ == '__main__':
si = SeqIf()
ipa = si.g2p(u'überhaupt')
print ipa