Menu

Help - resultListener

Help
2011-10-12
2012-09-22
  • Alexander Alvarez Jurado

    Hi, I appreciate if somebody can help me whit this problem:
    I need to implements an aplication that allows to recognize a given phrase
    again and again, and i can change the phrase. it is based on jsgf grammar
    jsapi demo, the problem is that at first time all is perfect, but when i
    select another phrase and call again the Result result =
    recognizer.recognize(); the result is always null. i had read and read looking
    for a solution, and i think that the solution may be using events, by result
    listener, or result adapter (i'm not clear), through jsapi or directly with
    sphinx, i tried whit jsapi but it do not work. i don't know how to do it.
    please help me, i realy need it... thanks, this is actualy my code:

    import com.sun.speech.engine.recognition.BaseRecognizer;
    import com.sun.speech.engine.recognition.BaseRuleGrammar;

    import javax.speech.EngineCentral;

    import javax.speech.EngineList;

    import javax.speech.EngineModeDesc;

    //import javax.speech.;
    //import javax.speech.recognition.
    ;

    import javax.speech.AudioException;
    import javax.speech.Central;
    import javax.speech.EngineException;
    import javax.speech.EngineStateError;
    import javax.speech.recognition.FinalRuleResult;
    import javax.speech.recognition.RecognizerModeDesc;
    import javax.speech.recognition.ResultAdapter;
    import javax.speech.recognition.ResultEvent;
    import javax.speech.recognition.ResultListener;
    import javax.speech.recognition.ResultToken;

    import edu.cmu.sphinx.jsapi.SphinxEngineCentral;
    import edu.cmu.sphinx.jsapi.SphinxRecognizerModeDesc;

    //import edu.cmu.sphinx.jsapi.SphinxEngineCentral;
    //import edu.cmu.sphinx.jsapi.SphinxRecognizerModeDesc;

    public class JSGFRecognizer extends ResultAdapter {
    private Recognizer recognizer;
    private JSGFGrammar jsgfGrammar;
    private Microphone microphone;
    private BaseRecognizer jsapiRecognizer;
    Controller controller=new Controller(null);
    URL url;
    public void resultAccepted(ResultEvent e)
    {
    String bestResult="";
    Result result = (Result)(e.getSource());
    bestResult = result.getBestFinalResultNoFiller();
    JOptionPane.showMessageDialog(null, bestResult);
    controller.setResult(bestResult);
    ResultToken tokens = (ResultToken) result.getResultTokens();

    /for(int i=0; i<tokens.length; i++){
    bestResult=bestResult.concat(tokens_.getSpokenText()+" ");
    }
    /
    controller.setResult(bestResult);
    //pauseRecognizing();
    }
    public void startEngine() throws IOException, PropertyException,
    InstantiationException, EngineException, EngineStateError{
    url = JSGFRecognizer.class.getResource("conf.config.xml");
    ConfigurationManager cm = new ConfigurationManager(url);
    recognizer = (Recognizer) cm.lookup("recognizer");
    jsgfGrammar = (JSGFGrammar) cm.lookup("jsgfGrammar");
    microphone = (Microphone) cm.lookup("microphone");

    jsapiRecognizer = new BaseRecognizer(jsgfGrammar.getGrammarManager());
    jsapiRecognizer.allocate();
    jsapiRecognizer.addResultListener(this);
    recognizer.allocate();
    microphone.startRecording();
    /*try {
    Central.registerEngineCentral(
    SphinxEngineCentral.class.getCanonicalName());

    RecognizerModeDesc mode = new SphinxRecognizerModeDesc(
    "/speechrecognition/conf.config.xml");
    recognizer = (Recognizer) Central.createRecognizer(mode);

    recognizer.allocate();

    grammar = recognizer.loadJSGF(
    null, "speechrecognition.commands");
    grammar.setEnabled(true);
    recognizer.commitChanges();
    //gram= recognizer.loadJSGF(url, grammarName);
    //FileReader reader = new FileReader(url);
    //RuleGrammar gram = recognizer.loadJSGF(reader);
    recognizer.addResultListener(this);
    recognizer.commitChanges();
    //recognizer.requestFocus();
    //recognizer.resume();
    //recognizer.pause();
    }
    catch (Exception e) {
    throw e;
    }
    jsgfGrammar = (JSGFGrammar) cm.lookup("jsgfGrammar");
    microphone = (Microphone) cm.lookup("microphone");
    jsapiRecognizer = new BaseRecognizer(jsgfGrammar.getGrammarManager());
    jsapiRecognizer.allocate();/
    }
    /

    Executes the demo. The demo will cycle through four separate
    JSGF grammars,a 'movies' grammar, a 'news' grammar a 'books'
    grammar and a 'music' grammar. The news, books and movies
    grammars are loaded from the corresponding JSGF grammar file,
    while the music grammar is loaded from a file, but then
    augmented via RuleGrammar.setRule.
    /
    /* public void execute() throws IOException, GrammarException
    {

    if(recognizer.getState()==State.READY)
    {
    if (microphone.startRecording())
    {
    System.out.println("Loading...");
    }
    else
    {
    System.out.println("Can't start the microphone.");
    }
    }
    else
    {
    //recognizer.allocate();
    //recognizer.requestFocus();
    //recognizer.resume();
    if (microphone.startRecording())
    {
    System.out.println("Loading...");
    }
    else
    {
    System.out.println("Can't start the microphone.");
    }
    }
    }
    public String recognizeAndReport(String phrase) throws GrammarException {

    boolean done=false;
    String bestResult="";
    while (!done) {
    Result result = recognizer.recognize();
    if(result !=null){
    bestResult = result.getBestFinalResultNoFiller();
    RuleGrammar ruleGrammar = new BaseRuleGrammar (jsapiRecognizer,
    jsgfGrammar.getRuleGrammar());
    RuleParse ruleParse = ruleGrammar.parse(bestResult, null);
    if (ruleParse != null) {
    done=isExit(ruleParse);
    if(done==true){
    bestResult="Finished...";
    JOptionPane.showMessageDialog(null, bestResult, "Finish",
    JOptionPane.INFORMATION_MESSAGE);
    }
    else{
    }
    }
    }
    }
    //microphone.stopRecording();
    return bestResult;
    }/
    /
    private boolean isExit(RuleParse ruleParse) {
    String tags = ruleParse.getTags();

    for (int i = 0; tags != null && i < tags.length; i++) {
    if (tags_.trim().equals("exit")) {
    return true;
    }
    }
    return false;
    }*/

    /
    Set the rules to the grammar, that the people have to say.

    @throws Exception if an I/O error occurs
    /
    //ADD A PHRASE TO THE GRAMMAR IT WORK PERFECT.
    public void loadGrammar(String phraseCleaned) throws Exception{
    try {
    int n=(int) Math.random();
    String name= "phrase"+n;
    jsgfGrammar.loadJSGF("phrase");
    RuleGrammar ruleGrammar = new BaseRuleGrammar(jsapiRecognizer,
    jsgfGrammar.getRuleGrammar());
    addRule(ruleGrammar, name, "");
    jsgfGrammar.commitChanges();
    } catch (Exception e) {
    throw new IOException(e);
    }
    }
    private void addRule(RuleGrammar ruleGrammar, String ruleName, String jsgf)
    throws GrammarException {
    Rule newRule = ruleGrammar.ruleForJSGF(jsgf);
    ruleGrammar.setRule(ruleName, newRule, true);
    ruleGrammar.setEnabled(ruleName, true);
    }
    public void initRecognizing() throws Exception{
    try{
    jsapiRecognizer.requestFocus();
    jsapiRecognizer.resume();
    }
    catch(Exception e){
    throw e;
    }
    }
    public void pauseRecognizing() {
    jsapiRecognizer.pause();
    }
    }
    __

     
  • Nickolay V. Shmyrev

    Hello

    Please provide information about the sphinx4 version you are using. Please
    provide a complete example including all required files to run the test, not
    just the source file.

     
  • Alexander Alvarez Jurado

    OK, THE VERSION THAT I AM USING IS sphinx4-1.0beta6, how can i provide you all
    resources?.

    the project has an interface, a config.xml, two .gram files, and another three
    classes. but i yust need to know how to use the result listener, or one
    solution for the problen that i explain above. All demos have an loop while,
    when do the recognizing, but it is not optimal cause blocks the form, and when
    i call again that function the result is null, and it cause an exception.
    thaks so much for reply.

     
  • Nickolay V. Shmyrev

    how can i provide you all resources?

    You can pack everything into archive and upload to a public file sharing
    resource (dropbox, google docs)

    i yust need to know how to use the result listener

    Resul listener is used like any other listener, you just attach a listener and
    listen. I does not seem like you are doing that right now.

     
  • Alexander Alvarez Jurado

    at first thanks for to answer, and i do not know why the event is never
    generated... :(

    do you know what i need to the event be generated?

     
  • Alexander Alvarez Jurado

    hi, my problem continues, i have this code, i´m tring to do that an event be
    generated when i speak someting, but it never occurs, what is missing? what im
    doing wrong?

    package speechrecognition;

    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    import org.apache.commons.lang.StringUtils;
    import com.mysql.jdbc.ResultSet;
    import javax.speech.recognition.ResultListener;

    import edu.cmu.sphinx.jsgf.JSGFGrammar;
    import edu.cmu.sphinx.recognizer.Recognizer;
    import edu.cmu.sphinx.frontend.util.Microphone;
    import edu.cmu.sphinx.result.Result;
    import edu.cmu.sphinx.util.props.ConfigurationManager;
    import edu.cmu.sphinx.util.props.PropertyException;
    import edu.cmu.sphinx.util.props.PropertySheet;
    import java.io.IOException;
    import java.net.URL;
    import javax.speech.recognition.GrammarException;
    import javax.speech.recognition.Rule;
    import javax.speech.recognition.RuleGrammar;
    import javax.swing.JOptionPane;
    import com.sun.speech.engine.recognition.BaseRecognizer;
    import com.sun.speech.engine.recognition.BaseRuleGrammar;
    //import edu.cmu.sphinx.decoder.ResultListener;

    //import javax.speech.;
    //import javax.speech.recognition.
    ;
    import javax.speech.EngineException;
    import javax.speech.EngineStateError;

    import javax.speech.recognition.ResultEvent;

    import java.awt.;
    import java.awt.event.
    ;

    public class Mainform extends JFrame implements ActionListener
    {
    Container contenedor = getContentPane();

    private JLabel lblTitle;
    private JLabel lblCourse;
    private JLabel lblLesson;
    private JLabel lblstatusTitle;
    private JLabel lblState;

    private JTextField txtCourse;
    private JTextField txtLesson;
    private JTextArea txtPhraseContainer;
    private JTextArea txtResults;

    private JScrollPane panelPhraseToSay;
    private JScrollPane panelList;
    private JScrollPane panelResults;

    private JTable lessonSpeak;

    private JPanel panelState;
    private JScrollPane scrollPanePanel;

    private JButton btnSay;
    private JButton btnClose;

    ImageIcon imageSpeak;
    ImageIcon imageNoSpeak;

    private int userId;
    String result=null;
    String phrase=null;
    String phraseCleaned="";
    JSGFRecognizer jsgfrecognizer = new JSGFRecognizer();

    public Mainform(int userId)
    {
    super("Private Teacher");
    this.userId=userId;
    addWindowListener( new Closing());
    InitComponents();
    lblState.setText("Loading...");
    try{
    jsgfrecognizer.startEngine();
    }
    catch(Exception e){
    JOptionPane.showMessageDialog(null, "Error al iniciar el sistema.\nPor favor
    intente nuevamente.", "Error", JOptionPane.ERROR_MESSAGE);
    e.printStackTrace();
    System.exit(0);
    }
    lblState.setText("Ready.");
    }
    private void InitComponents()
    {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    contenedor.setLayout(null);
    {
    btnSay=new JButton();
    btnSay.setText("Say");
    btnSay.setEnabled(false);
    btnSay.addActionListener(this);
    btnSay.setBounds(250, 240, 60, 25);
    contenedor.add(btnSay);
    }
    {
    imageSpeak = new ImageIcon("images/speak.gif");
    imageNoSpeak = new ImageIcon("images/nospeak.gif");
    }
    {
    btnClose=new JButton();
    btnClose.setText("Exit");
    btnClose.setEnabled(true);
    btnClose.addActionListener(this);
    btnClose.setBounds(590, 510, 80, 20);
    contenedor.add(btnClose);
    }
    {
    Font f = new Font( "Helvetica",Font.BOLD, 14 );
    lblTitle=new JLabel();
    contenedor.add(lblTitle);
    lblTitle.setBounds(26, 30, 200, 20);
    lblTitle.setText("Speech recognition");
    lblTitle.setFont(f);
    }
    {
    lblCourse=new JLabel();
    contenedor.add(lblCourse);
    lblCourse.setBounds(30, 60, 80, 20);
    lblCourse.setText("Course:");
    }
    {
    lblLesson=new JLabel();
    contenedor.add(lblLesson);
    lblLesson.setBounds(30, 90, 80, 20);
    lblLesson.setText("Lesson:");
    }
    {
    txtCourse=new JTextField();
    contenedor.add (txtCourse);
    txtCourse.setBounds(100, 60, 250, 20);
    txtCourse.setEditable(false);
    }
    {
    txtLesson=new JTextField();
    contenedor.add (txtLesson);
    txtLesson.setBounds(100, 90, 250, 20);
    txtLesson.setEditable(false);
    }
    {
    panelState=new JPanel();
    panelState.setLayout(new BorderLayout());
    lblstatusTitle=new JLabel("State: ");
    lblState=new JLabel();
    panelState.add(lblstatusTitle, BorderLayout.WEST);
    panelState.add(lblState, BorderLayout.CENTER);
    panelState.setBounds(2, 545, 600, 25);
    contenedor.add(panelState);
    }
    {
    panelPhraseToSay=new JScrollPane();
    TitledBorder title;
    title = BorderFactory.createTitledBorder("Select a part of the sentence or say
    the whole sentence:");
    panelPhraseToSay.setBorder(title);
    contenedor.add(panelPhraseToSay);
    panelPhraseToSay.setBounds(30, 150, 500, 80);
    panelPhraseToSay.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROL
    LBAR_AS_NEEDED);
    }
    {
    panelResults=new JScrollPane();
    TitledBorder title;
    title = BorderFactory.createTitledBorder("You Said:");
    panelResults.setBorder(title);
    contenedor.add(panelResults);
    panelResults.setBounds(30, 280, 500, 80);
    panelResults.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR
    _AS_NEEDED);
    }
    {
    Border blackline;
    blackline = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
    panelList=new JScrollPane();
    panelList.setBorder(blackline);
    contenedor.add(panelList);
    panelList.setBounds(30, 380, 500, 150);
    }
    {
    txtPhraseContainer = new JTextArea();
    txtPhraseContainer.setEditable(false);
    panelPhraseToSay.setViewportView(txtPhraseContainer);
    }
    {
    txtResults = new JTextArea();
    txtResults.setEditable(false);
    panelResults.setViewportView(txtResults);
    }
    initCourseData();
    inittabledata();
    setResizable(false);
    pack();
    setSize(700, 600);
    setLocationRelativeTo(null);
    setVisible(true);
    }
    public void inittabledata()
    {
    DefaultTableModel modelo = new DefaultTableModel();
    modelo.addColumn("Text");
    modelo.addColumn("Rating");
    modelo.addColumn("Start");
    lessonSpeak = new JTable();
    lessonSpeak.setModel(modelo);
    //lessonSpeak.setEnabled(false);
    TableColumn agregarColumn;
    agregarColumn = lessonSpeak.getColumnModel().getColumn(2);
    agregarColumn.setCellEditor(new MyEditor(lessonSpeak, this));
    agregarColumn.setCellRenderer(new MyRenderer(true));
    TableColumn columna = lessonSpeak.getColumnModel().getColumn(0);
    columna.setPreferredWidth(395);

    columna = lessonSpeak.getColumnModel().getColumn(1);
    columna.setPreferredWidth(40);
    columna = lessonSpeak.getColumnModel().getColumn(2);
    columna.setPreferredWidth(60);
    try
    {
    Database dataBase=new Database();
    //ResultSet result= privateteacher.getsectioncontent(usuario.sectionid);
    ResultSet result=(ResultSet) dataBase.getsectioncontent(250);
    while (result.next())
    {
    // Se crea un array que ser� una de las filas de la tabla.
    Object fila = new Object; // Hay tres columnas en la tabla
    // Se rellena cada posici�n del array con una de las columnas de la tabla en
    base de datos.
    //for (int i=0;i<3;i++)
    fila = result.getString("text"); // El primer indice en rs es el 1, no el
    cero, por eso se suma 1.
    fila = "none";
    //fila = "hola";
    // Se a�ade al modelo la fila completa.
    modelo.addRow(fila);
    }
    panelList.setViewportView(lessonSpeak);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    public void getPhraseToSay(String phraseToClean)
    {
    this.phrase=phraseToClean;
    result="";
    try{
    phraseCleaned = cleanPhrase (phrase);
    jsgfrecognizer.loadGrammar(phraseCleaned);
    lblState.setText("Recognizing...");
    jsgfrecognizer.initRecognizing();
    //getResult(result);
    }
    catch (Exception e) {
    JOptionPane.showMessageDialog(null, "Error al iniciar la gramatica.\nPor favor
    intente nuevamente.", "Error", JOptionPane.ERROR_MESSAGE);
    e.printStackTrace();
    btnSay.setEnabled(true);
    }
    }
    private double StringCompRatio(String sCad1, String sCad2)
    {
    // Preparacion previa
    if ((sCad1.length() > sCad2.length()))
    {
    String sAux;
    sAux = sCad1;
    sCad1 = sCad2;
    sCad2 = sAux;
    }
    int lLong;
    lLong = sCad2.length();
    if ((lLong == 0))
    {
    return 1;

    }
    sCad1 = (sCad1 + Space("", (lLong - sCad1.length())));
    //Recorremos las cadenas
    String sChar;
    int i;
    int j;
    int iPos = 0;
    int iDist = 0;
    int iTotDist = 0;
    int x=0;
    for (i = 1; (i <= lLong); i++)
    {
    x=i-1;
    sChar = sCad1.substring(x, x+1);
    iDist = lLong;
    for (j = 1; j <= lLong; j++)
    {
    x=j-1;
    // Si coinciden 2 caracteres vemos la distancia y nos quedamos con la menor
    String m=sCad2.substring(x, x+1);
    //int x=Math.abs(j - i);
    //if(sChar.equalsIgnoreCase(m)){JOptionPane.showMessageDialog(null, "if1");}
    //if(Math.abs((j - i)) < iDist){JOptionPane.showMessageDialog(null, "if2");}

    if (((sChar.equalsIgnoreCase(sCad2.substring(x, x+1))) && (Math.abs(j - i) <
    iDist)))
    {
    iDist = Math.abs((j - i));
    iPos = j;
    }
    }
    // Eliminamos el caracter para que no se "multicompare"
    // Se cambia por "~", caracter que no debe estar en las cadenas que se
    comparan
    if ((iDist < lLong))
    {
    sCad2 = (sCad2.substring(0, (iPos - 1)) + ("~" + sCad2.substring(iPos)));
    }
    iTotDist = (iTotDist + iDist);
    }
    // Formula establecida
    return (1 - (iTotDist / Math.pow(lLong , 2)));
    }
    private String Space(String s, int n)
    {
    return StringUtils.leftPad(s, n, "");
    }
    public void putPhrase(String phrase){
    txtPhraseContainer.setLineWrap(true);
    txtPhraseContainer.setText("");
    txtPhraseContainer.setText(phrase);
    btnSay.setEnabled(true);
    }
    public String cleanPhrase (String phrase)
    {
    String newPhrase="";
    String aRemplazar=phrase;
    aRemplazar=aRemplazar.trim();
    newPhrase=aRemplazar.replace("?", "");
    aRemplazar = newPhrase;
    newPhrase = aRemplazar.replace(" ", "]");
    jsgfGrammar.commitChanges();
    } catch (Exception e) {
    throw new IOException(e);
    }
    }
    private void addRule(RuleGrammar ruleGrammar, String ruleName, String jsgf)
    throws GrammarException {
    Rule newRule = ruleGrammar.ruleForJSGF(jsgf);
    ruleGrammar.setRule(ruleName, newRule, true);
    ruleGrammar.setEnabled(ruleName, true);
    }
    public void initRecognizing() throws Exception{
    try{
    jsapiRecognizer.requestFocus();
    jsapiRecognizer.resume();
    }
    catch(Exception e){
    throw e;
    }
    }
    public void pauseRecognizing() {
    jsapiRecognizer.pause();
    }

    public void audioReleased(ResultEvent re) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void grammarFinalized(ResultEvent re) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void resultCreated(ResultEvent re) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void resultRejected(ResultEvent re) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void resultUpdated(ResultEvent re) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void trainingInfoReleased(ResultEvent re) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void newResult(Result result) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void newProperties(PropertySheet ps) throws PropertyException {
    throw new UnsupportedOperationException("Not supported yet.");
    }
    }
    }

     

Log in to post a comment.