利用分詞技術實現,生成兩個字符串匹配度和相似率。


  業務場景是客戶在業務辦理時候需要提交一個材料列表,材料會入材料庫,下次客戶再來辦理業務時候輸入客戶的身份證,會通過材料庫進行加載,我們通過材料名稱匹配材料相似度就不用再手動上傳材料。(首先需要IKAnalyzer2012FF_u1.jar 進行下載支持的jar)

1.以下是對兩個詞進行處理的核心算法

package com.ikanalyzer;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

/**
 * Description:相似度百分比
 * @author: administrator
 * @Date: 2015-1-22下午1:20:34
 * @version 1.0
 */
public class IKAnalyzerUtil
{
    //閾值
    public static double YUZHI = 0.2 ;
    
    /**
     * 返回百分比
     * @author: Administrator
     * @Date: 2015年1月22日
     * @param T1
     * @param T2
     * @return
     */
    public static double getSimilarity(Vector<String> T1, Vector<String> T2) throws Exception {
        int size = 0 , size2 = 0 ;
        if ( T1 != null && ( size = T1.size() ) > 0 && T2 != null && ( size2 = T2.size() ) > 0 ) {
            
            Map<String, double[]> T = new HashMap<String, double[]>();
            
            //T1和T2的並集T
            String index = null ;
            for ( int i = 0 ; i < size ; i++ ) {
                index = T1.get(i) ;
                if( index != null){
                    double[] c = T.get(index);
                    c = new double[2];
                    c[0] = 1;    //T1的語義分數Ci
                    c[1] = YUZHI;//T2的語義分數Ci
                    T.put( index, c );
                }
            }
     
            for ( int i = 0; i < size2 ; i++ ) {
                index = T2.get(i) ;
                if( index != null ){
                    double[] c = T.get( index );
                    if( c != null && c.length == 2 ){
                        c[1] = 1; //T2中也存在,T2的語義分數=1
                    }else {
                        c = new double[2];
                        c[0] = YUZHI; //T1的語義分數Ci
                        c[1] = 1; //T2的語義分數Ci
                        T.put( index , c );
                    }
                }
            }
                
            //開始計算,百分比
            Iterator<String> it = T.keySet().iterator();
            double s1 = 0 , s2 = 0, Ssum = 0;  //S1、S2
            while( it.hasNext() ){
                double[] c = T.get( it.next() );
                Ssum += c[0]*c[1];
                s1 += c[0]*c[0];
                s2 += c[1]*c[1];
            }
            //百分比
            return Ssum / Math.sqrt( s1*s2 );
        } else {
            throw new Exception("傳入參數有問題!");
        }
    }
}

 

2.以下是調用方法進行分詞處理返回兩個詞的相似度

package com.ikanalyzer;

import java.io.IOException;
import java.io.StringReader;
import java.util.Vector;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

public class CheckTheSame {
    
/**
 * 分詞
 * @author: administrator
 * @Date: 2016年3月5日15:10:47
 * @param str
 * @return
 */
public static Vector<String> participle( String str ) {
    
    Vector<String> str1 = new Vector<String>() ;//對輸入進行分詞
    
    try {
        
        StringReader reader = new StringReader( str ); 
        IKSegmenter ik = new IKSegmenter(reader,false);//當為true時,分詞器進行智能切分 
        Lexeme lexeme = null ;            
        
        while( ( lexeme = ik.next() ) != null ) {
            str1.add( lexeme.getLexemeText() ); 
        }            
        
        if( str1.size() == 0 ) {
            return null ;
        }
        
         //分詞后
       // System.out.println( "str分詞后:" + str1 );
        
    } catch ( IOException e1 ) {
        //System.out.println();
    }
    return str1;
}
/**
 * 返回比較的兩個字符串的相似度
 * @param strone
 * @param strtwo
 * @return
 */
public String getSemblance(String strone,String strtwo) {
    String semblanceString = "0.0000";
    //分詞
    Vector<String> strs1 = participle(strone) ;
    Vector<String> strs2 = participle(strtwo) ;
    //根據分詞返回相似度
    double same = 0 ;
    try {
        same = IKAnalyzerUtil.getSimilarity( strs1 , strs2 );
    } catch (Exception e) {
        //System.out.println( e.getMessage() );
    }
    semblanceString=String.valueOf(same);
    //System.out.println( "相似度:" + same );
    return semblanceString;
}
    public static void main(String[] args) {
        
        //分詞
        Vector<String> strs1 = participle( "身份證明" ) ;
        Vector<String> strs2 = participle( "個人身份證明復印件" ) ;
        
        //根據分詞返回相似度
        double same = 0 ;
        try {
            same = IKAnalyzerUtil.getSimilarity( strs1 , strs2 );
        } catch (Exception e) {
            System.out.println( e.getMessage() );
        }
        
        System.out.println( "相似度:" + same );
    }
}

具體在實現中如下

ikanalyzer還有好多算法去進行相似度匹配忘以后更加研究


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM