RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
Sphinx4.java
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright 2015 RAPP
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16  * Authors: Thanos Kintsakis, Manos Tsardoulias, Aris Thallas
17 
18 ******************************************************************************/
19 
20 import edu.cmu.sphinx.api.Configuration;
21 import edu.cmu.sphinx.api.Context;
22 import edu.cmu.sphinx.api.SpeechResult;
23 import edu.cmu.sphinx.api.StreamSpeechRecognizer;
24 import edu.cmu.sphinx.linguist.flat.FlatLinguist;
25 import edu.cmu.sphinx.recognizer.Recognizer;
26 import edu.cmu.sphinx.util.props.ConfigurationManager;
27 import java.io.BufferedReader;
28 import java.io.BufferedWriter;
29 import java.io.OutputStreamWriter;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.io.PrintWriter;
35 import java.net.URL;
36 import java.net.URLClassLoader;
37 import java.net.Socket;
38 import java.util.HashMap;
39 import java.util.Map;
40 
49 public class Sphinx4 {
50 
51  public static String dictionary_path = "";
52  public static String dictionary_path_prev = "";
53 
54  public static String language_path = "";
55  public static String language_path_prev = "";
56 
57  public static String acoustic_model_path = "";
58  public static String acoustic_model_path_prev = "";
59 
60  public static String grammar_model_file_path = "";
61  public static String grammar_model_file_path_prev = "";
62  public static String grammar_model_folder_path = "";
63  public static String grammar_model_folder_path_prev = "";
64 
65  public static String configuration_model_path = "";
66  public static String configuration_model_path_prev = "";
67 
68  public static Configuration configuration = new Configuration();
69  public static ConfigurationManager cm;
70  public static StreamSpeechRecognizer recognizer;
71 
72  public static BufferedReader bufferRead;
73  public static PrintWriter bufferWrite;
74 
75  public static boolean grammar_enabled = false;
76  public static boolean grammar_enabled_prev = false;
77 
79  public static void updateConfiguration() throws IOException{
80  boolean new_info = false;
83  configuration.setDictionaryPath(dictionary_path);
84  new_info = true;
85  }
88  configuration.setLanguageModelPath(language_path);
89  new_info = true;
90  }
93  configuration.setAcousticModelPath(acoustic_model_path);
94  new_info = true;
95  }
98  configuration.setGrammarName(grammar_model_file_path);
99  new_info = true;
100  }
103  configuration.setGrammarPath(grammar_model_folder_path);
104  new_info = true;
105  }
108  cm = new ConfigurationManager(configuration_model_path);
109  new_info = true;
110  }
113  configuration.setUseGrammar(grammar_enabled);
114  new_info = true;
115  }
116 
117  if(new_info == true){
118  try{
119  recognizer = new StreamSpeechRecognizer(configuration);
120  }
121  catch (IOException e) {
122  e.printStackTrace();
123  bufferWrite.println("CatchedException " + e);
124  }
125  }
126  }
127 
134  public static void main(String[] args) throws IOException {
135 
136  int socketPort;
137  Socket sphinxSocket;
138 
139  if (args.length == 1)
140  {
141  socketPort = Integer.parseInt(args[0]);
142  System.err.println("Socket port: " + socketPort);
143  sphinxSocket = new Socket( "127.0.0.1", socketPort );
144 
145  bufferRead = new BufferedReader(new InputStreamReader(sphinxSocket.getInputStream()));
146  bufferWrite = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sphinxSocket.getOutputStream())), true);
147  }
148  else
149  {
150  System.err.println("CatchedException");
151  System.exit(-1);
152  }
153 
154 
155  String[] tmp;
156  File test_file;
157  // Waiting for input from the Python node
158  while (true) {
159  try {
160  configuration.setUseGrammar(false);
161  String s = bufferRead.readLine();
162  tmp = s.split("#");
163  // Dictionary file setup
164  if(tmp[0].contains("dictionary")){
165  test_file = new File(tmp[1]);
166  if(!test_file.exists())
167  {
168  bufferWrite.println("Fatal error, dictionary file does not exist");
169  }
170  else
171  {
172  dictionary_path = tmp[1];
173  bufferWrite.println("Dictionary set");
174  }
175  }
176  // Language model file setup
177  else if(tmp[0].contains("languageModel")){
178  test_file = new File(tmp[1]);
179  if(!test_file.exists())
180  {
181  bufferWrite.println("Fatal error, language file does not exist");
182  }
183  else
184  {
185  language_path = tmp[1];
186  bufferWrite.println("Language model set");
187  }
188  }
189  // Acoustic model folder setup
190  else if(tmp[0].contains("acousticModel")){
191  configuration.setAcousticModelPath(tmp[1]);
192  bufferWrite.println("Acoustic model set");
193  }
194  // Grammar file and folder setup
195  else if(tmp[0].contains("grammarName")){
196  grammar_model_file_path = tmp[1];
197  grammar_model_folder_path = tmp[2];
198  bufferWrite.println("Grammar model set");
199  }
200  // Configuration file path set
201  else if(tmp[0].contains("configurationPath")){
202  test_file = new File(tmp[1]);
203  if(!test_file.exists())
204  {
205  bufferWrite.println("Fatal error, configuration file does not exist");
206  }
207  else
208  {
209  configuration_model_path = tmp[1];
210  bufferWrite.println("Configuration path set");
211  }
212  }
213  // Perform forced configuration
214  else if(tmp[0].contains("forceConfiguration")){
216  bufferWrite.println("Configuration performed");
217  }
218 
219  // Enable grammar
220  else if(tmp[0].contains("enableGrammar")){
221  grammar_enabled = true;
222  bufferWrite.println("Grammar enabled");
223  }
224  // Disable grammar
225  else if(tmp[0].contains("disableGrammar")){
226  grammar_enabled = false;
227  bufferWrite.println("Grammar disabled");
228  }
229  // Perform audio recognition
230  else if (tmp[0].contains("audioInput")) {
232  test_file = new File(tmp[1]);
233  String temp;
234  if(!test_file.exists())
235  {
236  bufferWrite.println("Fatal error, audio file does not exist");
237  temp = bufferRead.readLine();
238  }
239  else
240  {
241  recognizer.startRecognition(new FileInputStream(tmp[1]));
242  SpeechResult result;
243  while ((result = recognizer.getResult()) != null) {
244  bufferWrite.println("#" + result.getHypothesis());
245  temp = bufferRead.readLine();
246  }
247  recognizer.stopRecognition();
248  bufferWrite.println("stopPython");
249  temp = bufferRead.readLine();
250  }
251  }
252  }
253  catch (IOException | RuntimeException e) {
254  //catch (IOException e) {
255  e.printStackTrace();
256  //bufferWrite.println("#"+e);
257  bufferWrite.println("CatchedException " + e);
258  //bufferWrite.println("stopPython");
259  }
260  }
261  }
262 }
static boolean grammar_enabled
Definition: Sphinx4.java:75
static boolean grammar_enabled_prev
Definition: Sphinx4.java:76
static String configuration_model_path
Definition: Sphinx4.java:65
static String acoustic_model_path_prev
Definition: Sphinx4.java:58
static String grammar_model_folder_path
Definition: Sphinx4.java:62
static StreamSpeechRecognizer recognizer
Definition: Sphinx4.java:70
static PrintWriter bufferWrite
Definition: Sphinx4.java:73
static String grammar_model_file_path_prev
Definition: Sphinx4.java:61
static String grammar_model_file_path
Definition: Sphinx4.java:60
static String language_path_prev
Definition: Sphinx4.java:55
Performs speech recognition employing Sphinx.
Definition: Sphinx4.java:49
static String language_path
Definition: Sphinx4.java:54
static String dictionary_path
Definition: Sphinx4.java:51
static ConfigurationManager cm
Definition: Sphinx4.java:69
static void main(String[] args)
The main function.
Definition: Sphinx4.java:134
static String configuration_model_path_prev
Definition: Sphinx4.java:66
static String acoustic_model_path
Definition: Sphinx4.java:57
static void updateConfiguration()
Updates the Sphinx configuration.
Definition: Sphinx4.java:79
static String grammar_model_folder_path_prev
Definition: Sphinx4.java:63
static Configuration configuration
Definition: Sphinx4.java:68
static BufferedReader bufferRead
Definition: Sphinx4.java:72
static String dictionary_path_prev
Definition: Sphinx4.java:52