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

Commit

Permalink
PMH's Filter API Added
Browse files Browse the repository at this point in the history
Co-authored-by: ttakkku <ttakkku@outlook.com>
  • Loading branch information
pmh-only and ttakkku committed Jun 16, 2019
1 parent e1b8f28 commit 45bc99d
Show file tree
Hide file tree
Showing 6 changed files with 749 additions and 4 deletions.
22 changes: 22 additions & 0 deletions javascript/PMH/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2019. Proj-Filtering Develoment Team / PMH.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
195 changes: 194 additions & 1 deletion javascript/PMH/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,194 @@
const discord = require('discord.js')
/**
* @name FilteringAPI
* @description 이 API는 웹 전용 REST API입니다
*/

'use strict' // 엄격모드

/** Application Port */
const PORT = 8080

/** Loging Module */
const markup = require('chalk')

/** Rest API Module */
const express = require('express')

/** Dialogflow Module */
process.env.GOOGLE_APPLICATION_CREDENTIALS = './lib/BadWordsFilter-e34ed9d4dd5b.json'
const dialogflowId = 'badwordsfilter-esqgxe'
const dialogflow = require('dialogflow')

/** Nano ID Module */
const nanoid = require('nanoid')

/** Hangul Module */
const hangul = require('hangul-js')

/** English Module */
const isEnglish = require('is-alphabetical')

/** Dialogflow Client */
const dialogflowClient = new dialogflow.SessionsClient()

/** Rest API Router */
const app = express()

/** Bad Words DataBase */
const DB = require('../../public/BadWords.json')

/** Memory */
let temp = {}

app.get('/', (req, res) => {
console.log(markup.cyan.underline('REQUEST') + ' ' + markup.cyan(req.ip + ' ' + req.protocol + ' ' + req.path))
temp = {
name: 'FilteringAPI',
description: '이 API는 웹 전용 REST API입니다',
uses: '/check/<문장>'
}
res.send(temp)
console.log(markup.green.underline('RESPONSE') + ' ' + markup.green(JSON.stringify(temp)))
})

app.get('/check/:query', (req, res) => {
console.log(markup.cyan.underline('REQUEST') + ' ' + markup.cyan(req.ip + ' ' + req.protocol + ' ' + req.path))

/** @type {String} */
let query = req.params.query
let queryArr = query.split(' ')

proc(query, (result) => {
temp = {
query: {
sentense: query,
length: query.length,
splitBySpace: queryArr
},
result: result
}
res.send(temp)
console.log(markup.gray('---------\n') + markup.green.underline('RESPONSE') + ' ' + markup.green(JSON.stringify(temp)))
})

})

app.listen(PORT)
console.log(markup.hex('#7289DA').bold('Application is Booted! App on at http://localhost:' + PORT + '/'))

/* --------------------- */
/**
* 욕설 체크
* @param {String} query 욕설인지 체크할 문자열
* @param {function(boolean)} cb 욕설 여/부 콜백
*/
function check (query, cb) {
console.log(markup.yellow.underline('PROCESS') + ' ' + markup.yellow(dialogflowId) + ': ' + markup.red(query))
let dialogflowPath = dialogflowClient.sessionPath(dialogflowId, nanoid())

let dialogflowRequest = {
session: dialogflowPath,
queryInput: {
text: {
text: query,
languageCode: 'ko-KR'
}
}
}

dialogflowClient.detectIntent(dialogflowRequest).then((dialogflowResponse) => {
let dialogflowResponseText = dialogflowResponse[0].queryResult.fulfillmentText

console.log(markup.magenta.underline('COMPLETE') + ' ' + markup.magenta(dialogflowResponseText))

if (dialogflowResponseText.startsWith('badword: ')) {
if (eval(dialogflowResponseText.split(' ')[1]) === true) {
cb(true)
} else {
cb(false)
}
}
})
}

