Class StrSimilarity

java.lang.Object
com.sure.tool.text.StrSimilarity

public class StrSimilarity extends Object
字符串相似度工具类:编辑距离、相似度、Jaccard、余弦相似度,零依赖。
Since:
0.2.0
Author:
suretool
  • Method Details

    • levenshtein

      public static int levenshtein(String a, String b)
      Levenshtein 编辑距离(插入/删除/替换各计 1)。
      Parameters:
      a - 字符串
      b - 字符串
      Returns:
      编辑距离;任一为 null 按空串处理
    • similarity

      public static double similarity(String a, String b)
      相似度(1 - 编辑距离 / 最大长度),0~1,1 表示完全相同。
      Parameters:
      a - 字符串
      b - 字符串
      Returns:
      相似度
    • jaccard

      public static double jaccard(String a, String b)
      Jaccard 相似度(字符集合交集/并集),0~1。
      Parameters:
      a - 字符串
      b - 字符串
      Returns:
      Jaccard 相似度;两串均空返回 1
    • cosine

      public static double cosine(String a, String b)
      余弦相似度(按字符频率向量),0~1。
      Parameters:
      a - 字符串
      b - 字符串
      Returns:
      余弦相似度;两串均空返回 1
    • identical

      public static double identical(String a, String b)
      完全相同返回 1.0,否则返回 similarity(String, String) 相似度分数。
      Parameters:
      a - 字符串
      b - 字符串
      Returns:
      1.0 或相似度