在口袋里的狮身人面像中定义一个新关键字

时间:2016-06-04 11:25:28

标签: android keyword pocketsphinx

我对此很新。
我想做的是:

  • 从语音到文本引擎
  • 获取用户定义的KEYWORD
  • 因为它识别,我的应用程序做了一些事情

我读了PocketSphinix并且找不到我的, 我也觉得它很难,所以我更喜欢改变它并使用默认值。

现在我的问题是:如何定义新的关键字"我的手机"在* .gram文件中?

这是我的代码 - 我把它here

import android.app.Activity;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.SpeechRecognizer;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

import edu.cmu.pocketsphinx.Assets;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.SpeechRecognizerSetup;

/**
 * Created by Mina Joon jooni on 6/4/2016.
 */
public class PracticeActivity  extends Activity implements RecognitionListener, edu.cmu.pocketsphinx.RecognitionListener {

private static final String DIGITS_SEARCH = "digits";
    private edu.cmu.pocketsphinx.SpeechRecognizer recognizer;

    @Override
public void onCreate(Bundle state)
{
    super.onCreate(state);

    setContentView(R.layout.practice);

    ((TextView) findViewById(R.id.caption_text)).setText("Preparing the recognizer");

    try
    {
        Assets assets = new Assets(PracticeActivity.this);
        File assetDir = assets.syncAssets();
        setupRecognizer(assetDir);
    }
    catch (IOException e)
    {
        // oops
    }

    ((TextView) findViewById(R.id.caption_text)).setText("Say up, down, left, right, forwards, backwards");

    reset();
}

@Override
public void onPartialResult(Hypothesis hypothesis)
{//DO STH
}

@Override
public void onResult(Hypothesis hypothesis)
{
    ((TextView) findViewById(R.id.result_text)).setText("");

    if (hypothesis != null)
    {
        String text = hypothesis.getHypstr();
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onError(Exception e) {

}

@Override
public void onTimeout() {

}

@Override
public void onReadyForSpeech(Bundle params) {

}

@Override
public void onBeginningOfSpeech()
{
}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech()
{
    reset();
}

@Override
public void onError(int error) {

}

@Override
public void onResults(Bundle results) {

}

@Override
public void onPartialResults(Bundle partialResults) {

}

@Override
public void onEvent(int eventType, Bundle params) {

}

private void setupRecognizer(File assetsDir) throws IOException {
    File modelsDir = new File(assetsDir, "models");

    recognizer = SpeechRecognizerSetup.defaultSetup()
            .setAcousticModel(new File(modelsDir, "en-us-ptm"))
            .setDictionary(new File(modelsDir, "cmudict-en-us.dict"))
            .setRawLogDir(assetsDir).setKeywordThreshold(1e-20f)
            .getRecognizer();

    recognizer.addListener(this);

    File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
    recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);
}

private void reset()
{
    recognizer.stop();
    recognizer.startListening(DIGITS_SEARCH);
}
}

我怎样才能做第一个?
请帮帮我!

1 个答案:

答案 0 :(得分:3)

使用

recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);

而不是

recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);

还要根据文件名修改assests.lst。