1. CLASS


public static String getSearchKeyWord(String searchKey){

    String searchName = "";
   
    try {
        searchName = createNameKeyword("", searchKey.split(",|\\s+"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return searchName;
}
       
public static String getSearchKeyWord(String searchKey, String resetKey){
    String searchName = "";
 
    try {
        searchName = createNameKeyword(resetKey.trim(), searchKey.split(",|\\s+"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return searchName;
}
  
public static String createNameKeyword(String rsKey, String... tkey)  throws Exception {
    if("resetKey".equals(rsKey)){
        System.out.println("맵에 저장된 검색어 리셋!");
        hs = new HashMap<Integer, Object>();
    }
     
    Pattern p = Pattern.compile("[a-zA-Zㄱ-ㅎ가-힣0-9]");

    String addKeyWord = "";

        
    for(String value : tkey){
        value = value.trim();
        System.out.println("value : " + value);
         
        Matcher m = p.matcher(value);
         
        if(!value.equals("\\s+")) while(m.find()) addKeyWord += makeRexKey(m.group(0));
        addKeyWord = value.matches("\\s+") || value.matches("") ? addKeyWord : addKeyWord + "|";
    }


    String repAddr = (tkey.length == 1 && tkey[0].length() > 0) ? addKeyWord.substring(0, addKeyWord.length() - 1) : (tkey.length > 1 && addKeyWord.substring(addKeyWord.length() - 1, addKeyWord.length()).equals("|")) ? addKeyWord.substring(0, addKeyWord.length() - 1) : addKeyWord;


    return repAddr;

}

    
public static String makeRexKey(String searchKor){
    Map<String, Object> rs = new HashMap<String, Object>();
     
    String addKeyWord = "";
     
    if(hs.containsKey(searchKor.hashCode()) == true){ 
        addKeyWord += hs.get(searchKor.hashCode());
    }else{
        String searchKeyWord = "";
         
        /* DB용
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("master", searchKor);
        */

   

        if(searchKor.matches("[a-zA-Zㄱ-ㅎ0-9]")){
            searchKeyWord += searchKor;
        }else{
            rs = getSearchWord(searchKor);

            if(rs == null) searchKeyWord += searchKor;

            else{
                String k[] = rs.get("other") != null ?  rs.get("other").toString().split(",") : null;
              
                if(k != null){   
                    for(int i = 0; i < k.length; i++){
                        if(i == 0) searchKeyWord += "(" + searchKor + "|" + k[0].trim();
                        else searchKeyWord += "|" + k[i].trim();
                    }
                    searchKeyWord += ")";
                    hs.put(searchKor.hashCode(), searchKeyWord);
                }else searchKeyWord += searchKor;    
            }
        }
        addKeyWord += searchKeyWord;
    }
    return addKeyWord;
}


// 검색용 테스트 데이터
public static HashMap<String, Object> getSearchWord(String tKey){
     List<HashMap<String, Object>> resultSearch = new ArrayList<HashMap<String, Object>>();


     Map<String, Object> rs = new HashMap<String, Object>();
     rs.put("master", "류");
     rs.put("other", "유, 꾸");
     resultSearch.add((HashMap<String, Object>) rs);
     
     Map<String, Object> rs1 = new HashMap<String, Object>();
     rs1.put("master", "유");
     rs1.put("other", "류, 꾸");
     resultSearch.add((HashMap<String, Object>) rs1);
     
     Map<String, Object> rs2 = new HashMap<String, Object>();
     rs2.put("master", "용");
     rs2.put("other", "룡");
     resultSearch.add((HashMap<String, Object>) rs2);
     
     Map<String, Object> rs3 = new HashMap<String, Object>();
     rs3.put("master", "룡");
     rs3.put("other", "용");
     resultSearch.add((HashMap<String, Object>) rs3);
     
     HashMap<String, Object> returnForm = new HashMap<String, Object>();
     
     for(int i = 0; i < resultSearch.size();i++){
        if(tKey.equals(resultSearch.get(i).get("master"))){
            returnForm.put("other", resultSearch.get(i).get("other"));
        }
    }
    return returnForm;     
}


public static void main(String arg[]) throws Exception{
    getSearchKeyWord("aa 유성룡, 김수로", "");
}




2. RUN


aa|(유|류|꾸)성(룡|용)|김수로

'course > 자바' 카테고리의 다른 글

VO에 MAP등의 값을 대칭입력.  (0) 2017.09.27
문자열의 일정 자릿수까지 특정 문자로 변경.  (0) 2017.04.18
간단한 문자열 검색  (0) 2017.04.18