Skip to content
This repository was archived by the owner on Sep 11, 2021. It is now read-only.

Commit 45bc99d

Browse files
pmh-onlyttakkku
andcommitted
PMH's Filter API Added
Co-authored-by: ttakkku <[email protected]>
1 parent e1b8f28 commit 45bc99d

File tree

6 files changed

+749
-4
lines changed

6 files changed

+749
-4
lines changed

javascript/PMH/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2019. Proj-Filtering Develoment Team / PMH.
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

javascript/PMH/index.js

Lines changed: 194 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,194 @@
1-
const discord = require('discord.js')
1+
/**
2+
* @name FilteringAPI
3+
* @description 이 API는 웹 전용 REST API입니다
4+
*/
5+
6+
'use strict' // 엄격모드
7+
8+
/** Application Port */
9+
const PORT = 8080
10+
11+
/** Loging Module */
12+
const markup = require('chalk')
13+
14+
/** Rest API Module */
15+
const express = require('express')
16+
17+
/** Dialogflow Module */
18+
process.env.GOOGLE_APPLICATION_CREDENTIALS = './lib/BadWordsFilter-e34ed9d4dd5b.json'
19+
const dialogflowId = 'badwordsfilter-esqgxe'
20+
const dialogflow = require('dialogflow')
21+
22+
/** Nano ID Module */
23+
const nanoid = require('nanoid')
24+
25+
/** Hangul Module */
26+
const hangul = require('hangul-js')
27+
28+
/** English Module */
29+
const isEnglish = require('is-alphabetical')
30+
31+
/** Dialogflow Client */
32+
const dialogflowClient = new dialogflow.SessionsClient()
33+
34+
/** Rest API Router */
35+
const app = express()
36+
37+
/** Bad Words DataBase */
38+
const DB = require('../../public/BadWords.json')
39+
40+
/** Memory */
41+
let temp = {}
42+
43+
app.get('/', (req, res) => {
44+
console.log(markup.cyan.underline('REQUEST') + ' ' + markup.cyan(req.ip + ' ' + req.protocol + ' ' + req.path))
45+
temp = {
46+
name: 'FilteringAPI',
47+
description: '이 API는 웹 전용 REST API입니다',
48+
uses: '/check/<문장>'
49+
}
50+
res.send(temp)
51+
console.log(markup.green.underline('RESPONSE') + ' ' + markup.green(JSON.stringify(temp)))
52+
})
53+
54+
app.get('/check/:query', (req, res) => {
55+
console.log(markup.cyan.underline('REQUEST') + ' ' + markup.cyan(req.ip + ' ' + req.protocol + ' ' + req.path))
56+
57+
/** @type {String} */
58+
let query = req.params.query
59+
let queryArr = query.split(' ')
60+
61+
proc(query, (result) => {
62+
temp = {
63+
query: {
64+
sentense: query,
65+
length: query.length,
66+
splitBySpace: queryArr
67+
},
68+
result: result
69+
}
70+
res.send(temp)
71+
console.log(markup.gray('---------\n') + markup.green.underline('RESPONSE') + ' ' + markup.green(JSON.stringify(temp)))
72+
})
73+
74+
})
75+
76+
app.listen(PORT)
77+
console.log(markup.hex('#7289DA').bold('Application is Booted! App on at http://localhost:' + PORT + '/'))
78+
79+
/* --------------------- */
80+
/**
81+
* 욕설 체크
82+
* @param {String} query 욕설인지 체크할 문자열
83+
* @param {function(boolean)} cb 욕설 여/부 콜백
84+
*/
85+
function check (query, cb) {
86+
console.log(markup.yellow.underline('PROCESS') + ' ' + markup.yellow(dialogflowId) + ': ' + markup.red(query))
87+
let dialogflowPath = dialogflowClient.sessionPath(dialogflowId, nanoid())
88+
89+
let dialogflowRequest = {
90+
session: dialogflowPath,
91+
queryInput: {
92+
text: {
93+
text: query,
94+
languageCode: 'ko-KR'
95+
}
96+
}
97+
}
98+
99+
dialogflowClient.detectIntent(dialogflowRequest).then((dialogflowResponse) => {
100+
let dialogflowResponseText = dialogflowResponse[0].queryResult.fulfillmentText
101+
102+
console.log(markup.magenta.underline('COMPLETE') + ' ' + markup.magenta(dialogflowResponseText))
103+
104+
if (dialogflowResponseText.startsWith('badword: ')) {
105+
if (eval(dialogflowResponseText.split(' ')[1]) === true) {
106+
cb(true)
107+
} else {
108+
cb(false)
109+
}
110+
}
111+
})
112+
}
113+
114+
function proc (query, cb) {
115+
let isHangul = false
116+
query.split('').forEach((v, i) => {
117+
if (hangul.isHangul(v)) {
118+
isHangul = true
119+
}
120+
})
121+
if (isHangul) {
122+
console.log(markup.gray('---------KR-P1'))
123+
check(query, (first) => {
124+
if (first) {
125+
cb(true)
126+
} else {
127+
console.log(markup.gray('---------KR-P2'))
128+
let onlyKorean = ''
129+
query.split('').forEach((v, i) => {
130+
if (hangul.isHangul(v)) {
131+
onlyKorean += v
132+
}
133+
})
134+
check(onlyKorean, (second) => {
135+
if (second) {
136+
cb(true)
137+
} else {
138+
console.log(markup.gray('---------KR-P3'))
139+
let hangulArr = hangul.disassemble(onlyKorean)
140+
let toEng = ''
141+
hangulArr.forEach((v, i) => {
142+
if (DB.hanYongKey.koreans.includes(v)) {
143+
toEng += DB.hanYongKey.englishs[DB.hanYongKey.koreans.indexOf(v)]
144+
}
145+
})
146+
check(toEng, (third) => {
147+
if (third) {
148+
cb(true)
149+
} else {
150+
cb(false)
151+
}
152+
})
153+
}
154+
})
155+
}
156+
})
157+
} else {
158+
console.log(markup.gray('---------EN-P1'))
159+
check(query, (first) => {
160+
if (first) {
161+
cb(true)
162+
} else {
163+
console.log(markup.gray('---------EN-P2'))
164+
let onlyEnglish = ''
165+
query.split('').forEach((v) => {
166+
if (isEnglish(v)) {
167+
onlyEnglish += v
168+
}
169+
})
170+
check(onlyEnglish, (second) => {
171+
if (second) {
172+
cb(true)
173+
} else {
174+
console.log(markup.gray('---------EN-P3'))
175+
let toKor = ''
176+
onlyEnglish.split('').forEach((v) => {
177+
if (DB.hanYongKey.englishs.includes(v)) {
178+
toKor += DB.hanYongKey.koreans[DB.hanYongKey.englishs.indexOf(v)]
179+
}
180+
})
181+
toKor = hangul.assemble(toKor)
182+
check(toKor, (third) => {
183+
if (third) {
184+
cb(true)
185+
} else {
186+
cb(false)
187+
}
188+
})
189+
}
190+
})
191+
}
192+
})
193+
}
194+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "badwordsfilter-esqgxe",
4+
"private_key_id": "e34ed9d4dd5bc53b2d4b99c9802c2f38aeaeca6f",
5+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC49AwxqZxc9bK5\nshRjgO+To1g+UTlM4JliptaHQXgF7n0uW1vmsG252nliwmXTWUjl8lhBKuJwJwJ2\ncYU46GnYEIkEabEgMMG0wsXHST7uSz0nghjEj2+xmDZlltonUN0AhFyAklSzyCNl\nB01WTURHTGy15WeSWjx7b29vcVwtRY1NCoDpu8iaAR2Mklapl3RDgtOKQEj475mZ\nEXtxCmcvitAA2eF0uw25tUbl/7n6wdFtrFLq+UotkZSJ9px28bxv7Kh87qRag2qc\nGLvdZhQXJoYwUy6XbAOusIPh8HdpFlB7v/6EfO+Zjny8LE2FBZA2vCv89yAAoa7b\nrA7JDf9RAgMBAAECggEAJ8mkd+iPcTYP8T+8gB1kLKQWny1VANNOW/kdLHKqkcgY\n2Oihc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rlHS3pY7dq3f53rqDY+4xYS\nEBcz8sznDF0mEZsvggclieHXB3gfnDdnOueGoIosPEuGDfAZ/SeUkTO5AToyJRtS\neM+CAJ5F5xRm2Ju0PDpsd8Q=\n-----END PRIVATE KEY-----\n",
6+
"client_email": "[email protected]",
7+
"client_id": "102570021671472683136",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dialogflow-ijnccr%40badwordsfilter-esqgxe.iam.gserviceaccount.com"
12+
}

javascript/PMH/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"start": "node index.js"
88
},
99
"dependencies": {
10-
"express": "^4.17.1"
10+
"chalk": "^2.4.2",
11+
"dialogflow": "^0.10.1",
12+
"express": "^4.17.1",
13+
"hangul-js": "^0.2.5",
14+
"is-alphabetical": "^1.0.3",
15+
"nanoid": "^2.0.3"
1116
}
1217
}

0 commit comments

Comments
 (0)