Class TextSimilarity

java.lang.Object
org.miaixz.bus.core.text.TextSimilarity

public class TextSimilarity extends Object
文本相似度计算
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    最长公共子串,采用动态规划算法。 其不要求所求得的字符在所给的字符串中是连续的。 算法解析见:zui-chang-gong-gong-zi-xu-lie-by-leetcod-y7u0
    static int
    求公共子串,采用动态规划算法。 其不要求所求得的字符在所给的字符串中是连续的。 2023-04-06 优化堆内存占用,此处不需要matrix[m][n]的完整矩阵,仅需右下角值
    static double
    similar(String strA, String strB)
    利用莱文斯坦距离(Levenshtein distance)算法计算相似度,两个都是空串相似度为1,被认为是相同的串 比较方法为: 只比较两个字符串字母、数字、汉字部分,其他符号去除 计算出两个字符串最大子串,除以最长的字符串,结果即为相似度
    static String
    similar(String strA, String strB, int scale)
    利用莱文斯坦距离(Levenshtein distance)算法计算相似度百分比

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TextSimilarity

      public TextSimilarity()
  • Method Details

    • similar

      public static double similar(String strA, String strB)
      利用莱文斯坦距离(Levenshtein distance)算法计算相似度,两个都是空串相似度为1,被认为是相同的串 比较方法为:
      • 只比较两个字符串字母、数字、汉字部分,其他符号去除
      • 计算出两个字符串最大子串,除以最长的字符串,结果即为相似度
      Parameters:
      strA - 字符串1
      strB - 字符串2
      Returns:
      相似度
    • similar

      public static String similar(String strA, String strB, int scale)
      利用莱文斯坦距离(Levenshtein distance)算法计算相似度百分比
      Parameters:
      strA - 字符串1
      strB - 字符串2
      scale - 保留小数
      Returns:
      百分比
    • longestCommonSubstring

      public static String longestCommonSubstring(String strA, String strB)
      最长公共子串,采用动态规划算法。 其不要求所求得的字符在所给的字符串中是连续的。 算法解析见:zui-chang-gong-gong-zi-xu-lie-by-leetcod-y7u0
      Parameters:
      strA - 字符串1
      strB - 字符串2
      Returns:
      最长公共子串
    • longestCommonSubstringLength

      public static int longestCommonSubstringLength(String strA, String strB)
      求公共子串,采用动态规划算法。 其不要求所求得的字符在所给的字符串中是连续的。 2023-04-06 优化堆内存占用,此处不需要matrix[m][n]的完整矩阵,仅需右下角值
      Parameters:
      strA - 字符串1
      strB - 字符串2
      Returns:
      公共子串