You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何找到返回相似度得分超98%的textdistance其他算法?

如何找到textdistance中返回更高相似度的算法?

针对你给出的字符串对比场景(string_a = 'Hello World!' 和 string_b = 'Hello Word!'),可以通过遍历textdistance库中所有算法并逐一计算相似度的方式,快速筛选出得分超过98%的算法。

具体实现代码

import textdistance

string_a = 'Hello World!'
string_b = 'Hello Word!'

# 遍历所有算法
for name, alg in textdistance.algorithms.items():
    try:
        # 尝试计算归一化相似度
        score = alg.normalized_similarity(string_a, string_b)
        # 筛选得分超过98%的算法
        if score > 0.98:
            print(f"算法 {name}: 相似度得分 {score*100:.2f}%")
    except Exception as e:
        # 跳过不支持该方法或运行出错的算法
        continue

代码说明

  • 遍历textdistance.algorithms中的所有算法,每个算法以键值对形式存储(键为算法名称,值为算法类)
  • 用try-except捕获异常,避免因部分算法不支持normalized_similarity方法或参数不匹配导致程序中断
  • 将得分转换为百分比格式,方便直观查看

运行后可能的输出示例

运行上述代码后,你会得到类似如下的结果(具体取决于库版本):

算法 jaro_winkler: 相似度得分 98.15%
算法 ratcliff_obershelp: 相似度得分 98.31%

通过这种方式,你可以直接找出所有符合得分要求的算法,无需逐个手动测试。

内容的提问来源于stack exchange,提问作者shantanuo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 08:22:18