Java字符串相似度匹配

原文:http://wdhdmx.iteye.com/blog/1343856#bc2319361

package com.xfl.boot.common.utils;

/**
 * Created by XFL
 * time on 2018/11/16 0:10
 * description:
 */
public class SimilarityUtils {
    public static void main(String[] args) {
        //要比较的两个字符串
        String str1 = "鞋子不能在实体店买";
        String str2 = "不能在实体店买鞋子";
        levenshtein(str1.toLowerCase(),str2.toLowerCase());
    }

    /**
     *
     * @param str1
     * @param str2
     */
    public static void levenshtein(String str1,String str2) {
        //计算两个字符串的长度。
        int len1 = str1.length();
        int len2 = str2.length();
        //建立上面说的数组,比字符长度大一个空间
        int[][] dif = new int[len1 + 1][len2 + 1];
        //赋初值,步骤B。
        for (int a = 0; a <= len1; a++) {
            dif[a][0] = a;
        }
        for (int a = 0; a <= len2; a++) {
            dif[0][a] = a;
        }
        //计算两个字符是否一样,计算左上的值
        int temp;
        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    temp = 0;
                } else {
                    temp = 1;
                }
                //取三个值中最小的
                dif[i][j] = min(dif[i - 1][j - 1] + temp, dif[i][j - 1] + 1,
                        dif[i - 1][j] + 1);
            }
        }
        System.out.println("字符串\""+str1+"\"与\""+str2+"\"的比较");
        //取数组右下角的值,同样不同位置代表不同字符串的比较
        System.out.println("差异步骤:"+dif[len1][len2]);
        //计算相似度
        float similarity =1 - (float) dif[len1][len2] / Math.max(str1.length(), str2.length());
        System.out.println("相似度:"+similarity);
    }

    //得到最小值
    private static int min(int... is) {
        int min = Integer.MAX_VALUE;
        for (int i : is) {
            if (min > i) {
                min = i;
            }
        }
        return min;
    }
}

在这里插入图片描述

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是Java实现字符串余弦相似匹配的示例代码: ``` import java.util.HashMap; import java.util.Map; public class CosineSimilarity { public static double cosineSimilarity(String text1, String text2) { Map<String, Integer> wordFrequencyMap1 = new HashMap<>(); Map<String, Integer> wordFrequencyMap2 = new HashMap<>(); String[] words1 = text1.toLowerCase().split("\\W+"); String[] words2 = text2.toLowerCase().split("\\W+"); for (String word : words1) { if (wordFrequencyMap1.containsKey(word)) { wordFrequencyMap1.put(word, wordFrequencyMap1.get(word) + 1); } else { wordFrequencyMap1.put(word, 1); } } for (String word : words2) { if (wordFrequencyMap2.containsKey(word)) { wordFrequencyMap2.put(word, wordFrequencyMap2.get(word) + 1); } else { wordFrequencyMap2.put(word, 1); } } double dotProduct = 0.0; double normA = 0.0; double normB = 0.0; for (String word : wordFrequencyMap1.keySet()) { if (wordFrequencyMap2.containsKey(word)) { dotProduct += wordFrequencyMap1.get(word) * wordFrequencyMap2.get(word); } normA += Math.pow(wordFrequencyMap1.get(word), 2); } for (String word : wordFrequencyMap2.keySet()) { normB += Math.pow(wordFrequencyMap2.get(word), 2); } return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB)); } public static void main(String[] args) { String text1 = "Hello world"; String text2 = "hello world"; double cosineSimilarity = cosineSimilarity(text1, text2); System.out.println("Cosine similarity: " + cosineSimilarity); } } ``` 在`cosineSimilarity`方法中,我们将每个字符串转换为单词列表,然后计算它们的词频。然后,我们可以计算两个字符串的余弦相似度。在`main`方法中,我们定义了两个字符串并计算它们的余弦相似度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值