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

📄 jsgf.jj

📁 It is the Speech recognition software. It is platform independent. To execute the source code,
💻 JJ
📖 第 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. *//**  * JavaCC grammar for parsing and creating grammars from JSGF files  * using JSAPI. *//** * This grammar can be compiled with javacc, which is available at *   http://www.webgain.com/products/java_cc/ */options {    JAVA_UNICODE_ESCAPE = true;    STATIC=false;}PARSER_BEGIN(JSGFParser)package com.sun.speech.engine.recognition;import javax.speech.*;import javax.speech.recognition.*;import java.io.*;import java.net.*;import java.util.*;public class JSGFParser {    // For now we create one global parser, if needed JavaCC can be set    // to allow the creation of multiple parser instances    //    static final String version = "1.0";    static JSGFParser parser = null;    // This main method simplly checks the syntax of a jsgf Grammar    //    public static void main(String args[]) {        if (args.length == 0) {            System.out.println("JSGF Parser Version " + version                                + ":  Reading from standard input . . .");            parser = new JSGFParser(System.in);        } else if (args.length > 0) {            System.out.println("JSGF Parser Version " + version                               +" :  Reading from file " + args[0] + " . . .");            try {	       URL codeBase = null;               File f = new File(".");	       String path = f.getAbsolutePath() + "/" + args[0];	                            try {	                     codeBase = new URL("file:" + path);               } catch (MalformedURLException e) {                   System.out.println(                    "Could not get URL for current directory " + e);                     return;               }               BufferedInputStream i = new BufferedInputStream(codeBase.openStream(),256);	       Object a[] = getJSGFEncoding(i);	       Reader rdr;	       if ((a != null) && (a[1] != null)) {	         System.out.println("Grammar Character Encoding \""+a[1]+"\"");		 rdr = new InputStreamReader(i,(String)a[1]);               }	       else { 	         if (a==null) System.out.println("WARNING: Grammar missing self identifying header");	         rdr = new InputStreamReader(i);               }               parser = new JSGFParser(rdr);            } catch (Exception e) {                System.out.println("JSGF Parser Version " + version                                   + ":  File " + args[0] + " not found.");                return;            }        } else {            System.out.println("JSGF Parser Version " + version                               + ":  Usage is one of:");            System.out.println("         java JSGFParser < inputfile");            System.out.println("OR");            System.out.println("         java JSGFParser inputfile");            return;        }        try {            parser.GrammarUnit(null);            System.out.println("JSGF Parser Version " + version                               + ":  JSGF Grammar parsed successfully.");        } catch (ParseException e) {            System.out.println("JSGF Parser Version " + version                               + ":  Encountered errors during parse."                                + e.getMessage());        }    }    /**     * newGrammarFromJSGF - Once JavaCC supports Readers we will change this     */    public static RuleGrammar newGrammarFromJSGF(InputStream i, Recognizer R)         throws GrammarException {        RuleGrammar G = null;        if (parser == null) {            parser = new JSGFParser(i);        } else {            parser.ReInit(i);        }        try {            G = parser.GrammarUnit(R);            return G;        } catch (ParseException e) {            Token etoken = e.currentToken;            GrammarSyntaxDetail gsd = new GrammarSyntaxDetail(                null,                null,                null,                null,                etoken.beginLine,                etoken.beginColumn,                e.getMessage());            GrammarSyntaxDetail gsda[] = new GrammarSyntaxDetail[1];            gsda[0] = gsd;            GrammarException ge = new GrammarException("Grammar Error",gsda);            throw ge;        }    }    /**     * newGrammarFromJSGF - Once JavaCC supports Readers we will change this     */    public static RuleGrammar newGrammarFromJSGF(Reader i, Recognizer R)         throws GrammarException {        RuleGrammar G = null;        if (parser == null) {            parser = new JSGFParser(i);        } else {            parser.ReInit(i);        }        try {            G = parser.GrammarUnit(R);            return G;        } catch (ParseException e) {            Token etoken = e.currentToken;            GrammarSyntaxDetail gsd = new GrammarSyntaxDetail(                null,                null,                null,                null,                etoken.beginLine,                etoken.beginColumn,                e.getMessage());            GrammarSyntaxDetail gsda[] = new GrammarSyntaxDetail[1];            gsda[0] = gsd;            GrammarException ge = new GrammarException("Grammar Error",gsda);            throw ge;        }    }  // returns object array containing the following  // three strings:  //    arry[0] = JSGF Version string  //    arry[1] = ISO Char encoding  //    arry[2] = Locale  //   private static Object[] getJSGFEncoding(BufferedInputStream is) {    int i=0;    byte b[] = new byte[2];    byte c[] = new byte[80];    is.mark(256);    /* read 2 bytes */    try {      if (is.read(b,0,2) != 2) {	is.reset();	return null;      }            // UTF-8      if ((b[0] == 0x23) && (b[1] == 0x4A)) {		i=0;	c[i++]=b[0];	c[i++]=b[1];	while (i<80) {	  if (is.read(b,0,1) != 1) { is.reset(); return null; }	  if ((b[0] == 0x0A)||(b[0]==0x0D)) break;	  c[i++]=b[0];	}      } else	// UTF-16 BE	if ((b[0] == 0x23) && (b[1] == 0x00)) {	  i=0;	  c[i++]=b[0];	  while (i<80) {	    if (is.read(b,0,2) != 2) { is.reset(); return null; }	    if (b[1] != 0) return null;	    if ((b[0] == 0x0A)||(b[0]==0x0D)) break;	    c[i++]=b[0];	  }	} else {	  // UTF-16 LE	  if ((b[0] == 0x00) && (b[1] == 0x23)) {	    i=0;	    c[i++]=b[1];	    while (i<80) {	      if (is.read(b,0,2) != 2) { is.reset(); return null; }	      if (b[0] != 0) return null;	      if ((b[1] == 0x0A)||(b[1]==0x0D)) break;	      c[i++]=b[1];	    }	  }	}    } catch (IOException ioe) {      try { is.reset(); } catch (IOException ioe2) { }      return null;    }    if (i==0) {       try { is.reset(); } catch (IOException ioe2) { }      return null;     }    //    // Now c[] should have first line of text in UTF-8 format    //        String estr = new String(c,0,i);    StringTokenizer st = new StringTokenizer(estr," \t\n\r\f;");        String id = null;    String ver = null;    String enc = null;    String loc = null;    if (st.hasMoreTokens()) id = st.nextToken();    if (!id.equals("#JSGF")) {       try { is.reset(); } catch (IOException ioe2) { }      return null;    }        if (st.hasMoreTokens()) ver = st.nextToken();        if (st.hasMoreTokens()) enc = st.nextToken();        if (st.hasMoreTokens()) loc = st.nextToken();    Object rv[] = new Object[3];    rv[0] = ver;    rv[1] = enc;    rv[2] = loc;    return rv;      }    /**     * newGrammarFromURL     */    public static RuleGrammar newGrammarFromJSGF(URL u, Recognizer R)         throws GrammarException, IOException {        BufferedInputStream i = new BufferedInputStream(u.openStream(),256);	Object a[] = getJSGFEncoding(i);	Reader rdr;	if ((a != null) && (a[1] != null)) {	        System.out.println("Grammar Character Encoding \""+a[1]+"\"");		rdr = new InputStreamReader(i,(String)a[1]);        }	else { 	   if (a==null) System.out.println("WARNING: Grammar missing self identifying header");	   rdr = new InputStreamReader(i);        }        return newGrammarFromJSGF(rdr,R);    }    /**     * ruleForJSGF     */    public static Rule ruleForJSGF(String text) {        Rule r = null;        try {	    StringReader sread = new StringReader(text);            if (parser == null) parser = new JSGFParser(sread);            else parser.ReInit(sread);            r = parser.alternatives();            // System.out.println("JSGF Parser Version " + version            //                    + ":  JSGF RHS parsed successfully.");        } catch (ParseException e) {            System.out.println("JSGF Parser Version " + version                               + ":  Encountered errors during parse.");        }        return r;    }    /**     * Parse an apparent rulename reference.     */    public static RuleName parseRuleName(String text)      throws GrammarException    {       RuleName r = null;       try {         ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes());         if (parser == null) parser = new JSGFParser(stream);         else parser.ReInit(stream);         r = parser.ruleRef();       } catch (ParseException e) {	 throw new GrammarException("JSGF Parser Version " + version                                     + " error", null);       }       return r;    }    /**     * Parse and apparent import declaration     */    public static RuleName parseImport(String text)      throws GrammarException    {      RuleName r = null;      try {        ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes());        if (parser == null) parser = new JSGFParser(stream);        else parser.ReInit(stream);        r = parser.importRef();      } catch (ParseException e) {	throw new GrammarException("JSGF Parser Version 0.1 error", null);      }      return r;    }    /**     * extract @xxxx keywords from documention comments     */    static void extractKeywords(Grammar G,String rname,String comment) {        if (!(G instanceof BaseRuleGrammar)) {	    return;	}        String sample;        BaseRuleGrammar JG = (BaseRuleGrammar) G;        int i = comment.indexOf("@example ");        while (i > 0) {            int j = comment.indexOf('\n',i);            if (j < 0) {                sample = comment.substring(i+8);                i = -1;            } else {                sample = comment.substring(i+8,j);                i = j;            }            i = comment.indexOf("@example ",i);            JG.addSampleSentence(rname,sample);        }    }}PARSER_END(JSGFParser)/* WHITE SPACE */SKIP :{  " "| "\t"| "\n"| "\r"| "\f"}/* COMMENTS */MORE :{  "//" : IN_SINGLE_LINE_COMMENT|  <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT|  "/*" : IN_MULTI_LINE_COMMENT}<IN_SINGLE_LINE_COMMENT>SPECIAL_TOKEN :{  <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : DEFAULT}<IN_FORMAL_COMMENT>SPECIAL_TOKEN :{  <FORMAL_COMMENT: "*/" > : DEFAULT}<IN_MULTI_LINE_COMMENT>SPECIAL_TOKEN :{  <MULTI_LINE_COMMENT: "*/" > : DEFAULT}

⌨️ 快捷键说明

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