📄 baserulegrammar.java
字号:
RuleName tmp = (RuleName)(matches.elementAt(i)); if (i > 0) { b.append(" and "); } b.append(tmp); } throw new GrammarException(b.toString(), null); } else { // Return successfully return (RuleName)(matches.elementAt(0)); } } /** * Import all rules or a specified rule from another grammar. * From javax.speech.recognition.RuleGrammar. * @param importName the name of the rule(s) to import. */ public void addImport(RuleName importName) { if (!imports.contains(importName)) { imports.addElement(importName); grammarChanged=true; } } /** * Remove an import. * From javax.speech.recognition.RuleGrammar. * @param importName the name of the rule(s) to remove. */ public void removeImport(RuleName importName) throws IllegalArgumentException { if (imports.contains(importName)) { imports.removeElement(importName); grammarChanged=true; } } /** * List the current imports. * From javax.speech.recognition.RuleGrammar. */ public RuleName[] listImports() { if (imports == null) { return new RuleName[0]; } RuleName rn[] = new RuleName[imports.size()]; int i=0; Enumeration e = imports.elements(); while (e.hasMoreElements()) { RuleName r = (RuleName) e.nextElement(); rn[i++] = r; } return rn; } /** * Parse the text string against the specified rule. * Uses the RuleParser class. * From javax.speech.recognition.RuleGrammar. * @param text the text to parse. * @param ruleName the name of rule to use for parsing. */ public RuleParse parse(String text, String ruleName) throws GrammarException { if (ruleName != null) { ruleName = stripRuleName(ruleName); } return RuleParser.parse(text,myRec,this,ruleName); } /** * Parse the tokens string against the specified rule. * Uses the RuleParser class. * From javax.speech.recognition.RuleGrammar. * @param tokens the tokens to parse. * @param ruleName the name of rule to use for parsing. */ public RuleParse parse(String tokens[], String ruleName) throws GrammarException { if (ruleName != null) { ruleName = stripRuleName(ruleName); } return RuleParser.parse(tokens,myRec,this,ruleName); } /** * Parse the nth best result of a FinalRuleResult against the specified * rule. * Uses the RuleParser class. * From javax.speech.recognition.RuleGrammar. * @param r the FinalRuleResult. * @param nBest the nth best result to use. * @param ruleName the name of rule to use for parsing. */ public RuleParse parse(FinalRuleResult r, int nBest, String ruleName) throws GrammarException { // Some JSAPI implementations we run into are not JSAPI compliant, // so try a few alternatives ResultToken rt[] = r.getAlternativeTokens(nBest); if (rt != null || (rt = r.getBestTokens()) != null) { String tokens[] = new String[rt.length]; for (int i=0; i<rt.length; i++) { tokens[i] = rt[i].getSpokenText(); } return parse(tokens, ruleName); } else { return parse(r.toString(), ruleName); } } /** * NOT IMPLEMENTED YET. * Return a String containing the specification for this Grammar. */ public String toString() { throw new RuntimeException( "toString not yet implemented."); }//////////////////////// End RuleGrammar Methods////////////////////////////////////////////// NON-JSAPI METHODS////////////////////// /** * Marked a rulename as changed. */ protected void setRuleChanged(String ruleName, boolean x) { GRule r = (GRule)rules.get(stripRuleName(ruleName)); if (r == null) { return; } r.hasChanged=x; grammarChanged=true; } /** * Resolve and linkup all rule references contained in all rules. */ protected void resolveAllRules() throws GrammarException { StringBuffer b = new StringBuffer(); // First make sure that all imports are resolvable RuleName imports[] = listImports(); if (imports != null) { for(int i=0; i<imports.length; i++) { String gname = imports[i].getFullGrammarName(); RuleGrammar GI = myRec.getRuleGrammar(gname); if (GI == null) { b.append("Undefined grammar " + gname + " imported in " + getName() + "\n"); } } } if (b.length() > 0) { throw new GrammarException(b.toString(), null); } String rn[] = listRuleNames(); for(int i=0; i<rn.length; i++) { resolveRule(getRuleInternal(rn[i])); } } /** * Resolve the given rule. */ protected void resolveRule(Rule r) throws GrammarException { if (r instanceof RuleToken) { return; } if (r instanceof RuleAlternatives) { RuleAlternatives ra = (RuleAlternatives)r; Rule array[] = ra.getRules(); for(int i=0; i<array.length; i++) { resolveRule(array[i]); } return; } if (r instanceof RuleSequence) { RuleSequence rs = (RuleSequence)r; Rule array[] = rs.getRules(); for(int i=0; i<array.length; i++) { resolveRule(array[i]); } return; } if (r instanceof RuleCount) { RuleCount rc = (RuleCount) r; resolveRule(rc.getRule()); return; } if (r instanceof RuleTag) { RuleTag rt = (RuleTag) r; resolveRule(rt.getRule()); return; } if (r instanceof BaseRuleName) { BaseRuleName rn = (BaseRuleName) r; RuleName resolved = resolve(rn); if (resolved == null) { throw new GrammarException( "Unresolvable rulename in grammar " + getName() + ": " + rn.toString(), null); } else { //[[[WDW - This forces all rule names to be fully resolved. //This should be changed.]]] rn.resolvedRuleName = resolved.getRuleName(); rn.setRuleName(resolved.getRuleName()); return; } } throw new GrammarException("Unknown rule type", null); } /** * Add a rule as imported. */ protected void setRuleIsImported(String simpleName) { if (!importedRules.contains(simpleName)) { importedRules.addElement(simpleName); } } /** * See if a rule has changed. */ protected boolean isRuleChanged(String ruleName) { GRule r = (GRule)rules.get(stripRuleName(ruleName)); if (r == null) { return false; } return r.hasChanged; } /** * Storage for documentation comments for rules for jsgfdoc. */ Properties ruleDocComments = new Properties(); /** * Storage for documentation comments for imports for jsgfdoc. */ Properties importDocComments = new Properties(); /** * Storage for documentation comments for the grammar for jsgfdoc. */ String grammarDocComment = null; /** * Add a new RuleGrammar comment. */ void addRuleDocComment(String rname, String comment) { ruleDocComments.put(rname, comment); } /** * Retrieve a RuleGrammar comment. */ public String getRuleDocComment(String rname) { return ruleDocComments.getProperty(rname, null); } /** * Add a new import comment. */ void addImportDocComment(RuleName imp, String comment) { importDocComments.put(imp.toString(), comment); } /** * Retrieve an import comment. */ public String getImportDocComment(RuleName imp) { return importDocComments.getProperty(imp.toString(), null); } /** * Add the Grammar comment. */ void addGrammarDocComment(String comment) { grammarDocComment = comment; } /** * Retrieve the Grammar comment. */ public String getGrammarDocComment() { return grammarDocComment; } /** * annote the specified rule with its line number from the * source file. This is used for debugging purposes. */ void setSourceLine(String rname,int i) { GRule r = (GRule)rules.get(stripRuleName(rname)); /* TODO : exception */ if (r == null) { return; } r.lineno=i; } /** * get the line number in the source file for the specified rule name */ public int getSourceLine(String rname) { GRule r = (GRule)rules.get(stripRuleName(rname)); /* TODO : exception */ if (r == null) { return 0; } return r.lineno; } /** * add a sample sentence to the list of sample sentences that * go with the specified rule */ void addSampleSentence(String rname,String sample) { GRule r = (GRule)rules.get(stripRuleName(rname)); /* TODO : exception */ if (r == null) { return; } r.samples.addElement(sample); } /** * add a sample sentence to the list of sample sentences that * go with the specified rule */ void setSampleSentences(String rname,Vector samps) { GRule r = (GRule)rules.get(stripRuleName(rname)); /* TODO : exception */ if (r == null) { return; } r.samples = samps; } /** * get the list of sample sentences that * go with the specified rule */ public Vector getSampleSentences(String rname) { GRule r = (GRule)rules.get(stripRuleName(rname)); if (r == null) { return null; } return r.samples; } /** * Non-JSAPI method * returns a unique 32-bit id for the specified rule */ public int getRuleID(String rname) { GRule r = (GRule)rules.get(stripRuleName(rname)); if (r == null) { return -1; } return r.hashCode(); } /** * Remove a pair of leading/trailing angle brackets <>. */ protected String stripRuleName(String n) { if (n.startsWith("<") && n.endsWith(">")) { return n.substring(1, n.length()-1); } return n; } /** * Internal class used to hold rules and their attributes */ class GRule implements Serializable { String ruleName; Rule rule; boolean isPublic; boolean isEnabled; boolean hasChanged; Vector samples = new Vector(); int lineno = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -