DMKael

Apply rhyme algorithm to app.js

...@@ -100,13 +100,14 @@ exports.rhyme_kr = function(sg, raw) ...@@ -100,13 +100,14 @@ exports.rhyme_kr = function(sg, raw)
100 r_lst.push(dictData[i][1]); 100 r_lst.push(dictData[i][1]);
101 } 101 }
102 } 102 }
103 - return (r_lst) 103 + const foo = new Set(r_lst);
104 + return (foo);
104 }; 105 };
105 106
106 /* 107 /*
107 line = refine.refine_String_Last(line); 108 line = refine.refine_String_Last(line);
108 line = refine.refine_String(line); 109 line = refine.refine_String(line);
109 -//console.log(line); 110 +console.log(line);
110 111
111 console.log(rhyme_kr("무지개",line)); 112 console.log(rhyme_kr("무지개",line));
112 */ 113 */
......
...@@ -2,8 +2,20 @@ var express = require('express'); ...@@ -2,8 +2,20 @@ var express = require('express');
2 var app = express(); 2 var app = express();
3 const line = require('@line/bot-sdk'); 3 const line = require('@line/bot-sdk');
4 const dotenv = require('dotenv'); 4 const dotenv = require('dotenv');
5 +var rhyme_kr = require('./Rhyme_Kr')
6 +var refine_string = require('./Refine_String')
5 dotenv.config(); 7 dotenv.config();
6 8
9 +// txt 읽어오기
10 +const fs = require('fs');
11 +fs.readFile('./songdata/all.txt','utf8',function(err,data)
12 +{
13 + data = refine_string.refine_String_Last(data);
14 + lyric_data = refine_string.refine_String(data);
15 +});
16 +
17 +
18 +
7 //papago api 19 //papago api
8 var request = require('request'); 20 var request = require('request');
9 21
...@@ -17,7 +29,8 @@ var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs' ...@@ -17,7 +29,8 @@ var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs'
17 //새로 발급받은 naver papago api id, pw 입력 29 //새로 발급받은 naver papago api id, pw 입력
18 var client_id = process.env.client_id; 30 var client_id = process.env.client_id;
19 var client_secret = process.env.client_secret; 31 var client_secret = process.env.client_secret;
20 -const config = { 32 +const config =
33 +{
21 channelAccessToken: process.env.channelAccessToken, 34 channelAccessToken: process.env.channelAccessToken,
22 channelSecret: process.env.channelSecret, 35 channelSecret: process.env.channelSecret,
23 }; 36 };
...@@ -31,32 +44,39 @@ const client = new line.Client(config); ...@@ -31,32 +44,39 @@ const client = new line.Client(config);
31 44
32 // register a webhook handler with middleware 45 // register a webhook handler with middleware
33 // about the middleware, please refer to doc 46 // about the middleware, please refer to doc
34 -app.post('/webhook', line.middleware(config), (req, res) => { 47 +app.post('/webhook', line.middleware(config), (req, res) =>
48 +{
35 Promise 49 Promise
36 .all(req.body.events.map(handleEvent)) 50 .all(req.body.events.map(handleEvent))
37 .then((result) => res.json(result)) 51 .then((result) => res.json(result))
38 - .catch((err) => { 52 + .catch((err) =>
53 + {
39 console.error(err); 54 console.error(err);
40 res.status(200).end(); 55 res.status(200).end();
41 }); 56 });
42 }); 57 });
43 58
44 // event handler 59 // event handler
45 -function handleEvent(event) { 60 +function handleEvent(event)
46 - if (event.type !== 'message' || event.message.type !== 'text') { 61 +{
62 + if (event.type !== 'message' || event.message.type !== 'text')
63 + {
47 // ignore non-text-message event 64 // ignore non-text-message event
48 return Promise.resolve(null); 65 return Promise.resolve(null);
49 } 66 }
50 - return new Promise(function(resolve, reject) { 67 + return new Promise(function(resolve, reject)
68 + {
51 //언어 감지 option 69 //언어 감지 option
52 - var detect_options = { 70 + var detect_options =
71 + {
53 url : languagedetect_api_url, 72 url : languagedetect_api_url,
54 form : {'query': event.message.text}, 73 form : {'query': event.message.text},
55 headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} 74 headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
56 }; 75 };
57 76
58 //papago 언어 감지 77 //papago 언어 감지
59 - request.post(detect_options,function(error,response,body){ 78 + request.post(detect_options,function(error,response,body)
79 + {
60 console.log(response.statusCode); 80 console.log(response.statusCode);
61 if(!error && response.statusCode == 200){ 81 if(!error && response.statusCode == 200){
62 var detect_body = JSON.parse(response.body); 82 var detect_body = JSON.parse(response.body);
...@@ -69,11 +89,13 @@ function handleEvent(event) { ...@@ -69,11 +89,13 @@ function handleEvent(event) {
69 89
70 90
71 //번역은 한국어->영어 / 영어->한국어만 지원 91 //번역은 한국어->영어 / 영어->한국어만 지원
72 - if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ 92 + if(detect_body.langCode == 'ko'||detect_body.langCode == 'en')
93 + {
73 source = detect_body.langCode == 'ko' ? 'ko':'en'; 94 source = detect_body.langCode == 'ko' ? 'ko':'en';
74 target = source == 'ko' ? 'en':'ko'; 95 target = source == 'ko' ? 'en':'ko';
75 //papago 번역 option 96 //papago 번역 option
76 - var options = { 97 + var options =
98 + {
77 url: translate_api_url, 99 url: translate_api_url,
78 // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) 100 // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
79 form: {'source':source, 'target':target, 'text':event.message.text}, 101 form: {'source':source, 'target':target, 'text':event.message.text},
...@@ -81,30 +103,41 @@ function handleEvent(event) { ...@@ -81,30 +103,41 @@ function handleEvent(event) {
81 }; 103 };
82 104
83 // Naver Post API 105 // Naver Post API
84 - request.post(options, function(error, response, body){ 106 + request.post(options, function(error, response, body)
107 + {
85 // Translate API Sucess 108 // Translate API Sucess
86 - if(!error && response.statusCode == 200){ 109 + if(!error && response.statusCode == 200)
110 + {
87 // JSON 111 // JSON
88 var objBody = JSON.parse(response.body); 112 var objBody = JSON.parse(response.body);
89 // Message 잘 찍히는지 확인 113 // Message 잘 찍히는지 확인
90 114
115 + /*
91 result.text = objBody.message.result.translatedText; 116 result.text = objBody.message.result.translatedText;
92 console.log(result.text); 117 console.log(result.text);
93 //번역된 문장 보내기 118 //번역된 문장 보내기
94 client.replyMessage(event.replyToken,result).then(resolve).catch(reject); 119 client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
120 + */
121 +
122 + var rhyme_result = rhyme_kr.rhyme_kr(event.message.text,lyric_data);
123 + rhyme_result = [...rhyme_result];
124 + var string_result = "";
125 + for(var i = 0 ; i < rhyme_result.length - 1;i++) // '이'가 결과값에 포함되는 것을 방지. 이건 임시 방편. 원인 찾아서 해결 필요.
126 + string_result = string_result + rhyme_result[i] + " ";
127 + result.text = string_result;
128 + console.log(result.text);
129 + client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
95 } 130 }
96 }); 131 });
97 } 132 }
98 // 메시지의 언어가 영어 또는 한국어가 아닐 경우 133 // 메시지의 언어가 영어 또는 한국어가 아닐 경우
99 - else{ 134 + else
135 + {
100 result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; 136 result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.';
101 client.replyMessage(event.replyToken,result).then(resolve).catch(reject); 137 client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
102 } 138 }
103 -
104 } 139 }
105 -
106 }); 140 });
107 -
108 }); 141 });
109 } 142 }
110 143
......
...@@ -522,6 +522,15 @@ ...@@ -522,6 +522,15 @@
522 "sshpk": "^1.7.0" 522 "sshpk": "^1.7.0"
523 } 523 }
524 }, 524 },
525 + "iconv": {
526 + "version": "2.3.5",
527 + "resolved": "https://registry.npmjs.org/iconv/-/iconv-2.3.5.tgz",
528 + "integrity": "sha512-U5ajDbtDfadp7pvUMC0F2XbkP5vQn9Xrwa6UptePl+cK8EILxapAt3sXers9B3Gxagk+zVjL2ELKuzQvyqOwug==",
529 + "requires": {
530 + "nan": "^2.14.0",
531 + "safer-buffer": "^2.1.2"
532 + }
533 + },
525 "iconv-lite": { 534 "iconv-lite": {
526 "version": "0.4.24", 535 "version": "0.4.24",
527 "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", 536 "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
...@@ -629,6 +638,11 @@ ...@@ -629,6 +638,11 @@
629 "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 638 "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
630 "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 639 "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
631 }, 640 },
641 + "nan": {
642 + "version": "2.14.0",
643 + "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz",
644 + "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg=="
645 + },
632 "negotiator": { 646 "negotiator": {
633 "version": "0.6.2", 647 "version": "0.6.2",
634 "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", 648 "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
13 "@line/bot-sdk": "^6.8.3", 13 "@line/bot-sdk": "^6.8.3",
14 "dotenv": "^8.2.0", 14 "dotenv": "^8.2.0",
15 "express": "^4.17.1", 15 "express": "^4.17.1",
16 + "iconv": "^2.3.5",
16 "request": "^2.88.0", 17 "request": "^2.88.0",
17 "request-promise-native": "^1.0.8" 18 "request-promise-native": "^1.0.8"
18 } 19 }
......