5 from cStringIO
import StringIO
12 from speech_recognition_msgs.msg
import Grammar
13 from speech_recognition_msgs.msg
import PhraseRule
14 from speech_recognition_msgs.msg
import Vocabulary
17 _REGEX_HIRAGANA = re.compile(
r'^(?:\xE3\x81[\x81-\xBF]|\xE3\x82[\x80-\x93])+$')
21 return _REGEX_HIRAGANA.search(s)
is not None 25 assert os.path.exists(path)
26 return subprocess.check_output([
"nkf",
"-w", path]).split(os.linesep)
30 assert os.path.isdir(path)
32 grammar_path = os.path.join(path,
"%s.grammar" % name)
36 sym, defi = l.strip().split(
':')
38 r.symbol = sym.strip()
39 r.definition = [d.strip()
for d
in defi.split()]
41 voca_path = os.path.join(path,
"%s.voca" % name)
46 g.categories.append(l[1:].strip())
48 g.vocabularies.append(voca)
51 sp = l.strip().split()
52 voca.words.append(sp[0])
53 voca.phonemes.append(
' '.join(sp[1:]))
55 g.vocabularies.append(voca)
60 cmd = [
"rosrun",
"julius",
"yomi2voca.pl"]
61 stdin = os.linesep.join([
"%s %s" % (w, w)
for w
in words]) + os.linesep
62 rospy.logdebug(
"Executing %s" % cmd)
63 p = subprocess.Popen([
"rosrun",
"julius",
"yomi2voca.pl"],
64 stdin=subprocess.PIPE,
65 stdout=subprocess.PIPE,
66 stderr=subprocess.PIPE)
67 result, error = p.communicate(unicode(stdin,
'utf-8').encode(
'euc-jp'))
68 rospy.logdebug(
"STDOUT: %s" % result)
69 rospy.logdebug(
"STDERR: %s" % error)
71 if error
and "Error:" in error:
72 error = unicode(error,
'euc-jp').encode(
'utf-8')
73 rospy.logerr(
"Error: %s" % error)
76 result = unicode(result,
'euc-jp').encode(
'utf-8')
77 result = result.split(os.linesep)[:-1]
78 result = [r.split(
"\t")[1]
for r
in result]
86 ss.write(
"{symbol}: {definition}{linesep}".format(
88 definition=
" ".join(r.definition),
95 for c, vs
in zip(cats, vocas):
96 ss.write(
"% {category}{linesep}".format(
99 phonemes = vs.phonemes
100 if len(phonemes) == 0:
102 for w, p
in zip(vs.words, phonemes):
103 ss.write(
"{word}\t{phoneme}{linesep}{linesep}".format(
112 temp_dir = tempfile.mkdtemp(prefix=
"mkdfa")
113 rospy.logdebug(
"created temp dir: %s" % temp_dir)
114 with open(os.path.join(temp_dir,
"{name}.grammar".format(name=name)),
"w")
as f:
116 with open(os.path.join(temp_dir,
"{name}.voca".format(name=name)),
"w")
as f:
119 cmd = [
"rosrun",
"julius",
"mkdfa.pl", name]
120 rospy.logdebug(
"Executing %s" % cmd)
121 p = subprocess.Popen(cmd,
122 stdin=subprocess.PIPE,
123 stdout=subprocess.PIPE,
124 stderr=subprocess.PIPE,
126 result, error = p.communicate(temp_dir)
127 rospy.logdebug(
"STDOUT: %s" % result)
128 rospy.logdebug(
"STDERR: %s" % error)
130 if "generated:" not in result:
131 rospy.logerr(
"Failed to compile grammar to DFA: %s" % error.strip())
134 with open(os.path.join(temp_dir,
"{name}.dfa".format(name=name)),
"r") as f: 136 with open(os.path.join(temp_dir, "{name}.dict".format(name=name)),
"r") as f: 141 if __name__ ==
'__main__':
143 assert result[0] ==
"u d o N" 144 assert result[1] ==
"s o b a" def readlines_with_utf8(path)
def make_phonemes_from_words(words)
def load_grammar(path, name)
def make_dfa(grammar, voca)
def make_grammar_from_rules(rules)
def make_voca_from_categories(cats, vocas)