【问题标题】:Import grammar rules from string-based grammar从基于字符串的语法中导入语法规则
【发布时间】:2016-01-26 06:16:02
【问题描述】:

我在 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 自己的资产同步实用程序中得到的)。

使用包名 如果我将 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 对象中?

【问题讨论】:

  • 这个我也不懂

标签: android pocketsphinx pocketsphinx-android


【解决方案1】:

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

 import com.package.name;

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

main.jsgf
com
  package
    name.jsgf

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

【讨论】:

  • 使用 pocketsphinx-android 的 Android 应用程序的“主语法文件夹”是什么?我必须配置它吗?如果有,怎么做?
  • 当您调用 recognizer.addGrammarSearch(SEARCH_NAME, new File(/path/main.jsgf)); 时,它会从文件夹路径加载 jsgf。该路径是主语法文件夹。
  • 正确,问题是通过 String 而不是 File 添加语法必须使用 recognizer.getDecoder().setJsgfString(SEARCH_NAME, searchAsString 完成,这不提供主语法文件夹的上下文.
  • 好的,我现在看到问题了。在这种情况下,它使用您可以通过getAbsolutePath(".") 获得的当前工作文件夹。但是,在 Android 上没有简单的方法来修改它。我认为如果您需要此功能,最好使用您需要的更改重新编译 pocketsphinx-android,或者从文件而不是字符串加载 jsgf。
  • 不幸的是File defaultDir = new File("."); String defaultDirstring = defaultDir.getAbsolutePath(); 产生/. 由于目录名称中有点,因此无法使用绝对路径访问这些文件。 IE。尝试使用/storage/emulated/0/Android/data/com.sample.vr/files/sync/help.r_help 导致PS 试图找到这个文件/storage/emulated/0/Android/data/com/sample/vr/files/sync/help.gram,问题是Android 的应用程序文件夹本身就是一个包(com.sample.vr),用点分隔。我想我会求助于将字符串保存到文件并使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 2017-06-10
  • 1970-01-01
  • 2013-01-24
  • 2019-02-03
相关资源
最近更新 更多