zoukankan      html  css  js  c++  java
  • 利用分词技术实现,生成两个字符串匹配度和相似率。

      业务场景是客户在业务办理时候需要提交一个材料列表,材料会入材料库,下次客户再来办理业务时候输入客户的身份证,会通过材料库进行加载,我们通过材料名称匹配材料相似度就不用再手动上传材料。(首先需要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还有好多算法去进行相似度匹配忘以后更加研究
  • 相关阅读:
    android studio中timber的配置
    bufferknife框架的正确使用方式 -终于他么知道了
    开源的图像滤镜库----for Android
    HSV做通道分离是出现的Vector内存越界错误
    HSV与RGB的相互转换的公式
    Windows下python的第三方库的安装
    Python出现"Non-ASCII character 'xe6' in file"错误解决方法
    Verilog的IDE Quartus II
    Java-ZipUtil工具类
    Java-JDBCUtil工具类
  • 原文地址:https://www.cnblogs.com/Demcia/p/5453906.html
Copyright © 2011-2022 走看看