/tags/Sphinx4-1.0beta4/src/jsapi/com/sun/speech/engine/recognition/BaseRuleGrammar.java

# · Java · 411 lines · 236 code · 53 blank · 122 comment · 46 complexity · bf11eb7d0079bbff5155e8a8224eadab MD5 · raw file

  1. /**
  2. * Copyright 1998-2003 Sun Microsystems, Inc.
  3. *
  4. * See the file "license.terms" for information on usage and
  5. * redistribution of this file, and for a DISCLAIMER OF ALL
  6. * WARRANTIES.
  7. */
  8. package com.sun.speech.engine.recognition;
  9. import javax.speech.recognition.*;
  10. import edu.cmu.sphinx.jsgf.JSGFGrammarException;
  11. import edu.cmu.sphinx.jsgf.JSGFRuleGrammar;
  12. import edu.cmu.sphinx.jsgf.parser.JSGFParser;
  13. import edu.cmu.sphinx.jsgf.rule.JSGFRule;
  14. import edu.cmu.sphinx.jsgf.rule.JSGFRuleAlternatives;
  15. import edu.cmu.sphinx.jsgf.rule.JSGFRuleCount;
  16. import edu.cmu.sphinx.jsgf.rule.JSGFRuleName;
  17. import edu.cmu.sphinx.jsgf.rule.JSGFRuleSequence;
  18. import edu.cmu.sphinx.jsgf.rule.JSGFRuleTag;
  19. import edu.cmu.sphinx.jsgf.rule.JSGFRuleToken;
  20. import java.io.Serializable;
  21. import java.util.*;
  22. /** Implementation of javax.speech.recognition.RuleGrammar. */
  23. public class BaseRuleGrammar extends BaseGrammar implements RuleGrammar, Serializable {
  24. private JSGFRuleGrammar jsgfGrammar;
  25. /**
  26. * Create a new BaseRuleGrammar
  27. *
  28. * @param rec the BaseRecognizer for this Grammar.
  29. * @param name the name of this Grammar.
  30. */
  31. public BaseRuleGrammar(BaseRecognizer rec, String name, JSGFRuleGrammar grammar) {
  32. super(rec, name);
  33. assert grammar != null;
  34. this.jsgfGrammar = grammar;
  35. }
  36. /**
  37. * Set the enabled property of the Grammar. From javax.speech.recognition.Grammar.
  38. *
  39. * @param enabled the new desired state of the enabled property.
  40. */
  41. public void setEnabled(boolean enabled) {
  42. super.setEnabled(enabled);
  43. jsgfGrammar.setEnabled(enabled);
  44. }
  45. /** Parse partial JSGF text to a Rule object. From javax.speech.recognition.RuleGrammar. */
  46. public Rule ruleForJSGF (String text) {
  47. return convert (JSGFParser.ruleForJSGF(text));
  48. }
  49. /* Recursively go through the rules and create the same structures from
  50. * JSAPI
  51. */
  52. public JSGFRule convert (Rule rule) {
  53. if (rule == null)
  54. return null;
  55. if (rule instanceof RuleName) {
  56. if (rule == RuleName.NULL)
  57. return JSGFRuleName.NULL;
  58. if (rule == RuleName.VOID)
  59. return JSGFRuleName.NULL;
  60. return new JSGFRuleName (((RuleName)rule).getRuleName());
  61. }
  62. if (rule instanceof RuleAlternatives) {
  63. RuleAlternatives ruleAlternatives = (RuleAlternatives) rule;
  64. List<JSGFRule> subrules = new ArrayList<JSGFRule>();
  65. for (Rule r : ruleAlternatives.getRules()) {
  66. subrules.add (convert (r));
  67. }
  68. List<Float> weights = null;
  69. if (ruleAlternatives.getWeights() != null) {
  70. weights = new ArrayList<Float>();
  71. for (float f : ruleAlternatives.getWeights()) {
  72. weights.add (f);
  73. }
  74. }
  75. return new JSGFRuleAlternatives(subrules, weights);
  76. }
  77. if (rule instanceof RuleSequence) {
  78. RuleSequence ruleSequence = (RuleSequence) rule;
  79. List<JSGFRule> subrules = new ArrayList<JSGFRule>();
  80. for (Rule r : ruleSequence.getRules()) {
  81. subrules.add (convert (r));
  82. }
  83. return new JSGFRuleSequence(subrules);
  84. }
  85. if (rule instanceof RuleCount) {
  86. RuleCount ruleCount = (RuleCount) rule;
  87. return new JSGFRuleCount(convert (ruleCount.getRule()), ruleCount.getCount());
  88. }
  89. if (rule instanceof RuleTag) {
  90. RuleTag ruleTag = (RuleTag) rule;
  91. return new JSGFRuleTag(convert (ruleTag.getRule()), ruleTag.getTag());
  92. }
  93. if (rule instanceof RuleToken) {
  94. RuleToken ruleToken = (RuleToken) rule;
  95. return new JSGFRuleToken(ruleToken.getText());
  96. }
  97. System.out.println ("Can't convert rule " + rule.getClass());
  98. return null;
  99. }
  100. /* Recursively go through the rules and create the same structures for
  101. * JSAPI
  102. */
  103. private Rule convert (JSGFRule rule) {
  104. if (rule == null)
  105. return null;
  106. if (rule instanceof JSGFRuleName) {
  107. if (rule == JSGFRuleName.NULL)
  108. return RuleName.NULL;
  109. if (rule == JSGFRuleName.VOID)
  110. return RuleName.NULL;
  111. JSGFRuleName ruleName = (JSGFRuleName)rule;
  112. return new RuleName (ruleName.getRuleName());
  113. }
  114. if (rule instanceof JSGFRuleAlternatives) {
  115. JSGFRuleAlternatives ruleAlternatives = (JSGFRuleAlternatives) rule;
  116. Rule[] subrules = new Rule[ruleAlternatives.getRules().size()];
  117. int i = 0;
  118. for (JSGFRule subrule : ruleAlternatives.getRules()) {
  119. subrules[i] = convert (subrule);
  120. i++;
  121. }
  122. float[] weights = null;
  123. if (ruleAlternatives.getWeights() != null) {
  124. weights = new float[ruleAlternatives.getWeights().size()];
  125. i = 0;
  126. for (Float f : ruleAlternatives.getWeights()) {
  127. weights[i] = f;
  128. i++;
  129. }
  130. }
  131. return new RuleAlternatives(subrules, weights);
  132. }
  133. if (rule instanceof JSGFRuleSequence) {
  134. JSGFRuleSequence ruleSequence = (JSGFRuleSequence) rule;
  135. Rule[] subrules = new Rule[ruleSequence.getRules().size()];
  136. int i = 0;
  137. for (JSGFRule subrule : ruleSequence.getRules()) {
  138. subrules[i] = convert (subrule);
  139. i++;
  140. }
  141. return new RuleSequence(subrules);
  142. }
  143. if (rule instanceof JSGFRuleCount) {
  144. JSGFRuleCount ruleCount = (JSGFRuleCount) rule;
  145. return new RuleCount(convert (ruleCount.getRule()), ruleCount.getCount());
  146. }
  147. if (rule instanceof JSGFRuleTag) {
  148. JSGFRuleTag ruleTag = (JSGFRuleTag) rule;
  149. return new RuleTag(convert (ruleTag.getRule()), ruleTag.getTag());
  150. }
  151. if (rule instanceof JSGFRuleToken) {
  152. JSGFRuleToken ruleToken = (JSGFRuleToken) rule;
  153. return new RuleToken(ruleToken.getText());
  154. }
  155. System.out.println ("Unknown rule type " + rule.getClass());
  156. return null;
  157. }
  158. /**
  159. * Set a rule in the grammar either by creating a new rule or updating an existing rule.
  160. *
  161. * @param ruleName the name of the rule.
  162. * @param rule the definition of the rule.
  163. * @param isPublic whether this rule is public or not.
  164. */
  165. public void setRule(String ruleName, Rule rule, boolean isPublic)
  166. throws NullPointerException, IllegalArgumentException {
  167. jsgfGrammar.setRule(ruleName, convert(rule), isPublic);
  168. grammarChanged = true;
  169. }
  170. /**
  171. * Return a copy of the data structure for the named rule. From javax.speech.recognition.RuleGrammar.
  172. *
  173. * @param ruleName the name of the rule.
  174. */
  175. public Rule getRule(String ruleName) {
  176. return convert(jsgfGrammar.getRule(ruleName));
  177. }
  178. /**
  179. * Return the data structure for the named rule. From javax.speech.recognition.RuleGrammar.
  180. *
  181. * @param ruleName the name of the rule.
  182. */
  183. public Rule getRuleInternal(String ruleName) {
  184. return convert(jsgfGrammar.getRule(ruleName));
  185. }
  186. /**
  187. * Test whether the specified rule is public. From javax.speech.recognition.RuleGrammar.
  188. *
  189. * @param ruleName the name of the rule.
  190. */
  191. public boolean isRulePublic(String ruleName)
  192. throws IllegalArgumentException {
  193. return jsgfGrammar.isRulePublic(ruleName);
  194. }
  195. /** List the names of all rules define in this Grammar. From javax.speech.recognition.RuleGrammar. */
  196. public String[] listRuleNames() {
  197. Set<String> names = jsgfGrammar.getRuleNames();
  198. return names.toArray(new String[names.size()]);
  199. }
  200. /**
  201. * Delete a rule from the grammar. From javax.speech.recognition.RuleGrammar.
  202. *
  203. * @param ruleName the name of the rule.
  204. */
  205. public void deleteRule(String ruleName) throws IllegalArgumentException {
  206. jsgfGrammar.deleteRule(ruleName);
  207. grammarChanged = true;
  208. }
  209. /**
  210. * Set the enabled state of the listed rule. From javax.speech.recognition.RuleGrammar.
  211. *
  212. * @param ruleName the name of the rule.
  213. * @param enabled the new enabled state.
  214. */
  215. public void setEnabled(String ruleName, boolean enabled) throws IllegalArgumentException {
  216. jsgfGrammar.setEnabled(enabled);
  217. }
  218. /**
  219. * Set the enabled state of the listed rules. From javax.speech.recognition.RuleGrammar.
  220. *
  221. * @param ruleNames the names of the rules.
  222. * @param enabled the new enabled state.
  223. */
  224. public void setEnabled(String[] ruleNames, boolean enabled) throws IllegalArgumentException {
  225. for (String ruleName : ruleNames)
  226. setEnabled(ruleName, enabled);
  227. }
  228. /**
  229. * Return enabled state of rule. From javax.speech.recognition.RuleGrammar.
  230. *
  231. * @param ruleName the name of the rule.
  232. */
  233. public boolean isEnabled(String ruleName) throws IllegalArgumentException {
  234. return jsgfGrammar.isEnabled(ruleName);
  235. }
  236. /**
  237. * Resolve a simple or qualified rulename as a full rulename. From javax.speech.recognition.RuleGrammar.
  238. *
  239. * @param ruleName the name of the rule.
  240. */
  241. public RuleName resolve(RuleName ruleName) throws GrammarException {
  242. JSGFRuleName jsgfRuleName = (JSGFRuleName) convert (ruleName);
  243. try {
  244. return (RuleName)convert (jsgfGrammar.resolve(jsgfRuleName));
  245. } catch (JSGFGrammarException e) {
  246. throw new GrammarException(e.getMessage());
  247. }
  248. }
  249. /**
  250. * Import all rules or a specified rule from another grammar. From javax.speech.recognition.RuleGrammar.
  251. *
  252. * @param importName the name of the rule(s) to import.
  253. */
  254. public void addImport(RuleName importName) {
  255. jsgfGrammar.addImport((JSGFRuleName)convert (importName));
  256. grammarChanged = true;
  257. }
  258. /**
  259. * Remove an import. From javax.speech.recognition.RuleGrammar.
  260. *
  261. * @param importName the name of the rule(s) to remove.
  262. */
  263. public void removeImport(RuleName importName)
  264. throws IllegalArgumentException {
  265. jsgfGrammar.addImport((JSGFRuleName)convert (importName));
  266. grammarChanged = true;
  267. }
  268. /** List the current imports. From javax.speech.recognition.RuleGrammar. */
  269. public RuleName[] listImports() {
  270. assert jsgfGrammar != null;
  271. List<JSGFRuleName> imports = jsgfGrammar.getImports();
  272. RuleName[] result = new RuleName[imports.size()];
  273. for (int i = 0; i < imports.size(); i++ ) {
  274. result[i] = (RuleName)convert (imports.get(i));
  275. }
  276. return result;
  277. }
  278. /**
  279. * Parse the text string against the specified rule. Uses the RuleParser class. From
  280. * javax.speech.recognition.RuleGrammar.
  281. *
  282. * @param text the text to parse.
  283. * @param ruleName the name of rule to use for parsing.
  284. */
  285. public RuleParse parse(String text, String ruleName) throws GrammarException {
  286. return RuleParser.parse(text, recognizer, this, ruleName == null ? ruleName : JSGFRuleName.stripRuleName(ruleName));
  287. }
  288. /**
  289. * Parse the tokens string against the specified rule. Uses the RuleParser class. From
  290. * javax.speech.recognition.RuleGrammar.
  291. *
  292. * @param tokens the tokens to parse.
  293. * @param ruleName the name of rule to use for parsing.
  294. */
  295. public RuleParse parse(String tokens[], String ruleName) throws GrammarException {
  296. return RuleParser.parse(tokens, recognizer, this, ruleName == null ? ruleName : JSGFRuleName.stripRuleName(ruleName));
  297. }
  298. /**
  299. * Parse the nth best result of a FinalRuleResult against the specified rule. Uses the RuleParser class. From
  300. * javax.speech.recognition.RuleGrammar.
  301. *
  302. * @param r the FinalRuleResult.
  303. * @param nBest the nth best result to use.
  304. * @param ruleName the name of rule to use for parsing.
  305. */
  306. public RuleParse parse(FinalRuleResult r, int nBest, String ruleName) throws GrammarException {
  307. // Some JSAPI implementations we run into are not JSAPI complaint,
  308. // so try a few alternatives
  309. ResultToken rt[] = r.getAlternativeTokens(nBest);
  310. if (rt != null || (rt = r.getBestTokens()) != null) {
  311. String tokens[] = new String[rt.length];
  312. for (int i = 0; i < rt.length; i++) {
  313. tokens[i] = rt[i].getSpokenText();
  314. }
  315. return parse(tokens, ruleName);
  316. } else {
  317. return parse(r.toString(), ruleName);
  318. }
  319. }
  320. /**
  321. * Returns a string containing the specification for this grammar.
  322. *
  323. * @return specification for this grammar.
  324. */
  325. public String toString() {
  326. return jsgfGrammar.toString();
  327. }
  328. /** Add a new RuleGrammar comment. */
  329. public void addRuleDocComment(String rname, String comment) {
  330. jsgfGrammar.addRuleDocComment(rname, comment);
  331. }
  332. /** Retrieve a RuleGrammar comment. */
  333. public String getRuleDocComment(String rname) {
  334. return jsgfGrammar.getRuleDocComment(rname);
  335. }
  336. /** Add a new import comment. */
  337. public void addImportDocComment(RuleName imp, String comment) {
  338. jsgfGrammar.addImportDocComment ((JSGFRuleName)convert (imp), comment);
  339. }
  340. /** Retrieve an import comment. */
  341. public String getImportDocComment(RuleName imp) {
  342. return jsgfGrammar.getImportDocComment((JSGFRuleName)convert (imp));
  343. }
  344. /** Add the Grammar comment. */
  345. public void addGrammarDocComment(String comment) {
  346. jsgfGrammar.addGrammarDocComment(comment);
  347. }
  348. /** Retrieve the Grammar comment. */
  349. public String getGrammarDocComment() {
  350. return jsgfGrammar.getGrammarDocComment();
  351. }
  352. public boolean isRuleChanged(String ruleName) {
  353. return jsgfGrammar.isRuleChanged (ruleName);
  354. }
  355. public void setRuleChanged(String ruleName, boolean changed) {
  356. jsgfGrammar.setRuleChanged (ruleName, changed);
  357. }
  358. }