hi, i´´m tring to implments a result listener using jsapi and sphinx4, but the event is never generated, what is missing or what i´m doing wrong? this is my code:
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(" ", "][");
return newPhrase;
}
public void initCourseData()
{
User user = new User();
user.initUser(userId);
user.getcurrentlocation();
txtCourse.setText(user.course);
txtLesson.setText(user.sectionname);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnSay){
btnSay.setEnabled(false);
lblState.setText("Loading…");
String phraseToClean=null;
phraseToClean=txtPhraseContainer.getSelectedText();
if(phraseToClean == null){
phraseToClean = txtPhraseContainer.getText();
}
getPhraseToSay(phraseToClean);
}
if(e.getSource()==btnClose){
System.exit(0);
}
}
public class Closing extends WindowAdapter {
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
public void putResults(String bestResult) {
txtResults.setText("");
txtResults.setText(bestResult);
//txtResults.getSelectedTextColor(); para que el usuario pueda decir solo lo que quiera seleccionar.
}
public void getResult(String bestResult) {
String strQualitativeRating = "";
double compRatio=StringCompRatio(phrase, bestResult);
if(compRatio<0.6){
if(bestResult.equals("") || bestResult==null){
bestResult="I can't hear you…";
}
putResults(bestResult+"… Tray again!!!");
jsgfrecognizer.pauseRecognizing();
btnSay.setEnabled(true);
}
else{
if (compRatio > 0.6 && compRatio < 0.7)
strQualitativeRating = "You can do it better…";
else if (compRatio > 0.7 && compRatio < 0.8)
strQualitativeRating = "Good";
else if (compRatio > 0.8 && compRatio < 0.9)
strQualitativeRating = "Very good";
else if (compRatio > 0.9 && compRatio < 1)
strQualitativeRating = "Excellent";
else if (compRatio == 1)
strQualitativeRating = "perfect";
putResults(bestResult+"… "+strQualitativeRating);
jsgfrecognizer.pauseRecognizing();
btnSay.setEnabled(true);
}
}
public class JSGFRecognizer extends ResultAdapter{
private Recognizer recognizer;
private JSGFGrammar jsgfGrammar;
private Microphone microphone;
private BaseRecognizer jsapiRecognizer;
URL url;
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();
}
hi, i´´m tring to implments a result listener using jsapi and sphinx4, but the event is never generated, what is missing or what i´m doing wrong? this is my code:
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 javax.speech.*;
import javax.speech.recognition.*;
import java.io.FileReader;
import java.util.Locale;
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_SCROLLBAR_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(" ", "][");
return newPhrase;
}
public void initCourseData()
{
User user = new User();
user.initUser(userId);
user.getcurrentlocation();
txtCourse.setText(user.course);
txtLesson.setText(user.sectionname);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnSay){
btnSay.setEnabled(false);
lblState.setText("Loading…");
String phraseToClean=null;
phraseToClean=txtPhraseContainer.getSelectedText();
if(phraseToClean == null){
phraseToClean = txtPhraseContainer.getText();
}
getPhraseToSay(phraseToClean);
}
if(e.getSource()==btnClose){
System.exit(0);
}
}
public class Closing extends WindowAdapter {
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
public void putResults(String bestResult) {
txtResults.setText("");
txtResults.setText(bestResult);
//txtResults.getSelectedTextColor(); para que el usuario pueda decir solo lo que quiera seleccionar.
}
public void getResult(String bestResult) {
String strQualitativeRating = "";
double compRatio=StringCompRatio(phrase, bestResult);
if(compRatio<0.6){
if(bestResult.equals("") || bestResult==null){
bestResult="I can't hear you…";
}
putResults(bestResult+"… Tray again!!!");
jsgfrecognizer.pauseRecognizing();
btnSay.setEnabled(true);
}
else{
if (compRatio > 0.6 && compRatio < 0.7)
strQualitativeRating = "You can do it better…";
else if (compRatio > 0.7 && compRatio < 0.8)
strQualitativeRating = "Good";
else if (compRatio > 0.8 && compRatio < 0.9)
strQualitativeRating = "Very good";
else if (compRatio > 0.9 && compRatio < 1)
strQualitativeRating = "Excellent";
else if (compRatio == 1)
strQualitativeRating = "perfect";
putResults(bestResult+"… "+strQualitativeRating);
jsgfrecognizer.pauseRecognizing();
btnSay.setEnabled(true);
}
}
public class JSGFRecognizer extends ResultAdapter{
private Recognizer recognizer;
private JSGFGrammar jsgfGrammar;
private Microphone microphone;
private BaseRecognizer jsapiRecognizer;
URL url;
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();
}
public void resultAccepted(ResultEvent e)
{
String bestResult="";
//bestResult = result.getBestFinalResultNoFiller();
JOptionPane.showMessageDialog(null, bestResult);
//getResult(bestResult);
Result r = (Result)(e.getSource());
ResultToken tokens = r.getBestTokens();
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens_.getSpokenText() + " ");
pauseRecognizing();
}
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();
}
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.");
}
}
}
thanks…_