"I Think, I Conceptualize, I Analyze, I Design, and I Create." ~ Puneet Kalra

Cognitive Robotics Research Centre Of University Of Wales, Newport Puneet Kalra

Home Studies Research Projects Tutorials Portfolio

Puneet Kalra - www.puneetk.com - Socializing Robots

Basics of Java Speech Grammar Format ( JSGF )

March 12th, 2010 by Puneet Kalra Leave a reply »

Hello Everyone,

This post is my response to those 4 help requests that i received in last few days. This one is going to be very basic and essentials of Java Speech Grammar Format ( JSGF ).

Every single file defines a single grammar only. Each grammar contains two parts:
The grammar header and the grammar body.

Grammar header format : #JSGF version char-encoding locale;

“#JSGF” is required and “version char-encoding locale;” is optional.

Grammar header example : #JSGF V1.0; & #JSGF V1.0 ISO8859-5 en;



After declaring Grammar Header, We need to specify the Grammar name.

Grammar name format : grammar grammarName;

This is a mandatory line. Else javax.speech.recognition.GrammarException will be thrown. Not only on Grammar Name, you will get GrammarExpection If you made any kind of mistake in grammar file.

Grammar Name Example : grammar helloWorld;



Once you are done with above part, you next step will be defining Grammar body.
The grammar body defines rules. you can define a rule only once. If you declare same rule twice then it will overwritten.

Rule Definition Format : public <ruleName> = ruleExpansion;

“public” is optional and remaining part is mandatory.
The rule expansion defines how the rule may be spoken. It is a logical combination of tokens (text that may be spoken) and references to other rules.

Rule Example : public <greet> = Hello;

The rule <greet> refers to a single token Hello. So to say rule <greet>, User must say word “Hello”.

A simple rule expansion can refer to one or more tokens or rules.

public <greet> = Hello;
public <completeGreet> = <greet> World;

Now, rule <completeGreet> refers to rule <greet> and token World. To say rule <completeGreet>, User must say “Hello World”.

Lets complete a simple “Hello World” grammar file.

#JSGF V1.0;

grammar simpleExample;

public <greet> = Hello;
public <completeGreet> = <greet> World;

This grammar file will allow you SR application to recognize 2 sentences. “Hello” and “Hello World”.

Now lets play a little bit with rule expansions.

Alternatives : “|”

public <greet> = Hello | Hey | Hi;

To say rule <greet>, User must say “Hello” or “Hey” or “Hi” But ONLY one of these three words.

Parentheses : “( )”

public <greet> = Hello ( World | User | Friend );
public <command> = ( Open | Close ) ( Door | Window );

To say rule <greet>, User must say “Hello World” or “Hello User” or “Hello Friend”.
And, to say rule <command>, User must say “Open Door” or “Open Window” or “Close Door” or “Close Window”.

Optional Grouping : “[ ]”

public <greet> = [ Hello ] World;

To say rule <greet>, User must say “Hello World” or “World”. As “Hello” is defined inside the Optional Grouping. So user may say it or not but “World” is mandatory.

Kleene Star : “*”

public <greet> = ( Hello | Hey | Hi )* World;

Any group or expansion followed by asterisk symbol indicates that it may be spoken zero or more times. For example “Hey Hello World” or “World” or “Hello Hello Hello World”.

Plus Operator : “+”

public <greet> = ( Hello | Hey | Hi )+ World;

A Plus Operator works same as “Kleene Star”, The only thing that makes difference is any group or expansion followed by plus symbol indicates that it may be spoken one ( NOT ZERO ) or more times.

You can also add comments in grammar file.

// One line comment
/* Multiple lines comment*/
/**
* Documentation comment
* @author Puneet Kalra
*/


Hope this tutorial clears all your doubts on JSGF.

More updates to come soon!
Regards,

Advertisement

13 Responses

  1. Thanks for this great post – I will be sure to check out your blog more often

  2. Gravura says:

    Thank you for the work you have put into your nice blog. We will bookmark to your blog because it is very informational. We love the site and will come back to see your new posts.

  3. Rahul says:

    HI puneet i need more info on JSGF …

  4. elisabeth says:

    hello, thanks for the post but i would like to ask soe thing after writting the grammer where do i load it

  5. shashank says:

    this blog is really proving worth to every one who wanna work with speech recognition thanx for such hard work and providing it here

  6. arun says:

    I am facing problem in allocating heap in eclipse and in you tutorial resolution is not good so not understood allocation o heap 2nd video tutorial can you explain more

  7. Abhinay saxena says:

    hi,
    i am an engg student i have created speech recog for fedora I have to increase my lib and grammar.
    so i want to know does it require any wave file for recognition or just this grammar can do?????

  8. Hi Puneet,
    First of all, great work by you.
    I was able to run the hello world application. However, now I am trying to develop a more complicated application and it should use the runtime dictionary (i.e. it should recognize whatever I say)
    Do you have any idea of how to do it?

    Thanks in Advance,
    Varun Vijaywargi

  9. Puneet Kalra says:

    Hey Varun,

    I’m afraid there’s no such way to do that with Sphinx4. But yea, if your speech will be limited to few thousand words then of course yes!

    You will need to work on Acoustic Model, Language Model and Sphinx config as well.

    Regards,
    Puneet Kalra

  10. Amit says:

    For Speech Recognition I have decided to use sphinx4, I am trying to run the demo Transcriber.jar provided with sphinx. That doesn’t work when I give some other file as input.

    How do I proceed ? Where do I get the language model for US English that can be used with Sphinx4 ?

    Any blog / tutorial will be helpful,

Leave a Reply

Please note : I will help you only if you will show some efforts! Don't expect replies to simple queries, You can easily find answers for them through searching.

I'm a student and I work as well. So i might reply late here. If you have something important to discuss Or stuck somewhere in your project. Please post on my Facebook page for a quick response.