Commit 1ecb9c55 authored by Akihiro Ota's avatar Akihiro Ota Committed by Commit Bot

ChromeVox: Language switching test infrastructure clean up.

This does two things:
1. Cleans up the language switching code to use consistent terms. Most
notably, it changes all instances of "inner-node switching" to
"sub-node switching".
2. Cleans up the language switching test infrastructure. A test
naming scheme was implemented to easily communicate the purpose of
each test. Also, tests were added to cover the two language switching
behaviors: node-level and sub-node-level switching. Each document
in the test databank is tested using node-level and sub-node-level
switching.

Bug: 923068
Change-Id: Ia16b785381966d97e089aae5dc08e23173876d08
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1754703
Commit-Queue: Akihiro Ota <akihiroota@chromium.org>
Reviewed-by: default avatarAlice Boxhall <aboxhall@chromium.org>
Reviewed-by: default avatarChris Hall <chrishall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690998}
parent e2dfd8ad
......@@ -5,29 +5,30 @@
/**
* @fileoverview Provides language switching services for ChromeVox, which
* uses language detection information to automatically change the ChromeVox
* output language.
* output voice.
*/
goog.provide('LanguageSwitching');
/**
* The current output language. Initialize to the language of the browser or
* empty string if unavailable.
* empty string if the former is unavailable.
* @private {string}
*/
LanguageSwitching.currentLanguage_ =
chrome.i18n.getUILanguage().toLowerCase() || '';
/**
* Confidence threshold to meet before assigning inner-node language.
* Confidence threshold to meet before assigning sub-node language.
* @const
* @private {number}
*/
LanguageSwitching.PROBABILITY_THRESHOLD = 0.9;
LanguageSwitching.PROBABILITY_THRESHOLD_ = 0.9;
/**
* Stores whether or not ChromeVox inner-node language switching is enabled or
* disabled.
* Stores whether or not ChromeVox sub-node language switching is enabled.
* Set to false as default, as sub-node language detection is still
* experimental.
* @private {boolean}
*/
LanguageSwitching.sub_node_switching_enabled_ = false;
......@@ -36,8 +37,7 @@ LanguageSwitching.sub_node_switching_enabled_ = false;
* Initialization function for language switching.
*/
LanguageSwitching.init = function() {
// Enable inner-node language switching if inner-node switching feature flag
// is enabled.
// Enable sub-node language switching if feature flag is enabled.
chrome.commandLinePrivate.hasSwitch(
'enable-experimental-accessibility-chromevox-sub-node-language-' +
'switching',
......@@ -64,7 +64,6 @@ LanguageSwitching.assignLanguagesForStringAttribute = function(
return;
var stringAttributeValue = node[stringAttribute];
var languageAnnotation;
// Quick note:
// The decideNewLanguage function, which contains the core language switching
// logic, is setup to prefer sub-node switching if the detected language's
......@@ -74,18 +73,10 @@ LanguageSwitching.assignLanguagesForStringAttribute = function(
languageAnnotation =
node.languageAnnotationForStringAttribute(stringAttribute);
} else {
// Use node-level language switching if inner-node switching is
// disabled. We use this logic because the API is disabled when sub-node
// switching is disabled.
var nodeLevelLanguageData = {};
// Ensure that we span the entire stringAttributeValue.
nodeLevelLanguageData.startIndex = 0;
nodeLevelLanguageData.endIndex = stringAttributeValue.length;
// To force the decideNewLanguage function to use node-level switching, we
// pass in an empty language with probability that doesn't exceed the
// PROBABILITY_THRESHOLD. As previously mentioned, if we are not confident
// enough in inner-node detected languages, we fall back on node-level
// language.
nodeLevelLanguageData.language = '';
nodeLevelLanguageData.probability = 0;
languageAnnotation = [nodeLevelLanguageData];
......@@ -100,9 +91,10 @@ LanguageSwitching.assignLanguagesForStringAttribute = function(
stringAttributeValue || '', LanguageSwitching.currentLanguage_);
return;
}
// Split output based on language annotation.
// Each object in languageAnnotation contains a language, probability,
// and start/end indices that define a substring.
// and start/end indices that define a substring of stringAttributeValue.
for (var i = 0; i < languageAnnotation.length; ++i) {
var speechProps = new Output.SpeechProperties();
var startIndex = languageAnnotation[i].startIndex;
......@@ -130,14 +122,14 @@ LanguageSwitching.assignLanguagesForStringAttribute = function(
/**
* Run error checks on language data and decide new output language.
* @param {!AutomationNode} node
* @param {string} innerNodeLanguage
* @param {string} subNodeLanguage
* @param {number} probability
* @return {string}
*/
LanguageSwitching.decideNewLanguage = function(
node, innerNodeLanguage, probability) {
node, subNodeLanguage, probability) {
// Use the following priority rankings when deciding language.
// 1. Inner-node language. If we can detect inner-node language with a high
// 1. Sub-node language. If we can detect sub-node language with a high
// enough probability of accuracy, then we should use it.
// 2. Node-level detected language.
// 3. Author-provided language. This language is also assigned at the node
......@@ -145,9 +137,9 @@ LanguageSwitching.decideNewLanguage = function(
// 4. LanguageSwitching.currentLanguage_. If we do not have enough language
// data, then we should not switch languages.
// Use innerNodeLanguage if probability exceeds threshold.
if (probability > LanguageSwitching.PROBABILITY_THRESHOLD)
return innerNodeLanguage.toLowerCase();
// Use subNodeLanguage if probability exceeds threshold.
if (probability > LanguageSwitching.PROBABILITY_THRESHOLD_)
return subNodeLanguage.toLowerCase();
// Use detected language as nodeLevelLanguage, if present.
// If no detected language, use author-provided language.
......@@ -181,7 +173,7 @@ LanguageSwitching.decideNewLanguage = function(
};
/**
* Returns a unicode-aware substring of text from startIndex to endIndex.
* Returns a unicode-aware substring of |text| from startIndex to endIndex.
* @param {string} text
* @param {number} startIndex
* @param {number} endIndex
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment