Showing
6 changed files
with
94 additions
and
20 deletions
server/controller/calcDistance.js
0 → 100644
1 | + | ||
2 | +const e = require('express'); | ||
3 | +const levenshtein = require('js-levenshtein'); | ||
4 | +const db = require('../model/db'); | ||
5 | +const {calcDistance} = require('../service/calcDistance'); | ||
6 | +const {getProfanity} = require('../service/query'); | ||
7 | +const {standardize, removeSpecial} = require('../service/standardization'); | ||
8 | + | ||
9 | + | ||
10 | +exports.calcDistance = async (req, res, next) => { | ||
11 | + const { word } = req.body; | ||
12 | + | ||
13 | + const profanityList = await getProfanity(); | ||
14 | + | ||
15 | + let specialRemoved = removeSpecial(word); | ||
16 | + let specialNotRemoved = word; | ||
17 | + | ||
18 | + specialRemoved = standardize(specialRemoved); | ||
19 | + specialNotRemoved = standardize(specialNotRemoved); | ||
20 | + | ||
21 | + const rareResult = calcDistance(word, profanityList); | ||
22 | + const specialRemovedResult = calcDistance(specialRemoved, profanityList); | ||
23 | + const specialNotRemovedResult = calcDistance(specialNotRemoved, profanityList); | ||
24 | + | ||
25 | + const minResult = Math.min(rareResult, specialRemovedResult, specialNotRemovedResult); | ||
26 | + | ||
27 | + if (minResult <= 1){ | ||
28 | + res.status(200).json({success : true, isProfanity : "YES"}); | ||
29 | + }else{ | ||
30 | + res.status(200).json({success : true, isProfanity : "NO"}); | ||
31 | + } | ||
32 | + }; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -2,16 +2,15 @@ const mysql = require('mysql2/promise'); | ... | @@ -2,16 +2,15 @@ const mysql = require('mysql2/promise'); |
2 | const dotenv = require('dotenv'); | 2 | const dotenv = require('dotenv'); |
3 | const path = require('path'); | 3 | const path = require('path'); |
4 | 4 | ||
5 | -dotenv.config({path:path.join(__dirname, "../.env")}); | 5 | +dotenv.config({ path: path.join(__dirname, '../.env') }); |
6 | 6 | ||
7 | -const db = mysql | 7 | +const db = mysql.createPool({ |
8 | - .createPool({ | ||
9 | host: process.env.DB_HOST, | 8 | host: process.env.DB_HOST, |
10 | user: process.env.DB_USER, | 9 | user: process.env.DB_USER, |
11 | port: process.env.DB_PORT, | 10 | port: process.env.DB_PORT, |
12 | password: process.env.DB_PASSWORD, | 11 | password: process.env.DB_PASSWORD, |
13 | database: process.env.DB_DATABASE, | 12 | database: process.env.DB_DATABASE, |
14 | connectionLimit: 10, | 13 | connectionLimit: 10, |
15 | - }); | 14 | +}); |
16 | 15 | ||
17 | module.exports = db; | 16 | module.exports = db; | ... | ... |
... | @@ -2,9 +2,9 @@ const express = require('express'); | ... | @@ -2,9 +2,9 @@ const express = require('express'); |
2 | 2 | ||
3 | const router = express.Router(); | 3 | const router = express.Router(); |
4 | 4 | ||
5 | +const { calcDistance } = require('../controller/calcDistance'); | ||
6 | + | ||
5 | /* GET home page. */ | 7 | /* GET home page. */ |
6 | -router.post('/checkProfanity', (req, res) => { | 8 | +router.post('/checkProfanity', calcDistance); |
7 | - res.send('hello'); | ||
8 | -}); | ||
9 | 9 | ||
10 | module.exports = router; | 10 | module.exports = router; | ... | ... |
1 | +/* eslint-disable no-restricted-syntax */ | ||
1 | const levenshtein = require('js-levenshtein'); | 2 | const levenshtein = require('js-levenshtein'); |
3 | +const Hangul = require('hangul-js'); | ||
2 | 4 | ||
3 | -const calcDistance = () => { | 5 | +exports.calcDistance = (word, profanityList) => { |
4 | - const result = levenshtein('개새끼', 'ㄱHsㅐㄲ!'); | 6 | + let leastLevenshtein = 1000; |
5 | - console.log(result); | 7 | + |
6 | -}; | 8 | + for (const profanity of profanityList[0]) { |
9 | + let compareWord = Hangul.disassemble(profanity.content); | ||
10 | + compareWord = compareWord.join(''); | ||
11 | + if (leastLevenshtein > levenshtein(word, compareWord)) { | ||
12 | + leastLevenshtein = levenshtein(word, compareWord); | ||
13 | + } | ||
14 | + } | ||
7 | 15 | ||
8 | -calcDistance(); | 16 | + return leastLevenshtein; |
17 | +}; | ... | ... |
server/service/query.js
0 → 100644
1 | /* eslint-disable no-use-before-define */ | 1 | /* eslint-disable no-use-before-define */ |
2 | /* eslint-disable no-restricted-syntax */ | 2 | /* eslint-disable no-restricted-syntax */ |
3 | +const { config } = require('dotenv'); | ||
3 | const Hangul = require('hangul-js'); | 4 | const Hangul = require('hangul-js'); |
4 | 5 | ||
5 | const standardize = (word) => { | 6 | const standardize = (word) => { |
... | @@ -12,7 +13,7 @@ const standardize = (word) => { | ... | @@ -12,7 +13,7 @@ const standardize = (word) => { |
12 | ? standardizeConsonent(letter) | 13 | ? standardizeConsonent(letter) |
13 | : standardizeVowel(letter); | 14 | : standardizeVowel(letter); |
14 | } else { | 15 | } else { |
15 | - const preLetter = index === 1 ? '' : disassembledWord[index - 1]; | 16 | + const preLetter = index === 0 ? '' : disassembledWord[index - 1]; |
16 | const nextLetter = index === disassembledWord.length - 1 ? '' : disassembledWord[index + 1]; | 17 | const nextLetter = index === disassembledWord.length - 1 ? '' : disassembledWord[index + 1]; |
17 | newLetter = isVowelSpecial(preLetter, letter, nextLetter) | 18 | newLetter = isVowelSpecial(preLetter, letter, nextLetter) |
18 | ? standardizeSpecialVowel(letter) | 19 | ? standardizeSpecialVowel(letter) |
... | @@ -120,18 +121,44 @@ const standardizeSpecialConsonent = (letter) => { | ... | @@ -120,18 +121,44 @@ const standardizeSpecialConsonent = (letter) => { |
120 | }; | 121 | }; |
121 | 122 | ||
122 | const standardizeSpecialVowel = (letter) => { | 123 | const standardizeSpecialVowel = (letter) => { |
124 | + let result = ''; | ||
125 | + | ||
123 | switch (letter) { | 126 | switch (letter) { |
124 | case ('a', 'A'): | 127 | case ('a', 'A'): |
125 | - return 'ㅏ'; | 128 | + result = 'ㅏ'; |
129 | + break; | ||
126 | case ('u', 'U'): | 130 | case ('u', 'U'): |
127 | - return 'ㅓ'; | 131 | + result = 'ㅓ'; |
132 | + break; | ||
128 | case ('@', 'H'): | 133 | case ('@', 'H'): |
129 | - return 'ㅐ'; | 134 | + result = 'ㅐ'; |
130 | - case ('l', '1', 'i', 'I', '!'): | 135 | + break; |
131 | - return 'ㅣ'; | 136 | + case ('l', 'i', 'I', '!', 1): |
137 | + result = 'ㅣ'; | ||
138 | + break; | ||
139 | + case '1': | ||
140 | + result = 'ㅣ'; | ||
141 | + break; | ||
132 | default: | 142 | default: |
133 | - return letter; | 143 | + result = letter; |
144 | + break; | ||
134 | } | 145 | } |
146 | + | ||
147 | + return result; | ||
148 | +}; | ||
149 | + | ||
150 | +const removeSpecial = (word) => { | ||
151 | + const numCheck = /[0-9]/g; // 숫자 | ||
152 | + | ||
153 | + const engCheck = /[a-zA-Z]/g; // 문자 | ||
154 | + | ||
155 | + const specialCheck = /[~!@#$%^&*()_+|<>?:{}]/g; // 특수문자 | ||
156 | + | ||
157 | + let result = word.replace(numCheck, ''); | ||
158 | + result = result.replace(engCheck, ''); | ||
159 | + result = result.replace(specialCheck, ''); | ||
160 | + | ||
161 | + return result; | ||
135 | }; | 162 | }; |
136 | 163 | ||
137 | -module.exports = { standardize }; | 164 | +module.exports = { standardize, removeSpecial }; | ... | ... |
-
Please register or login to post a comment