java字符串匹配+排序

參考資料

https://www.cnblogs.com/shangyangyang/p/11350322.html

https://www.iteye.com/blog/sunyuqian-2264336

https://my.oschina.net/u/3653755/blog/2248705

 

package com.hulk.util;

public class BondDto  implements Comparable<BondDto>{
    private String secid;
    
    private String secname;
    
    //匹配度
    private float similarity;

    public String getSecid() {
        return secid;
    }

    public void setSecid(String secid) {
        this.secid = secid;
    }

    public String getSecname() {
        return secname;
    }

    public void setSecname(String secname) {
        this.secname = secname;
    }

    
    public float getSimilarity() {
        return similarity;
    }

    public void setSimilarity(float similarity) {
        this.similarity = similarity;
    }

    @Override
    public int compareTo(BondDto o) {
        System.out.println(o.getSecid()+"=="+o.getSimilarity() +"====="+"=="+this.secid+"=="+ this.similarity);
        return (o.getSimilarity() > this.similarity) ? 1
                : ((o.getSimilarity() == this.similarity) ? 0 : -1);
    }
}
 

 

package com.hulk.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Test {
    
    private List<BondDto> dtoList=new ArrayList<BondDto>();
    
    public Test() {
//        System.out.println("數據準備開始: " + System.currentTimeMillis());
//        
//        for(int i=0;i<60000;i++) {
//            BondDto dto=new BondDto();
//            String formatter = "%0" + 15 + "d";
//            char c1=(char)(int)(Math.random()*26+97);
//            char c2=(char)(int)(Math.random()*26+97);
//            String secid=String.valueOf(c1) + String.valueOf(c2) +String.format(formatter, i);
//            dto.setSecid(secid);
//            dto.setSecname(secid);
//            //System.out.println(secid);
//            dtoList.add(dto);
//        }
    }
    
    public void preData(String filepath) {
        long start=System.currentTimeMillis();
        BufferedReader buf = null;
        try {
                buf = new BufferedReader(new InputStreamReader(new FileInputStream(filepath),"gb2312"));
                String line = null;
                while ((line = buf.readLine()) != null) {
                    String[] linearr=line.split("@!=");
                    BondDto dto=new BondDto();
                    dto.setSecid(linearr[0]);
                    dto.setSecname(linearr[1]);
                    dtoList.add(dto);
                }
        } catch(IOException e){
            e.printStackTrace();
        }finally {
            if (buf != null) {
                try {
                    buf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long end=System.currentTimeMillis();
        System.out.println("加載"+dtoList.size()+"耗時(毫秒): " + (end-start));
        System.out.println("===================================");
    }
    
    /**
     * 通配符匹配
     * @param pattern    通配符模式
     * @param str    待匹配的字符串
     * @return    匹配成功則返回true,否則返回false
     */
    private boolean wildcardMatch(String pattern, String str) {
        int patternLength = pattern.length();
        int strLength = str.length();
        int strIndex = 0;
        char ch;
        for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {
            ch = pattern.charAt(patternIndex);
            if (ch == '*') {
                //通配符星號*表示可以匹配任意多個字符
                while (strIndex < strLength) {
                    if (wildcardMatch(pattern.substring(patternIndex + 1),
                            str.substring(strIndex))) {
                        return true;
                    }
                    strIndex++;
                }
            } else if (ch == '?') {
                //通配符問號?表示匹配任意一個字符
                strIndex++;
                if (strIndex > strLength) {
                    //表示str中已經沒有字符匹配?了。
                    return false;
                }
            } else {
                if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {
                    return false;
                }
                strIndex++;
            }
        }
        return (strIndex == strLength);
    }
    
    public List<BondDto> test(String comparechar) {
        long start=System.currentTimeMillis();
        System.out.println("匹配字符: " + comparechar);
//        System.out.println("匹配開始: " + start);
        comparechar="*"+comparechar+"*";
        List<BondDto> matchedDtoList=new ArrayList<BondDto>();
        for(BondDto dto:dtoList) {
            //獲取匹配度
            dto.setSimilarity(this.getSimilarityRatio(comparechar, dto.getSecid()));
            if(wildcardMatch(comparechar, dto.getSecid())) {
                matchedDtoList.add(dto);
            }
        }
        System.out.println("匹配數量: " + matchedDtoList.size());
        long end=System.currentTimeMillis();
        System.out.println("匹配時間(毫秒): " + (end-start));
        System.out.println("===================================");
        
        return matchedDtoList;
//        for(BondDto dto:matchedDtoList) {
//            System.out.println(dto.getSecid());
//        }
    }
    
    /**
     * 兩個字符串的匹配度
     * @param str
     * @param target
     * @return
     */
    public float getSimilarityRatio(String str, String target) {

        int d[][]; // 矩陣
        int n = str.length();
        int m = target.length();
        int i; // 遍歷str的
        int j; // 遍歷target的
        char ch1; // str的
        char ch2; // target的
        int temp; // 記錄相同字符,在某個矩陣位置值的增量,不是0就是1
        if (n == 0 || m == 0) {
            return 0;
        }
        d = new int[n + 1][m + 1];
        for (i = 0; i <= n; i++) { // 初始化第一列
            d[i][0] = i;
        }

        for (j = 0; j <= m; j++) { // 初始化第一行
            d[0][j] = j;
        }

        for (i = 1; i <= n; i++) { // 遍歷str
            ch1 = str.charAt(i - 1);
            // 去匹配target
            for (j = 1; j <= m; j++) {
                ch2 = target.charAt(j - 1);
                if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
                    temp = 0;
                } else {
                    temp = 1;
                }
                // 左邊+1,上邊+1, 左上角+temp取最小
                d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + temp);
            }
        }

        return (1 - (float) d[n][m] / Math.max(str.length(), target.length())) * 100F;
    }
    public static void main(String[] args) {
        Test test=new Test();
        test.preData("F:/兆尹/文檔/中債登/12wdata.txt");
        System.out.println("數據準備結束...");
        System.out.println("===================================");
        test.test("1");
        test.test("18");
        test.test("180B");
        List<BondDto> list = test.test("180522");
        test.test("1805226.I");
        test.test("226.IB");
        
        long start=System.currentTimeMillis();
        //根據匹配度排序
        Collections.sort(list);
        
        long end=System.currentTimeMillis();
        System.out.println("根據匹配度排序(毫秒): " + (end-start));
        
        
        for (BondDto b : list) {
            System.out.println(b.getSecid());
        }
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章