In this article we will see how to create our own Voice Assistant using Java ?
You might have used Google Assistant, Siri OR Amazon Alexa. So that kind of voice assistant we are going to create in Java using Sphinx library.
In other words you can say that how to make a voice assistant in java ?Let's start by adding Sphinx dependencies in our pom.xml file.
<dependency> <groupId>de.sciss</groupId> <artifactId>sphinx4-data</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>net.sf.phat</groupId> <artifactId>sphinx4-core</artifactId> <version>5prealpha</version> </dependency>
Now let's have our own voice assistant to open Google Chrome browser and to close Google Chrome browser. First you need to go to Sphinx Knowledge Base Tool and provide your commands and download dictionary and language model file. That's what you need to pass while doing configuration.
import java.io.IOException; import edu.cmu.sphinx.api.Configuration; import edu.cmu.sphinx.api.LiveSpeechRecognizer; import edu.cmu.sphinx.api.SpeechResult; public class VoiceAssistant { public static void main(String[] st) { Configuration config = new Configuration(); config.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us"); config.setDictionaryPath("src\\main\\resources\\5057.dic"); config.setLanguageModelPath("src\\main\\resources\\5057.lm"); try { LiveSpeechRecognizer speech = new LiveSpeechRecognizer(config); speech.startRecognition(true); SpeechResult speechResult = null; while ((speechResult = speech.getResult()) != null) { String voiceCommand = speechResult.getHypothesis(); System.out.println("Voice Command is " + voiceCommand); if (voiceCommand.equalsIgnoreCase("Open Chrome")) { Runtime.getRuntime().exec("cmd.exe /c start chrome www.infybuzz.com"); } else if (voiceCommand.equalsIgnoreCase("Close Chrome")) { Runtime.getRuntime().exec("cmd.exe /c TASKKILL /IM chrome.exe"); } } } catch (IOException e) { e.printStackTrace(); } } }
0 Comments