2 * Copyright 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * This source code is a language specified script for English(en_GB).
24 * vc_search_word function found the text with similarity calculated using words in the @param.
26 * @param param param from voice-control.
27 * @param replace If replace true, function correct some confused charaters and try again.
29 function vc_search_word(param, replace) {
30 /* phase 2. search partial word in the webpage */
31 /* First, compare with links in html documents */
32 if (vc_flag_log == true) {
33 vc_rec_result.style.background = 'rgba(0, 100, 200, 1)';
35 var resultTokenArr = param.split(' ');
36 var threshold = resultTokenArr.length * 0.3;
40 vc_print_log('=== start vc_search_word');
42 for (var i = 0; i < vc_text_indicators.length; ++i) {
43 var text = vc_text_indicators[i].textContent.replace(/[ `~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase();
46 for (var j = 0; j < resultTokenArr.length; j++) {
47 if (vc_is_visible(vc_text_indicators[i], vc_scr, true) && text.indexOf(resultTokenArr[j].toLowerCase()) != -1) {
55 for (var i = 0; i < vc_text_indicators.length; i++) {
56 if (temp[i] >= threshold && (max == -1 || temp[i] > temp[max])) {
57 el = vc_text_indicators[i];
62 if (el != undefined) {
66 /* Second, compare with whole text of elements in html documents */
68 for (var i = 0; i < resultTokenArr.length; i++) {
69 var obj = vc_selector([resultTokenArr[i]]);
70 for (var j = 0; j < obj.length; j++) {
72 if (temp.childElementCount === 0) {
73 if (temp.hasAttribute('vc_count') == false) {
74 temp.setAttribute('vc_count', 1);
77 temp.setAttribute('vc_count', parseInt(temp.getAttribute('vc_count')) + 1);
83 for (var i = 0; i < result.length; i++) {
84 var vccnt = parseInt(result[i].getAttribute('vc_count'));
85 if (vccnt >= threshold && (el == undefined || vccnt > parseInt(el.getAttribute('vc_count')))) {
90 for (var i = 0; i < result.length; i++) {
91 result[i].removeAttribute('vc_count');
94 if (el != undefined) {
98 return vc_search_character(param);
102 * vc_search_word function found the text with similarity calculated using characters in the @param.
104 * @param param param from voice-control.
106 function vc_search_character(param) {
107 vc_print_log('=== start searching(character level)');
108 if (vc_flag_log == true) {
109 vc_rec_result.style.background = 'rgba(200, 100, 0, 1)';
114 param = param.toLowerCase().split(' ');
115 for (var i = 0; i < vc_all_elem.length; i++) {
116 if (vc_all_elem[i].childElementCount == 0 && vc_is_visible(vc_all_elem[i]) == true) {
117 var text = vc_all_elem[i].textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ' ').toLowerCase().split(' ');
120 for(var k = 0; k < param.length; k++) {
121 var min = 2147483647;
123 if (3 > param[k].length) {
127 for(var j = 0; j < text.length; j++) {
128 if (3 > text[j].length) {
136 if (text[j].length > param[k].length) {
144 for (var l = 0; l <= longStr.length; l++) {
148 for (var s = 1; s <= shortStr.length; s++) {
151 for (var l = 1; l <= longStr.length; l++) {
152 var match = shortStr[s - 1] == longStr[l - 1] ? 0 : 1;
154 var rep = cost[l - 1] + match;
155 var ins = cost[l] + 1;
156 var del = ncost[l - 1] + 1;
158 ncost[l] = Math.min(rep, ins, del);
166 if (param[k].length * 0.25 >= cost[longStr.length] && min > cost[longStr.length]) {
167 min = cost[longStr.length];
171 if (min < 2147483647) {
174 score = score + param[k].length;
178 similarity.push(score);
180 similarity.push(2147483647);
184 vc_print_log('=== finish searching(character level)');
186 var min = 2147483647;
187 for (var i = 0; i < similarity.length; i++) {
188 if (min > similarity[i]) {
193 for (var i = 0; i < param.length; i++) {
194 if (3 <= param[i].length) {
195 threshold = threshold + param[i].length;
199 if (threshold > min) {
200 return vc_all_elem[similarity.indexOf(min)];
207 * vc_search_word function found the text with similarity calculated using pronunciation keys in the @param.
209 * @param param param from voice-control.
211 function vc_search_pronunciation(param) {
214 /* TODO: Fill the logic here to find the link or text include the pronunciation keys in param.
215 * If you find the link or text, then allocate that into el.
216 * If the language does not need to pronunciation comapring, you can erase this function.
223 * vc_is_included_number function separate a number value from the @text.
225 * @param text text string from voice-control-webview.cpp.
227 function vc_is_included_number(text) {
228 var numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
229 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty',
230 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty',
231 'thirty one', 'thirty two', 'thirty three', 'thirty four', 'thirty five', 'thirty six', 'thirty seven', 'thirty eight', 'thirty nine', 'forty',
232 'forty one', 'forty two', 'forty three', 'forty four', 'forty five', 'forty six', 'forty seven', 'forty eight', 'forty nine', 'fifty',
233 'fifty one', 'fifty two', 'fifty three', 'fifty four', 'fifty five', 'fifty six', 'fifty seven', 'fifty eight', 'fifty nine', 'sixty',
234 'sixty one', 'sixty two', 'sixty three', 'sixty four', 'sixty five', 'sixty six', 'sixty seven', 'sixty eight', 'sixty nine', 'seventy',
235 'seventy one', 'seventy two', 'seventy three', 'seventy four', 'seventy five', 'seventy six', 'seventy seven', 'seventy eight', 'seventy nine', 'eighty',
236 'eighty one', 'eighty two', 'eighty three', 'eighty four', 'eighty five', 'eighty six', 'eighty seven', 'eighty eight', 'eighty nine', 'ninety',
237 'ninety one', 'ninety two', 'ninety three', 'ninety four', 'ninety five', 'ninety six', 'ninety seven', 'ninety eight', 'ninety nine', 'hundred'];
238 var convert = text.toLowerCase();
241 for (var i = 0; numbers.length > i; i++) {
242 if (true == convert.startsWith(numbers[i]) && (' ' == text[numbers[i].length] || text.length == numbers[i].length)) {
243 var partial = text.substr(numbers[i].length);
245 if (vc_visible_hints[i].type == 'input' || text.length == numbers[i].length) {
248 param : partial.trim()
255 if (true == convert.startsWith('to') && (' ' == text[2] || text.length == 2)) {
256 if (vc_visible_hints[1].type == 'input' || text.length == 2) {
259 param : text.substr(3).trim()
263 } else if (true == convert.startsWith('for') && (' ' == text[3] || text.length == 3)) {
264 if (vc_visible_hints[3].type == 'input' || text.length == 3) {
267 param : text.substr(4).trim()
282 * vc_correct_parameter function correct the voice recognition result.
284 * @param text text string from voice-control-webview.cpp.
286 function vc_correct_parameter(text) {
287 var result = vc_is_included_number(text),
288 words = result.param.split(' ');
290 if (isNaN(result.cmd) == true && isNaN(words[0]) == false) {
291 result.cmd = parseFloat(words[0]);
292 result.param = result.param.substr(words[0].length + 1).trim();
296 if (isNaN(result.cmd) == true) {
297 if (0 < (idx_sep = result.param.includes('st ')) && !isNaN(result.param.substr(0, idx_sep))) {
298 if (idx_sep == result.param.length - 1) {
299 result.cmd = parseFloat(result.param.substr(0, idx_sep));
302 result.cmd = parseFloat(result.param.substr(0, idx_sep));
303 result.param = result.param.substr(idx_sep + 3, result.param.length);
305 } else if (0 < (idx_sep = result.param.includes('nd ')) && !isNaN(result.param.substr(0, idx_sep))) {
306 if (idx_sep == result.param.length - 1) {
307 result.cmd = parseFloat(result.param.substr(0, idx_sep));
310 result.cmd = parseFloat(result.param.substr(0, idx_sep));
311 result.param = result.param.substr(idx_sep + 3, result.param.length);
313 } else if (0 < (idx_sep = result.param.includes('th ')) && !isNaN(result.param.substr(0, idx_sep))) {
314 if (idx_sep == result.param.length - 1) {
315 result.cmd = parseFloat(result.param.substr(0, idx_sep));
318 result.cmd = parseFloat(result.param.substr(0, idx_sep));
319 result.param = result.param.substr(idx_sep + 3, result.param.length);
328 * vc_check_web_control function check some special keyword and run it.
330 * @param spokenWord voice recognized result string.
332 function vc_check_web_control(text) {
333 text = text.toLowerCase();
334 var convert = text.replace(/ /g, '');
335 var googleSearch = 'search google';
336 var googleSearch2 = ['search', 'on google'];
337 var youtubeSearch = 'search youtube';
338 var youtubeSearch2 = ['search', 'on youtube'];
340 if (text.startsWith(googleSearch) == true) {
341 location.href = 'https://www.google.com/search?q=' + (text.substr(googleSearch.length)).trim();
342 } else if (text.startsWith(googleSearch2[0]) && text.endsWith(googleSearch2[1])) {
343 var query = text.substr(googleSearch2[0].length, text.length - googleSearch2[0].length - googleSearch2[1].length);
344 location.href = 'https://www.google.com/search?q=' + query.trim();
345 } else if (text.startsWith(youtubeSearch) == true) {
346 location.href = 'https://www.youtube.com/results?search_query=' + (text.substr(youtubeSearch.length)).trim();
347 } else if (text.startsWith(youtubeSearch2[0]) && text.endsWith(youtubeSearch2[1])) {
348 var query = text.substr(youtubeSearch2[0].length, text.length - youtubeSearch2[0].length - youtubeSearch2[1].length);
349 location.href = 'https://www.youtube.com/results?search_query=' + query.trim();
350 } else if (convert == 'refresh' || convert == 'reload') {
352 } else if (convert == 'google') {
353 location.href = 'https://www.google.com/';
354 } else if (convert == 'facebook') {
355 location.href = 'https://www.facebook.com/';
356 } else if (convert == 'amazon') {
357 location.href = 'https://www.amazon.com/';
358 } else if (convert == 'yahoo') {
359 location.href = 'https://www.yahoo.com/';
360 } else if (convert == 'youtube') {
361 location.href = 'https://www.youtube.com/';
362 } else if (convert == 'scrolldown') {
363 vc_scroll_event_firing('DOWN');
364 } else if (convert == 'scrollup') {
365 vc_scroll_event_firing('UP');
366 } else if (convert == 'tothetop') {
367 vc_scroll_event_firing('TOP');
368 } else if (convert == 'showsourcecode') {
370 } else if (convert == 'next' || convert == 'forward') {
372 } else if (convert == 'before' || convert == 'back' || convert == 'previous') {
374 } else if (convert == 'playlog' && vc_flag_log) {
375 if (vc_log_area.style.visibility == 'visible') vc_log_area.style.visibility = 'hidden';
376 else vc_log_area.style.visibility = 'visible';