Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 1999-2004 Carnegie Mellon University.
- * Portions Copyright 2004 Sun Microsystems, Inc.
- * Portions Copyright 2004 Mitsubishi Electric Research Laboratories.
- * All Rights Reserved. Use is subject to license terms.
- *
- * See the file "license.terms" for information on usage and
- * redistribution of this file in the Sphinx package, and for a DISCLAIMER OF ALL
- * WARRANTIES.
- *
- * i have just modified the actual HelloWorld.java file by adding few lines of code.
- * make sure you have a look on the grammar file that i have uploaded.
- * all the best.
- *
- */
- package demo.sphinx.helloworld;
- 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;
- import edu.cmu.sphinx.util.props.PropertyException;
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- import java.awt.AWTException;
- //For keyboard inputs
- import java.awt.Robot;
- import java.awt.event.InputEvent;
- import java.awt.event.KeyEvent;
- //For network related
- import java.net.HttpURLConnection;
- ////For text to speech
- //import com.sun.speech.freetts.Voice;
- //import com.sun.speech.freetts.VoiceManager;
- /**
- * A simple HelloWorld demo showing a simple speech application
- * built using Sphinx-4. This application uses the Sphinx-4 endpointer,
- * which automatically segments incoming audio into utterances and silences.
- */
- public class HelloWorld {
- /**
- * Main method for running the HelloWorld demo.
- */
- static int i=1;
- static String resultText;
- public static void main(String[] args) {
- // Voice v;
- // VoiceManager vm = VoiceManager.getInstance();
- // v=vm.getVoice("kevin16");
- // v.allocate();
- try {
- URL url;
- if (args.length > 0) {
- url = new File(args[0]).toURI().toURL();
- } else {
- url = HelloWorld.class.getResource("helloworld.config.xml");
- }
- System.out.println("Loading...");
- ConfigurationManager cm = new ConfigurationManager(url);
- Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
- Microphone microphone = (Microphone) cm.lookup("microphone");
- /* allocate the resource necessary for the recognizer */
- recognizer.allocate();
- /* the microphone will keep recording until the program exits */
- if (microphone.startRecording())
- {
- System.out.println("Say: (Command | Program| Browser | Bluetooth | Device Manager |Power Options |Cal | Control | Player |task manager | Windows Security Center)");
- System.out.println("Say: ( open word | open phot oshop|open Access|start Excel|start nero |start fire wall| open Pad |open Paint)");
- while (true)
- {
- System.out.println("Start speaking. Press Ctrl-C to quit.\n");
- /*
- * This method will return when the end of speech
- * is reached. Note that the endpointer will determine
- * the end of speech.
- */
- Result result = recognizer.recognize();
- if (result != null)
- {
- System.out.println("Enter your choise"+ "\n");
- resultText = result.getBestFinalResultNoFiller();
- System.out.println("You said: " + resultText + "\n");
- // Applications*********************************************
- if(resultText.equalsIgnoreCase("Command Prompt"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start cmd");
- }
- catch(Exception er){
- }
- }
- if (resultText.equalsIgnoreCase("close command prompt"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im cmd.exe /f");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("Program list"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start appwiz.cpl");
- }catch(Exception ae){}
- }
- if (resultText.equalsIgnoreCase("open calculator"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start calc");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("close calculator"))
- { try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im calc.exe /f");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("open windows paint"))
- { try{
- Runtime.getRuntime().exec("cmd /c start mspaint");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("close windows paint"))
- { try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im mspaint.exe /f");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open word pad"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c write");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("close word pad"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im wordpad.exe /f");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open note pad"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start notepad");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("close note pad"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im notepad.exe /f");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open Player"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start wmplayer");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open microsoft word"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start winword");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("close microsoft word"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im winword.exe /f");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open microsoft Excel"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start excel");
- // System.out.println("inside");
- }catch(Exception ae){}
- }else if (resultText.equalsIgnoreCase("close mircosoft Excel"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im excel.exe /f");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- // Utility Action Command
- if (resultText.equalsIgnoreCase("show Power Options"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c powercfg.cpl");
- }catch(Exception ae){}
- }
- if (resultText.equalsIgnoreCase("show Blue tooth"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c fsquirt");
- }catch(Exception ae){}
- }
- if (resultText.equalsIgnoreCase("show Windows Security Center"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c wscui.cpl");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("show Control Panel"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c control");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("show task manager"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start taskmgr.exe");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("hide task manager"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im taskmgr.exe /f");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("show Device Manager"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start devmgmt.msc");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- }
- // NETWORk related action commands
- if(resultText.equalsIgnoreCase("site face book"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start chrome www.facebook.com");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- if(resultText.equalsIgnoreCase("site mail"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start chrome www.gmail.com");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- if(resultText.equalsIgnoreCase("site go girl"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start chrome www.google.com");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- if(resultText.equalsIgnoreCase("site news"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start chrome www.news.yahoo.com/");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- if(resultText.equalsIgnoreCase("close brew sir"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c taskkill /f /im chrome.exe");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- // Simulate action commands by importing the robot class above
- if(resultText.equalsIgnoreCase("scroll up"))
- {
- try {
- Robot r = new Robot();
- r.keyPress(KeyEvent.VK_UP);
- r.delay(500);
- r.keyPress(KeyEvent.VK_UP);
- r.delay(500);
- r.keyPress(KeyEvent.VK_UP);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- if(resultText.equalsIgnoreCase("scroll down"))
- {
- try {
- Robot r = new Robot();
- r.keyPress(KeyEvent.VK_DOWN);
- r.delay(500);
- r.keyPress(KeyEvent.VK_DOWN);
- r.delay(500);
- r.keyPress(KeyEvent.VK_DOWN);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- // System Command
- if(resultText.equalsIgnoreCase("go to sleep"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start /wait scrnsave.scr /s");
- }
- catch(Exception ae)
- {}
- }
- if(resultText.equalsIgnoreCase("wake up"))
- {
- try {
- Robot r = new Robot();
- r.mouseMove(100, 100);
- r.mousePress(InputEvent.BUTTON1_MASK);
- r.mouseRelease(InputEvent.BUTTON1_MASK);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- if(resultText.equalsIgnoreCase("check internet connection"))
- {
- try {
- try {
- URL url1 = new URL("http://www.google.com");
- System.out.println(url1.getHost());
- HttpURLConnection con = (HttpURLConnection) url1
- .openConnection();
- con.connect();
- if (con.getResponseCode() == 200){
- System.out.println("Connection established!!");
- }
- } catch (Exception exception) {
- System.out.println("No Connection");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // Program Action Command ABOUT
- if(resultText.equalsIgnoreCase("type your name"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start notepad");
- // System.out.println("inside");
- } catch (Exception ae){
- }
- try {
- Robot r = new Robot();
- r.keyPress(KeyEvent.VK_S);
- r.delay(500);
- r.keyPress(KeyEvent.VK_P);
- r.delay(500);
- r.keyPress(KeyEvent.VK_E);
- r.delay(500);
- r.keyPress(KeyEvent.VK_E);
- r.delay(500);
- r.keyPress(KeyEvent.VK_C);
- r.delay(500);
- r.keyPress(KeyEvent.VK_H);
- r.delay(500);
- r.keyPress(KeyEvent.VK_SPACE);
- r.delay(500);
- r.keyPress(KeyEvent.VK_R);
- r.delay(500);
- r.keyPress(KeyEvent.VK_E);
- r.delay(500);
- r.keyPress(KeyEvent.VK_C);
- r.delay(500);
- r.keyPress(KeyEvent.VK_O);
- r.delay(500);
- r.keyPress(KeyEvent.VK_G);
- r.delay(500);
- r.keyPress(KeyEvent.VK_N);
- r.delay(500);
- r.keyPress(KeyEvent.VK_I);
- r.delay(500);
- r.keyPress(KeyEvent.VK_T);
- r.delay(500);
- r.keyPress(KeyEvent.VK_I);
- r.delay(500);
- r.keyPress(KeyEvent.VK_O);
- r.delay(500);
- r.keyPress(KeyEvent.VK_N);
- r.delay(500);
- r.keyPress(KeyEvent.VK_SPACE);
- r.delay(500);
- r.keyPress(KeyEvent.VK_S);
- r.delay(500);
- r.keyPress(KeyEvent.VK_Y);
- r.delay(500);
- r.keyPress(KeyEvent.VK_S);
- r.delay(500);
- r.keyPress(KeyEvent.VK_T);
- r.delay(500);
- r.keyPress(KeyEvent.VK_E);
- r.delay(500);
- r.keyPress(KeyEvent.VK_M);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- if(resultText.equalsIgnoreCase("show commands"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c commands.txt");
- }
- catch(Exception ae)
- {}
- }
- else if(resultText.equalsIgnoreCase("recognition stop"))
- {
- try{
- //recognizer.wait();
- System.out.println("See you later!");
- System.exit(0);}
- catch(Exception estop ){}
- }
- else
- {
- System.out.println("I can't hear what you said.\n");
- }
- }
- }
- else
- {
- System.out.println("Cannot start microphone.");
- recognizer.deallocate();
- System.exit(1);
- }
- } catch (IOException e) {
- System.err.println("Problem when loading HelloWorld: " + e);
- e.printStackTrace();
- } catch (PropertyException e) {
- System.err.println("Problem configuring HelloWorld: " + e);
- e.printStackTrace();
- } catch (InstantiationException e) {
- System.err.println("Problem creating HelloWorld: " + e);
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement