Add in expansion rules to speak more like how code is read.

This commit is contained in:
Peter Xiao 2013-08-13 16:51:12 -07:00
commit 5ea65fe246

View file

@ -123,6 +123,17 @@ var NO_MATCH_EARCON = 'INVALID_KEYPRESS';
var INSERT_MODE_STATE = 'insertMode'; var INSERT_MODE_STATE = 'insertMode';
var COMMAND_MODE_STATE = 'start'; var COMMAND_MODE_STATE = 'start';
var REPLACE_LIST = [
{
substr: ';',
newSubstr: ' semicolon '
},
{
substr: ':',
newSubstr: ' colon '
}
];
/** /**
* Context menu commands. * Context menu commands.
*/ */
@ -265,14 +276,59 @@ var isWord = function(cursor) {
}; };
/** /**
* A mapping of syntax type to speech properties. * A mapping of syntax type to speech properties / expanding rules.
*/ */
var rules = { var rules = {
'constant': CONSTANT_PROP, 'constant': {
'entity': ENTITY_PROP, prop: CONSTANT_PROP
'keyword': KEYWORD_PROP, },
'storage': STORAGE_PROP, 'entity': {
'variable': VARIABLE_PROP prop: ENTITY_PROP
},
'keyword': {
prop: KEYWORD_PROP
},
'storage': {
prop: STORAGE_PROP
},
'variable': {
prop: VARIABLE_PROP
},
'meta': {
prop: DEFAULT_PROP,
replace: [
{
substr: '<',
newSubstr: ' tag start '
},
{
substr: '>',
newSubstr: ' tag end '
}
]
}
};
/**
* Default rule to be used.
*/
var DEFAULT_RULE = {
prop: DEFAULT_RULE
};
/**
* Expands substrings to how they are read based on the given rules.
* @param {string} value Text to be expanded.
* @param {Array.<Object>} replaceRules Rules to determine expansion.
* @return {string} New expanded value.
*/
var expand = function(value, replaceRules) {
var newValue = value;
for (var i = 0; i < replaceRules.length; i++) {
var replaceRule = replaceRules[i];
newValue = newValue.replace(replaceRule.substr, replaceRule.newSubstr);
}
return newValue;
}; };
/** /**
@ -307,7 +363,7 @@ var mergeLikeTokens = function(tokens) {
for (var i = 1; i < tokens.length; i++) { for (var i = 1; i < tokens.length; i++) {
var lastLikeToken = tokens[lastLikeIndex]; var lastLikeToken = tokens[lastLikeIndex];
var currToken = tokens[i]; var currToken = tokens[i];
if (getTokenProp(lastLikeToken) !== getTokenProp(currToken)) { if (getTokenRule(lastLikeToken) !== getTokenRule(currToken)) {
newTokens.push(mergeTokens(tokens, lastLikeIndex, i)); newTokens.push(mergeTokens(tokens, lastLikeIndex, i));
lastLikeIndex = i; lastLikeIndex = i;
} }
@ -360,7 +416,7 @@ var speakTokenQueue = function(token) {
* @param {!cvoxAce.Token} token Token to speak. * @param {!cvoxAce.Token} token Token to speak.
* Get the token speech property. * Get the token speech property.
*/ */
var getTokenProp = function(token) { var getTokenRule = function(token) {
/* Types are period delimited. In this case, we only syntax speak the outer /* Types are period delimited. In this case, we only syntax speak the outer
* most type of token. */ * most type of token. */
if (!token || !token.type) { if (!token || !token.type) {
@ -371,11 +427,11 @@ var getTokenProp = function(token) {
return; return;
} }
var type = split[0]; var type = split[0];
var prop = rules[type]; var rule = rules[type];
if (!prop) { if (!rule) {
prop = DEFAULT_PROP; return DEFAULT_RULE;
} }
return prop; return rule;
}; };
/** /**
@ -385,21 +441,12 @@ var getTokenProp = function(token) {
* @param {number} queue Queue mode. * @param {number} queue Queue mode.
*/ */
var speakToken_ = function(token, queue) { var speakToken_ = function(token, queue) {
/* Types are period delimited. In this case, we only syntax speak the outer var rule = getTokenRule(token);
* most type of token. */ var value = expand(token.value, REPLACE_LIST);
if (!token || !token.type) { if (rule.replace) {
return; value = expand(value, rule.replace);
} }
var split = token.type.split('.'); cvox.Api.speak(value, queue, rule.prop);
if (split.length === 0) {
return;
}
var type = split[0];
var prop = rules[type];
if (!prop) {
prop = DEFAULT_PROP;
}
cvox.Api.speak(token.value, queue, prop);
}; };
/** /**