| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Rule store for math syntax tree nodes. | 6 * @fileoverview Rule store for math syntax tree nodes. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 goog.provide('cvox.MathStore'); | 9 goog.provide('cvox.MathStore'); |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 */ | 23 */ |
| 24 cvox.MathStore = function() { | 24 cvox.MathStore = function() { |
| 25 goog.base(this); | 25 goog.base(this); |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * @override | 28 * @override |
| 29 */ | 29 */ |
| 30 this.dynamicCstrAttribs = [ | 30 this.dynamicCstrAttribs = [ |
| 31 cvox.SpeechRule.DynamicCstrAttrib.DOMAIN, | 31 cvox.SpeechRule.DynamicCstrAttrib.DOMAIN, |
| 32 cvox.SpeechRule.DynamicCstrAttrib.STYLE | 32 cvox.SpeechRule.DynamicCstrAttrib.STYLE |
| 33 ]; | 33 ]; |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * @override | 36 * @override |
| 37 */ | 37 */ |
| 38 this.defaultTtsProps = [cvox.AbstractTts.PITCH]; | 38 this.defaultTtsProps = [cvox.AbstractTts.PITCH]; |
| 39 }; | 39 }; |
| 40 goog.inherits(cvox.MathStore, cvox.BaseRuleStore); | 40 goog.inherits(cvox.MathStore, cvox.BaseRuleStore); |
| 41 | 41 |
| 42 /** This adds domain to dynamic constraint annotation. */ | 42 /** This adds domain to dynamic constraint annotation. */ |
| 43 cvox.SpeechRule.DynamicCstrAttrib.DOMAIN = 'domain'; | 43 cvox.SpeechRule.DynamicCstrAttrib.DOMAIN = 'domain'; |
| 44 | 44 |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * @override | 47 * @override |
| 48 */ | 48 */ |
| 49 cvox.MathStore.prototype.defineRule = function( | 49 cvox.MathStore.prototype.defineRule = function( |
| 50 name, dynamic, action, query, cstr) { | 50 name, dynamic, action, query, cstr) { |
| 51 var dynamicCstr = this.parseDynamicConstraint(dynamic); | 51 var dynamicCstr = this.parseDynamicConstraint(dynamic); |
| 52 var cstrList = Array.prototype.slice.call(arguments, 4); | 52 var cstrList = Array.prototype.slice.call(arguments, 4); |
| 53 // We can not use goog.base due to variable number of constraint arguments. | 53 // We can not use goog.base due to variable number of constraint arguments. |
| 54 var rule = cvox.MathStore.superClass_.defineRule.apply( | 54 var rule = cvox.MathStore.superClass_.defineRule.apply(this, [ |
| 55 this, [name, dynamicCstr[cvox.SpeechRule.DynamicCstrAttrib.STYLE], | 55 name, dynamicCstr[cvox.SpeechRule.DynamicCstrAttrib.STYLE], action, query |
| 56 action, query].concat(cstrList)); | 56 ].concat(cstrList)); |
| 57 // In the superclass the dynamic constraint only contains style annotations. | 57 // In the superclass the dynamic constraint only contains style annotations. |
| 58 // We now set the proper dynamic constraint that contains in addition a | 58 // We now set the proper dynamic constraint that contains in addition a |
| 59 // a domain attribute/value pair. | 59 // a domain attribute/value pair. |
| 60 rule.dynamicCstr = dynamicCstr; | 60 rule.dynamicCstr = dynamicCstr; |
| 61 this.removeDuplicates(rule); | 61 this.removeDuplicates(rule); |
| 62 return rule; | 62 return rule; |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 | 65 |
| 66 /** | 66 /** |
| (...skipping 29 matching lines...) Expand all Loading... |
| 96 /** | 96 /** |
| 97 * Adds an alias for an existing rule. | 97 * Adds an alias for an existing rule. |
| 98 * @param {string} name The name of the rule. | 98 * @param {string} name The name of the rule. |
| 99 * @param {string} dynamic A math domain and style assignment. | 99 * @param {string} dynamic A math domain and style assignment. |
| 100 * @param {string} query Precondition query of the rule. | 100 * @param {string} query Precondition query of the rule. |
| 101 * @param {...string} cstr Additional static precondition constraints. | 101 * @param {...string} cstr Additional static precondition constraints. |
| 102 */ | 102 */ |
| 103 cvox.MathStore.prototype.defineUniqueRuleAlias = function( | 103 cvox.MathStore.prototype.defineUniqueRuleAlias = function( |
| 104 name, dynamic, query, cstr) { | 104 name, dynamic, query, cstr) { |
| 105 var dynamicCstr = this.parseDynamicConstraint(dynamic); | 105 var dynamicCstr = this.parseDynamicConstraint(dynamic); |
| 106 var rule = this.findRule( | 106 var rule = this.findRule(goog.bind(function(rule) { |
| 107 goog.bind( | 107 return rule.name == name && this.testDynamicConstraints(dynamicCstr, rule); |
| 108 function(rule) { | 108 }, this)); |
| 109 return rule.name == name && | |
| 110 this.testDynamicConstraints(dynamicCstr, rule);}, | |
| 111 this)); | |
| 112 if (!rule) { | 109 if (!rule) { |
| 113 throw new cvox.SpeechRule.OutputError( | 110 throw new cvox.SpeechRule.OutputError( |
| 114 'Rule named ' + name + ' with style ' + dynamic + ' does not exist.'); | 111 'Rule named ' + name + ' with style ' + dynamic + ' does not exist.'); |
| 115 } | 112 } |
| 116 this.addAlias_(rule, query, Array.prototype.slice.call(arguments, 3)); | 113 this.addAlias_(rule, query, Array.prototype.slice.call(arguments, 3)); |
| 117 }; | 114 }; |
| 118 | 115 |
| 119 | 116 |
| 120 /** | 117 /** |
| 121 * Adds an alias for an existing rule. | 118 * Adds an alias for an existing rule. |
| 122 * @param {string} name The name of the rule. | 119 * @param {string} name The name of the rule. |
| 123 * @param {string} query Precondition query of the rule. | 120 * @param {string} query Precondition query of the rule. |
| 124 * @param {...string} cstr Additional static precondition constraints. | 121 * @param {...string} cstr Additional static precondition constraints. |
| 125 */ | 122 */ |
| 126 cvox.MathStore.prototype.defineRuleAlias = function(name, query, cstr) { | 123 cvox.MathStore.prototype.defineRuleAlias = function(name, query, cstr) { |
| 127 var rule = this.findRule(function(rule) { | 124 var rule = this.findRule(function(rule) { |
| 128 return rule.name == name;}); | 125 return rule.name == name; |
| 126 }); |
| 129 if (!rule) { | 127 if (!rule) { |
| 130 throw new cvox.SpeechRule.OutputError( | 128 throw new cvox.SpeechRule.OutputError( |
| 131 'Rule with named ' + name + ' does not exist.'); | 129 'Rule with named ' + name + ' does not exist.'); |
| 132 } | 130 } |
| 133 this.addAlias_(rule, query, Array.prototype.slice.call(arguments, 2)); | 131 this.addAlias_(rule, query, Array.prototype.slice.call(arguments, 2)); |
| 134 }; | 132 }; |
| 135 | 133 |
| 136 | 134 |
| 137 /** | 135 /** |
| 138 * Adds an alias for an existing rule. | 136 * Adds an alias for an existing rule. |
| 139 * @param {string} name The name of the rule. | 137 * @param {string} name The name of the rule. |
| 140 * @param {string} query Precondition query of the rule. | 138 * @param {string} query Precondition query of the rule. |
| 141 * @param {...string} cstr Additional static precondition constraints. | 139 * @param {...string} cstr Additional static precondition constraints. |
| 142 */ | 140 */ |
| 143 cvox.MathStore.prototype.defineRulesAlias = function(name, query, cstr) { | 141 cvox.MathStore.prototype.defineRulesAlias = function(name, query, cstr) { |
| 144 var rules = this.findAllRules(function(rule) {return rule.name == name;}); | 142 var rules = this.findAllRules(function(rule) { |
| 143 return rule.name == name; |
| 144 }); |
| 145 if (rules.length == 0) { | 145 if (rules.length == 0) { |
| 146 throw new cvox.SpeechRule.OutputError( | 146 throw new cvox.SpeechRule.OutputError( |
| 147 'Rule with name ' + name + ' does not exist.'); | 147 'Rule with name ' + name + ' does not exist.'); |
| 148 } | 148 } |
| 149 var cstrList = Array.prototype.slice.call(arguments, 2); | 149 var cstrList = Array.prototype.slice.call(arguments, 2); |
| 150 rules.forEach(goog.bind( | 150 rules.forEach(goog.bind(function(rule) { |
| 151 function(rule) { | 151 this.addAlias_(rule, query, cstrList); |
| 152 this.addAlias_(rule, query, cstrList); | 152 }, this)); |
| 153 }, | |
| 154 this)); | |
| 155 }; | 153 }; |
| 156 | 154 |
| 157 | 155 |
| 158 /** | 156 /** |
| 159 * Adds a new speech rule as alias of the given rule. | 157 * Adds a new speech rule as alias of the given rule. |
| 160 * @param {cvox.SpeechRule} rule The existing rule. | 158 * @param {cvox.SpeechRule} rule The existing rule. |
| 161 * @param {string} query Precondition query of the rule. | 159 * @param {string} query Precondition query of the rule. |
| 162 * @param {Array<string>} cstrList List of additional constraints. | 160 * @param {Array<string>} cstrList List of additional constraints. |
| 163 * @private | 161 * @private |
| 164 */ | 162 */ |
| 165 cvox.MathStore.prototype.addAlias_ = function(rule, query, cstrList) { | 163 cvox.MathStore.prototype.addAlias_ = function(rule, query, cstrList) { |
| 166 var prec = new cvox.SpeechRule.Precondition(query, cstrList); | 164 var prec = new cvox.SpeechRule.Precondition(query, cstrList); |
| 167 var newRule = new cvox.SpeechRule( | 165 var newRule = |
| 168 rule.name, rule.dynamicCstr, prec, rule.action); | 166 new cvox.SpeechRule(rule.name, rule.dynamicCstr, prec, rule.action); |
| 169 newRule.name = rule.name; | 167 newRule.name = rule.name; |
| 170 this.addRule(newRule); | 168 this.addRule(newRule); |
| 171 }; | 169 }; |
| 172 | 170 |
| 173 | 171 |
| 174 // Evaluator | 172 // Evaluator |
| 175 /** | 173 /** |
| 176 * @override | 174 * @override |
| 177 */ | 175 */ |
| 178 cvox.MathStore.prototype.evaluateDefault = function(node) { | 176 cvox.MathStore.prototype.evaluateDefault = function(node) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 if (num) { | 208 if (num) { |
| 211 descs.push(this.evaluate_(num[0])); | 209 descs.push(this.evaluate_(num[0])); |
| 212 rest = rest.substring(num[0].length); | 210 rest = rest.substring(num[0].length); |
| 213 } else if (alpha) { | 211 } else if (alpha) { |
| 214 descs.push(this.evaluate_(alpha[0])); | 212 descs.push(this.evaluate_(alpha[0])); |
| 215 rest = rest.substring(alpha[0].length); | 213 rest = rest.substring(alpha[0].length); |
| 216 } else { | 214 } else { |
| 217 // Dealing with surrogate pairs. | 215 // Dealing with surrogate pairs. |
| 218 var chr = rest[0]; | 216 var chr = rest[0]; |
| 219 var code = chr.charCodeAt(0); | 217 var code = chr.charCodeAt(0); |
| 220 if (0xD800 <= code && code <= 0xDBFF && | 218 if (0xD800 <= code && code <= 0xDBFF && rest.length > 1 && |
| 221 rest.length > 1 && !isNaN(rest.charCodeAt(1))) { | 219 !isNaN(rest.charCodeAt(1))) { |
| 222 descs.push(this.evaluate_(rest.slice(0, 2))); | 220 descs.push(this.evaluate_(rest.slice(0, 2))); |
| 223 rest = rest.substring(2); | 221 rest = rest.substring(2); |
| 224 } else { | 222 } else { |
| 225 descs.push(this.evaluate_(rest[0])); | 223 descs.push(this.evaluate_(rest[0])); |
| 226 rest = rest.substring(1); | 224 rest = rest.substring(1); |
| 227 } | 225 } |
| 228 } | 226 } |
| 229 } | 227 } |
| 230 } | 228 } |
| 231 } | 229 } |
| 232 return descs; | 230 return descs; |
| 233 }; | 231 }; |
| 234 | 232 |
| 235 | 233 |
| 236 /** | 234 /** |
| 237 * Creates a new Navigation Description for a math expression that be used by | 235 * Creates a new Navigation Description for a math expression that be used by |
| 238 * the background tts. | 236 * the background tts. |
| 239 * @param {string} text to be translated. | 237 * @param {string} text to be translated. |
| 240 * @return {cvox.NavDescription} Navigation description for the | 238 * @return {cvox.NavDescription} Navigation description for the |
| 241 * math expression. | 239 * math expression. |
| 242 * @private | 240 * @private |
| 243 */ | 241 */ |
| 244 cvox.MathStore.prototype.evaluate_ = function(text) { | 242 cvox.MathStore.prototype.evaluate_ = function(text) { |
| 245 if (cvox.ChromeVox.host['mathMap']) { | 243 if (cvox.ChromeVox.host['mathMap']) { |
| 246 // VS: Change this for android! | 244 // VS: Change this for android! |
| 247 return cvox.ChromeVox.host['mathMap'].evaluate( | 245 return cvox.ChromeVox.host['mathMap'].evaluate( |
| 248 text, | 246 text, cvox.TraverseMath.getInstance().domain, |
| 249 cvox.TraverseMath.getInstance().domain, | |
| 250 cvox.TraverseMath.getInstance().style); | 247 cvox.TraverseMath.getInstance().style); |
| 251 } | 248 } |
| 252 return new cvox.NavMathDescription( | 249 return new cvox.NavMathDescription({ |
| 253 {'text': text, | 250 'text': text, |
| 254 'domain': cvox.TraverseMath.getInstance().domain, | 251 'domain': cvox.TraverseMath.getInstance().domain, |
| 255 'style': cvox.TraverseMath.getInstance().style | 252 'style': cvox.TraverseMath.getInstance().style |
| 256 }); | 253 }); |
| 257 }; | 254 }; |
| 258 | 255 |
| 259 | 256 |
| 260 /** | 257 /** |
| 261 * Removes all empty strings from an array of strings. | 258 * Removes all empty strings from an array of strings. |
| 262 * @param {Array<string>} strs An array of strings. | 259 * @param {Array<string>} strs An array of strings. |
| 263 * @return {Array<string>} The cleaned array. | 260 * @return {Array<string>} The cleaned array. |
| 264 * @private | 261 * @private |
| 265 */ | 262 */ |
| 266 cvox.MathStore.removeEmpty_ = function(strs) { | 263 cvox.MathStore.removeEmpty_ = function(strs) { |
| 267 return strs.filter(function(str) {return str;}); | 264 return strs.filter(function(str) { |
| 265 return str; |
| 266 }); |
| 268 }; | 267 }; |
| OLD | NEW |