package itn.let.mjo.spammsg.web; import java.util.List; import itn.let.hangulparser.HangulParser; public class ComGetSpamStringParser { /** * @methodName : getSpamTextParse * @author : 이호영 * @date : 2024.11.13 * @description : 리펙토링 * 원본 : ComGetSpamStringParser_advc_backup_20241113.java * @param strString * @return * @throws Exception */ public static String getSpamTextParse(String strString) throws Exception { String smsTxt = strString; String repSmsTxt = smsTxt.replaceAll("[^ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z0-9①ⓒ⒪㈄β]", ""); // 한글, 영문, 숫자 이외의 문자는 제거 String[] split = repSmsTxt.split(" "); StringBuilder reMakeStringText = new StringBuilder(); try { for (String txt : split) { txt = processCharacters(txt); reMakeStringText.append(" ").append(txt); } // 분리된 자/모음을 합쳐 최종 문자열 생성 List jasoList = HangulParser.disassemble(reMakeStringText.toString()); String assembleStr = HangulParser.assemble(jasoList); return assembleStr; } catch (Exception e) { System.out.println("++++++++++ getSpamTextParse Error !!! " + e); return "getSpamTextParse 오류가 발생하였습니다."; } } // 한 단어 내 각 문자에 대한 처리 로직 private static String processCharacters(String txt) throws Exception { StringBuilder processedText = new StringBuilder(txt); for (int i = 0; i < txt.length(); i++) { char currentChar = txt.charAt(i); if (isKorean(currentChar)) continue; String tmpStr = Character.toString(currentChar); // 자음, 영문, 숫자, 특수문자 각각에 대한 처리 if (isKoreanConsonant(tmpStr)) { txt = handleConsonant(txt, tmpStr, i); } else if (isEnglish(tmpStr)) { txt = handleEnglish(txt, tmpStr, i); } else if (isNumber(tmpStr)) { txt = handleNumber(txt, tmpStr, i); } else { txt = handleSpecialCharacter(txt, tmpStr); } } return processedText.toString(); } // 자음 처리 로직 private static String handleConsonant(String txt, String tmpStr, int index) throws Exception { if (index == txt.length() - 1) return txt; String nextCharAt = Character.toString(txt.charAt(index + 1)); if (isEnglish(nextCharAt)) { String repCharAt = getEngToHanglue(nextCharAt); txt = txt.replace(tmpStr + nextCharAt, tmpStr + repCharAt); } return txt; } // 영문 처리 로직 private static String handleEnglish(String txt, String engStr, int index) { if (index < txt.length() - 1 && index > 0) { char nextChar = txt.charAt(index + 1); char previousChar = txt.charAt(index - 1); if (isKorean(previousChar) && isKorean(nextChar)) { txt = txt.replace(engStr, engStr.equalsIgnoreCase("b") ? "비" : ""); } } else if (index > 0 && isKorean(txt.charAt(index - 1))) { txt = txt.replace(engStr, ""); } return txt; } // 숫자 처리 로직 private static String handleNumber(String txt, String tmpStr, int index) throws Exception { if (index == 0) return txt; String previousStr = Character.toString(txt.charAt(index - 1)); if (isKoreanConsonant(previousStr)) { String numToStr = getNumberToString(tmpStr); txt = txt.replace(previousStr + tmpStr, previousStr + numToStr); } return txt; } // 특수문자 처리 로직 private static String handleSpecialCharacter(String txt, String tmpStr) throws Exception { String repSpcStr = getRepSpacialString(tmpStr); if (!repSpcStr.isEmpty()) { txt = txt.replace(tmpStr, repSpcStr); } return txt; } // 문자 유형 판별 메서드 private static boolean isKorean(char ch) { return ch >= 0xAC00; } private static boolean isKoreanConsonant(String str) { return str.matches(".*[ㄱ-ㅎ]+.*"); } private static boolean isEnglish(String str) { return str.matches(".*[a-zA-Z]+.*"); } private static boolean isNumber(String str) { return str.matches(".*[0-9]+.*"); } /* * * 한글과 유사한 영문자를 변환 처리함 * * */ public static String getEngToHanglue(String strWord) throws Exception { String returnStr = ""; try { if(strWord.toLowerCase().equals("r")) { returnStr = "ㅏ"; }else if(strWord.toLowerCase().equals("h")) { returnStr = "ㅐ"; }else if(strWord.toLowerCase().equals("i") || strWord.toLowerCase().equals("I") || strWord.toLowerCase().equals("l")) { returnStr = "ㅣ"; } } catch (Exception e) { System.out.println("++++++++++++++ getEngToHanglue Error !!! "+e); } return returnStr; } /* * * 한글과 유사한 숫자를 변환처리함 * * */ public static String getNumberToString(String strWord) throws Exception { String returnStr = ""; try { if(strWord.equals("1")) { returnStr = "ㅣ"; } } catch (Exception e) { System.out.println("++++++++++++++ getNumberToString Error !!! "+e); } return returnStr; } /* * * 한글과 유사한 특수문자들 변환처리함 * * */ public static String getRepSpacialString(String strWord) throws Exception{ String returnStr = ""; try { if(strWord.equals("ⓒ")) { returnStr = "c"; }else if(strWord.equals("β")) { returnStr = "비"; }else if(strWord.equals("⒪")) { returnStr = "ㅇ"; }else if(strWord.equals("㈄")) { returnStr = "ㅁ"; }else if(strWord.equals("①")) { returnStr = "ㅣ"; } } catch (Exception e) { System.out.println("++++++++++++++ getRepSpacialString Error !!! "+e); } return returnStr; } }