Advertisement
Robin_Lery

Logic Program

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