用于python的jspeech语法格式(jsgf)编译器、匹配器和解析器包。
pyjsgf的Python项目详细描述
jspeech语法格式(jsgf)编译器、匹配器和解析器包 Python。
jsgf是一种用于以文本形式表示语音语法的格式。 识别引擎。您可以阅读jsgf规范 here。
pyjsgf可以用来构造jsgf规则和语法,并编译它们 在字符串或文件中,找到与语音匹配的语法规则 假设字符串。还支持将语音字符串与标记匹配。 还有语法、规则和规则扩展的解析器。
PyJSGF已经为Python2.7和Python3.5编写和测试过。
这个项目的文档是on readthedocs.org。
安装
要安装pyjsgf,请运行以下命令:
$ pip install pyjsgf
如果您是为了developmentpyjsgf而安装,请克隆/下载存储库,移动到根目录并运行:
$ pip install -e .
用法示例
下面是如何创建jsgf语法的使用示例 一个规则,编译它并根据语音字符串找到匹配的规则 “Hello World”:
fromjsgfimportPublicRule,Literal,Grammar# Create a public rule with the name 'hello' and a Literal expansion 'hello world'.rule=PublicRule("hello",Literal("hello world"))# Create a grammar and add the new rule to it.grammar=Grammar()grammar.add_rule(rule)# Compile the grammar using compile()# compile_to_file(file_path) may be used to write a compiled grammar to# a file instead.# Compilation is not required for finding matching rules.print(grammar.compile())# Find rules in the grammar that match 'hello world'.matching=grammar.find_matching_rules("hello world")print("Matching: %s"%matching[0])
运行上述代码将输出:
#JSGF V1.0; grammar default; public <hello> = hello world; Matching: PublicRule(name='hello', expansion=Literal('hello world'))
语法的第一行可以使用jsgf_version更改, charset_name,和language_name成员$ 上课。
在pyjsgf/examples中有一些用法示例,可以帮助您入门。
多语言支持
由于python支持unicode,pyjsgf可以与unicode一起使用 用于语法、导入和规则名称以及规则文本的字符。 如果需要,最好使用python 3或更高版本 默认情况下,字符串是Unicode字符串。
如果必须使用Python2.x,则需要将Unicode字符串定义为 不是u"text"就是unicode(text, encoding),这有点 麻烦。如果要在源代码文件中定义Unicode字符串, 您需要定义source code file encoding。