2016-01-26 41 views
2

我在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中的命令行參數,我可以將其作爲字符串參數添加到配置/設置對象?

+0

我不明白爲什麼這會在發佈的第一秒鐘內發生downvote。 –

+0

我也不明白 –

回答

0

在JSGF語法包名稱映射到作爲編碼的位置,這意味着,如果寫

import com.package.name; 

相應語法必須在相對於主語法文件夾的文件夾com/package。與java包相同。因此,佈局必須爲:

main.jsgf 
com 
    package 
    name.jsgf 

如果從字符串加載JSGF,則會搜索當前路徑以查找導入。搜索路徑也由JSGF_PATH環境變量配置。但是,在Java中設置Android中的當前工作文件夾或環境變量並不容易。出於這個原因,最好用你需要的硬編碼更改重新編譯pocketsphinx-android,或者從文件而不是字符串加載jsgf。

+0

使用pocketsphinx-android的Android應用程序的「主語法文件夾」是什麼?我必須配置它嗎?如果是這樣,怎麼樣? – frozenkoi

+0

當您調用'recognitionizer.addGrammarSearch(SEARCH_NAME,new File(/path/main.jsgf));'它從文件夾路徑加載jsgf。這個路徑是主要的語法文件夾。 –

+0

正確,問題在於通過'String'而不是'File'來添加語法,它必須使用'recognizer.getDecoder()。setJsgfString(SEARCH_NAME,searchAstring',它不提供main語法文件夾是 – frozenkoi