Sphinx

javadoc

The Sphinx service wraps the Spinx 4 Java speech recognizer project, and allows speech recognition to be incorporated with other services.

References 

JSGF Example Diagram (Source )


#file : service/Sphinx.py edit raw
from java.lang import String
from org.myrobotlab.service import Sphinx
from org.myrobotlab.service import Runtime

# create ear and mouth
ear = Runtime.createAndStart("ear","Sphinx")
mouth = Runtime.createAndStart("mouth","MarySpeech")

# start listening for the words we are interested in
ear.addComfirmations("yes","correct","ya")
ear.addNegations("no","wrong","nope","nah")
#ear.startListening("hello world|happy monkey|go forward|stop|yes|correct|ya|no|wrong|nope|nah")
ear.startListening("hello world|happy monkey|go forward|stop")

ear.addCommand("hello world", "python", "helloWorld")

# set up a message route from the ear --to--> python method "heard"
# ear.addListener("recognized", python.name, "heard");

# ear.addComfirmations("yes","correct","yeah","ya")
# ear.addNegations("no","wrong","nope","nah")

# ear.addCommand("hello world", "python", "helloworld")

# set up a message route from the ear --to--> python method "heard"
# ear.addListener("recognized", python.name, "heard");


# this method is invoked when something is
# recognized by the ear - in this case we
# have the mouth "talk back" the word it recognized
#def heard(phrase):
#      mouth.speak("you said " + phrase)
#      print "heard ", phrase

# prevent infinite loop - this will suppress the
# recognition when speaking - default behavior
# when attaching an ear to a mouth :)

ear.attach(mouth)

def helloworld(phrase):
    print "This is hello world in python."
    print phrase
 


#file : toSort/Sphinx.Arduino.py edit raw
from java.lang import String
 
# a demo of using speech recognition to 
# turn an Arduino's pin 13 off or on
 
# the commands are 2 stage - with the command, the the system
# asking if that command was said, then a affirmation or negation
# e.g. - you say "on", the system asks if you said "on", you say "yes"
# the system turns the Arduino pin 13 on
 
# create services
python = Runtime.createAndStart("python", "Python")
mouth = Runtime.createAndStart("mouth", "Speech")
arduino = Runtime.createAndStart("arduino", "Arduino")
ear = Runtime.createAndStart("ear", "Sphinx")
 
# connect mrl to the arduino - change the port on your system
arduino.connect("COM4")
 
# attaching the mouth to the ear
# prevents listening when speaking
# which causes an undesired feedback loop
ear.attach(mouth)
 
# for anything recognized we'll send that data back to python to be printed
# in the python tab
# ear.addListener("recognized", "python", "heard", String.class);
# add a "on" -> arduino.digitalWrite(13, 1)  - turn's pin 13 on
ear.addCommand("on", arduino.getName(), "digitalWrite", 13, 1)
# add a "off" -> arduino.digitalWrite(13, 0)  - turn's pin 13 off
ear.addCommand("off", arduino.getName(), "digitalWrite", 13, 0)
 
arduino.pinMode(13,1)
 
# add confirmations and negations - this makes any command a 2 part commit
# where first you say the command then mrl asks you if that is what you said
# after recognition
ear.addComfirmations("yes","correct","yeah","ya") 
ear.addNegations("no","wrong","nope","nah")
 
# begin listening
ear.startListening()
 
def heard(data):
  # print it
  print "heard ", data         
 
 

 

From JSGF to Grammar Graph

After the JSGF grammar is read in, it is converted to a graph of words representing the grammar. Lets call this the grammar graph. It is from this grammar graph that the eventual search structure used for speech recognition is built. Below, we show the grammar graphs created from the above JSGF grammars. The nodes "<sil>" means "silence".

 
Figure 3: Grammar graph created from the Hello World grammar.

 

Examples:


#file : service/Sphinx.py edit raw
from java.lang import String
from org.myrobotlab.service import Sphinx
from org.myrobotlab.service import Runtime

# create ear and mouth
ear = Runtime.createAndStart("ear","Sphinx")
mouth = Runtime.createAndStart("mouth","MarySpeech")

# start listening for the words we are interested in
ear.addComfirmations("yes","correct","ya")
ear.addNegations("no","wrong","nope","nah")
#ear.startListening("hello world|happy monkey|go forward|stop|yes|correct|ya|no|wrong|nope|nah")
ear.startListening("hello world|happy monkey|go forward|stop")

ear.addCommand("hello world", "python", "helloWorld")

# set up a message route from the ear --to--> python method "heard"
# ear.addListener("recognized", python.name, "heard");

# ear.addComfirmations("yes","correct","yeah","ya")
# ear.addNegations("no","wrong","nope","nah")

# ear.addCommand("hello world", "python", "helloworld")

# set up a message route from the ear --to--> python method "heard"
# ear.addListener("recognized", python.name, "heard");


# this method is invoked when something is
# recognized by the ear - in this case we
# have the mouth "talk back" the word it recognized
#def heard(phrase):
#      mouth.speak("you said " + phrase)
#      print "heard ", phrase

# prevent infinite loop - this will suppress the
# recognition when speaking - default behavior
# when attaching an ear to a mouth :)

ear.attach(mouth)

def helloworld(phrase):
    print "This is hello world in python."
    print phrase