public static void main(String args[]) { WebAsrServer Recognizer; Log log = new Log(); log.setLevelByName("PATlog_level"); try { Recognizer = new WebAsrServer("PATnamerecognizer", PATport_recognizer); Log.info("Waiting to connect to PATnameinterpreter"); boolean connected = false; int waiting = 0; while (!connected && waiting < 1000) { sleep(waitTime); String result = Recognizer.executeReply(connect1); if (result.startsWith("OK:")) { connected = true; break; } else { waiting++; System.out.println("Waiting ... " + waiting); continue; } } if (!connected) { Log.severe("Waiting for too long, terminating ..."); return; } else { Log.info("Connected to PATnameinterpreter"); Log.info("Waiting to establish who credentials"); } sleep(waitTime * 4); boolean established = false; waiting = 0; while (!established && waiting < 100) { sleep(waitTime); String result = Recognizer.executeReply(who1); if (result.startsWith("OK:")) { established = true; break; } else { waiting++; Log.info("Establishing credentials ... " + waiting); continue; } } if (!established) { Log.severe("Establishing did not work, terminating ..."); return; } else { Log.info("Established credentials with PATnameinterpreter"); Log.info("Processing recognition results"); } sleep(waitTime * 4); // loop in SpeechInput processSpeech(Recognizer); System.out.println("Terminating ..."); System.exit(1); } catch (Exception e) { e.printStackTrace(); } }
String[] gettags(String words) { StringTokenizer st = new StringTokenizer(words, terms, true); int n = st.countTokens(); String seq[] = new String[n]; for (int i = 0; i < n; i++) { seq[i] = st.nextToken(); } if (seq[0].trim().length() == 0) { Log.severe("First word blank in " + words); } return seq; }
public void run() { try { while (true) { Socket link = server.accept(); String id = Name + "_" + idCount; idCount++; Responder r = new Responder(); Session ias = new Session(id, this, link, r); clients.add(ias); Log.info(Name + ": Added client " + ias.sid); } } catch (Exception e) { Log.severe(Name + ": run " + e.toString()); } }
/** * Create a server with a specific id, which listens at the designated port. * * @param name Name of the agent * @param port Listen to this port for connection requests */ public Server(String name, int port) { Name = name; listenPort = port; idCount = 0; try { server = new ServerSocket(listenPort); start(); serverId = server.getInetAddress().getHostName() + "_" + port; Log.info("started server " + serverId + " name " + Name); clients = new Vector<Session>(); Valid = true; } catch (Exception e) { Log.severe(Name + e.toString()); // Valid will be false } }
/** Run the agent's thread, making connections on requests to create a speech session */ public void run() { try { while (true) { Socket link = server.accept(); int count = getIdCount(); String id = Name + "_" + getIdCount(); setIdCount(count + 1); SphinxResponder r = new SphinxResponder(); SpeechSession ias = new SpeechSession(id, this, link, r); addSession(ias); Log.info("Added client " + ias.getSid()); } } catch (Exception e) { Log.severe(Name + e.toString()); } }
static void processSpeech(SphinxServer server) { ConfigurationManager cm = new ConfigurationManager(PATrecognizerTest.class.getResource(config)); // allocate the recognizer Log.info("Loading..."); Recognizer recognizer = (Recognizer) cm.lookup("recognizer"); recognizer.allocate(); // start the microphone or exit if the programm if this is not possible Microphone microphone = (Microphone) cm.lookup("microphone"); if (!microphone.startRecording()) { Log.severe("Cannot start microphone."); recognizer.deallocate(); System.exit(1); } System.out.println("Sample questions are in PATlm_training_file"); // loop the recognition until the programm exits. while (true) { System.out.println("Start speaking. Press Ctrl-C to quit.\n"); Result result = recognizer.recognize(); if (result != null) { String resultText = result.getBestResultNoFiller(); System.out.println("You said: " + resultText + '\n'); String name = server.getServerId(); String message = work1 + "\"" + resultText + "\"}"; server.execute(message); try { Thread.sleep(4000); } catch (Exception e) { System.out.println(e.toString()); break; } } else { System.out.println("I can't hear what you said."); } } }
public void execute(String command) { try { Vector<String> tokens = new Vector<String>(); StringTokenizer st = new StringTokenizer(command); while (st.hasMoreTokens()) { String token = st.nextToken(); tokens.add(token); } int ntok = tokens.size(); if (command.startsWith("connect")) { if (ntok != 3) { Log.warning("Syntax: connect host port"); return; } String host = tokens.elementAt(1); int port = Integer.parseInt(tokens.elementAt(2)); Socket link = new Socket(host, port); int count = sessionCount(); String id = Name + "_" + count; SphinxResponder rtest = new SphinxResponder(); SpeechSession ias = new SpeechSession(id, this, link, rtest); rtest.setOwner(ias); addSession(ias); Log.fine("Made connection client id " + id); } else if (command.startsWith("disconnect")) { if (ntok != 2) { Log.warning("Sytax: disconnect sessionid"); return; } String id = tokens.elementAt(1); Session ias = findSession(id); if (ias == null) { Log.warning("No session with id " + id); return; } // send a terminate message to the session Log.info("disconnecting from " + id); String req = "{action: JviaTerminate, from: " + getId() + ", to: " + ias.getSid(); req += ", message: JviaTerminate}"; ias.outbuffer = req; sleep(waitTime); ias.terminate(); // then terminate it } else if (command.startsWith("send")) { // write it to the outstream of the session? if (ntok < 3) { Log.warning("Sytax: send sessionid message"); return; } String id = tokens.elementAt(1); Session ias = findSession(id); if (ias == null) { Log.warning("No session with id " + id); return; } StringBuffer sb = new StringBuffer(); sb.append(tokens.elementAt(2)); for (int i = 3; i < ntok; i++) { sb.append(' '); sb.append(tokens.elementAt(i)); } String message = new String(sb); // String req = "{action: send, from: "+getId ()+", to: "+ias.getSid (); // req += ", message: "+message+"}"; // send a terminate message to the session ias.outbuffer = message; Log.fine("sending \"" + message + "\" to " + id); if (message.equals(TestResponder.terminateRequest)) { sleep(waitTime); ias.terminate(); } } else if (command.equals("terminate")) { // terminate all clients, then stop Vector<Session> clients = getClients(); for (int i = 0; i < clients.size(); i++) { Session ias = clients.elementAt(i); String req = "{action: JviaTerminate, from: " + getId() + ", to: " + ias.getSid(); req += ", message: JviaTerminate}"; ias.outbuffer = req; sleep(waitTime); Log.info("Terminating " + ias.getSid()); ias.terminate(); } server.close(); interrupt(); return; } else { Log.warning("illegal command: " + command); } } catch (Exception e) { Log.severe("sphinxServer:execute " + e.toString()); } }