DMKael

Apply rhyme algorithm to app.js

......@@ -100,13 +100,14 @@ exports.rhyme_kr = function(sg, raw)
r_lst.push(dictData[i][1]);
}
}
return (r_lst)
const foo = new Set(r_lst);
return (foo);
};
/*
line = refine.refine_String_Last(line);
line = refine.refine_String(line);
//console.log(line);
console.log(line);
console.log(rhyme_kr("무지개",line));
*/
......
......@@ -2,8 +2,20 @@ var express = require('express');
var app = express();
const line = require('@line/bot-sdk');
const dotenv = require('dotenv');
var rhyme_kr = require('./Rhyme_Kr')
var refine_string = require('./Refine_String')
dotenv.config();
// txt 읽어오기
const fs = require('fs');
fs.readFile('./songdata/all.txt','utf8',function(err,data)
{
data = refine_string.refine_String_Last(data);
lyric_data = refine_string.refine_String(data);
});
//papago api
var request = require('request');
......@@ -17,7 +29,8 @@ var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs'
//새로 발급받은 naver papago api id, pw 입력
var client_id = process.env.client_id;
var client_secret = process.env.client_secret;
const config = {
const config =
{
channelAccessToken: process.env.channelAccessToken,
channelSecret: process.env.channelSecret,
};
......@@ -31,32 +44,39 @@ const client = new line.Client(config);
// register a webhook handler with middleware
// about the middleware, please refer to doc
app.post('/webhook', line.middleware(config), (req, res) => {
app.post('/webhook', line.middleware(config), (req, res) =>
{
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err) => {
.catch((err) =>
{
console.error(err);
res.status(200).end();
});
});
// event handler
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
function handleEvent(event)
{
if (event.type !== 'message' || event.message.type !== 'text')
{
// ignore non-text-message event
return Promise.resolve(null);
}
return new Promise(function(resolve, reject) {
return new Promise(function(resolve, reject)
{
//언어 감지 option
var detect_options = {
var detect_options =
{
url : languagedetect_api_url,
form : {'query': event.message.text},
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
};
//papago 언어 감지
request.post(detect_options,function(error,response,body){
request.post(detect_options,function(error,response,body)
{
console.log(response.statusCode);
if(!error && response.statusCode == 200){
var detect_body = JSON.parse(response.body);
......@@ -69,11 +89,13 @@ function handleEvent(event) {
//번역은 한국어->영어 / 영어->한국어만 지원
if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){
if(detect_body.langCode == 'ko'||detect_body.langCode == 'en')
{
source = detect_body.langCode == 'ko' ? 'ko':'en';
target = source == 'ko' ? 'en':'ko';
//papago 번역 option
var options = {
var options =
{
url: translate_api_url,
// 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
form: {'source':source, 'target':target, 'text':event.message.text},
......@@ -81,30 +103,41 @@ function handleEvent(event) {
};
// Naver Post API
request.post(options, function(error, response, body){
request.post(options, function(error, response, body)
{
// Translate API Sucess
if(!error && response.statusCode == 200){
if(!error && response.statusCode == 200)
{
// JSON
var objBody = JSON.parse(response.body);
// Message 잘 찍히는지 확인
/*
result.text = objBody.message.result.translatedText;
console.log(result.text);
//번역된 문장 보내기
client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
*/
var rhyme_result = rhyme_kr.rhyme_kr(event.message.text,lyric_data);
rhyme_result = [...rhyme_result];
var string_result = "";
for(var i = 0 ; i < rhyme_result.length - 1;i++) // '이'가 결과값에 포함되는 것을 방지. 이건 임시 방편. 원인 찾아서 해결 필요.
string_result = string_result + rhyme_result[i] + " ";
result.text = string_result;
console.log(result.text);
client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
}
});
}
// 메시지의 언어가 영어 또는 한국어가 아닐 경우
else{
else
{
result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.';
client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
}
}
});
});
}
......
......@@ -522,6 +522,15 @@
"sshpk": "^1.7.0"
}
},
"iconv": {
"version": "2.3.5",
"resolved": "https://registry.npmjs.org/iconv/-/iconv-2.3.5.tgz",
"integrity": "sha512-U5ajDbtDfadp7pvUMC0F2XbkP5vQn9Xrwa6UptePl+cK8EILxapAt3sXers9B3Gxagk+zVjL2ELKuzQvyqOwug==",
"requires": {
"nan": "^2.14.0",
"safer-buffer": "^2.1.2"
}
},
"iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
......@@ -629,6 +638,11 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"nan": {
"version": "2.14.0",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz",
"integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg=="
},
"negotiator": {
"version": "0.6.2",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
......
......@@ -13,6 +13,7 @@
"@line/bot-sdk": "^6.8.3",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"iconv": "^2.3.5",
"request": "^2.88.0",
"request-promise-native": "^1.0.8"
}
......