Showing
1 changed file
with
58 additions
and
0 deletions
papago.js
0 → 100644
1 | +var express = require('express'); | ||
2 | +var app = express(); | ||
3 | +var client_id = '6hpmInU0TCrQIiiJwgJX'; | ||
4 | +var client_secret = 'jpYZbkj0_g'; | ||
5 | +// var query = "언어를 감지할 문장을 입력하세요."; | ||
6 | +// app.get('/detectLangs', function (req, res) { | ||
7 | +// var api_url = 'https://openapi.naver.com/v1/papago/detectLangs'; | ||
8 | +// var request = require('request'); | ||
9 | +// var options = { | ||
10 | +// url: api_url, | ||
11 | +// form: {'query': query}, | ||
12 | +// headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret} | ||
13 | +// }; | ||
14 | +// request.post(options, function (error, response, body) { | ||
15 | +// if (!error && response.statusCode == 200) { | ||
16 | +// console.log(JSON.parse(body).langCode); | ||
17 | +// res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'}); | ||
18 | +// res.end(body); | ||
19 | +// } else { | ||
20 | +// res.status(response.statusCode).end(); | ||
21 | +// console.log('error = ' + response.statusCode); | ||
22 | +// } | ||
23 | +// }); | ||
24 | +// }); | ||
25 | +// app.listen(3000, function () { | ||
26 | +// console.log('http://127.0.0.1:3000/detectLangs app listening on port 3000!'); | ||
27 | +// }); | ||
28 | + | ||
29 | +async function findLanguage(sourceText) { | ||
30 | + const api_url = 'https://openapi.naver.com/v1/papago/detectLangs'; | ||
31 | + const request = require('request'); | ||
32 | + let options = { | ||
33 | + url: api_url, | ||
34 | + form: {'query': sourceText}, | ||
35 | + headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret} | ||
36 | + }; | ||
37 | + | ||
38 | + return new Promise((resolve, reject) => { | ||
39 | + let resultLang = {}; | ||
40 | + | ||
41 | + request.post(options, function (error, response, body) { | ||
42 | + if (!error && response.statusCode === 200) { | ||
43 | + resultLang = JSON.parse(body).langCode | ||
44 | + console.log(resultLang); | ||
45 | + resolve(resultLang); | ||
46 | + } | ||
47 | + else { | ||
48 | + console.log('error = ' + response.statusCode); | ||
49 | + reject("error"); | ||
50 | + } | ||
51 | + }); | ||
52 | +}); | ||
53 | +} | ||
54 | + | ||
55 | +export async function getLang(comment) { | ||
56 | + const lang = await findLanguage(comment); | ||
57 | + return lang; | ||
58 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment