Merge "Add new language specified script" into tizen_4.0
[platform/core/uifw/vc-webview-js.git] / js / vc-webview-pt_BR.js
1 /**
2  *   Copyright 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *       http://floralicense.org/license/
9  *
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.
15  */
16
17 /**
18  * vc-webview-pt_BR.js
19  *
20  *  This source code is a language specified script for Portuguese(pt_BR).
21  */
22
23 /**
24  * vc_search_word function found the text with similarity calculated using words in the @param.
25  *
26  * @param param  param from voice-control.
27  * @param replace  If replace true, function correct some confused charaters and try again.
28  */
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)';
34         }
35         var resultTokenArr = param.split(' ');
36         var threshold = resultTokenArr.length * 0.3;
37         var temp = [];
38         var el = undefined;
39
40         vc_print_log('=== start vc_search_word');
41
42         for (var i = 0; i < vc_text_indicators.length; ++i) {
43                 var text = vc_text_indicators[i].textContent.replace(/[ `~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase();
44                 temp[i] = 0;
45
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) {
48                                 temp[i]++;
49                         }
50                 }
51         }
52
53         var max = -1;
54
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];
58                         max = i;
59                 }
60         }
61
62         if (el != undefined) {
63                 return el;
64         }
65
66         /* Second, compare with whole text of elements in html documents */
67         var result = [];
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++) {
71                         temp = obj[j];
72                         if (temp.childElementCount === 0) {
73                                 if (temp.hasAttribute('vc_count') == false) {
74                                         temp.setAttribute('vc_count', 1);
75                                         result.push(temp);
76                                 } else {
77                                         temp.setAttribute('vc_count', parseInt(temp.getAttribute('vc_count')) + 1);
78                                 }
79                         }
80                 }
81         }
82
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')))) {
86                         el = result[i];
87                 }
88         }
89
90         for (var i = 0; i < result.length; i++) {
91                 result[i].removeAttribute('vc_count');
92         }
93
94         if (el != undefined) {
95                 return el;
96         }
97
98         return vc_search_character(param);
99 }
100
101 /**
102  * vc_search_word function found the text with similarity calculated using characters in the @param.
103  *
104  * @param param  param from voice-control.
105  */
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)';
110         }
111         var similarity = [];
112         var threshold = 0;
113
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(' ');
118                         var score = 0;
119
120                         for(var k = 0; k < param.length; k++) {
121                                 var min = 2147483647;
122
123                                 if (3 > param[k].length) {
124                                         continue;
125                                 }
126
127                                 for(var j = 0; j < text.length; j++) {
128                                         if (3 > text[j].length) {
129                                                 continue;
130                                         }
131                                         var cost = [];
132                                         var ncost = [];
133                                         var longStr;
134                                         var shortStr;
135
136                                         if (text[j].length > param[k].length) {
137                                                 longStr = text[j];
138                                                 shortStr = param[k];
139                                         } else {
140                                                 longStr = param[k];
141                                                 shortStr = text[j];
142                                         }
143
144                                         for (var l = 0; l <= longStr.length; l++) {
145                                                 cost[l] = l;
146                                         }
147
148                                         for (var s = 1; s <= shortStr.length; s++) {
149                                                 ncost[0] = s;
150
151                                                 for (var l = 1; l <= longStr.length; l++) {
152                                                         var match = shortStr[s - 1] == longStr[l - 1] ? 0 : 1;
153
154                                                         var rep = cost[l - 1] + match;
155                                                         var ins = cost[l] + 1;
156                                                         var del = ncost[l - 1] + 1;
157
158                                                         ncost[l] = Math.min(rep, ins, del);
159                                                 }
160
161                                                 var arr = cost;
162                                                 cost = ncost;
163                                                 ncost = arr;
164                                         }
165
166                                         if (param[k].length * 0.25 >= cost[longStr.length] && min > cost[longStr.length]) {
167                                                 min = cost[longStr.length];
168                                         }
169                                 }
170
171                                 if (min < 2147483647) {
172                                         score = score + min;
173                                 } else {
174                                         score = score + param[k].length;
175                                 }
176                         }
177
178                         similarity.push(score);
179                 } else {
180                         similarity.push(2147483647);
181                 }
182         }
183
184         vc_print_log('=== finish searching(character level)');
185
186         var min = 2147483647;
187         for (var i = 0; i < similarity.length; i++) {
188                 if (min > similarity[i]) {
189                         min = similarity[i];
190                 }
191         }
192
193         for (var i = 0; i < param.length; i++) {
194                 if (3 <= param[i].length) {
195                         threshold = threshold + param[i].length;
196                 }
197         }
198
199         if (threshold > min) {
200                 return vc_all_elem[similarity.indexOf(min)];
201         }
202
203         return undefined;
204 }
205
206 /**
207  * vc_search_word function found the text with similarity calculated using pronunciation keys in the @param.
208  *
209  * @param param  param from voice-control.
210  */
211 function vc_search_pronunciation(param) {
212         var el = undefined;
213
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.
217          */
218
219         return el;
220 }
221
222 /**
223  * vc_is_included_number function separate a number value from the @text.
224  *
225  * @param text  text string from voice-control-webview.cpp.
226  */
227 function vc_is_included_number(text) {
228         var numbers = [['um', 'uma'], ['dois', 'duas'], 'três', 'quartro', 'cinco', 'seis', 'sete', 'oito', 'nove', 'dez',
229                 'onze', 'doze', 'treze', 'catorze', 'quinze', 'dezesseis', 'dezessete', 'dezeoito', 'dezenove', 'vinte',
230                 ['vinte e um', 'vinte e uma'], ['vinte e dois', 'vinte e duas'], 'vinte e três', 'vinte e quartro', 'vinte e cinco', 'vinte e seis', 'vinte e sete', 'vinte e oito', 'vinte e nove', 'trinda',
231                 ['trinda e um', 'trinda e uma'], ['trinda e dois', 'trinda e duas'], 'trinda e três', 'trinda e quartro', 'trinda e cinco', 'trinda e seis', 'trinda e sete', 'trinda e oito', 'trinda e nove', 'quarenta',
232                 ['quarenta e um', 'quarenta e uma'], ['quarenta e dois', 'quarenta e duas'], 'quarenta e três', 'quarenta e quartro', 'quarenta e cinco', 'quarenta e seis', 'quarenta e sete', 'quarenta e oito', 'quarenta e nove', 'cinquenta',
233                 ['cinquenta e um', 'cinquenta e uma'], ['cinquenta e dois', 'cinquenta e duas'], 'cinquenta e três', 'cinquenta e quartro', 'cinquenta e cinco', 'cinquenta e seis', 'cinquenta e sete', 'cinquenta e oito', 'cinquenta e nove', 'sessenta',
234                 ['sessenta e um', 'sessenta e uma'], ['sessenta e dois', 'sessenta e duas'], 'sessenta e três', 'sessenta e quartro', 'sessenta e cinco', 'sessenta e seis', 'sessenta e sete', 'sessenta e oito', 'sessenta e nove', 'setenta',
235                 ['setenta e um', 'setenta e uma'], ['setenta e dois', 'setenta e duas'], 'setenta e três', 'setenta e quartro', 'setenta e cinco', 'setenta e seis', 'setenta e sete', 'setenta e oito', 'setenta e nove', 'oitenta',
236                 ['oitenta e um', 'oitenta e uma'], ['oitenta e dois', 'oitenta e duas'], 'oitenta e três', 'oitenta e quartro', 'oitenta e cinco', 'oitenta e seis', 'oitenta e sete', 'oitenta e oito', 'oitenta e nove', 'noventa',
237                 ['noventa e um', 'noventa e uma'], ['noventa e dois', 'noventa e duas'], 'noventa e três', 'noventa e quartro', 'noventa e cinco', 'noventa e seis', 'noventa e sete', 'noventa e oito', 'noventa e nove', 'cem'];
238         var convert = text.toLowerCase();
239         var result;
240
241         for (var i = 0; numbers.length > i; i++) {
242                 if (true == Array.isArray(numbers[i])) {
243                         for (var j = 0; numbers[i].length > j; j++) {
244                                 if (true == convert.startsWith(numbers[i][j]) && (' ' == text[numbers[i][j].length] || text.length == numbers[i][j].length)) {
245                                         var partial = text.substr(numbers[i][j].length);
246
247                                         if (vc_visible_hints[i].type == 'input' || text.length == numbers[i][j].length) {
248                                                 result = {
249                                                         cmd : (i + 1),
250                                                         param : partial.trim()
251                                                 };
252                                                 return result;
253                                         }
254                                 }       
255                         }
256                 } else {
257                         if (true == convert.startsWith(numbers[i]) && (' ' == text[numbers[i].length] || text.length == numbers[i].length)) {
258                                 var partial = text.substr(numbers[i].length);
259
260                                 if (vc_visible_hints[i].type == 'input' || text.length == numbers[i].length) {
261                                         result = {
262                                                 cmd : (i + 1),
263                                                 param : partial.trim()
264                                         };
265                                         return result;
266                                 }
267                         }
268                 }
269         }
270
271         result = {
272                 cmd : NaN,
273                 param : text.trim()
274         };
275
276         return result;
277 }
278
279 /**
280  * vc_correct_parameter function correct the voice recognition result.
281  *
282  * @param text  text string from voice-control-webview.cpp.
283  */
284 function vc_correct_parameter(text) {
285         var result = vc_is_included_number(text),
286         words = result.param.split(' ');
287
288         if (isNaN(result.cmd) == true && isNaN(words[0]) == false) {
289                 result.cmd = parseFloat(words[0]);
290                 result.param = result.param.substr(words[0].length + 1).trim();
291         }
292
293         var idx_sep;
294         if (0 < (idx_sep = result.param.includes('º ')) && !isNaN(result.param.substr(0, idx_sep))) {
295                 if (idx_sep == result.param.length - 1) {
296                         result.cmd = parseFloat(result.param.substr(0, idx_sep));
297                         result.param = '';
298                 } else {
299                         result.cmd = parseFloat(result.param.substr(0, idx_sep));
300                         result.param = result.param.substr(idx_sep + 2, result.param.length);
301                 }
302         } else if (0 < (idx_sep = result.param.includes('ª ')) && !isNaN(result.param.substr(0, idx_sep))) {
303                 if (idx_sep == result.param.length - 1) {
304                         result.cmd = parseFloat(result.param.substr(0, idx_sep));
305                         result.param = '';
306                 } else {
307                         result.cmd = parseFloat(result.param.substr(0, idx_sep));
308                         result.param = result.param.substr(idx_sep + 2, result.param.length);
309                 }
310         }
311
312         return result
313 }
314
315 /**
316  * vc_check_web_control function check some special keyword and run it.
317  *
318  * @param spokenWord  voice recognized result string.
319  */
320 function vc_check_web_control(text) {
321         text = text.toLowerCase();
322         var convert = text.replace(/ /g, '');
323         var googleSearch = 'busca no google';
324         var googleSearch2 = ['pesquisa', 'no google'];
325         var youtubeSearch = 'busca no youtube';
326         var youtubeSearch2 = ['pesquisa', 'no youtube'];
327
328         if (text.startsWith(googleSearch) == true) {
329                 location.href = 'https://www.google.com/search?q=' + (text.substr(googleSearch.length)).trim();
330         } else if (text.startsWith(googleSearch2[0]) && text.endsWith(googleSearch2[1])) {
331                 var query = text.substr(googleSearch2[0].length, text.length - googleSearch2[0].length - googleSearch2[1].length);
332                 location.href = 'https://www.google.com/search?q=' + query.trim();
333         } else if (text.startsWith(youtubeSearch) == true) {
334                 location.href = 'https://www.youtube.com/results?search_query=' + (text.substr(youtubeSearch.length)).trim();
335         } else if (text.startsWith(youtubeSearch2[0]) && text.endsWith(youtubeSearch2[1])) {
336                 var query = text.substr(youtubeSearch2[0].length, text.length - youtubeSearch2[0].length - youtubeSearch2[1].length);
337                 location.href = 'https://www.youtube.com/results?search_query=' + query.trim();
338         } else if (convert == 'recarregar' || convert == 'refrescar') {
339                 location.reload();
340         } else if (convert == 'google') {
341                 location.href = 'https://www.google.com/';
342         } else if (convert == 'facebook') {
343                 location.href = 'https://www.facebook.com/';
344         } else if (convert == 'amazon') {
345                 location.href = 'https://www.amazon.com/';
346         } else if (convert == 'yahoo') {
347                 location.href = 'https://www.yahoo.com/';
348         } else if (convert == 'youtube') {
349                 location.href = 'https://www.youtube.com/';
350         } else if (convert == 'iratéotopo') {
351                 vc_scroll_event_firing('TOP');
352         } else if (convert == 'códigofonte') {
353                 vc_print_html();
354         } else if (convert == 'próximo') {
355                 history.forward();
356         } else if (convert == 'antesde' || convert == 'devolta' || convert == 'anterior') {
357                 history.back();
358         } else if (convert == 'playlog' && vc_flag_log) {
359                 if (vc_log_area.style.visibility == 'visible') vc_log_area.style.visibility = 'hidden';
360                 else vc_log_area.style.visibility = 'visible';
361         } else {
362                 return false;
363         }
364         return true;
365 }