Saturday, March 17, 2012

Basics of Java Speech Grammar Format

 Basics of Java Speech Grammar Format (JSGF)

Hello Everyone,
This post 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 let’s 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 Dipayan Dev*/

Monday, March 12, 2012

Speech Recognition By Java :: A sphinx4 implementation

Speech recognition is one of the challenging areas in computer science, a lot of pattern recognition methodology tried to resolve a good way and higher percentage of recognition. I tried lots of technology., etc.  But ultimately found Java the most simpler and easy way. 

One of the best ways to be use is Hidden Markov Model :

"The process of speech recognition is to find the best possible sequence of words (or units) that will fit the given input speech. It is a search problem, and in the case of HMM-based recognizers, a graph search problem. The graph represents all possible sequences of phonemes in the entire language of the task under consideration. The graph is typically composed of the HMMs of sound units concatenated in a guided manner, as specified by the grammar of the task."

There are a lot of Java Based Speech Recognition Engines, you can find them here:

In this post I will select one of the famous open source Java speech recognition software that I used in my final year project.

1) Download the software:

Here is the download link:

2) Create new Java project:

Add to class-path 3 jars from the lib folder: jsapi.jar (if not exist jsapi.exe will extract it on windows or will extract it) ,sphinx4.jar (APIs Jar),WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar (this is the jar containing the speech)
3) Create a new class to write the logic on it: e.g.
Let the package name dipayan_java

4) Add the file dict.gram to the same package: dipayan_java
It should looks like:
#JSGF V1.0;
grammar hello; 
public = (Good morning | Hello | Hi | Welcome) ( Dipayan | Paul | Philip | Rita | Will );

The structure is to write the alternative words between () and separated by |.

The grammar specs in this URL:
A quick tutorial you can find in my blog !

 5) Add the config file myconfig.xml to the same package: dipayan_java
You can get the file structure from the jar in bin folder HelloWorld.jar in the location "HelloWorld.jar\edu\cmu\sphinx\demo\helloworld\helloworld.config.xml"

You need to edit the following entry:
<component name="jsgfGrammar" type="edu.cmu.sphinx.jsgf.JSGFGrammar">
<property name="dictionary" value="dictionary"/>
<property name="grammarLocation"
<property name="grammarName" value="dict"/>
<property name="logMath" value="logMath"/>

6) Write the following code in the created class:

import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;

public static void main(String args[]){
ConfigurationManager cm = new ConfigurationManager(ListenToMe.class.getResource("myconfig.xml"));
Recognizer recognizer = (Recognizer)cm.lookup("recognizer");
Microphone microphone = (Microphone)cm.lookup("microphone");
System.out.println("Cannot start microphone.");
System.out.println("Say: (Good morning | Hello | Hi | Welcome) (Dipayan | Paul | Philip | Rita | Will )");
do {
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
Result result = recognizer.recognize();
if(result != null) {
String resultText = result.getBestFinalResultNoFiller();
System.out.println((new StringBuilder()).append("You said: ").append(resultText).append('\n').toString());
} else {
System.out.println("I can't hear what you said.\n");
} while(true);

For more details about the usage you can refer to this URL:

NOTE :: I did this project on ecllipse . If you want to do it on the same, while running the program,you will find a OutOfMemory exception. This is because , the available Virtual Memory is not enough for Sphinx4.

The thing you have to do, is just increase the heap memory of ecllipse . Go to Virtual memory arguement ,  type    " -Xmx256m  "

Thanks :)
For any query feel free to mail me

Saturday, March 10, 2012

How to Fix a Computer Crash

If your computer crashes unexpectedly, everything comes to a halt. Prior to crashing your computer will likely start freezing up. This is an indication that your computer has to be fixed before it gives up completely. Fixing the computer does not need to involve replacement of the computer with a new one. Your current computer likely just needs a little attention.

Identifying the reason for crashing is essential prior to fixing the computer. The initial step is to reboot your computer. If the rebooting goes smoothly, it is apparent that you are facing a problem with your registry. If you cannot reboot instantly, then rebooting your computer in safe mode and locating a registry cleaner to fix your problem is essential.

Every file present in your registry contains a command or direction for every program and application on your computer. Registry files are the files that give your computer the instructions for what to do next. If these registry files get corrupt or misplaced, your computer fails to understand what to do. In these instances, your computer loses control, eventually leading to a crash.

At this stage the priority is to fix these registry files. In such situations it is suggested that you do not try to fix the registry yourself. Immediately find registry cleaner software and scan your computer to locate any errors. This software will identify all the missing entries or errors, restoring them right away. In many cases, your computer will be back to normal. It is recommended that you run this software once a month for best results.

Another cause of crashes is when new software is installed. If you have recently installed a new program or file, this could also be the source of the problem. You should locate the program and uninstall it. In case you are incapable of booting up your computer in normal mode, you are advised to try booting in safe mode first. If the new program is at fault, removing it should solve the problem. Obviously you would not re-install that program later on.

Most computer crashes occur due to a problem with the computer's registry files. This is the core reason for any crash. So, running registry cleaning software and eliminating all errors is essential to avoid computer crashes in the future. Subsequently, fixing the registry files will keep you from having to ask the question 'how do I fix a computer crash' again.

 For any more queries..feel free to mail me @