package itn.let.mjo.spammsg.web; import java.util.List; import itn.let.hangulparser.HangulParser; public class ComGetSpamStringParser { /* * 스팸 문자 필터링을 위한 변형된 한글 SMS 문장의 정규화 기법을 기반으로 개발 * Kang Seung-Shik * * 20230419 * */ //이상문자열 파싱 후 결합하여 public static String getSpamTextParse(String strString) throws Exception { String smsTxt = strString; String repSmsTxt = smsTxt.replaceAll("[^ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z0-9①ⓒ⒪㈄β]", ""); //한글, 영문, 숫자 이외의 문자는 제거 String[] split = repSmsTxt.split(" "); //띄어쓰기를 기준으로 분할 String reMakeStringText = ""; try { for(String txt : split) { for(int i=0; i < txt.length(); i++) { char test = txt.charAt(i); if(test >= 0xAC00) {//한글 문자이면 스킵 continue; }else { String tmpStr = Character.toString(test); if(tmpStr.matches(".*[ㄱ-ㅎ]+.*")) { String nextCharAt = Character.toString(txt.charAt(i+1));//현재 단어 다음 단어가 어떤것인지 가져온다. if(i == txt.length() - 1 ) {//일단 마지막 자음은 스킵 한다. break; }/*else if(!nextCharAt.matches(".*[ㄱ-ㅎㅏ-ㅣa-zA-Z]+.*")) {//현재 자음이면서 다음글자가 자/모음이 아니면 삭제 액ㄱ정 이런 경우 삭제하려고 함. txt = txt.replace(tmpStr, ""); break; }*/ if(nextCharAt.matches(".*[a-zA-Z]+.*")) {//다음 단어가 영문이면 영문과 유사한 한글 모음을 찾아서 합쳐준다. String repCharAt = getEngToHanglue(nextCharAt); String repStringChar = tmpStr + repCharAt; String repTxt = tmpStr + nextCharAt; txt = txt.replace(repTxt, repStringChar); //System.out.println(txt); }/*else if(nextCharAt.matches(".*[ㅏ-ㅣ]+.*")) {//다음 단어가 모음이면 앞 자음과 합쳐 주기 String repStringChar = tmpStr + nextCharAt; //String norString = Normalizer.normalize(repStringChar, Normalizer.Form.NFC); //System.out.println(repStringChar); //txt = txt.replace(repStringChar, repStringChar);//분리된 자믐, 모음을 합쳐진 한 단어로 치환해준다. //System.out.println(txt); }*/ }else if(tmpStr.matches(".*[a-zA-Z]+.*")) {//영문단어가 나오면 앞뒤 단어 체크하여 연관성 없으면 삭제 처리. String engStr = tmpStr; if(i < txt.length() - 1) {//마지막 글자 제외 char nextEngChar = txt.charAt(i+1); if(i > 0) {//첫글자가 영문인 경우 제외 char befEngChar = txt.charAt(i-1); //한글 단어사이에 영문자 한글자만 있는 경우 삭제 처리함. if(nextEngChar >= 0xAC00 && befEngChar >= 0xAC00) { if(tmpStr.toLowerCase().equals("b")) {//B를 넣어서 한글로 읽힐수 있게도 함, 서B스 와 같은 형태 txt = txt.replace(engStr, "비"); }else { txt = txt.replace(engStr, ""); } } } }else { char befEngChar = txt.charAt(i-1); //마지막 글자이고 앞글자가 한글이면 마지막 영단어 삭제 처리 if(befEngChar >= 0xAC00) { txt = txt.replace(engStr, ""); //continue; } } }else if(tmpStr.matches(".*[0-9]+.*")) { if(i-1 < 0) {//한글자만 있는경우 패스 continue; } String befString = Character.toString(txt.charAt(i-1)); if(befString.matches(".*[ㄱ-ㅎ]+.*")) {//이전 글자가 자음이면 변환 문자 체크 String numToStr = getNumberToString(tmpStr); //숫자를 모음으로 변환 처리 String orgStringChar = befString + tmpStr; //원래 앞자음 + 숫자 String repStringChar = befString + numToStr; //원래 앞자음 + 숫자를 변환한 모음 txt = txt.replace(orgStringChar, repStringChar);//분리된 자믐, 모음을 합쳐진 한 단어로 치환해준다. //System.out.println(txt); }else { //숫자면 스킵 continue; } }else { //특수문자들 처리 String repSpcStr = getRepSpacialString(tmpStr); if(!repSpcStr.equals("")) {//변환문자가 있는경우만 치환 txt = txt.replace(tmpStr, repSpcStr); } } } } reMakeStringText = reMakeStringText + " " + txt; } //System.out.println("++++++++++++++ ::: "+reMakeStringText); //넘어오는 모든 글자에서 한글에 대해서 자/모음을 분리해준다. List jasoList = HangulParser.disassemble(reMakeStringText); //분리된 자/모음 리스트를 다시 한글로 합쳐준다. String assembleStr = HangulParser.assemble(jasoList); reMakeStringText = assembleStr; } catch (Exception e) { System.out.println("++++++++++ getSpamTextParse Error !!! "+e); return "getSpamTextParse 오류가 발생하였습니다."; } return reMakeStringText; } /* * * 한글과 유사한 영문자를 변환 처리함 * * */ 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; } }