장준영

feat : calcDistance 구현

const e = require('express');
const levenshtein = require('js-levenshtein');
const db = require('../model/db');
const {calcDistance} = require('../service/calcDistance');
const {getProfanity} = require('../service/query');
const {standardize, removeSpecial} = require('../service/standardization');
exports.calcDistance = async (req, res, next) => {
const { word } = req.body;
const profanityList = await getProfanity();
let specialRemoved = removeSpecial(word);
let specialNotRemoved = word;
specialRemoved = standardize(specialRemoved);
specialNotRemoved = standardize(specialNotRemoved);
const rareResult = calcDistance(word, profanityList);
const specialRemovedResult = calcDistance(specialRemoved, profanityList);
const specialNotRemovedResult = calcDistance(specialNotRemoved, profanityList);
const minResult = Math.min(rareResult, specialRemovedResult, specialNotRemovedResult);
if (minResult <= 1){
res.status(200).json({success : true, isProfanity : "YES"});
}else{
res.status(200).json({success : true, isProfanity : "NO"});
}
};
\ No newline at end of file
......@@ -2,16 +2,15 @@ const mysql = require('mysql2/promise');
const dotenv = require('dotenv');
const path = require('path');
dotenv.config({path:path.join(__dirname, "../.env")});
dotenv.config({ path: path.join(__dirname, '../.env') });
const db = mysql
.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
port: process.env.DB_PORT,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
connectionLimit: 10,
});
const db = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
port: process.env.DB_PORT,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
connectionLimit: 10,
});
module.exports = db;
......
......@@ -2,9 +2,9 @@ const express = require('express');
const router = express.Router();
const { calcDistance } = require('../controller/calcDistance');
/* GET home page. */
router.post('/checkProfanity', (req, res) => {
res.send('hello');
});
router.post('/checkProfanity', calcDistance);
module.exports = router;
......
/* eslint-disable no-restricted-syntax */
const levenshtein = require('js-levenshtein');
const Hangul = require('hangul-js');
const calcDistance = () => {
const result = levenshtein('개새끼', 'ㄱHsㅐㄲ!');
console.log(result);
};
exports.calcDistance = (word, profanityList) => {
let leastLevenshtein = 1000;
for (const profanity of profanityList[0]) {
let compareWord = Hangul.disassemble(profanity.content);
compareWord = compareWord.join('');
if (leastLevenshtein > levenshtein(word, compareWord)) {
leastLevenshtein = levenshtein(word, compareWord);
}
}
calcDistance();
return leastLevenshtein;
};
......
const db = require('../model/db');
exports.getProfanity = async () => {
const selectResult = await db.query("SELECT DISTINCT content from profanity");
return selectResult;
};
/* eslint-disable no-use-before-define */
/* eslint-disable no-restricted-syntax */
const { config } = require('dotenv');
const Hangul = require('hangul-js');
const standardize = (word) => {
......@@ -12,7 +13,7 @@ const standardize = (word) => {
? standardizeConsonent(letter)
: standardizeVowel(letter);
} else {
const preLetter = index === 1 ? '' : disassembledWord[index - 1];
const preLetter = index === 0 ? '' : disassembledWord[index - 1];
const nextLetter = index === disassembledWord.length - 1 ? '' : disassembledWord[index + 1];
newLetter = isVowelSpecial(preLetter, letter, nextLetter)
? standardizeSpecialVowel(letter)
......@@ -120,18 +121,44 @@ const standardizeSpecialConsonent = (letter) => {
};
const standardizeSpecialVowel = (letter) => {
let result = '';
switch (letter) {
case ('a', 'A'):
return 'ㅏ';
result = 'ㅏ';
break;
case ('u', 'U'):
return 'ㅓ';
result = 'ㅓ';
break;
case ('@', 'H'):
return 'ㅐ';
case ('l', '1', 'i', 'I', '!'):
return 'ㅣ';
result = 'ㅐ';
break;
case ('l', 'i', 'I', '!', 1):
result = 'ㅣ';
break;
case '1':
result = 'ㅣ';
break;
default:
return letter;
result = letter;
break;
}
return result;
};
const removeSpecial = (word) => {
const numCheck = /[0-9]/g; // 숫자
const engCheck = /[a-zA-Z]/g; // 문자
const specialCheck = /[~!@#$%^&*()_+|<>?:{}]/g; // 특수문자
let result = word.replace(numCheck, '');
result = result.replace(engCheck, '');
result = result.replace(specialCheck, '');
return result;
};
module.exports = { standardize };
module.exports = { standardize, removeSpecial };
......