| 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 stores for the basic components of math expressions: | 6 * @fileoverview Rule stores for the basic components of math expressions: |
| 7 * Unicode symbols and functions. | 7 * Unicode symbols and functions. |
| 8 * | 8 * |
| 9 * The idea of these stores is to provide a more efficient data structure to | 9 * The idea of these stores is to provide a more efficient data structure to |
| 10 * look up rules in the background page than the usual flat array of rules | 10 * look up rules in the background page than the usual flat array of rules |
| 11 * implemented by other stores. | 11 * implemented by other stores. |
| 12 * | 12 * |
| 13 */ | 13 */ |
| 14 | 14 |
| 15 goog.provide('cvox.MathCompoundStore'); | 15 goog.provide('cvox.MathCompoundStore'); |
| 16 goog.provide('cvox.MathSimpleStore'); | 16 goog.provide('cvox.MathSimpleStore'); |
| 17 | 17 |
| 18 goog.require('cvox.MathStore'); | 18 goog.require('cvox.MathStore'); |
| 19 goog.require('cvox.SpeechRule'); | 19 goog.require('cvox.SpeechRule'); |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * A base store for simple Math objects. | 22 * A base store for simple Math objects. |
| 23 * @constructor | 23 * @constructor |
| 24 * @extends {cvox.MathStore} | 24 * @extends {cvox.MathStore} |
| 25 */ | 25 */ |
| 26 cvox.MathSimpleStore = function() { | 26 cvox.MathSimpleStore = function() { |
| 27 goog.base(this); | 27 goog.base(this); |
| 28 }; | 28 }; |
| 29 goog.inherits(cvox.MathSimpleStore, cvox.MathStore); | 29 goog.inherits(cvox.MathSimpleStore, cvox.MathStore); |
| 30 | 30 |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Turns a domain mapping from its JSON representation containing simple strings | 33 * Turns a domain mapping from its JSON representation containing simple strings |
| 34 * only into a list of speech rules. | 34 * only into a list of speech rules. |
| 35 * @param {string} name Name for the rules. | 35 * @param {string} name Name for the rules. |
| 36 * @param {string} str String for precondition and constraints. | 36 * @param {string} str String for precondition and constraints. |
| 37 * @param {Object<Object<string>>} mapping Simple string | 37 * @param {Object<Object<string>>} mapping Simple string |
| 38 * mapping. | 38 * mapping. |
| 39 */ | 39 */ |
| 40 cvox.MathSimpleStore.prototype.defineRulesFromMappings = function( | 40 cvox.MathSimpleStore.prototype.defineRulesFromMappings = function( |
| 41 name, str, mapping) { | 41 name, str, mapping) { |
| 42 for (var domain in mapping) { | 42 for (var domain in mapping) { |
| 43 for (var style in mapping[domain]) { | 43 for (var style in mapping[domain]) { |
| 44 var content = mapping[domain][style]; | 44 var content = mapping[domain][style]; |
| 45 var cstr = 'self::text() = "' + str + '"'; | 45 var cstr = 'self::text() = "' + str + '"'; |
| 46 var rule = this.defineRule( | 46 var rule = this.defineRule( |
| 47 name, domain + '.' + style, '[t] "' + content + '"', | 47 name, domain + '.' + style, '[t] "' + content + '"', 'self::text()', |
| 48 'self::text()', cstr); | 48 cstr); |
| 49 } | 49 } |
| 50 } | 50 } |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 | 53 |
| 54 /** | 54 /** |
| 55 * A compound store for simple Math objects. | 55 * A compound store for simple Math objects. |
| 56 * @constructor | 56 * @constructor |
| 57 */ | 57 */ |
| 58 cvox.MathCompoundStore = function() { | 58 cvox.MathCompoundStore = function() { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 * constraints. These are matched against properties of a rule. | 128 * constraints. These are matched against properties of a rule. |
| 129 * @return {!string} The string resulting from the action of speech rule. | 129 * @return {!string} The string resulting from the action of speech rule. |
| 130 */ | 130 */ |
| 131 cvox.MathCompoundStore.prototype.lookupString = function(text, dynamic) { | 131 cvox.MathCompoundStore.prototype.lookupString = function(text, dynamic) { |
| 132 var textNode = document.createTextNode(text); | 132 var textNode = document.createTextNode(text); |
| 133 var rule = this.lookupRule(textNode, dynamic); | 133 var rule = this.lookupRule(textNode, dynamic); |
| 134 if (!rule) { | 134 if (!rule) { |
| 135 return ''; | 135 return ''; |
| 136 } | 136 } |
| 137 return rule.action.components | 137 return rule.action.components |
| 138 .map(function(comp) { | 138 .map(function(comp) { |
| 139 return comp.content.slice(1, -1);}) | 139 return comp.content.slice(1, -1); |
| 140 .join(' '); | 140 }) |
| 141 .join(' '); |
| 141 }; | 142 }; |
| 142 | 143 |
| 143 | 144 |
| 144 /** | 145 /** |
| 145 * Get a set of all dynamic constraint values. | 146 * Get a set of all dynamic constraint values. |
| 146 * @return {!Object<cvox.SpeechRule.DynamicCstrAttrib, Array<string>>} The | 147 * @return {!Object<cvox.SpeechRule.DynamicCstrAttrib, Array<string>>} The |
| 147 * object with all annotations. | 148 * object with all annotations. |
| 148 */ | 149 */ |
| 149 cvox.MathCompoundStore.prototype.getDynamicConstraintValues = function() { | 150 cvox.MathCompoundStore.prototype.getDynamicConstraintValues = function() { |
| 150 var newCstr = {}; | 151 var newCstr = {}; |
| 151 for (var store in this.subStores_) { | 152 for (var store in this.subStores_) { |
| 152 var cstr = this.subStores_[store].getDynamicConstraintValues(); | 153 var cstr = this.subStores_[store].getDynamicConstraintValues(); |
| 153 for (var key in cstr) { | 154 for (var key in cstr) { |
| 154 var set = newCstr[key]; | 155 var set = newCstr[key]; |
| 155 if (set) { | 156 if (set) { |
| 156 newCstr[key] = cvox.MathUtil.union(set, cstr[key]); | 157 newCstr[key] = cvox.MathUtil.union(set, cstr[key]); |
| 157 } else { | 158 } else { |
| 158 newCstr[key] = cstr[key]; | 159 newCstr[key] = cstr[key]; |
| 159 } | 160 } |
| 160 } | 161 } |
| 161 } | 162 } |
| 162 return newCstr; | 163 return newCstr; |
| 163 }; | 164 }; |
| 164 | 165 |
| 165 | 166 |
| 166 /** | 167 /** |
| 167 * Parses a string with a hex representatino of a unicode code point into the | 168 * Parses a string with a hex representatino of a unicode code point into the |
| 168 * corresponding unicode character. | 169 * corresponding unicode character. |
| 169 * @param {string} number The code point to be parsed. | 170 * @param {string} number The code point to be parsed. |
| 170 * @return {string} The unicode character. | 171 * @return {string} The unicode character. |
| 171 * @private | 172 * @private |
| 172 */ | 173 */ |
| 173 cvox.MathSimpleStore.parseUnicode_ = function(number) { | 174 cvox.MathSimpleStore.parseUnicode_ = function(number) { |
| 174 var keyValue = parseInt(number, 16); | 175 var keyValue = parseInt(number, 16); |
| 175 if (keyValue < 0x10000) { | 176 if (keyValue < 0x10000) { |
| 176 return String.fromCharCode(keyValue); | 177 return String.fromCharCode(keyValue); |
| 177 } | 178 } |
| 178 keyValue -= 0x10000; | 179 keyValue -= 0x10000; |
| 179 var hiSurrogate = (keyValue >> 10) + 0xD800; | 180 var hiSurrogate = (keyValue >> 10) + 0xD800; |
| 180 var lowSurrogate = (keyValue & 0x3FF) + 0xDC00; | 181 var lowSurrogate = (keyValue & 0x3FF) + 0xDC00; |
| 181 return String.fromCharCode(hiSurrogate, lowSurrogate); | 182 return String.fromCharCode(hiSurrogate, lowSurrogate); |
| 182 }; | 183 }; |
| OLD | NEW |