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!


25 Responses

  1. 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.

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

  3. 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

  4. 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?????

  5. 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

  6. 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.


    Puneet Kalra

    1. u mean v will be able to recognize 1000 words???.. then wat will be the grammar… do v need explicitly specify all the 1000 tokens that is present in the dictionary??.. please answer this…

  7. Pingback: Another Title
  8. Hello,puneet i have created a media player in java .I want to use voice recognition using sphinx to stop and play the songs using the voice .I have seen you video you have used eclipse but i am using command prompt which does’nt give same result .iam also using apache ant to build sphinx pgm through ant command but when i run the pgm i am getting the following error

    Buildfile:build.xml does’nt exist.

    can you tell me the procedure of running the voice recognition pgm through command prompt.I will be greatly thankful to you please reply fast .it’s very urgent.

  9. 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,

  10. Hi Puneet, i need to know how to implement dictation grammar(i mean adding words to grammar file dymnamically[which are not present in grammar file]),Can you please post source code for that)

    with framework which you are going to use for thateg:sphinx4,freetts..

  11. Hii Puneet, Currently i m working on project that should recognize whatever I say..So can u please help me with its the grammar file..

    that is..whether i have to specify each and every word in grammar file or whether i can to load/import english dictionary or something like that…

    Thanks in Advance,

    Ram Mulchandani..

  12. My sphinx4 helloworld demo working fine but i wanna add some other features like is i say open calculator that will open the calculator . I have also written grammar file but How can I add my .gram file into hello world..? When i run the program it runs helloworld demo …please help me out

  13. Hi Punnet,

    I have one query lets say I have sentences like “Summary Balance Sheet”, “Summary Variance by Dimension” etc. Now I want to make a grammar in a way so that when I speak “summary” then it should return all the occurrences of summary, which means the result should be both the text in quotes.

  14. Hi,
    I have used sphinx4-5prealpha-src for “Full sentence voice recognition” where i have transcriber.jar which is a language model but when i run that .class file i could able to see
    “one zero zero zero one
    nine oh two one oh
    zero one eight zero three” and i couldn’t understand this
    what if i want to use other english words how do i need to do it?

  15. Hi,
    i want to add some question in grammar like,
    1. Who are the Top 5 Doctors with highest Laboratory claims

    2. How many members have claimed more than 3 times

    3. How many claims are greater than 3500 AED

    4. What are the Top 10 medicines utilized/prescribed

    5. What is the Claim amount for the month of Jan’17

    please help me.

    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *