⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ruleparser.java

📁 It is the Speech recognition software. It is platform independent. To execute the source code,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * 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 java.util.StringTokenizer;import java.util.Vector;import javax.speech.recognition.Recognizer;import javax.speech.recognition.Rule;import javax.speech.recognition.RuleAlternatives;import javax.speech.recognition.RuleCount;import javax.speech.recognition.RuleGrammar;import javax.speech.recognition.RuleName;import javax.speech.recognition.RuleParse;import javax.speech.recognition.RuleSequence;import javax.speech.recognition.RuleTag;import javax.speech.recognition.RuleToken;/** * Implementation of the parse method(s) on  * javax.speech.recognition.RuleGrammar. * * @version 1.5 10/27/99 16:33:49 */public class RuleParser {    private Recognizer theRec;        /*     * parse a text string against a particular rule from a particluar grammar     * returning a RuleParse data structure is successful and null otherwise     */    public static RuleParse parse(String text,Recognizer R, RuleGrammar G,String ruleName) {        String inputTokens[] = tokenize(text);        return parse(inputTokens,R,G,ruleName);    }        public static RuleParse parse(String inputTokens[],Recognizer R, RuleGrammar G,String ruleName) {        RuleParse rpa[] = mparse(inputTokens,R,G,ruleName);        if (rpa == null) {            return null;        } else {            return rpa[0];        }    }        public static RuleParse[] mparse(String text,Recognizer R, RuleGrammar G,String ruleName) {        String inputTokens[] = tokenize(text);        return mparse(inputTokens,R,G,ruleName);    }        public static RuleParse[] mparse(String inputTokens[],Recognizer R, RuleGrammar G,String ruleName) {        RuleParser rp = new RuleParser();        rp.theRec = R;        String rNames[];        Rule startRule = null;        if (ruleName != null) {            rNames = new String[1];            rNames[0] = ruleName;        } else {            rNames = G.listRuleNames();        }        Vector p = null;        int j = 0;         Vector t = new Vector();        for (j=0; j<rNames.length; j++) {            if ((ruleName == null) && !(G.isEnabled(rNames[j]))) {                continue;            }            startRule = G.getRule(rNames[j]);            if (startRule == null) {                 System.out.println("BAD RULENAME " + rNames[j]);                continue;            }            p = rp.parse(G,startRule,inputTokens,0);            if ((p != null) && (p.size() != 0)) {                for (int i=0; i<p.size(); i++) {                    tokenPos tp = (tokenPos) p.elementAt(i);                    if (tp.getPos() == inputTokens.length) {                        RuleName rn = new RuleName(rNames[j]);                        t.addElement(new RuleParse(rn,(Rule)tp));                    }                }            }        }        if (t.size() == 0) {            return null;        }        RuleParse rpa[] = new RuleParse[t.size()];        t.copyInto(rpa);        return rpa;    }      /*     * parse routine called recursively while traversing the Rule structure     * in a depth first manner. Returns a list of valid parses.     */    private Vector parse(RuleGrammar G, Rule r, String input[], int iPos) {        //System.out.println("PARSE " + r.getClass().getName() + " " + iPos + " " + r);        /*         * RULE REFERENCES         */        if (r instanceof RuleName) {            RuleName rn = (RuleName)r;            String simpleName = rn.getSimpleRuleName();	    if (simpleName.equals("VOID")) return null;	    if (simpleName.equals("NULL")) {	      Vector p = new Vector();	      jsgfRuleParse rp1 = new jsgfRuleParse(rn,RuleName.NULL);	      rp1.setPos(iPos);	      p.addElement(rp1);	      return p;	    }            Rule ruleref = G.getRule(simpleName);            if (ruleref == null) {                String gname = rn.getFullGrammarName();                //System.out.println("gname=" + gname);                if ((gname != null) && (gname.length() > 0)) {                    RuleGrammar RG1 = theRec.getRuleGrammar(gname);                    if (RG1 != null) {                        //System.out.println("simpleName=" + simpleName);                        ruleref = RG1.getRule(simpleName);                        //System.out.println("ruleRef=" + ruleref);                        G = RG1;                    } else {                        System.out.println("ERROR: UNKNOWN GRAMMAR " + gname);                        //Thread.dumpStack();                    }                }                if (ruleref == null) {                    System.out.println("ERROR: UNKNOWN RULE NAME " + rn.getRuleName() + " " + rn);                    //Thread.dumpStack();                    return null;                }            }            Vector p = parse(G,ruleref,input,iPos);            if (p == null) {                return null;            }            Vector t = new Vector();            for (int j=0; j<p.size(); j++) {                tokenPos tp = (tokenPos) p.elementAt(j);                if (tp instanceof emptyToken) {                     t.addElement(tp);                     continue;                 }                Rule ar[] = new Rule[1];                ar[0] = (Rule)p.elementAt(j);                try {                    jsgfRuleParse rulep = new jsgfRuleParse(rn,ar[0]);                    rulep.setPos(tp.getPos());                    t.addElement(rulep);                } catch (IllegalArgumentException e) {                    System.out.println("ERROR " + e);                }            }            return t;        }        /*         * LITERAL TOKENS         */        if (r instanceof RuleToken) {            if (iPos >= input.length) {                return null;            }            RuleToken rt = (RuleToken)r;            //System.out.println(rt.getText() + " ?= " + input[iPos]);            // TODO: what about case sensitivity ??????            String tText = rt.getText().toLowerCase();            if (tText.equals(input[iPos]) || (input[iPos].equals("%"))  || (input[iPos].equals("*"))) {                Vector v = new Vector();                jsgfRuleToken tok = new jsgfRuleToken(rt.getText());                tok.setPos(iPos+1);                v.addElement(tok);                if (input[iPos].equals("*")) {                    jsgfRuleToken tok2 = new jsgfRuleToken(rt.getText());                    tok2.setPos(iPos);                    v.addElement(tok2);                }                return v;            } else {                if (tText.indexOf(' ') < 0) {                    return null;                }                if (!tText.startsWith(input[iPos])) {                    return null;                }                String ta[] = tokenize(tText);                int j = 0;                while (true) {                    if (j >= ta.length) {                        break;                    }                    if (iPos >= input.length) {                        return null;                    }                    if (!ta[j].equals(input[iPos])) {                        return null;                    }                    iPos++;                    j++;                }                Vector v = new Vector();                jsgfRuleToken tok = new jsgfRuleToken(rt.getText());                tok.setPos(iPos);                v.addElement(tok);                return v;            }        }            /*         * ALTERNATIVES         */        if (r instanceof RuleAlternatives) {            RuleAlternatives ra = (RuleAlternatives)r;            Rule rar[] = ra.getRules();            Vector alts = new Vector();            for (int i=0; i<rar.length; i++) {                Vector p = parse(G,rar[i],input,iPos);                if (p == null) {                    continue;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -