Advertisement
Robin_Lery

Logic

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