+/**
+ * Copyright 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * vc-webview-en_GB.js
+ *
+ * This source code is a language specified script for English(en_GB).
+ */
+
+/**
+ * vc_search_word function found the text with similarity calculated using words in the @param.
+ *
+ * @param param param from voice-control.
+ * @param replace If replace true, function correct some confused charaters and try again.
+ */
+function vc_search_word(param, replace) {
+ /* phase 2. search partial word in the webpage */
+ /* First, compare with links in html documents */
+ if (vc_flag_log == true) {
+ vc_rec_result.style.background = 'rgba(0, 100, 200, 1)';
+ }
+ var resultTokenArr = param.split(' ');
+ var threshold = resultTokenArr.length * 0.3;
+ var temp = [];
+ var el = undefined;
+
+ vc_print_log('=== start vc_search_word');
+
+ for (var i = 0; i < vc_text_indicators.length; ++i) {
+ var text = vc_text_indicators[i].textContent.replace(/[ `~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase();
+ temp[i] = 0;
+
+ for (var j = 0; j < resultTokenArr.length; j++) {
+ if (vc_is_visible(vc_text_indicators[i], vc_scr, true) && text.indexOf(resultTokenArr[j].toLowerCase()) != -1) {
+ temp[i]++;
+ }
+ }
+ }
+
+ var max = -1;
+
+ for (var i = 0; i < vc_text_indicators.length; i++) {
+ if (temp[i] >= threshold && (max == -1 || temp[i] > temp[max])) {
+ el = vc_text_indicators[i];
+ max = i;
+ }
+ }
+
+ if (el != undefined) {
+ return el;
+ }
+
+ /* Second, compare with whole text of elements in html documents */
+ var result = [];
+ for (var i = 0; i < resultTokenArr.length; i++) {
+ var obj = vc_selector([resultTokenArr[i]]);
+ for (var j = 0; j < obj.length; j++) {
+ temp = obj[j];
+ if (temp.childElementCount === 0) {
+ if (temp.hasAttribute('vc_count') == false) {
+ temp.setAttribute('vc_count', 1);
+ result.push(temp);
+ } else {
+ temp.setAttribute('vc_count', parseInt(temp.getAttribute('vc_count')) + 1);
+ }
+ }
+ }
+ }
+
+ for (var i = 0; i < result.length; i++) {
+ var vccnt = parseInt(result[i].getAttribute('vc_count'));
+ if (vccnt >= threshold && (el == undefined || vccnt > parseInt(el.getAttribute('vc_count')))) {
+ el = result[i];
+ }
+ }
+
+ for (var i = 0; i < result.length; i++) {
+ result[i].removeAttribute('vc_count');
+ }
+
+ if (el != undefined) {
+ return el;
+ }
+
+ return vc_search_character(param);
+}
+
+/**
+ * vc_search_word function found the text with similarity calculated using characters in the @param.
+ *
+ * @param param param from voice-control.
+ */
+function vc_search_character(param) {
+ vc_print_log('=== start searching(character level)');
+ if (vc_flag_log == true) {
+ vc_rec_result.style.background = 'rgba(200, 100, 0, 1)';
+ }
+ var similarity = [];
+ var threshold = 0;
+
+ param = param.toLowerCase().split(' ');
+ for (var i = 0; i < vc_all_elem.length; i++) {
+ if (vc_all_elem[i].childElementCount == 0 && vc_is_visible(vc_all_elem[i]) == true) {
+ var text = vc_all_elem[i].textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ' ').toLowerCase().split(' ');
+ var score = 0;
+
+ for(var k = 0; k < param.length; k++) {
+ var min = 2147483647;
+
+ if (3 > param[k].length) {
+ continue;
+ }
+
+ for(var j = 0; j < text.length; j++) {
+ if (3 > text[j].length) {
+ continue;
+ }
+ var cost = [];
+ var ncost = [];
+ var longStr;
+ var shortStr;
+
+ if (text[j].length > param[k].length) {
+ longStr = text[j];
+ shortStr = param[k];
+ } else {
+ longStr = param[k];
+ shortStr = text[j];
+ }
+
+ for (var l = 0; l <= longStr.length; l++) {
+ cost[l] = l;
+ }
+
+ for (var s = 1; s <= shortStr.length; s++) {
+ ncost[0] = s;
+
+ for (var l = 1; l <= longStr.length; l++) {
+ var match = shortStr[s - 1] == longStr[l - 1] ? 0 : 1;
+
+ var rep = cost[l - 1] + match;
+ var ins = cost[l] + 1;
+ var del = ncost[l - 1] + 1;
+
+ ncost[l] = Math.min(rep, ins, del);
+ }
+
+ var arr = cost;
+ cost = ncost;
+ ncost = arr;
+ }
+
+ if (param[k].length * 0.25 >= cost[longStr.length] && min > cost[longStr.length]) {
+ min = cost[longStr.length];
+ }
+ }
+
+ if (min < 2147483647) {
+ score = score + min;
+ } else {
+ score = score + param[k].length;
+ }
+ }
+
+ similarity.push(score);
+ } else {
+ similarity.push(2147483647);
+ }
+ }
+
+ vc_print_log('=== finish searching(character level)');
+
+ var min = 2147483647;
+ for (var i = 0; i < similarity.length; i++) {
+ if (min > similarity[i]) {
+ min = similarity[i];
+ }
+ }
+
+ for (var i = 0; i < param.length; i++) {
+ if (3 <= param[i].length) {
+ threshold = threshold + param[i].length;
+ }
+ }
+
+ if (threshold > min) {
+ return vc_all_elem[similarity.indexOf(min)];
+ }
+
+ return undefined;
+}
+
+/**
+ * vc_search_word function found the text with similarity calculated using pronunciation keys in the @param.
+ *
+ * @param param param from voice-control.
+ */
+function vc_search_pronunciation(param) {
+ var el = undefined;
+
+ /* TODO: Fill the logic here to find the link or text include the pronunciation keys in param.
+ * If you find the link or text, then allocate that into el.
+ * If the language does not need to pronunciation comapring, you can erase this function.
+ */
+
+ return el;
+}
+
+/**
+ * vc_is_included_number function separate a number value from the @text.
+ *
+ * @param text text string from voice-control-webview.cpp.
+ */
+function vc_is_included_number(text) {
+ var numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
+ 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty',
+ 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty',
+ 'thirty one', 'thirty two', 'thirty three', 'thirty four', 'thirty five', 'thirty six', 'thirty seven', 'thirty eight', 'thirty nine', 'forty',
+ 'forty one', 'forty two', 'forty three', 'forty four', 'forty five', 'forty six', 'forty seven', 'forty eight', 'forty nine', 'fifty',
+ 'fifty one', 'fifty two', 'fifty three', 'fifty four', 'fifty five', 'fifty six', 'fifty seven', 'fifty eight', 'fifty nine', 'sixty',
+ 'sixty one', 'sixty two', 'sixty three', 'sixty four', 'sixty five', 'sixty six', 'sixty seven', 'sixty eight', 'sixty nine', 'seventy',
+ 'seventy one', 'seventy two', 'seventy three', 'seventy four', 'seventy five', 'seventy six', 'seventy seven', 'seventy eight', 'seventy nine', 'eighty',
+ 'eighty one', 'eighty two', 'eighty three', 'eighty four', 'eighty five', 'eighty six', 'eighty seven', 'eighty eight', 'eighty nine', 'ninety',
+ 'ninety one', 'ninety two', 'ninety three', 'ninety four', 'ninety five', 'ninety six', 'ninety seven', 'ninety eight', 'ninety nine', 'hundred'];
+ var convert = text.toLowerCase();
+ var result;
+
+ for (var i = 0; numbers.length > i; i++) {
+ if (true == convert.startsWith(numbers[i]) && (' ' == text[numbers[i].length] || text.length == numbers[i].length)) {
+ var partial = text.substr(numbers[i].length);
+
+ if (vc_visible_hints[i].type == 'input' || text.length == numbers[i].length) {
+ result = {
+ cmd : (i + 1),
+ param : partial.trim()
+ };
+ return result;
+ }
+ }
+ }
+
+ if (true == convert.startsWith('to') && (' ' == text[2] || text.length == 2)) {
+ if (vc_visible_hints[1].type == 'input' || text.length == 2) {
+ result = {
+ cmd : 2,
+ param : text.substr(3).trim()
+ };
+ return result;
+ }
+ } else if (true == convert.startsWith('for') && (' ' == text[3] || text.length == 3)) {
+ if (vc_visible_hints[3].type == 'input' || text.length == 3) {
+ result = {
+ cmd : 4,
+ param : text.substr(4).trim()
+ };
+ return result;
+ }
+ }
+
+ result = {
+ cmd : NaN,
+ param : text.trim()
+ };
+
+ return result;
+}
+
+/**
+ * vc_correct_parameter function correct the voice recognition result.
+ *
+ * @param text text string from voice-control-webview.cpp.
+ */
+function vc_correct_parameter(text) {
+ var result = vc_is_included_number(text),
+ words = result.param.split(' ');
+
+ if (isNaN(result.cmd) == true && isNaN(words[0]) == false) {
+ result.cmd = parseFloat(words[0]);
+ result.param = result.param.substr(words[0].length + 1).trim();
+ }
+
+ var idx_sep;
+ if (isNaN(result.cmd) == true) {
+ if (0 < (idx_sep = result.param.includes('st ')) && !isNaN(result.param.substr(0, idx_sep))) {
+ if (idx_sep == result.param.length - 1) {
+ result.cmd = parseFloat(result.param.substr(0, idx_sep));
+ result.param = '';
+ } else {
+ result.cmd = parseFloat(result.param.substr(0, idx_sep));
+ result.param = result.param.substr(idx_sep + 3, result.param.length);
+ }
+ } else if (0 < (idx_sep = result.param.includes('nd ')) && !isNaN(result.param.substr(0, idx_sep))) {
+ if (idx_sep == result.param.length - 1) {
+ result.cmd = parseFloat(result.param.substr(0, idx_sep));
+ result.param = '';
+ } else {
+ result.cmd = parseFloat(result.param.substr(0, idx_sep));
+ result.param = result.param.substr(idx_sep + 3, result.param.length);
+ }
+ } else if (0 < (idx_sep = result.param.includes('th ')) && !isNaN(result.param.substr(0, idx_sep))) {
+ if (idx_sep == result.param.length - 1) {
+ result.cmd = parseFloat(result.param.substr(0, idx_sep));
+ result.param = '';
+ } else {
+ result.cmd = parseFloat(result.param.substr(0, idx_sep));
+ result.param = result.param.substr(idx_sep + 3, result.param.length);
+ }
+ }
+ }
+
+ return result
+}
+
+/**
+ * vc_check_web_control function check some special keyword and run it.
+ *
+ * @param spokenWord voice recognized result string.
+ */
+function vc_check_web_control(text) {
+ text = text.toLowerCase();
+ var convert = text.replace(/ /g, '');
+ var googleSearch = 'search google';
+ var googleSearch2 = ['search', 'on google'];
+ var youtubeSearch = 'search youtube';
+ var youtubeSearch2 = ['search', 'on youtube'];
+
+ if (text.startsWith(googleSearch) == true) {
+ location.href = 'https://www.google.com/search?q=' + (text.substr(googleSearch.length)).trim();
+ } else if (text.startsWith(googleSearch2[0]) && text.endsWith(googleSearch2[1])) {
+ var query = text.substr(googleSearch2[0].length, text.length - googleSearch2[0].length - googleSearch2[1].length);
+ location.href = 'https://www.google.com/search?q=' + query.trim();
+ } else if (text.startsWith(youtubeSearch) == true) {
+ location.href = 'https://www.youtube.com/results?search_query=' + (text.substr(youtubeSearch.length)).trim();
+ } else if (text.startsWith(youtubeSearch2[0]) && text.endsWith(youtubeSearch2[1])) {
+ var query = text.substr(youtubeSearch2[0].length, text.length - youtubeSearch2[0].length - youtubeSearch2[1].length);
+ location.href = 'https://www.youtube.com/results?search_query=' + query.trim();
+ } else if (convert == 'refresh' || convert == 'reload') {
+ location.reload();
+ } else if (convert == 'google') {
+ location.href = 'https://www.google.com/';
+ } else if (convert == 'facebook') {
+ location.href = 'https://www.facebook.com/';
+ } else if (convert == 'amazon') {
+ location.href = 'https://www.amazon.com/';
+ } else if (convert == 'yahoo') {
+ location.href = 'https://www.yahoo.com/';
+ } else if (convert == 'youtube') {
+ location.href = 'https://www.youtube.com/';
+ } else if (convert == 'scrolldown') {
+ vc_scroll_event_firing('DOWN');
+ } else if (convert == 'scrollup') {
+ vc_scroll_event_firing('UP');
+ } else if (convert == 'tothetop') {
+ vc_scroll_event_firing('TOP');
+ } else if (convert == 'showsourcecode') {
+ vc_print_html();
+ } else if (convert == 'next' || convert == 'forward') {
+ history.forward();
+ } else if (convert == 'before' || convert == 'back' || convert == 'previous') {
+ history.back();
+ } else if (convert == 'playlog' && vc_flag_log) {
+ if (vc_log_area.style.visibility == 'visible') vc_log_area.style.visibility = 'hidden';
+ else vc_log_area.style.visibility = 'visible';
+ } else {
+ return false;
+ }
+ return true;
+}
\ No newline at end of file