📄 baseresult.java
字号:
////////////////////////////////////////////// Begin FinalDictationResult Methods////////////////////// /** * NOT IMPLEMENTED YET. * Get a set of alternative token guesses for a single known token or * sequence of tokens. */ public ResultToken[][] getAlternativeTokens(ResultToken from, ResultToken to, int max) throws ResultStateError, IllegalArgumentException { checkResultState(UNFINALIZED); if (!(grammar instanceof DictationGrammar)) { throw new ResultStateError("Result is not a FinalDicationResult"); } return null; }//////////////////////// End FinalDictationResult 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() { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(this, 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() { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(this, 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() { setResultState(Result.ACCEPTED); SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(this, 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() { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(this, 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() { setResultState(Result.REJECTED); SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(this, 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() { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(this, 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() { SpeechEventUtilities.postSpeechEvent( this, new ResultEvent(this, 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////////////////////// /** * Concatenate the best tokens in the Result. */ public String toString() { StringBuffer sb = new StringBuffer(getBestToken(0).getWrittenText()); for (int i = 1; i < numTokens(); i++) sb.append(" " + getBestToken(i).getWrittenText()); return sb.toString(); } protected class BaseResultToken implements ResultToken { String token; BaseResultToken(String t) { token = t; } public Result getResult() { return null; } public int getAttachmentHint() { return 0; } public int getCapitalizationHint() { return 0; } public String getWrittenText() { return token; } public String getSpokenText() { return token; } public long getStartTime() { return 0; } public long getEndTime() { return 0; } public int getSpacingHint() { return ResultToken.SEPARATE; } } /** * Utility function to set the result state. */ public void setResultState(int state) { this.state = state; } /** * If the result is in the given state, throw a ResultStateError. */ protected void checkResultState(int state) throws ResultStateError { if (getResultState() == state) { throw new EngineStateError("Invalid ResultState: " + getResultState()); } } /** * Dispatch a SpeechEvent. This is a method from SpeechEventDispatcher. * The dispatcher should notify all listeners of the speech event * from this method. */ public void dispatchSpeechEvent(SpeechEvent event) { switch (event.getId()) { case ResultEvent.AUDIO_RELEASED: fireAudioReleased((ResultEvent) event); break; case ResultEvent.GRAMMAR_FINALIZED: fireGrammarFinalized((ResultEvent) event); break; case ResultEvent.RESULT_ACCEPTED: fireResultAccepted((ResultEvent) event); break; case ResultEvent.RESULT_CREATED: fireResultCreated((ResultEvent) event); break; case ResultEvent.RESULT_REJECTED: fireResultRejected((ResultEvent) event); break; case ResultEvent.RESULT_UPDATED: fireResultUpdated((ResultEvent) event); break; case ResultEvent.TRAINING_INFO_RELEASED: fireTrainingInfoReleased((ResultEvent) event); break; } } /** * Set the grammar that goes with this Result. NOT JSAPI. */ public void setGrammar(Grammar g) { grammar = g; } /** * Try to set the Grammar and tokens of this result. NOT JSAPI. */ public boolean tryTokens(Grammar G, String S) { if ((S == null) || (G == null)) { return false; } if (G instanceof RuleGrammar) { try { RuleParse rp = ((RuleGrammar) G).parse(S, null); if (rp != null) { grammar = G; tags = rp.getTags(); ruleName = rp.getRuleName().getSimpleRuleName(); StringTokenizer st = new StringTokenizer(S); nTokens = st.countTokens(); int i = 0; theText = new String[nTokens]; while (st.hasMoreTokens()) { theText[i++]=st.nextToken(); } return true; } } catch (GrammarException e) { } } return false; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -