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

📄 recognizerutilities.java

📁 It is the Speech recognition software. It is platform independent. To execute the source code,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    //System.out.println("  ("+r+") ("+nr+")");	    if (nr != r) {	      nochange=false;	      somethingchanged=true;	      if (NRG[i] == RG[i]) {		NRG[i] = new BaseRuleGrammar(null,RG[i].getName());		RecognizerUtilities.copyGrammar(RG[i],NRG[i]);	      }	      boolean isPublic;	      try { 		isPublic = NRG[i].isRulePublic(rnames[j]);	      } catch (IllegalArgumentException nse) {		throw new GrammarException(nse.toString(), null);	      }	      NRG[i].setRule(rnames[j],nr,isPublic);	    }	  }	}      }      if (nochange) return NRG;      /*       * Now remove unused rules       */      Vector nr = new Vector();      for(int i=0;i<NRG.length;i++) {	RuleGrammar NG = null;	String rnames[] = NRG[i].listRuleNames();	int k=0;	for (int j=0; j < rnames.length; j++) {	  Rule r1 = NRG[i].getRuleInternal(rnames[j]);	  RuleState s = new RuleState(r1,NRG);	  //System.out.println(rnames[j] + " " + r1.getClass() + " " + s.isvoid + " " + s.isnull);	  if ((eliminateVOID && s.isvoid) || (eliminateNULL && s.isnull)) {	    NRG[i].deleteRule(rnames[j]);	  } else k++;	}	if (k>0) nr.addElement(NRG[i]);      }            /*       * Now return all non-null grammars       */      RuleGrammar FRG[] = new RuleGrammar[nr.size()];      nr.copyInto(FRG);      return FRG;    }    static Rule transform(Rule r,RuleGrammar RGL[],boolean en, boolean ev) {    //System.out.println("xf " + r.getClass() + r);        /*     * RULESEQUENCE     */    if (r instanceof RuleSequence) {      RuleSequence rs = (RuleSequence) r;      Rule rarry[] = rs.getRules();      if ((rarry == null) || (rarry.length == 0)) {	return null;      }      int cnull=0;      for(int i=0; i<rarry.length; i++) {	Rule r1 = transform(rarry[i],RGL,en,ev);	RuleState s1 = new RuleState(r1,RGL);	if (ev && s1.isvoid) return RuleName.VOID;	if ((en && s1.isnull) || (r1 != rarry[i])) {	  if (rs == (RuleSequence) r) {	    rs = (RuleSequence) rs.copy();	    rarry = rs.getRules();	  }	  if (en && s1.isnull) { cnull++; r1=null; }	  rarry[i]=r1;	  rs.setRules(rarry);	}      }      if (cnull==0) return rs;      if (cnull==rarry.length) return RuleName.NULL;      Rule barry[] = new Rule[rarry.length-cnull];      int j=0;      for(int i=0; i<rarry.length; i++) {	if (rarry[i]!=null) {	  barry[j++]=rarry[i];	}      }      rs.setRules(barry);      return rs;    }    /*     * RULEALTERNATIVES     */    if (r instanceof RuleAlternatives) {      RuleAlternatives ra = (RuleAlternatives) r;      Rule rarry[] = ra.getRules();      if ((rarry == null) || (rarry.length == 0)) {	return null;      }      int cnull=0;      int cvoid=0;      for(int i=0; i<rarry.length; i++) {	Rule r1 = transform(rarry[i],RGL,en,ev);	//System.out.println("rs: " + r1);	RuleState s1 = new RuleState(r1,RGL);	if ((en && s1.isnull) || (ev && s1.isvoid) || (r1 != rarry[i])) {	  if (ra == (RuleAlternatives) r) {	    ra = (RuleAlternatives) ra.copy();	    rarry = ra.getRules();	  }	  if (en && s1.isnull) { cnull++; r1=null; }	  if (ev && s1.isvoid) { cvoid++; r1=null; }	  rarry[i]=r1;	  ra.setRules(rarry);	}      }      if ((cnull==0) && (cvoid==0)) return ra;      if (cvoid==rarry.length) return RuleName.VOID;      if ((cvoid+cnull)==rarry.length) return RuleName.NULL;      Rule barry[] = new Rule[rarry.length-(cnull+cvoid)];      int j=0;      for(int i=0; i<rarry.length; i++) {	if (rarry[i]!=null) {	  barry[j++]=rarry[i];	}      }      ra.setRules(barry);      if (cnull > 0) return new RuleCount(ra,RuleCount.OPTIONAL);      else return ra;    }    /*     * RULECOUNT (e.g. [], *, or + )     */    if (r instanceof RuleCount) {      RuleCount rc = (RuleCount)r;      int rcount = rc.getCount();      Rule r1 = rc.getRule();      Rule r2 = transform(r1,RGL,en,ev);      RuleState s1 = new RuleState(r2,RGL);      if (ev && s1.isvoid) {	if (rcount == RuleCount.ONCE_OR_MORE) return RuleName.VOID;	return RuleName.NULL;      }      if (en && s1.isnull) return RuleName.NULL;      if (r1 == r2) return rc;      return new RuleCount(r2,rcount);    }    /*     * TAGS     */    if (r instanceof RuleTag) {      RuleTag rtag = (RuleTag)r;      Rule rtr = rtag.getRule();      Rule r1 = transform(rtr,RGL,en,ev);      RuleState s1 = new RuleState(r1,RGL);            if (en && s1.isnull) return RuleName.NULL;      if (ev && s1.isvoid) return RuleName.VOID;      if (r1 == rtr) return rtag;      return new RuleTag(r1,rtag.getTag());    }    return r;  }    /*   * Test code   */  static public void main(String args[]) {    Recognizer R = null;    if (args.length < 1) {      System.out.println("usage: java GXFormer grammar-file");      return;    }    boolean elimVOID = true;    boolean elimNULL = true;    if (args.length > 1) elimNULL = (Boolean.valueOf(args[1])).booleanValue();    if (args.length > 2) elimVOID = (Boolean.valueOf(args[2])).booleanValue();    try {      R = new BaseRecognizer();    } catch (Exception e) {      e.printStackTrace();    }    if (R == null) {      System.out.println("failed to create a recognizer");      return;    }    try {      R.allocate();    } catch (Exception e) {      e.printStackTrace();    }    RuleGrammar G = null;            URL u = null;    File f = new File(".");    try {      u = new URL("file:" + f.getAbsolutePath());    } catch (MalformedURLException e) {      System.out.println("Could not get URL for current directory " + e);      return;    }    try {      G = R.loadJSGF(u, args[0]);    } catch (Exception e1) {      System.out.println("GRAMMAR ERROR: In grammar " + args[0] 			 + ", or its imports, at URL base " + u 			 + " " + e1);      e1.printStackTrace();      return;    }    if (G == null) {      System.out.println("ERROR LOADING GRAMMAR");      return;    }    try {      R.commitChanges();    } catch (Exception ge) {      System.out.println("Grammar " + G.getName() 			 + " caused a GrammarException when initially loaded");      ge.printStackTrace();      return;    }    System.out.println("Grammar loaded -- transforming");    RuleGrammar RG[] = R.listRuleGrammars();        RuleGrammar NRG[] = null;    try {      NRG = transform(RG,elimNULL,elimVOID);    } catch (Exception e) {      e.printStackTrace();      return;    }    for(int j=0; j<NRG.length; j++) {      System.out.println("Grammar: " + NRG[j].getName());      String rnames[] = NRG[j].listRuleNames();      for (int k=0; k < rnames.length; k++) {	System.out.print("<"+rnames[k]+"> = ");	Rule r = NRG[j].getRuleInternal(rnames[k]);	System.out.println(r);      }      System.out.println("");      System.out.println("");    }  }}class RuleState {      Rule getRule(RuleName rn, RuleGrammar RGL[]) {    String gname = rn.getSimpleGrammarName();    for(int i=0; i<RGL.length; i++) {      if (RGL[i].getName().equals(gname)) {	return RGL[i].getRuleInternal(rn.getSimpleRuleName());      }    }    return null;  }      RuleState(Rule r, RuleGrammar RGL[]) {    if (r instanceof RuleName) {      RuleName rn = (RuleName)r;	      /* simple check for void/null */      String simpleName = rn.getSimpleRuleName();      if (simpleName.equals("VOID")) { isvoid=true; return; }      if (simpleName.equals("NULL")) { isnull=true; return; }	      /* now lookup the rule and see if its RHS is null/void */      Rule r1 = getRule(rn,RGL);      if (r1 == null) System.out.println("ERROR: Could not find rule " + rn);      if (r1 instanceof RuleName) {	RuleName rn1 = (RuleName)r1;	  	/* simple check for void/null */	String simpleName1 = rn1.getSimpleRuleName();	if (simpleName1.equals("VOID")) { isvoid=true; return; }	if (simpleName1.equals("NULL")) { isnull=true; return; }      }      return;    }  }      boolean isvoid = false;  boolean isnull = false;}

⌨️ 快捷键说明

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