문자열 싱크로 율

업무 수요 로 인해 회사 의 데 이 터 는 고객 의 데이터 진도 와 일치 해 야 하기 때문에 인터넷 에서 방법 을 찾 았 는데 괜 찮 은 것 같 습 니 다. 저 는 코사인 알고리즘 을 사 용 했 습 니 다.제 참고 주소 입 니 다.http://www.maoguangpu.com/post/117.html。 안에 중국어 만 처리 하고 저 는 개선 을 했 습 니 다. 중, 영, 기 호 는 모두 진행 할 수 있 습 니 다.상위 코드
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class StringSimilar {
    
    
    public static void main (String [] args){
        System.out.println(StringSimilar.getSimilarity("a ��", "��a"));
    }
    public static double getSimilarity(String doc1, String doc2) {
        doc1=doc1.trim();
        doc2=doc2.trim();
        if (doc1 != null && doc1.length() > 0 && doc2 != null
                && doc2.length() > 0) {
            
            Map<Integer, int[]> AlgorithmMap = new HashMap<Integer, int[]>();
            
            //                      ,AlgorithmMap 
            for (int i = 0; i < doc1.length(); i++) {
                char d1 = doc1.charAt(i);
                if(isHanZi(d1)){
                    int charIndex = getGB2312Id(d1);
                    if(charIndex != -1){
                        int[] fq = AlgorithmMap.get(charIndex);
                        if(fq != null && fq.length == 2){
                            fq[0]++;
                        }else {
                            fq = new int[2];
                            fq[0] = 1;
                            fq[1] = 0;
                            AlgorithmMap.put(charIndex, fq);
                        }
                    }
                }else{
                    int by=(int)-String.valueOf(d1).getBytes()[0];
                    int[] fq = AlgorithmMap.get(by);
                    if(fq != null && fq.length == 2){
                        fq[0]++;
                    }else {
                        fq = new int[2];
                        fq[0] = 1;
                        fq[1] = 0;
                        AlgorithmMap.put(by, fq);
                    }
                }
            }

            for (int i = 0; i < doc2.length(); i++) {
                char d2 = doc2.charAt(i);
                if(isHanZi(d2)){
                    int charIndex = getGB2312Id(d2);
                    if(charIndex != -1){
                        int[] fq = AlgorithmMap.get(charIndex);
                        if(fq != null && fq.length == 2){
                            fq[1]++;
                        }else {
                            fq = new int[2];
                            fq[0] = 0;
                            fq[1] = 1;
                            AlgorithmMap.put(charIndex, fq);
                        }
                    }
                }else{
                    int by=(int)-String.valueOf(d2).getBytes()[0];
                    int[] fq = AlgorithmMap.get(by);
                    if(fq != null && fq.length == 2){
                        fq[1]++;
                    }else {
                        fq = new int[2];
                        fq[0] = 0;
                        fq[1] = 1;
                        AlgorithmMap.put(by, fq);
                    }
                }
            }
            
            Iterator<Integer> iterator = AlgorithmMap.keySet().iterator();
            double sqdoc1 = 0;
            double sqdoc2 = 0;
            double denominator = 0; 
            while(iterator.hasNext()){
                int[] c = AlgorithmMap.get(iterator.next());
                denominator += c[0]*c[1];
                sqdoc1 += c[0]*c[0];
                sqdoc2 += c[1]*c[1];
            }
            
            return denominator / Math.sqrt(sqdoc1*sqdoc2);
        } else {
            throw new NullPointerException(
                    " the Document is null or have not cahrs!!");
        }
    }

    public static boolean isHanZi(char ch) {
        //       
        return (ch >= 0x4E00 && ch <= 0x9FA5);

    }

    /**
     *      Unicode  ,    GB2312    ascii  ,
     * 
     * @param ch
     *               GB2312      ASCII  (128 )
     * @return ch GB2312    ,-1        
     */
    public static short getGB2312Id(char ch) {
        try {
            byte[] buffer = Character.toString(ch).getBytes("GB2312");
            if (buffer.length != 2) {
                //      buffer       ,    ch   GB2312  ,   '?',          
                
                return -1;
            }
            int b0 = (int) (buffer[0] & 0x0FF) - 161; //    A1  ,    0xA1=161
            int b1 = (int) (buffer[1] & 0x0FF) - 161; //                 ,       16*6-2=94   
            return (short) (b0 * 94 + b1);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return -1;
    }
}

좋은 웹페이지 즐겨찾기