25c4cdd7f4ce8f423fc66c3e6404a236714118bb
[platform/core/uifw/vc-webview-js.git] / js / vc-webview-ko_KR.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-ko_KR.js
19  *
20  *  This source code is a language specified script for Korean(ko_KR).
21  *  If there is no script for some language, this script is loaded as default.
22  */
23
24 /**
25  * vc_search_word function found the text with similarity calculated using words in the @param.
26  *
27  * @param param  param from voice-control.
28  * @param replace  If replace true, function correct some confused charaters and try again.
29  */
30 function vc_search_word(param, replace) {
31         /* phase 2. search partial word in the webpage */
32         /* second, compare with links in html documents */
33         if (vc_flag_log == true) {
34                 vc_rec_result.style.background = 'rgba(0, 100, 200, 1)';
35         }
36         var resultTokenArr = param.split(' ');
37         var threshold = resultTokenArr.length * 0.3;
38         var temp = [];
39         var el = undefined;
40
41         vc_print_log('=== start vc_search_word');
42
43         for (var i = 0; i < document.links.length; ++i) {
44                 var text = document.links[i].textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
45                 text = text.replace(/ /g, '').toLowerCase();
46                 temp[i] = 0;
47
48                 for (var j = 0; j < resultTokenArr.length; j++) {
49                         if (vc_is_visible(document.links[i], vc_scr, true) && text.indexOf(resultTokenArr[j].toLowerCase()) != -1) {
50                                 temp[i]++;
51                         }
52                 }
53         }
54
55         var max = -1;
56
57         for (var i = 0; i < document.links.length; i++) {
58                 if (temp[i] >= threshold && (max == -1 || temp[i] > temp[max])) {
59                         el = document.links[i];
60                         max = i;
61                 }
62         }
63
64         if (el != undefined) {
65                 return el;
66         }
67
68         /* first, compare with whole text of elements in html documents */
69         var result = [];
70         for (var i = 0; i < resultTokenArr.length; i++) {
71                 var obj = vc_selector([resultTokenArr[i]]);
72                 for (var j = 0; j < obj.length; j++) {
73                         temp = obj[j];
74                         if (temp.childElementCount === 0) {
75                                 if (temp.hasAttribute('vc_count') == false) {
76                                         temp.setAttribute('vc_count', 1);
77                                         result.push(temp);
78                                 } else {
79                                         temp.setAttribute('vc_count', parseInt(temp.getAttribute('vc_count')) + 1);
80                                 }
81                         }
82                 }
83         }
84
85         for (var i = 0; i < result.length; i++) {
86                 var vccnt = parseInt(result[i].getAttribute('vc_count'));
87                 if (vccnt >= threshold && (el == undefined || vccnt > parseInt(el.getAttribute('vc_count')))) {
88                         el = result[i];
89                 }
90         }
91
92         for (var i = 0; i < result.length; i++) {
93                 result[i].removeAttribute('vc_count');
94         }
95
96         if (el != undefined) {
97                 return el;
98         }
99
100         if (replace == true) {
101                 var rep = param.replace('이 ', '의 ');
102                 el = vc_search_word(rep, false);
103                 if (el != undefined) return el;
104
105                 rep = param.replace('으 ', '이 ');
106                 el = vc_search_word(rep, false);
107                 if (el != undefined) return el;
108
109                 rep = param.replace('에 ', '의 ');
110                 el = vc_search_word(rep, false);
111                 if (el != undefined) return el;
112
113                 rep = param.replace('의 ', '에 ');
114                 el = vc_search_word(rep, false);
115                 if (el != undefined) return el;
116         }
117
118         return vc_search_character(param);
119 }
120
121 /**
122  * vc_search_word function found the text with similarity calculated using characters in the @param.
123  *
124  * @param param  param from voice-control.
125  */
126 function vc_search_character(param) {
127         vc_print_log('=== start searching(character level)');
128         if (vc_flag_log == true) {
129                 vc_rec_result.style.background = 'rgba(200, 100, 0, 1)';
130         }
131         var similarity = [];
132         var threshold = 0;
133
134         param = param.toLowerCase().split(' ');
135         for (var i = 0; i < vc_all_elem.length; i++) {
136                 if (vc_all_elem[i].childElementCount == 0 && vc_is_visible(vc_all_elem[i]) == true) {
137                         var text = vc_all_elem[i].textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ' ').toLowerCase().split(' ');
138                         var score = 0;
139
140                         for(var k = 0; k < param.length; k++) {
141                                 var min = 2147483647;
142
143                                 if (3 > param[k].length) {
144                                         continue;
145                                 }
146
147                                 for(var j = 0; j < text.length; j++) {
148                                         if (3 > text[j].length) {
149                                                 continue;
150                                         }
151                                         var cost = [];
152                                         var ncost = [];
153                                         var longStr;
154                                         var shortStr;
155
156                                         if (text[j].length > param[k].length) {
157                                                 longStr = text[j];
158                                                 shortStr = param[k];
159                                         } else {
160                                                 longStr = param[k];
161                                                 shortStr = text[j];
162                                         }
163
164                                         for (var l = 0; l <= longStr.length; l++) {
165                                                 cost[l] = l;
166                                         }
167
168                                         for (var s = 1; s <= shortStr.length; s++) {
169                                                 ncost[0] = s;
170
171                                                 for (var l = 1; l <= longStr.length; l++) {
172                                                         var match = shortStr[s - 1] == longStr[l - 1] ? 0 : 1;
173
174                                                         var rep = cost[l - 1] + match;
175                                                         var ins = cost[l] + 1;
176                                                         var del = ncost[l - 1] + 1;
177
178                                                         ncost[l] = Math.min(rep, ins, del);
179                                                 }
180
181                                                 var arr = cost;
182                                                 cost = ncost;
183                                                 ncost = arr;
184                                         }
185
186                                         if (param[k].length * 0.25 >= cost[longStr.length] && min > cost[longStr.length]) {
187                                                 min = cost[longStr.length];
188                                         }
189                                 }
190
191                                 if (min < 2147483647) {
192                                         score = score + min;
193                                 } else {
194                                         score = score + param[k].length;
195                                 }
196                         }
197
198                         similarity.push(score);
199                 } else {
200                         similarity.push(2147483647);
201                 }
202         }
203
204         vc_print_log('=== finish searching(character level)');
205
206         var min = 2147483647;
207         for (var i = 0; i < similarity.length; i++) {
208                 if (min > similarity[i]) {
209                         min = similarity[i];
210                 }
211         }
212
213         for (var i = 0; i < param.length; i++) {
214                 if (3 <= param[i].length) {
215                         threshold = threshold + param[i].length;
216                 }
217         }
218
219         if (threshold > min) {
220                 return vc_all_elem[similarity.indexOf(min)];
221         }
222
223         return vc_search_pronunciation(param);
224 }
225
226 /**
227  * vc_search_word function found the text with similarity calculated using pronunciation keys in the @param.
228  *
229  * @param param  param from voice-control.
230  */
231 function vc_convert_to_syllable(param) {
232         var korUnicodeStart = 44032;
233         var korUnicodeNum = 11172;
234         var korUnicodeEnd = 55204;
235         var korChoCnt = 19;
236         var korJungCnt = 21;
237         var korJongCnt = 28;
238
239         var result = [];
240
241         for (var i = 0; i < param.length; i++) {
242                 var c = param.charCodeAt(i);
243
244                 if (korUnicodeStart > c || korUnicodeEnd < c) {
245                         continue;
246                 }
247
248                 var jongIdx = (c - korUnicodeStart) % korJongCnt;
249                 var jungIdx = (((c - korUnicodeStart) - jongIdx) / korJongCnt) % korJungCnt;
250                 var choIdx = ((((c - korUnicodeStart) - jongIdx) / korJongCnt) - jungIdx) / korJungCnt;
251
252                 result.push(choIdx, jungIdx, jongIdx);
253         }
254
255         return result;
256 }
257
258 /**
259  * vc_search_word function found the text with similarity calculated using pronunciation keys in the @param.
260  *
261  * @param param  param from voice-control.
262  */
263 function vc_search_pronunciation(param) {
264         if (vc_flag_log == true) {
265                 vc_rec_result.style.background = 'rgba(220, 0, 0, 1)';
266         }
267         if (param.length > 1) {
268                 return undefined;
269         }
270
271         vc_print_log('=== start searching(pronuciation level)');
272         param = param[0];
273
274         var sylla = vc_convert_to_syllable(param);
275         var threshold = sylla.length * 0.5;
276         var el = undefined;
277         var elScore = 2147483247;
278
279         for (var i = 0; i < vc_all_elem.length; i++) {
280                 if (vc_all_elem[i].childElementCount == 0 && vc_is_visible(vc_all_elem[i]) == true) {
281                         var text = vc_all_elem[i].textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ' ').toLowerCase().split(' ');
282                         var score = 2147483247;
283
284                         for (var j = 0; j < text.length; j++) {
285                                 if (param.length > text[j].length) {
286                                         continue;
287                                 }
288                                 var min = 2147483247;
289
290                                 for (var s = 0; s <= text[j].length - param.length; s++) {
291                                         var temp = vc_convert_to_syllable(text[j].substr(s, param.length));
292                                         var cnt = 0;
293
294                                         for (var k = 0; k < sylla.length; k++) {
295                                                 if (sylla[k] != temp[k]) {
296                                                         cnt++;
297                                                 }
298
299                                                 if (cnt == threshold) {
300                                                         cnt++;
301                                                         break;
302                                                 }
303                                         }
304
305                                         if (min > cnt) {
306                                                 min = cnt;
307                                         }
308                                 }
309
310                                 if (score > min) {
311                                         score = min;
312                                 }
313                         }
314
315                         if (threshold > score && elScore > score) {
316                                 el = vc_all_elem[i];
317                                 elScore = score;
318                         }
319                 }
320         }
321
322         vc_print_log('=== finish searching(pronuciation level)');
323
324         if (el != undefined) {
325                 return el;
326         }
327
328         return undefined;
329 }
330
331 /**
332  * vc_is_included_number function separate a number value from the @text.
333  *
334  * @param text  text string from voice-control-webview.cpp.
335  */
336 function vc_is_included_number(text) {
337         var numbers = ['일', '이', '삼', '사', '오', '육', '칠', '팔', '구', '십',
338                         '십일', '십이', '십삼', '십사', '십오', '십육', '십칠', '십팔', '십구', '이십',
339                         '이십일', '이십이', '이십삼', '이십사', '이십오', '이십육', '이십칠', '이십팔', '이십구', '삼십',
340                         '삼십일', '삼십이', '삼십삼', '삼십사', '삼십오', '삼십육', '삼십칠', '삼십팔', '삼십구', '사십',
341                         '사십일', '사십이', '사십삼', '사십사', '사십오', '사십육', '사십칠', '사십팔', '사십구', '오십',
342                         '오십일', '오십이', '오십삼', '오십사', '오십오', '오십육', '오십칠', '오십팔', '오십구', '육십',
343                         '육십일', '육십이', '육십삼', '육십사', '육십오', '육십육', '육십칠', '육십팔', '육십구', '칠십',
344                         '칠십일', '칠십이', '칠십삼', '칠십사', '칠십오', '칠십육', '칠십칠', '칠십팔', '칠십구', '팔십',
345                         '팔십일', '팔십이', '팔십삼', '팔십사', '팔십오', '팔십육', '팔십칠', '팔십팔', '팔십구', '구십',
346                         '구십일', '구십이', '구십삼', '구십사', '구십오', '구십육', '구십칠', '구십팔', '구십구', '백'];
347         var seven = ['채널', '질'];
348         var eight = ['발', '달'];
349         var convert = text.toLowerCase();
350         var result;
351
352         for (var i = 0; numbers.length > i; i++) {
353                 if (true == convert.startsWith(numbers[i]) && (' ' == text[numbers[i].length] || text.length == numbers[i].length)) {
354                         var partial = text.substr(numbers[i].length + 1);
355                         i = i + 1;
356
357                         for (var j = 0; 9 > j; j++) {
358                                 if (true == partial.toLowerCase().startsWith(numbers[j]) && (' ' == partial[numbers[j].length] || partial.length == numbers[j].length)) {
359                                         partial = partial.substr(numbers[j].length + 1);
360                                         i = i + j + 1;
361                                         break;
362                                 }
363                         }
364
365                         if (vc_visible_hints[i - 1].type == 'input' || text.length == numbers[i].length) {
366                                 result = {
367                                         cmd : i,
368                                         param : partial.trim()
369                                 };
370                                 return result;
371                         }
372                 }
373         }
374
375         for (var i = 0; seven.length > i; i++) {
376                 if (true == convert.startsWith(seven[i]) && (' ' == text[seven[i].length] || text.length == seven[i].length) && (vc_visible_hints[6].type == 'input' || text.length == seven[i].length)) {
377                         result = {
378                                 cmd : 7,
379                                 param : text.substr(seven[i].length + 1).trim()
380                         };
381                         return result;
382                 }
383         }
384
385         for (var i = 0; eight.length > i; i++) {
386                 if (true == convert.startsWith(eight[i]) && (' ' == text[eight[i].length] || text.length == eight[i].length) && (vc_visible_hints[7].type == 'input' || text.length == eight[i].length)) {
387                         result = {
388                                 cmd : 8,
389                                 param : text.substr(eight[i].length + 1).trim()
390                         };
391                         return result;
392                 }
393         }
394
395         result = {
396                 cmd : NaN,
397                 param : text.trim()
398         };
399         return result;
400 }
401
402 /**
403  * vc_correct_parameter function correct the voice recognition result.
404  *
405  * @param text  text string from voice-control-webview.cpp.
406  */
407 function vc_correct_parameter(text) {
408         var result = vc_is_included_number(text);
409
410         /* if param is a number, move the value in param to cmd */
411         var words = result.param.split(' ');
412         if (isNaN(result.cmd) == true && isNaN(words[0]) == false) {
413                 result.cmd = parseFloat(words[0]);
414                 result.param = result.param.substr(words[0].length + 1).trim();
415         }
416
417         var idx_sep = result.param.indexOf('번 ');
418         if (isNaN(result.cmd) == true && 0 < idx_sep) {
419                 if (idx_sep == result.param.length - 1) {
420                         result.cmd = parseFloat(result.param.substr(0, idx_sep));
421                         result.param = '';
422                 } else {
423                         result.cmd = parseFloat(result.param.substr(0, idx_sep));
424                         result.param = result.param.substr(idx_sep + 2, result.param.length);
425                 }
426         }
427
428         return result
429 }
430
431 /**
432  * vc_check_web_control function check some special keyword and run it.
433  *
434  * @param spokenWord  voice recognized result string.
435  */
436 function vc_check_web_control(text) {
437         text = text.toLowerCase();
438         var convert = text.replace(/ /g, '');
439         var googleSearch = '구글 검색';
440         var naverSearch = '네이버 검색';
441         var youtubeSearch = '유튜브 검색';
442         var googleSearch2 = '구글 ';
443         var naverSearch2 = '네이버 ';
444         var youtubeSearch2 = '유튜브 ';
445
446         if (text.startsWith(googleSearch) == true) {
447                 location.href = 'https://www.google.co.kr/search?q=' + (text.substr(googleSearch.length)).trim();
448         } else if (text.startsWith(naverSearch) == true) {
449                 location.href = 'https://search.naver.com/search.naver?where=nexearch&query=' + (text.substr(naverSearch.length)).trim();
450         } else if (text.startsWith(youtubeSearch) == true) {
451                 location.href = 'https://www.youtube.com/results?search_query=' + (text.substr(youtubeSearch.length)).trim();
452         } else if (convert == '유튜브TV') {
453                 location.href = 'https://www.youtube.com/tv';
454         } else if (text.startsWith(googleSearch2) == true) {
455                 location.href = 'https://www.google.co.kr/search?q=' + (text.substr(googleSearch2.length)).trim();
456         } else if (text.startsWith(naverSearch2) == true) {
457                 location.href = 'https://search.naver.com/search.naver?where=nexearch&query=' + (text.substr(naverSearch2.length)).trim();
458         } else if (text.startsWith(youtubeSearch2) == true) {
459                 location.href = 'https://www.youtube.com/results?search_query=' + (text.substr(youtubeSearch2.length)).trim();
460         } else if (convert.startsWith('구글') == true) {
461                 location.href = 'https://www.google.co.kr/';
462         } else if (convert.startsWith('네이버') == true) {
463                 location.href = 'http://www.naver.com/';
464         } else if (convert.startsWith('유튜브') == true) {
465                 location.href = 'https://www.youtube.com/';
466         } else if (convert == '취소') {
467                 vc_remove_hints();
468         } else if (convert == '리프레쉬' || convert == '새로고침') {
469                 location.reload();
470         } else if (convert == '아래로' || convert == '내려' || convert == '내려봐' || convert == '내려줘') {
471                 vc_scroll_event_firing('DOWN');
472         } else if (convert == '위로' || convert == '올려' || convert == '올려봐' || convert == '올려줘') {
473                 vc_scroll_event_firing('UP');
474         } else if (convert == '맨위로' || convert == '처음으로') {
475                 vc_scroll_event_firing('TOP');
476         } else if (convert == '광고닫기' || convert == '광고찾기') {
477                 document.querySelector('.videoAdUiSkipButton').click();
478         } else if (convert == '소스보기') {
479                 vc_print_html();
480         } else if (convert == '다음페이지' || convert == '다음으로' || convert == '앞으로가기' || convert == '앞으로') {
481                 history.forward();
482         } else if (convert == '이전페이지' || convert == '이전으로' || convert == '뒤로가기' || convert == '뒤로' || convert == '백' || convert == '벡') {
483                 history.back();
484         } else if (convert == '로그보기' && vc_flag_log) {
485                 if (vc_log_area.style.visibility == 'visible') vc_log_area.style.visibility = 'hidden';
486                 else vc_log_area.style.visibility = 'visible';
487         } else {
488                 return false;
489         }
490         return true;
491 }