android - 从基于字符串的语法导入语法规则

标签 android pocketsphinx pocketsphinx-android

我在 Android 中使用 pocketsphinx。而且我已经能够将一个 .gram 文件中的规则导入到另一个 .gram 文件中,并且我已经能够通过指定 String 使用规则,但似乎无法将两者结合起来。也就是说,我无法从基于字符串的语法中导入基于文件的语法;我不断收到 Failed to find grammar 错误。

将语法指定为字符串时:

private SpeechRecognizer mPsRecognizer;

//...

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

字符串是这样的:

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"
    + "    ;");

我明白了

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 自己的 Assets 同步实用程序中获得的)。

使用包名 如果我将 import 路径更改为

    //...
    + "import <com.sample.vr.commands/files/sync/actions.r_actions>;\n"
    + "import <com.sample.vr.commands/files/sync/help.r_help>;\n"
    //...

我明白了

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

完整路径

#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>
    ;

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

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 的源代码中,我看到他们正在用斜杠替换点:

/* 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;
}

我该怎么做让 pocketsphinx-android 知道在哪里可以找到我要导入的文件?我在 SpeechRecognizerDecoder 中都没有找到函数。 我在想也许有一种方法可以在配置中指定在哪里查找语法文件,但我似乎找不到它。添加一些参数到 SpeechRecognizerSetupConfig? 或者 pocketsphinx 中是否有命令行参数,我可以将其作为字符串参数添加到 Config/Setup 对象?

最佳答案

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

 import com.package.name;

相应的语法必须在相对于主语法文件夹的文件夹 com/package 中。与 java 包相同。所以,布局必须是:

main.jsgf
com
  package
    name.jsgf

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

关于android - 从基于字符串的语法导入语法规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35007985/

相关文章:

java - Android 将整数转换为字符串以及字符串转换为整数

android - onPurchasesUpdated 中的购买列表

android - 如何在后台捕获任何布局的图像

cmusphinx - 语言模型/集不包含 </s>

android - Android 上的长音频语音识别

android - 如何在android中使音频扬声器静音

javascript - 如何优化 pocketsphinx.js 中的阈值

raspberry-pi3 - Phonetisaurus 依赖问题

swift - 找不到体系结构 'TLSphinx' 的模块 'arm64';发现 : i386