function proc (query, cb) {
let isHangul = false
query.split('').forEach((v, i) => {
if (hangul.isHangul(v)) {
isHangul = true
}
})
if (isHangul) {
console.log(markup.gray('---------KR-P1'))
check(query, (first) => {
if (first) {
cb(true)
} else {
console.log(markup.gray('---------KR-P2'))
let onlyKorean = ''
query.split('').forEach((v, i) => {
if (hangul.isHangul(v)) {
onlyKorean += v
}
})
check(onlyKorean, (second) => {
if (second) {
cb(true)
} else {
console.log(markup.gray('---------KR-P3'))
let hangulArr = hangul.disassemble(onlyKorean)
let toEng = ''
hangulArr.forEach((v, i) => {
if (DB.hanYongKey.koreans.includes(v)) {
toEng += DB.hanYongKey.englishs[DB.hanYongKey.koreans.indexOf(v)]
}
})
check(toEng, (third) => {
if (third) {
cb(true)
} else {
cb(false)
}
})
}
})
}
})
} else {
console.log(markup.gray('---------EN-P1'))
check(query, (first) => {
if (first) {
cb(true)
} else {
console.log(markup.gray('---------EN-P2'))
let onlyEnglish = ''
query.split('').forEach((v) => {
if (isEnglish(v)) {
onlyEnglish += v
}
})
check(onlyEnglish, (second) => {
if (second) {
cb(true)
} else {
console.log(markup.gray('---------EN-P3'))
let toKor = ''
onlyEnglish.split('').forEach((v) => {
if (DB.hanYongKey.englishs.includes(v)) {
toKor += DB.hanYongKey.koreans[DB.hanYongKey.englishs.indexOf(v)]
}
})
toKor = hangul.assemble(toKor)
check(toKor, (third) => {
if (third) {
cb(true)
} else {
cb(false)
}
})
}
})
}
})
}
}
12 changes: 12 additions & 0 deletions javascript/PMH/lib/BadWordsFilter-e34ed9d4dd5b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "badwordsfilter-esqgxe",
"private_key_id": "e34ed9d4dd5bc53b2d4b99c9802c2f38aeaeca6f",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0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oDbwKBgCmSWKqEjDpXHiA1wkiHMMdERDvTI697zJ5mvpxSNqhp6PXx4mgo\nUmUjFPcaJHE1tc+iZ12RK00NvPmy/tOo7dRNHbY4nYK4DCZhhJufNHjT8/hFLyrM\naY1AYH82eNTBoQRM6BtoQ4xeJTUOyJSsa6xGERMLN8/5m53guGhgiL1xAoGBAJ4d\n3UmTgcbZL+k/CTK8JhOljoXWKz3jD4hWy4iT4ZAuNMKyG9tqyuVEMcvNZpK7ED0U\nk9ERYOb2KY3voTsAULiOm/B5Ckhy9JxwTxua2l77ddS2OeERQS70mcgC1Uc27vA2\n2jeHzqlztoDfJKvwltJk1k7GQZENkR4HTfSaVT3jAoGASH/j7PcwDglsawImYkd0\nXY+WFBtZzk0D6IpIvHUT9SSOUDU5QXV70kxwxe3BUrlHS3pY7dq3f53rqDY+4xYS\nEBcz8sznDF0mEZsvggclieHXB3gfnDdnOueGoIosPEuGDfAZ/SeUkTO5AToyJRtS\neM+CAJ5F5xRm2Ju0PDpsd8Q=\n-----END PRIVATE KEY-----\n",
"client_email": "dialogflow-ijnccr@badwordsfilter-esqgxe.iam.gserviceaccount.com",
"client_id": "102570021671472683136",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dialogflow-ijnccr%40badwordsfilter-esqgxe.iam.gserviceaccount.com"
}
7 changes: 6 additions & 1 deletion javascript/PMH/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"start": "node index.js"
},
"dependencies": {
"express": "^4.17.1"
"chalk": "^2.4.2",
"dialogflow": "^0.10.1",
"express": "^4.17.1",
"hangul-js": "^0.2.5",
"is-alphabetical": "^1.0.3",
"nanoid": "^2.0.3"
}
}
Loading

0 comments on commit 45bc99d

Please sign in to comment.