PocketSphinx:如何识别关键字和动态单词?

时间:2018-07-31 06:57:04

标签: android speech-recognition pocketsphinx pocketsphinx-android

我一直在研究PocketSphinx Android语音命令项目,在该项目中,用户可以使用命令“ open xxx”打开其Android上列出的应用程序(我将“ open”设置为“关键字”,而xxx是应用程序的名称),但遗憾的是,它没有工作。

我的问题是,PocketSphinx如何识别两个单词的命令,其中第一个单词是Keyword,第二个单词是动态单词(用户可以说出任何应用程序的名称)。

声明关键字:

public class PocketSphinxActivity extends Activity implements
    RecognitionListener {

/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";

/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "open";

我尝试使用Split()两个单词的关键字(例如:open camera)并成功打开了camera,但是我需要的是“ open xxx”,以便用户可以定义他们想要的任何应用程序。这就是为什么我只将关键字更改为“打开”。

public void onPartialResult(Hypothesis hypothesis) {
    if (hypothesis == null)
        return;

    String text = hypothesis.getHypstr();
    String[] split = text.split("\\s+");
    String word1 = split[0];
    String word2 = split[1];

    if (word2 != null) {
        if (word1.equals("open")) {
            PackageManager packageManager = getPackageManager();
            List<PackageInfo> packs = packageManager.getInstalledPackages(0);

            int size = packs.size();
            boolean uninstallApp = false;
            boolean exceptFlg = false;

            for (int v = 0; v < size; v++) {
                PackageInfo p = packs.get(v);
                String tmpAppName = p.applicationInfo.loadLabel(packageManager).toString();
                String pname = p.packageName;
                tmpAppName = tmpAppName.toLowerCase();
                if (tmpAppName.trim().toLowerCase().equals(word2.trim().toLowerCase())) {
                    PackageManager pm = this.getPackageManager();
                    Intent appStartIntent = pm.getLaunchIntentForPackage(pname);
                    if (null != appStartIntent) {
                        try {
                            this.startActivity(appStartIntent);
                        } catch (Exception e) {
                        }
                    }
                }
            }
            finish();

public void onResult(Hypothesis hypothesis) {
    ((TextView) findViewById(R.id.result_text)).setText("");
    if (hypothesis != null) {
        String text = hypothesis.getHypstr();
        String[] split = text.split("\\s+");
        String word1 = split[0];
        String word2 = split[1];
        makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }
}

任何对此的帮助将不胜感激。我真的需要它来完成我的最终项目。谢谢。

0 个答案:

没有答案