📄 recognizerutilities.java
字号:
/** * Copyright 1998-2003 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */package com.sun.speech.engine.recognition;import javax.speech.recognition.*;import java.io.PrintStream;import java.io.File;import java.util.Vector;import java.util.Hashtable;import java.util.Enumeration;import java.net.*;/** * Utilities methods. */public class RecognizerUtilities { static public PrintStream errOutput = System.err; /** * Copy all RuleGrammars from recFrom to recTo. */ public static void copyGrammars(Recognizer recFrom, Recognizer recTo) throws GrammarException { RuleGrammar gramFrom[] = recFrom.listRuleGrammars(); for(int i=0; i<gramFrom.length; i++) { String name = gramFrom[i].getName(); RuleGrammar gramTo = recTo.getRuleGrammar(name); // Create a new grammar - if necessary if (gramTo == null) { try { gramTo = recTo.newRuleGrammar(name); } catch(IllegalArgumentException gse) { throw new GrammarException("copyGrammars: " + gse.toString(), null); } } copyGrammar(gramFrom[i],gramTo); } } /** * Copy a grammar from gramFrom to gramTo. */ public static void copyGrammar(RuleGrammar gramFrom, RuleGrammar gramTo) throws GrammarException { // Copy imports RuleName imports[] = gramFrom.listImports(); for(int j=0; j<imports.length; j++) { gramTo.addImport(imports[j]); } // Copy each rule String rnames[] = gramFrom.listRuleNames(); for(int j=0; j<rnames.length; j++) { String ruleName = rnames[j]; boolean isPublic = false; try { isPublic = gramFrom.isRulePublic(ruleName); } catch (IllegalArgumentException nse) { throw new GrammarException(nse.toString(), null); } Rule rule = gramFrom.getRule(ruleName); try { gramTo.setRule(ruleName, rule, isPublic); if (gramFrom.isEnabled(ruleName)) { gramTo.setEnabled(ruleName, true); } } catch (IllegalArgumentException E) { throw new GrammarException(E.toString(), null); } // copy sample sentences if appropriate if ((gramTo instanceof BaseRuleGrammar) && (gramFrom instanceof BaseRuleGrammar)) { Vector a = ((BaseRuleGrammar)gramFrom).getSampleSentences(ruleName); ((BaseRuleGrammar)gramTo).setSampleSentences(ruleName,a); } } } static Hashtable xrefs = null; /** * For a specified recognizer, build a table that for each rule R * lists all rules in all grammars that reference R. * * @see #getRuleNameRefs * @see #getRefsToRuleName */ static public void buildXrefTable(Recognizer rec) { xrefs = new Hashtable(); RuleGrammar[] grams = rec.listRuleGrammars(); if (grams == null) { return; } for (int i=0; i<grams.length; i++) { String[] names = grams[i].listRuleNames(); if (names == null) { continue; } for (int j=0; j<names.length; j++) { // Get the definition of rule name[j] in gram[i] Rule r = grams[i].getRule(names[j]); // Build a fully-qualified RuleName for rule name[j] in gram[i] RuleName rn = new RuleName(grams[i].getName() + "." + names[j]); // Identify all rules referenced in r Vector refs = new Vector(); getRuleNameRefs(r, refs); for (int k=0; k<refs.size(); k++) { RuleName ref = (RuleName)(refs.elementAt(k)); // Get a fully-qualified reference RuleName fullref; try { fullref = ((BaseRuleGrammar)(grams[i])).resolve(ref); } catch(GrammarException e) { fullref = null; } if (fullref != null) { Hashtable h = (Hashtable)(xrefs.get(fullref.toString().intern())); if (h == null) { h = new Hashtable(); } h.put(rn.toString().intern(), "dummy"); xrefs.put(fullref.toString().intern(), h); } else { debugMessageOut("Warning: unresolved rule " + ref.toString() + " in grammar " + grams[i].getName()); } } } } } /** * Return an array of references to rule r in grammar g. */ static public RuleName[] getRefsToRuleName(RuleGrammar g, RuleName r) { // Ensure we have a fully qualified rulename // (that's how the xref table works) r = new RuleName(g.getName() + "." + r.getSimpleRuleName()); if (xrefs == null) { return null; } Hashtable h = (Hashtable)(xrefs.get(r.toString().intern())); if (h == null) { return null; } RuleName[] rulenames = new RuleName[h.size()]; int i=0; for (Enumeration e = h.keys(); e.hasMoreElements();) { String name = (String)(e.nextElement()); rulenames[i++] = new RuleName(name); } return rulenames; } static protected void getRuleNameRefs(Rule r, Vector refs) { if (r instanceof RuleAlternatives || r instanceof RuleSequence) { Rule[] array; if (r instanceof RuleAlternatives) { array = ((RuleAlternatives)r).getRules(); } else { array = ((RuleSequence)r).getRules(); } if (array != null) { for (int i=0; i<array.length; i++) { getRuleNameRefs(array[i], refs); } } } else if (r instanceof RuleTag) { getRuleNameRefs(((RuleTag)r).getRule(), refs); } else if (r instanceof RuleCount) { getRuleNameRefs(((RuleCount)r).getRule(), refs); } else if (r instanceof RuleName) { // Put a copy in the Xref list (avoid side-effects of linking) RuleName tmp = (RuleName)r; refs.addElement(new RuleName(tmp.getRuleName())); } } static private Hashtable oldMessages = new Hashtable(); /** * Print out a message - don't ever repeat it. */ static protected void debugMessageOut(String message) { message = message.intern(); if (oldMessages.get(message) != null) { return; } errOutput.println(message); oldMessages.put(message, "dummy"); } /* * transform grammar to eliminate <NULL> and <VOID> * * NULL and VOID can be removed by transforming the grammar * as follows: * * RuleSequence: * (x y <NULL>) --> (x y) * (x y <VOID>) --> <VOID> * (<NULL>) --> <NULL> * (<VOID>) --> <VOID> * * RuleAlternatives: * (x | y | <NULL>) --> [(x | y)] * (x | y | <VOID>) --> (x | y) * (<NULL>) --> <NULL> * (<VOID>) --> <VOID> * (<VOID> | <NULL>) --> <NULL> * * RuleCount: * [<VOID>] --> <NULL> * <VOID>* --> <NULL> * <VOID>+ --> <VOID> * [<NULL>] --> <NULL> * <NULL>* --> <NULL> * <NULL>+ --> <NULL> * * RuleTag: * <NULL>{tag} --> <NULL> * <VOID>{tag} --> <VOID> * * RuleName: * If a rule has been marked as being void or null then apply the * following: * <RuleName> --> <VOID> * * The steps of transformation are: * * just return the original grammar * * 2. Make a copy of the grammar and Mark all rules as non-void & non-null * * 3. Go through each rule in series and apply the transformations. * * 4. Repeat step 3 until one pass has been made through all the * rules in which no transformations were performed * * 5. Remove NULL and VOID rules from copies grammars * * 6. Remove any empty grammars * * 7. Return the transformed array of grammars * * Because all transforms are some form of a reduction, the process is * guaranteed to complete. * */ static RuleGrammar[] transform(RuleGrammar RG[], boolean eliminateNULL, boolean eliminateVOID) throws GrammarException { //if (!eliminateNULL && !eliminateVOID) return RG; Hashtable ruleinfo = new Hashtable(); RuleGrammar NRG[] = (RuleGrammar[]) RG.clone(); boolean somethingchanged = true; boolean nochange = true; while (somethingchanged) { somethingchanged=false; for(int i=0;i<NRG.length;i++) { RuleGrammar NG = null; String rnames[] = NRG[i].listRuleNames(); for (int j=0; j < rnames.length; j++) { Rule r = NRG[i].getRuleInternal(rnames[j]); Rule nr = transform(r,NRG,eliminateNULL,eliminateVOID); //System.out.println("do: " + rnames[j]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -