📄 baserecognizer.java
字号:
if (testEngineState(FOCUS_ON)) return; long[] states = setEngineState(FOCUS_OFF, FOCUS_ON); postFocusGained(states[0], states[1]); notifyGrammarActivation(); } /** * Release speech focus for this Recognizer from the underlying speech * recognition system. */ public void releaseFocus() throws EngineStateError { checkEngineState(DEALLOCATED | DEALLOCATING_RESOURCES); if (testEngineState(FOCUS_OFF)) return; long[] states = setEngineState(FOCUS_ON, FOCUS_OFF); postFocusLost(states[0], states[1]); notifyGrammarActivation(); } /** * Request notification of Result events from the Recognizer. * From javax.speech.recognition.Recognizer. * @param listener the listener to add. */ public void addResultListener(ResultListener listener) { if (!resultListeners.contains(listener)) { resultListeners.addElement(listener); } } /** * Remove a ResultListener from the list of ResultListeners. * From javax.speech.recognition.Recognizer. * @param listener the listener to remove. */ public void removeResultListener(ResultListener listener) { resultListeners.removeElement(listener); } /** * Get the RecognizerProperties of this Recognizer. * From javax.speech.recognition.Recognizer. */ public RecognizerProperties getRecognizerProperties() { return (RecognizerProperties) getEngineProperties(); } /** * NOT IMPLEMENTED YET. * Get the object that manages the speakers of the Recognizer. * From javax.speech.recognition.Recognizer. */ public SpeakerManager getSpeakerManager() { return null; } /** * Create a new gramar by reading in a grammar stored in a * vendor-specific format. Since BaseGrammar is serializable we just * use read/write object to store/restore grammars. * From javax.speech.recognition.Recognizer. */ public Grammar readVendorGrammar(InputStream input) throws VendorDataException, IOException, EngineStateError { checkEngineState(DEALLOCATED | DEALLOCATING_RESOURCES); BaseGrammar G = null; try { ObjectInputStream p = new ObjectInputStream(input); G = (BaseGrammar)p.readObject(); } catch (Exception e) { RecognizerUtilities.debugMessageOut("ERROR: readVendorGrammar: " + e); } return (Grammar) G; } /** * Create a new grammar by reading in a grammar stored in a * vendor-specific format. Since BaseGrammar is serializable we just * use read/write object to store/restore grammars. * From javax.speech.recognition.Recognizer. */ public void writeVendorGrammar(OutputStream output, Grammar gram) throws IOException, EngineStateError { checkEngineState(DEALLOCATED | DEALLOCATING_RESOURCES); ObjectOutputStream p = new ObjectOutputStream(output); p.writeObject(gram); } /** * Read a Result from a stream in a vendor-specific format. * Since BaseResult is serializable we just * use read/write object to store/restore grammars. * From javax.speech.recognition.Recognizer. */ public Result readVendorResult(InputStream output) throws VendorDataException, IOException, EngineStateError { checkEngineState(DEALLOCATED | DEALLOCATING_RESOURCES); BaseResult res = null; try { ObjectInputStream p = new ObjectInputStream(output); res = (BaseResult)p.readObject(); } catch (Exception e) { RecognizerUtilities.debugMessageOut("ERROR: readVendorResult: " + e); } return (FinalResult) res; } /** * Store a Result to a stream in a vendor-specific format. * Since BaseResult is serializable we just * use read/write object to store/restore grammars. * From javax.speech.recognition.Recognizer. */ public void writeVendorResult(OutputStream output, Result result) throws IOException, ResultStateError, EngineStateError { checkEngineState(DEALLOCATED | DEALLOCATING_RESOURCES); ObjectOutputStream p = new ObjectOutputStream(output); p.writeObject(result); }//////////////////////// End Recognizer Methods////////////////////////////////////////////// Begin utility methods for sending ResultEvents////////////////////// /** * Utility function to generate AUDIO_RELEASED event and post it * to the event queue. Eventually fireAudioReleased will be called * by dispatchSpeechEvent as a result of this action. */ public void postAudioReleased(Result result) { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(result, ResultEvent.AUDIO_RELEASED)); } /** * Utility function to send a AUDIO_RELEASED event to all result * listeners. */ public void fireAudioReleased(ResultEvent event) { Enumeration E; if (resultListeners != null) { E = resultListeners.elements(); while (E.hasMoreElements()) { ResultListener rl = (ResultListener) E.nextElement(); rl.audioReleased(event); } } } /** * Utility function to generate GRAMMAR_FINALIZED event and post it * to the event queue. Eventually fireGrammarFinalized will be called * by dispatchSpeechEvent as a result of this action. */ public void postGrammarFinalized(Result result) { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(result, ResultEvent.GRAMMAR_FINALIZED)); } /** * Utility function to send a GRAMMAR_FINALIZED event to all result * listeners. */ public void fireGrammarFinalized(ResultEvent event) { Enumeration E; if (resultListeners != null) { E = resultListeners.elements(); while (E.hasMoreElements()) { ResultListener rl = (ResultListener) E.nextElement(); rl.grammarFinalized(event); } } } /** * Utility function to generate RESULT_ACCEPTED event and post it * to the event queue. Eventually fireResultAccepted will be called * by dispatchSpeechEvent as a result of this action. */ public void postResultAccepted(Result result) { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(result, ResultEvent.RESULT_ACCEPTED)); } /** * Utility function to send a RESULT_ACCEPTED event to all result * listeners. */ public void fireResultAccepted(ResultEvent event) { Enumeration E; if (resultListeners != null) { E = resultListeners.elements(); while (E.hasMoreElements()) { ResultListener rl = (ResultListener) E.nextElement(); rl.resultAccepted(event); } } } /** * Utility function to generate RESULT_CREATED event and post it * to the event queue. Eventually fireResultCreated will be called * by dispatchSpeechEvent as a result of this action. */ public void postResultCreated(Result result) { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(result, ResultEvent.RESULT_CREATED)); } /** * Utility function to send a RESULT_CREATED event to all result * listeners. */ public void fireResultCreated(ResultEvent event) { Enumeration E; if (resultListeners != null) { E = resultListeners.elements(); while (E.hasMoreElements()) { ResultListener rl = (ResultListener) E.nextElement(); rl.resultCreated(event); } } } /** * Utility function to generate RESULT_REJECTED event and post it * to the event queue. Eventually fireResultRejected will be called * by dispatchSpeechEvent as a result of this action. */ public void postResultRejected(Result result) { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(result, ResultEvent.RESULT_REJECTED)); } /** * Utility function to send a RESULT_REJECTED event to all result * listeners. */ public void fireResultRejected(ResultEvent event) { Enumeration E; if (resultListeners != null) { E = resultListeners.elements(); while (E.hasMoreElements()) { ResultListener rl = (ResultListener) E.nextElement(); rl.resultRejected(event); } } } /** * Utility function to generate RESULT_UPDATED event and post it * to the event queue. Eventually fireResultUpdated will be called * by dispatchSpeechEvent as a result of this action. */ public void postResultUpdated(Result result) { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(result, ResultEvent.RESULT_UPDATED)); } /** * Utility function to send a RESULT_UPDATED event to all result * listeners. */ public void fireResultUpdated(ResultEvent event) { Enumeration E; if (resultListeners != null) { E = resultListeners.elements(); while (E.hasMoreElements()) { ResultListener rl = (ResultListener) E.nextElement(); rl.resultUpdated(event); } } } /** * Utility function to generate TRAINING_INFO_RELEASED event and post it * to the event queue. Eventually fireTrainingInfoReleased will be called * by dispatchSpeechEvent as a result of this action. */ public void postTrainingInfoReleased(Result result) { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(result, ResultEvent.TRAINING_INFO_RELEASED)); } /** * Utility function to send a TRAINING_INFO_RELEASED event to all result * listeners. */ public void fireTrainingInfoReleased(ResultEvent event) { Enumeration E; if (resultListeners != null) { E = resultListeners.elements(); while (E.hasMoreElements()) { ResultListener rl = (ResultListener) E.nextElement(); rl.trainingInfoReleased(event); } } }//////////////////////// End utility methods for sending ResultEvents////////////////////////////////////////////// NON-JSAPI METHODS////////////////////// /** * Add a grammar to the grammar list. */ protected void storeGrammar(RuleGrammar G) { if (caseSensitiveGrammarNames) { grammarList.put(G.getName(),G); } else { grammarList.put(G.getName().toLowerCase(),G); } } /** * Retrieve a grammar from the grammar list. */ protected RuleGrammar retrieveGrammar(String name) { if (caseSensitiveGrammarNames) { return (RuleGrammar) grammarList.get(name); } else { return (RuleGrammar) grammarList.get(name.toLowerCase()); } } /** * NOT JSAPI. * In the mean time since the above method is not implemented we support * loading JSGF grammars from an InputStream which is non-standard */ public RuleGrammar loadJSGF(InputStream JSGFinput) throws GrammarException, IOException, EngineStateError { checkEngineState(DEALLOCATED | DEALLOCATING_RESOURCES); RuleGrammar G = JSGFParser.newGrammarFromJSGF(JSGFinput, (Recognizer)this); if ((G != null) && (G.getName() != null)) { storeGrammar(G); } return G; } /** * Let listeners know the recognizer rejected something. NOT JSAPI. */ public void rejectUtterance() { BaseResult R = new BaseResult(null); this.postResultCreated(R); R.postResultRejected(); this.postResultRejected(R); } /** * Let listeners know the recognizer recognized/finalized/accepted * result. NOT JSAPI. */ public void notifyResult(String gname, String words) { BaseRuleGrammar G = (BaseRuleGrammar) getRuleGrammar(gname); if (G == null) { RecognizerUtilities.debugMessageOut( "ERROR: UNKNOWN GRAMMAR FOR RESULT " + gname); return; } BaseResult R = new BaseResult(G,words); G.postResultCreated(R); this.postResultCreated(R); R.postGrammarFinalized(); G.postGrammarFinalized(R); this.postGrammarFinalized(R); R.setResultState(Result.ACCEPTED); R.postResultAccepted(); G.postResultAccepted(R); this.postResultAccepted(R); }//////////////////////// Begin utility methods for sending RecognizerEvents.////////////////////// /** * Utility function to generate CHANGES_COMMITTED event and post it * to the event queue. Eventually fireChangesCommitted will be called * by dispatchSpeechEvent as a result of this action. */ protected void postChangesCommitted(long oldState, long newState, GrammarException ge) { SpeechEventUtilities.postSpeechEvent( this, new RecognizerEvent(this, RecognizerEvent.CHANGES_COMMITTED, oldState, newState, ge)); } /** * Utility function to send a CHANGES_COMMITTED event to all engine * listeners. */ public void fireChangesCommitted(RecognizerEvent event) { if (engineListeners == null) { return; } Enumeration E = engineListeners.elements(); while (E.hasMoreElements()) { EngineListener el = (EngineListener) E.nextElement(); if (el instanceof RecognizerListener) { ((RecognizerListener) el).changesCommitted(event); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -