Advertisement
Robin_Lery

Sphinx speech recognition program in java

Mar 10th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.40 KB | None | 0 0
  1. /*
  2.  * Copyright 1999-2004 Carnegie Mellon University.
  3.  * Portions Copyright 2004 Sun Microsystems, Inc.
  4.  * Portions Copyright 2004 Mitsubishi Electric Research Laboratories.
  5.  * All Rights Reserved.  Use is subject to license terms.
  6.  *
  7.  * See the file "license.terms" for information on usage and
  8.  * redistribution of this file in the Sphinx package, and for a DISCLAIMER OF ALL
  9.  * WARRANTIES.
  10.  *
  11.  * i have just modified the actual HelloWorld.java file by adding few lines of code.
  12.  * make sure you have a look on the grammar file that i have uploaded.
  13.  * all the best.
  14.  *
  15.  */
  16.  
  17.  
  18. package demo.sphinx.helloworld;
  19.  
  20. import edu.cmu.sphinx.frontend.util.Microphone;
  21. import edu.cmu.sphinx.recognizer.Recognizer;
  22. import edu.cmu.sphinx.result.Result;
  23. import edu.cmu.sphinx.util.props.ConfigurationManager;
  24. import edu.cmu.sphinx.util.props.PropertyException;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.net.URL;
  29. import java.awt.AWTException;
  30.  
  31. //For keyboard inputs
  32. import java.awt.Robot;
  33. import java.awt.event.InputEvent;
  34. import java.awt.event.KeyEvent;
  35.  
  36. //For network related
  37. import java.net.HttpURLConnection;
  38.  
  39. ////For text to speech
  40. //import com.sun.speech.freetts.Voice;
  41. //import com.sun.speech.freetts.VoiceManager;
  42.  
  43. /**
  44.  * A simple HelloWorld demo showing a simple speech application
  45.  * built using Sphinx-4. This application uses the Sphinx-4 endpointer,
  46.  * which automatically segments incoming audio into utterances and silences.
  47.  */
  48. public class HelloWorld {
  49.  
  50.     /**
  51.      * Main method for running the HelloWorld demo.
  52.      */
  53.     static int i=1;
  54.     static String resultText;
  55.    
  56.    
  57.    
  58.     public static void main(String[] args) {
  59. //      Voice v;
  60. //      VoiceManager vm = VoiceManager.getInstance();
  61. //      v=vm.getVoice("kevin16");
  62. //      v.allocate();
  63.        
  64.         try {
  65.             URL url;
  66.            if (args.length > 0) {
  67.                url = new File(args[0]).toURI().toURL();
  68.             } else {
  69.                 url = HelloWorld.class.getResource("helloworld.config.xml");
  70.             }
  71.  
  72.             System.out.println("Loading...");
  73.  
  74.             ConfigurationManager cm = new ConfigurationManager(url);
  75.  
  76.         Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
  77.         Microphone microphone = (Microphone) cm.lookup("microphone");
  78.  
  79.  
  80.             /* allocate the resource necessary for the recognizer */
  81.             recognizer.allocate();
  82.  
  83.             /* the microphone will keep recording until the program exits */
  84.        
  85.             if (microphone.startRecording())
  86.             {  
  87.                 System.out.println("Say: (Command | Program| Browser | Bluetooth |  Device Manager |Power Options |Cal | Control | Player |task manager | Windows Security Center)");
  88.                 System.out.println("Say: ( open word | open phot oshop|open Access|start Excel|start nero |start fire wall| open Pad |open Paint)");
  89.             while (true)
  90.             {
  91.                
  92.                
  93.             System.out.println("Start speaking. Press Ctrl-C to quit.\n");
  94.  
  95.                     /*
  96.                      * This method will return when the end of speech
  97.                      * is reached. Note that the endpointer will determine
  98.                      * the end of speech.
  99.                      */
  100.          
  101.          
  102.             Result result = recognizer.recognize();
  103.             if (result != null)
  104.             {
  105.                     System.out.println("Enter your choise"+ "\n");
  106.              resultText = result.getBestFinalResultNoFiller();
  107.             System.out.println("You said: " + resultText + "\n");
  108.            
  109. //          Applications*********************************************
  110.             if(resultText.equalsIgnoreCase("Command Prompt"))
  111.             {
  112.                 try{
  113.                     Runtime.getRuntime().exec("cmd /c start cmd");
  114.                     }
  115.                 catch(Exception er){   
  116.                 }
  117.             }
  118.             if (resultText.equalsIgnoreCase("close command prompt"))
  119.             {
  120.                 try{
  121.                     Runtime.getRuntime().exec("cmd /c start taskkill /im cmd.exe /f");
  122.              
  123.                 }catch(Exception ae){}
  124.             }
  125.             else  if (resultText.equalsIgnoreCase("Program list"))
  126.             {
  127.                 try{
  128.                     Runtime.getRuntime().exec("cmd /c start appwiz.cpl");
  129.                 }catch(Exception ae){}
  130.             }          
  131.             if (resultText.equalsIgnoreCase("open calculator"))
  132.             {
  133.                 try{
  134.                     Runtime.getRuntime().exec("cmd /c start calc");
  135.                 }catch(Exception ae){}
  136.             }
  137.             else if(resultText.equalsIgnoreCase("close calculator"))
  138.             {    try{
  139.                 Runtime.getRuntime().exec("cmd /c start taskkill /im calc.exe /f");
  140.                 }catch(Exception ae){}
  141.             }          
  142.             else if(resultText.equalsIgnoreCase("open windows paint"))
  143.             {    try{
  144.                 Runtime.getRuntime().exec("cmd /c start mspaint");
  145.                 }catch(Exception ae){}
  146.             }
  147.             else if(resultText.equalsIgnoreCase("close windows paint"))
  148.             {    try{
  149.                 Runtime.getRuntime().exec("cmd /c start taskkill /im mspaint.exe /f");
  150.                 }catch(Exception ae){}
  151.             }
  152.             else if (resultText.equalsIgnoreCase("open word pad"))
  153.             {
  154.                 try{
  155.                     Runtime.getRuntime().exec("cmd /c  write");
  156.                 }catch(Exception ae){}
  157.              }
  158.             else if (resultText.equalsIgnoreCase("close word pad"))
  159.             {
  160.                 try{
  161.                     Runtime.getRuntime().exec("cmd /c  start taskkill /im wordpad.exe /f");
  162.                 }catch(Exception ae){}
  163.              }
  164.             else if (resultText.equalsIgnoreCase("open note pad"))
  165.             {
  166.                 try{
  167.                     Runtime.getRuntime().exec("cmd /c start notepad");
  168.                 }catch(Exception ae){}
  169.              }
  170.             else if (resultText.equalsIgnoreCase("close note pad"))
  171.             {
  172.                 try{
  173.                     Runtime.getRuntime().exec("cmd /c start taskkill /im notepad.exe /f");
  174.                 }catch(Exception ae){}
  175.              }         
  176.             else if (resultText.equalsIgnoreCase("open Player"))
  177.             {
  178.                 try{
  179.                     Runtime.getRuntime().exec("cmd /c start wmplayer");
  180.                 }catch(Exception ae){}
  181.             }
  182.             else if (resultText.equalsIgnoreCase("open microsoft word"))
  183.             {
  184.                 try{
  185.                     Runtime.getRuntime().exec("cmd /c start winword");
  186.                // System.out.println("inside");
  187.                 }catch(Exception ae){}
  188.              }
  189.             else if (resultText.equalsIgnoreCase("close microsoft word"))
  190.             {
  191.                 try{
  192.                     Runtime.getRuntime().exec("cmd /c start taskkill /im winword.exe /f");
  193.                // System.out.println("inside");
  194.                 }catch(Exception ae){}
  195.              }         
  196.             else if (resultText.equalsIgnoreCase("open microsoft Excel"))
  197.             {
  198.                 try{
  199.                     Runtime.getRuntime().exec("cmd /c start excel");
  200.                // System.out.println("inside");
  201.                 }catch(Exception ae){}
  202.              }else if (resultText.equalsIgnoreCase("close mircosoft Excel"))
  203.                 {
  204.                     try{
  205.                         Runtime.getRuntime().exec("cmd /c start taskkill /im excel.exe /f");
  206.                    // System.out.println("inside");
  207.                     }catch(Exception ae){}
  208.                  }
  209.  
  210.            
  211.  
  212.            
  213. //          Utility Action Command
  214.             if (resultText.equalsIgnoreCase("show Power Options"))
  215.             {
  216.                 try{
  217.                     Runtime.getRuntime().exec("cmd /c powercfg.cpl");
  218.              
  219.                 }catch(Exception ae){}
  220.             }
  221.             if (resultText.equalsIgnoreCase("show Blue tooth"))
  222.             {
  223.                 try{
  224.                     Runtime.getRuntime().exec("cmd /c fsquirt");
  225.              
  226.                 }catch(Exception ae){}
  227.             }
  228.             if (resultText.equalsIgnoreCase("show Windows Security Center"))
  229.             {
  230.                 try{
  231.                     Runtime.getRuntime().exec("cmd /c wscui.cpl");
  232.              
  233.                 }catch(Exception ae){}
  234.             }          
  235.              else if (resultText.equalsIgnoreCase("show Control Panel"))
  236.                 {
  237.                     try{
  238.                         Runtime.getRuntime().exec("cmd /c control");
  239.                     }catch(Exception ae){}
  240.                 }          
  241.            
  242.             else if(resultText.equalsIgnoreCase("show task manager"))
  243.                 {
  244.                 try{
  245.                     Runtime.getRuntime().exec("cmd /c start taskmgr.exe");
  246.                    // System.out.println("inside");
  247.                     }catch(Exception ae){}
  248.                 }
  249.              else if(resultText.equalsIgnoreCase("hide task manager"))
  250.                 {    
  251.                  try{
  252.                      Runtime.getRuntime().exec("cmd /c start taskkill /im taskmgr.exe /f");
  253.                    // System.out.println("inside");
  254.                     }catch(Exception ae){}
  255.                 }
  256.             else if (resultText.equalsIgnoreCase("show Device Manager"))
  257.                 {
  258.                     try{
  259.                         Runtime.getRuntime().exec("cmd /c start devmgmt.msc");
  260.                    // System.out.println("inside");
  261.                     }catch(Exception ae){}
  262.                  }
  263.        
  264.             }
  265.            
  266. //          NETWORk related action commands
  267.             if(resultText.equalsIgnoreCase("site face book"))
  268.             {
  269.                 try{
  270.                     Runtime.getRuntime().exec("cmd /c start chrome www.facebook.com");
  271.                    // System.out.println("inside");
  272.                     }
  273.                 catch(Exception ae){}
  274.             }
  275.             if(resultText.equalsIgnoreCase("site mail"))
  276.             {
  277.                 try{
  278.                     Runtime.getRuntime().exec("cmd /c start chrome www.gmail.com");
  279.                    // System.out.println("inside");
  280.                     }
  281.                 catch(Exception ae){}
  282.             }
  283.             if(resultText.equalsIgnoreCase("site go girl"))
  284.             {
  285.                 try{
  286.                     Runtime.getRuntime().exec("cmd /c start chrome www.google.com");
  287.                    // System.out.println("inside");
  288.                     }
  289.                 catch(Exception ae){}
  290.             }
  291.             if(resultText.equalsIgnoreCase("site news"))
  292.             {
  293.                 try{
  294.                     Runtime.getRuntime().exec("cmd /c start chrome www.news.yahoo.com/");
  295.                    // System.out.println("inside");
  296.                     }
  297.                 catch(Exception ae){}
  298.             }
  299.             if(resultText.equalsIgnoreCase("close brew sir"))
  300.             {
  301.                 try{
  302.                     Runtime.getRuntime().exec("cmd /c taskkill /f /im chrome.exe");
  303.                    // System.out.println("inside");
  304.                     }
  305.                 catch(Exception ae){}
  306.             }
  307.            
  308.            
  309.            
  310. //          Simulate action commands by importing the robot class above
  311.            
  312.             if(resultText.equalsIgnoreCase("scroll up"))
  313.             {
  314.                 try {
  315.                     Robot r = new Robot();
  316.                     r.keyPress(KeyEvent.VK_UP);
  317.                     r.delay(500);
  318.                     r.keyPress(KeyEvent.VK_UP);
  319.                     r.delay(500);
  320.                     r.keyPress(KeyEvent.VK_UP);
  321.                 } catch (AWTException e) {
  322.                     e.printStackTrace();
  323.                 }
  324.             }
  325.             if(resultText.equalsIgnoreCase("scroll down"))
  326.             {
  327.                 try {
  328.                     Robot r = new Robot();
  329.                     r.keyPress(KeyEvent.VK_DOWN);
  330.                     r.delay(500);
  331.                     r.keyPress(KeyEvent.VK_DOWN);
  332.                     r.delay(500);
  333.                     r.keyPress(KeyEvent.VK_DOWN);
  334.                 } catch (AWTException e) {
  335.                     e.printStackTrace();
  336.                 }
  337.             }
  338.            
  339.  
  340. //          System Command
  341.            
  342.             if(resultText.equalsIgnoreCase("go to sleep"))
  343.             {
  344.                 try{
  345.                     Runtime.getRuntime().exec("cmd /c start /wait scrnsave.scr /s");
  346.                 }
  347.                 catch(Exception ae)
  348.                 {}
  349.             }
  350.             if(resultText.equalsIgnoreCase("wake up"))
  351.             {
  352.                 try {
  353.                     Robot r = new Robot();
  354.                     r.mouseMove(100, 100);    
  355.                     r.mousePress(InputEvent.BUTTON1_MASK);
  356.                     r.mouseRelease(InputEvent.BUTTON1_MASK);
  357.                 } catch (AWTException e) {
  358.                     e.printStackTrace();
  359.                 }
  360.             }
  361.             if(resultText.equalsIgnoreCase("check internet connection"))
  362.             {
  363.                 try {
  364.                     try {
  365.                         URL url1 = new URL("http://www.google.com");
  366.                         System.out.println(url1.getHost());
  367.                         HttpURLConnection con = (HttpURLConnection) url1
  368.                                 .openConnection();
  369.                         con.connect();
  370.                         if (con.getResponseCode() == 200){
  371.                             System.out.println("Connection established!!");
  372.                         }
  373.                     } catch (Exception exception) {
  374.                         System.out.println("No Connection");
  375.                     }
  376.                 } catch (Exception e) {
  377.                     e.printStackTrace();
  378.                 }
  379.             }
  380.            
  381.            
  382.            
  383. //          Program Action Command ABOUT
  384.             if(resultText.equalsIgnoreCase("type your name"))
  385.             {
  386.                 try{
  387.                     Runtime.getRuntime().exec("cmd /c start notepad");
  388.                    // System.out.println("inside");
  389.                     } catch (Exception ae){
  390.                     }
  391.                 try {
  392.                     Robot r = new Robot();
  393.                     r.keyPress(KeyEvent.VK_S);
  394.                     r.delay(500);
  395.                     r.keyPress(KeyEvent.VK_P);
  396.                     r.delay(500);
  397.                     r.keyPress(KeyEvent.VK_E);
  398.                     r.delay(500);
  399.                     r.keyPress(KeyEvent.VK_E);
  400.                     r.delay(500);
  401.                     r.keyPress(KeyEvent.VK_C);
  402.                     r.delay(500);
  403.                     r.keyPress(KeyEvent.VK_H);
  404.                     r.delay(500);
  405.                     r.keyPress(KeyEvent.VK_SPACE);
  406.                     r.delay(500);
  407.                     r.keyPress(KeyEvent.VK_R);
  408.                     r.delay(500);
  409.                     r.keyPress(KeyEvent.VK_E);
  410.                     r.delay(500);
  411.                     r.keyPress(KeyEvent.VK_C);
  412.                     r.delay(500);
  413.                     r.keyPress(KeyEvent.VK_O);
  414.                     r.delay(500);
  415.                     r.keyPress(KeyEvent.VK_G);
  416.                     r.delay(500);
  417.                     r.keyPress(KeyEvent.VK_N);
  418.                     r.delay(500);
  419.                     r.keyPress(KeyEvent.VK_I);
  420.                     r.delay(500);
  421.                     r.keyPress(KeyEvent.VK_T);
  422.                     r.delay(500);
  423.                     r.keyPress(KeyEvent.VK_I);
  424.                     r.delay(500);
  425.                     r.keyPress(KeyEvent.VK_O);
  426.                     r.delay(500);
  427.                     r.keyPress(KeyEvent.VK_N);
  428.                     r.delay(500);
  429.                     r.keyPress(KeyEvent.VK_SPACE);
  430.                     r.delay(500);
  431.                     r.keyPress(KeyEvent.VK_S);
  432.                     r.delay(500);
  433.                     r.keyPress(KeyEvent.VK_Y);
  434.                     r.delay(500);
  435.                     r.keyPress(KeyEvent.VK_S);
  436.                     r.delay(500);
  437.                     r.keyPress(KeyEvent.VK_T);
  438.                     r.delay(500);
  439.                     r.keyPress(KeyEvent.VK_E);
  440.                     r.delay(500);
  441.                     r.keyPress(KeyEvent.VK_M);
  442.                 } catch (AWTException e) {
  443.                     e.printStackTrace();
  444.                 }
  445.             }
  446.             if(resultText.equalsIgnoreCase("show commands"))
  447.             {
  448.                 try{
  449.                     Runtime.getRuntime().exec("cmd /c commands.txt");
  450.                 }
  451.                 catch(Exception ae)
  452.                 {}
  453.             }
  454.             else if(resultText.equalsIgnoreCase("recognition stop"))
  455.             {
  456.                 try{
  457.                    
  458.                     //recognizer.wait();
  459.                     System.out.println("See you later!");
  460.                     System.exit(0);}
  461.                 catch(Exception estop ){}
  462.             }                      
  463.            
  464.             else
  465.             {
  466.                         System.out.println("I can't hear what you said.\n");
  467.             }
  468.        }
  469.        }
  470.         else
  471.         {
  472.             System.out.println("Cannot start microphone.");
  473.             recognizer.deallocate();
  474.             System.exit(1);
  475.         }
  476.            
  477.         } catch (IOException e) {
  478.             System.err.println("Problem when loading HelloWorld: " + e);
  479.             e.printStackTrace();
  480.         } catch (PropertyException e) {
  481.             System.err.println("Problem configuring HelloWorld: " + e);
  482.             e.printStackTrace();
  483.         } catch (InstantiationException e) {
  484.             System.err.println("Problem creating HelloWorld: " + e);
  485.             e.printStackTrace();
  486.         }
  487.        
  488.     }
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement