我在Android中使用pocketsphinx。而且我已经能够将一个.gram
文件中的规则导入另一个.gram
文件,并且我已经能够通过指定 a 来使用规则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 知道在哪里可以找到我要导入的文件?我还没有找到函数 inSpeechRecognizer
或 in Decoder
。我在想也许有一种方法可以在配置中指定在哪里查找语法文件,但我似乎找不到它。SpeechRecognizerSetup
在or中添加一些参数Config
?或者,我可以将 pocketsphinx 中的命令行参数作为字符串参数添加到 Config/Setup 对象中吗?