package speech.recognizer;
import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
public class Sphinx
{
static String status;
/*private static ConfigurationManager cm;
private static Recognizer recognizer;
private static Microphone microphone;*/
/*public Sphinx()
{
cm = new ConfigurationManager(Sphinx.class.getResource("main.config.xml"));
recognizer = (Recognizer) cm.lookup("recognizer");
microphone = (Microphone) cm.lookup("microphone");
}*/
public String error(int error_code)
{
switch(error_code)
{
case 1:
return "Sorry, Can't start microphone !";
case 2:
return "Sorry, I can't hear what you said!";
default:
return null;
}
}
public String get_status()
{
return status;
}
public String get_direction()
{
ConfigurationManager cm;
Recognizer recognizer;
Microphone microphone;
cm = new ConfigurationManager(Sphinx.class.getResource("direction.config.xml"));
recognizer = (Recognizer) cm.lookup("recognizer");
microphone = (Microphone) cm.lookup("microphone");
recognizer.allocate();
if (! microphone.startRecording())
{
recognizer.deallocate();
error(1);
}
status = "Processing..";
Result result = recognizer.recognize();
if (result != null)
{
recognizer.deallocate();
return result.getBestResultNoFiller();
}
else
error(2);
return null;
}
public boolean get_answer()
{
ConfigurationManager cm;
Recognizer recognizer;
Microphone microphone;
cm = new ConfigurationManager(Sphinx.class.getResource("answer.config.xml"));
recognizer = (Recognizer) cm.lookup("recognizer");
microphone = (Microphone) cm.lookup("microphone");
recognizer.allocate();
if (! microphone.startRecording())
{
recognizer.deallocate();
error(1);
}
Result result = recognizer.recognize();
if (result != null)
{
recognizer.deallocate();
String answer = result.getBestFinalResultNoFiller();
if(answer.equalsIgnoreCase("yes"))
return true;
else
return false;
}
else
error(2);
return false;
}
}