首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从基于字符串的语法中导入语法规则

从基于字符串的语法中导入语法规则
EN

Stack Overflow用户
提问于 2016-01-26 06:16:02
回答 1查看 907关注 0票数 3

我用的是机器人里的口袋狮身人面像。我已经能够将一个.gram文件中的规则导入另一个.gram文件,并且能够通过指定一个String来使用规则,但似乎不能将两者结合起来。也就是说,我不能从基于字符串的语法中导入基于文件的语法;我总是得到Failed to find grammar错误。

当将语法指定为字符串时,使用:

代码语言:javascript
复制
private SpeechRecognizer mPsRecognizer;

//...

final StringGrammar sg = settings.getStringGrammar();
mPsRecognizer.getDecoder().setJsgfString(sg.getName(), sg.getString());

其中的字符串类似于:

代码语言:javascript
复制
name: "initial_grammar",

string: "#JSGF V1.0;\n"
    + "\n"
    + "grammar initial_grammar;\n"
    + "\n"
    + "import <digits.digits>;\n"
    + "import <actions.r_actions>;\n"
    + "import <help.r_help>;\n"
    + "\n"
    + "public <initial_grammar>\n"
    + "    = <r_actions>\n"
    + "    | <r_help>\n"
    + "    ;");

我得到了

代码语言:javascript
复制
com.sample.vr.commands E/cmusphinx: ERROR: "jsgf.c", line 775: Failed to find grammar digits.gram
com.sample.vr.commands E/cmusphinx: ERROR: "jsgf.c", line 775: Failed to find grammar actions.gram
com.sample.vr.commands E/cmusphinx: ERROR: "jsgf.c", line 775: Failed to find grammar help.gram
com.sample.vr.commands I/cmusphinx: INFO: jsgf.c(691): Defined rule: PUBLIC <initial_step.initial_grammar>
com.sample.vr.commands E/cmusphinx: ERROR: "jsgf.c", line 338: Undefined rule in RHS: <initial_step.digits>

这在某种程度上是预期的,因为没有可以搜索的基本文件。

我尝试过的:1)使用完整的包名,2)使用完整的路径(我从pocketsphinx自己的Asset实用程序中获得的)。

如果我将路径更改为

代码语言:javascript
复制
    //...
    + "import <com.sample.vr.commands/files/sync/actions.r_actions>;\n"
    + "import <com.sample.vr.commands/files/sync/help.r_help>;\n"
    //...

我得到了

代码语言:javascript
复制
com.sample.vr.commands E/cmusphinx: ERROR: "jsgf.c", line 775: Failed to find grammar com/sample/vr/commands/files/sync/help.gram
com.sample.vr.commands E/cmusphinx: ERROR: "jsgf.c", line 775: Failed to find grammar com/sample/vr/commands/files/sync/actions.gram

全路径

代码语言:javascript
复制
#JSGF V1.0;

grammar initial_step;

import </storage/emulated/0/Android/data/com.sample.vr.commands/files/sync/actions.r_actions>;
import </storage/emulated/0/Android/data/com.sample.vr.commands/files/sync/help.r_help>;
public <initial_grammar>
    = <r_actions>
    | <r_help>
    ;

我得到以下错误(请注意,包部分已转换为单独的目录):

代码语言:javascript
复制
com.sample.vr.commands E/cmusphinx: ERROR: "jsgf.c", line 775: Failed to find grammar /storage/emulated/0/Android/data/com/sample/vr/commands/files/sync/help.gram
com.sample.vr.commands E/cmusphinx: ERROR: "jsgf.c", line 775: Failed to find grammar /storage/emulated/0/Android/data/com/sample/vr/commands/files/sync/actions.gram
com.sample.vr.commands I/cmusphinx: INFO: jsgf.c(691): Defined rule: PUBLIC <initial_step.initial_grammar>

在pocketsphinx的来源中,我看到他们正在用圆点代替斜杠:

代码语言:javascript
复制
/* Construct a filename. */
for (c = path; *c; ++c)
    if (*c == '.')
        *c = '/';
strcat(path, ".gram");
newpath = path_list_search(jsgf->searchpath, path);
if (newpath == NULL) {
    E_ERROR("Failed to find grammar %s\n", path);
    ckd_free(path);
    return NULL;
}

,我怎样才能让知道在哪里找到我想要导入的文件呢?我还没有在SpeechRecognizerDecoder中找到一个函数。我认为可能有一种方法可以在配置中指定查找语法文件的位置,但我似乎找不到它。在SpeechRecognizerSetupConfig中添加一些参数?或者,我可以将这个命令行参数作为字符串参数添加到Config/Setup对象中吗?

EN

回答 1

Stack Overflow用户

发布于 2016-01-26 10:25:05

在JSGF语法包名称中,将映射到编码的位置,这意味着如果您编写

代码语言:javascript
复制
 import com.package.name;

对应的语法必须在相对于主语法文件夹的文件夹com/package中。和java包一样。因此,布局必须是:

代码语言:javascript
复制
main.jsgf
com
  package
    name.jsgf

如果从字符串加载了JSGF,则将搜索当前路径以查找导入。搜索路径也由JSGF_PATH环境变量配置。但是,在Java中使用Android设置当前的工作文件夹或环境变量并不容易。因此,最好用硬编码的更改重新编译pocketsphinx,或者从文件中加载jsgf,而不是字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35007985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档