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 Implementation of the speech rule engine. | 6 * @fileoverview Implementation of the speech rule engine. |
7 * | 7 * |
8 * The speech rule engine chooses and applies speech rules. Rules are chosen | 8 * The speech rule engine chooses and applies speech rules. Rules are chosen |
9 * from a set of rule stores wrt. their applicability to a node in a particular | 9 * from a set of rule stores wrt. their applicability to a node in a particular |
10 * markup type such as MathML or HTML. Rules are dispatched either by | 10 * markup type such as MathML or HTML. Rules are dispatched either by |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 // value from the node. | 101 // value from the node. |
102 return cvox.XpathUtil.evaluateString(expr, node); | 102 return cvox.XpathUtil.evaluateString(expr, node); |
103 }; | 103 }; |
104 | 104 |
105 | 105 |
106 // Dispatch functionality. | 106 // Dispatch functionality. |
107 /** | 107 /** |
108 * Computes a speech object for a given node. Returns the empty list if | 108 * Computes a speech object for a given node. Returns the empty list if |
109 * no node is given. | 109 * no node is given. |
110 * @param {Node} node The node to be evaluated. | 110 * @param {Node} node The node to be evaluated. |
111 * @return {!Array.<cvox.NavDescription>} A list of navigation descriptions for | 111 * @return {!Array<cvox.NavDescription>} A list of navigation descriptions for |
112 * that node. | 112 * that node. |
113 */ | 113 */ |
114 cvox.SpeechRuleEngine.prototype.evaluateNode = function(node) { | 114 cvox.SpeechRuleEngine.prototype.evaluateNode = function(node) { |
115 if (!node) { | 115 if (!node) { |
116 return []; | 116 return []; |
117 } | 117 } |
118 return this.evaluateTree_(node); | 118 return this.evaluateTree_(node); |
119 }; | 119 }; |
120 | 120 |
121 | 121 |
122 /** | 122 /** |
123 * Applies rules recursively to compute the final speech object. | 123 * Applies rules recursively to compute the final speech object. |
124 * @param {!Node} node Node to apply the speech rule to. | 124 * @param {!Node} node Node to apply the speech rule to. |
125 * @return {!Array.<cvox.NavDescription>} A list of Navigation descriptions. | 125 * @return {!Array<cvox.NavDescription>} A list of Navigation descriptions. |
126 * @private | 126 * @private |
127 */ | 127 */ |
128 cvox.SpeechRuleEngine.prototype.evaluateTree_ = function(node) { | 128 cvox.SpeechRuleEngine.prototype.evaluateTree_ = function(node) { |
129 var rule = this.activeStore_.lookupRule(node, this.dynamicCstr); | 129 var rule = this.activeStore_.lookupRule(node, this.dynamicCstr); |
130 if (!rule) { | 130 if (!rule) { |
131 return this.activeStore_.evaluateDefault(node); | 131 return this.activeStore_.evaluateDefault(node); |
132 } | 132 } |
133 var components = rule.action.components; | 133 var components = rule.action.components; |
134 var result = []; | 134 var result = []; |
135 for (var i = 0, component; component = components[i]; i++) { | 135 for (var i = 0, component; component = components[i]; i++) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } | 172 } |
173 // Adding personality to the nav descriptions. | 173 // Adding personality to the nav descriptions. |
174 result = result.concat(this.addPersonality_(navs, component)); | 174 result = result.concat(this.addPersonality_(navs, component)); |
175 } | 175 } |
176 return result; | 176 return result; |
177 }; | 177 }; |
178 | 178 |
179 | 179 |
180 /** | 180 /** |
181 * Evaluates a list of nodes into a list of navigation descriptions. | 181 * Evaluates a list of nodes into a list of navigation descriptions. |
182 * @param {!Array.<Node>} nodes Array of nodes. | 182 * @param {!Array<Node>} nodes Array of nodes. |
183 * @param {string} sepFunc Name of a function used to compute a separator | 183 * @param {string} sepFunc Name of a function used to compute a separator |
184 * between every element. | 184 * between every element. |
185 * @param {string} separator A string that is used as argument to the sepFunc or | 185 * @param {string} separator A string that is used as argument to the sepFunc or |
186 * interspersed directly between each node if sepFunc is not supplied. | 186 * interspersed directly between each node if sepFunc is not supplied. |
187 * @param {string} ctxtFunc Name of a function applied to compute the context | 187 * @param {string} ctxtFunc Name of a function applied to compute the context |
188 * for every element in the list. | 188 * for every element in the list. |
189 * @param {string} context Additional context string that is given to the | 189 * @param {string} context Additional context string that is given to the |
190 * ctxtFunc function or used directly if ctxtFunc is not supplied. | 190 * ctxtFunc function or used directly if ctxtFunc is not supplied. |
191 * @return {Array.<cvox.NavDescription>} A list of Navigation descriptions. | 191 * @return {Array<cvox.NavDescription>} A list of Navigation descriptions. |
192 * @private | 192 * @private |
193 */ | 193 */ |
194 cvox.SpeechRuleEngine.prototype.evaluateNodeList_ = function( | 194 cvox.SpeechRuleEngine.prototype.evaluateNodeList_ = function( |
195 nodes, sepFunc, separator, ctxtFunc, context) { | 195 nodes, sepFunc, separator, ctxtFunc, context) { |
196 if (nodes == []) { | 196 if (nodes == []) { |
197 return []; | 197 return []; |
198 } | 198 } |
199 var sep = separator || ''; | 199 var sep = separator || ''; |
200 var cont = context || ''; | 200 var cont = context || ''; |
201 var cFunc = this.activeStore_.contextFunctions.lookup(ctxtFunc); | 201 var cFunc = this.activeStore_.contextFunctions.lookup(ctxtFunc); |
(...skipping 28 matching lines...) Expand all Loading... |
230 */ | 230 */ |
231 cvox.SpeechRuleEngine.propMap = {'pitch': cvox.AbstractTts.RELATIVE_PITCH, | 231 cvox.SpeechRuleEngine.propMap = {'pitch': cvox.AbstractTts.RELATIVE_PITCH, |
232 'rate': cvox.AbstractTts.RELATIVE_RATE, | 232 'rate': cvox.AbstractTts.RELATIVE_RATE, |
233 'volume': cvox.AbstractTts.RELATIVE_VOLUME, | 233 'volume': cvox.AbstractTts.RELATIVE_VOLUME, |
234 'pause': cvox.AbstractTts.PAUSE | 234 'pause': cvox.AbstractTts.PAUSE |
235 }; | 235 }; |
236 | 236 |
237 | 237 |
238 /** | 238 /** |
239 * Adds personality to every Navigation Descriptions in input list. | 239 * Adds personality to every Navigation Descriptions in input list. |
240 * @param {Array.<cvox.NavDescription>} navs A list of Navigation descriptions. | 240 * @param {Array<cvox.NavDescription>} navs A list of Navigation descriptions. |
241 * @param {Object} props Property dictionary. | 241 * @param {Object} props Property dictionary. |
242 * TODO (sorge) Fully specify, when we have finalised the speech rule | 242 * TODO (sorge) Fully specify, when we have finalised the speech rule |
243 * format. | 243 * format. |
244 * @return {Array.<cvox.NavDescription>} The modified array. | 244 * @return {Array<cvox.NavDescription>} The modified array. |
245 * @private | 245 * @private |
246 */ | 246 */ |
247 cvox.SpeechRuleEngine.prototype.addPersonality_ = function(navs, props) { | 247 cvox.SpeechRuleEngine.prototype.addPersonality_ = function(navs, props) { |
248 var personality = {}; | 248 var personality = {}; |
249 for (var key in cvox.SpeechRuleEngine.propMap) { | 249 for (var key in cvox.SpeechRuleEngine.propMap) { |
250 var value = parseFloat(props[key]); | 250 var value = parseFloat(props[key]); |
251 if (!isNaN(value)) { | 251 if (!isNaN(value)) { |
252 personality[cvox.SpeechRuleEngine.propMap[key]] = value; | 252 personality[cvox.SpeechRuleEngine.propMap[key]] = value; |
253 } | 253 } |
254 } | 254 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 */ | 362 */ |
363 cvox.SpeechRuleEngine.debugNamedSpeechRule = function(name, node) { | 363 cvox.SpeechRuleEngine.debugNamedSpeechRule = function(name, node) { |
364 var store = cvox.SpeechRuleEngine.getInstance().activeStore_; | 364 var store = cvox.SpeechRuleEngine.getInstance().activeStore_; |
365 var allRules = store.findAllRules( | 365 var allRules = store.findAllRules( |
366 function(rule) {return rule.name == name;}); | 366 function(rule) {return rule.name == name;}); |
367 for (var i = 0, rule; rule = allRules[i]; i++) { | 367 for (var i = 0, rule; rule = allRules[i]; i++) { |
368 cvox.SpeechRuleEngine.outputDebug('Rule', name, 'number', i); | 368 cvox.SpeechRuleEngine.outputDebug('Rule', name, 'number', i); |
369 cvox.SpeechRuleEngine.debugSpeechRule(rule, node); | 369 cvox.SpeechRuleEngine.debugSpeechRule(rule, node); |
370 } | 370 } |
371 }; | 371 }; |
OLD | NEW |