问题
I am very new to this.
What I want to do is:
- Get a user defined KEYWORD from the Speech To Text engine
- as it recognized, my app does something
I read PocketSphinix and can't find mine, also I find it's difficult, therefore I prefer to change it and use the default one.
Now my problem is: how can I define a new keyword "my phone" in the *.gram file?
This is my code - I took it 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);
}
}
How can I do the first one?
Please help me!
回答1:
Use
recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
Instead of
recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);
Also modify the assests.lst according to the file name.
来源:https://stackoverflow.com/questions/37629636/define-a-new-keyword-in-pocket-sphinx