Python 查找两个字符串之间的相似性度量

Python 查找两个字符串之间的相似性度量,python,probability,similarity,metric,Python,Probability,Similarity,Metric,如何获得一个字符串与Python中的另一个字符串相似的概率 我希望得到一个十进制值,比如0.9(表示90%)等,最好使用标准Python和库 e、 g 您可以创建如下函数: def similar(w1, w2): w1 = w1 + ' ' * (len(w2) - len(w1)) w2 = w2 + ' ' * (len(w1) - len(w2)) return sum(1 if i == j else 0 for i, j in zip(w1, w2)) / f

如何获得一个字符串与Python中的另一个字符串相似的概率

我希望得到一个十进制值,比如0.9(表示90%)等,最好使用标准Python和库

e、 g


您可以创建如下函数:

def similar(w1, w2):
    w1 = w1 + ' ' * (len(w2) - len(w1))
    w2 = w2 + ' ' * (len(w1) - len(w2))
    return sum(1 if i == j else 0 for i, j in zip(w1, w2)) / float(len(w1))
有一个内置的

from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()
使用它:

>>> similar("Apple","Appel")
0.8
>>> similar("Apple","Mango")
0.0

我想你可能在寻找一种描述字符串之间距离的算法。以下是一些您可以参考的内容:


  • Fuzzy-Wuzzy
    是一个在python中实现Levenshtein距离的工具,在某些情况下,您可能希望两个不同的字符串被视为相同的,使用一些帮助函数可以提供帮助。例如:

    >>> fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
        91
    >>> fuzz.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
        100
    
    >>> a1 = "Apple"
    >>> a2 = "Appel"
    >>> a1 *= 50
    >>> a2 *= 50
    >>> SequenceMatcher(None, a1, a2).ratio()
    0.012  # very low
    >>> SequenceMatcher(None, a1, a2).get_matching_blocks()
    [Match(a=0, b=0, size=3), Match(a=250, b=250, size=0)]  # only the first block is recorded
    
    套餐包括Levenshtein距离:

    import distance
    distance.levenshtein("lenvestein", "levenshtein")
    # 3
    
    解决方案#1:Python内置 使用自

    专业人士: 本机python库,无需额外的软件包。
    缺点:太有限了,有太多其他好的字符串相似性算法

    例子: 解决方案2:图书馆 这是一个很好的图书馆,覆盖率高,发行量少。 它支持:
    -Levenshtein距离
    -达梅劳-列文施坦距离
    -雅罗距离
    -雅罗-温克勒距离
    -匹配评级方法比较
    -汉明距离

    专业人士: 易于使用,支持多种算法,经过测试。
    缺点:不是本机库

    例如:


    内置
    SequenceMatcher
    在大输入时速度非常慢,下面介绍如何使用:


    注意,
    difflib.SequenceMatcher
    仅查找最长的连续匹配子序列,这通常不是所需的,例如:

    >>> fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
        91
    >>> fuzz.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
        100
    
    >>> a1 = "Apple"
    >>> a2 = "Appel"
    >>> a1 *= 50
    >>> a2 *= 50
    >>> SequenceMatcher(None, a1, a2).ratio()
    0.012  # very low
    >>> SequenceMatcher(None, a1, a2).get_matching_blocks()
    [Match(a=0, b=0, size=3), Match(a=250, b=250, size=0)]  # only the first block is recorded
    
    寻找两个字符串之间的相似性与生物信息学中的成对序列比对概念密切相关。有许多专门用于此目的的库,包括。此示例实现以下功能:

    使用biopython或其他生物信息学软件包比python标准库的任何部分都更加灵活,因为有许多不同的评分方案和算法可用。此外,您还可以实际获取匹配序列,以可视化正在发生的事情:

    >>> alignment = next(aligner.align(a1, a2))
    >>> alignment.score
    200.0
    >>> print(alignment)
    Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-Apple-
    |||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-|||-|-
    App-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-elApp-el
    

    您可以在此链接下找到大多数文本相似性方法及其计算方法: 这里有一些例子

    • 归一化、度量、相似性和距离

    • (标准化)相似性和距离

    • 公制距离

    • 基于木瓦(n-gram)的相似性和距离
    • 列文施坦
    • 归一化列文施泰因
    • 加权Levenshtein
    • 达梅劳·列文施坦
    • 最佳字符串对齐
    • 雅罗·温克勒
    • 最长公共子序列
    • 度量最长公共子序列
    • N克
    • 基于木瓦(n-gram)的算法
    • Q-克
    • 余弦相似性
    • 雅卡索引
    • 索伦森骰子系数
    • 重叠系数(即Szymkiewicz-Simpson)

    如上所述,有许多度量标准可以定义字符串之间的相似性和距离。我将用
    Q-Grams
    edit distance
    展示一个
    Jaccard相似性的示例,并给出我的5美分

    图书馆

    from nltk.metrics.distance import jaccard_distance
    from nltk.util import ngrams
    from nltk.metrics.distance  import edit_distance
    
    Jaccard相似性

    1-jaccard_distance(set(ngrams('Apple', 2)), set(ngrams('Appel', 2)))
    
    我们得到:

    0.33333333333333337
    
    0.0
    
    2
    
    5
    
    0.5
    
    对于
    苹果
    芒果

    1-jaccard_distance(set(ngrams('Apple', 2)), set(ngrams('Mango', 2)))
    
    我们得到:

    0.33333333333333337
    
    0.0
    
    2
    
    5
    
    0.5
    
    编辑距离

    edit_distance('Apple', 'Appel')
    
    我们得到:

    0.33333333333333337
    
    0.0
    
    2
    
    5
    
    0.5
    
    最后

    edit_distance('Apple', 'Mango')
    
    我们得到:

    0.33333333333333337
    
    0.0
    
    2
    
    5
    
    0.5
    
    Q-Grams上的余弦相似性(Q=2)

    另一个解决方案是使用
    textdestance
    库。我将提供一个
    余弦相似性的示例

    import textdistance
    1-textdistance.Cosine(qval=2).distance('Apple', 'Appel')
    
    我们得到:

    0.33333333333333337
    
    0.0
    
    2
    
    5
    
    0.5
    

    text距离:

    import textdistance
    textdistance.hamming('test', 'text')
    
    import textdistance
    
    textdistance.hamming.normalized_similarity('test', 'text')
    
    import nltk
    from nltk.translate import bleu
    from nltk.translate.bleu_score import SmoothingFunction
    smoothie = SmoothingFunction().method4
    
    C1='Text'
    C2='Best'
    
    print('BLEUscore:',bleu([C1], C2, smoothing_function=smoothie))
    
    C1='It is tough.' C2='It is rough.'
    
    BLEUscore: 0.7348889200874658
    
    C1='It is tough.' C2='It is tough.'
    
    BLEUscore: 1.0
    
    TextDistance–python库,用于通过多种算法比较两个或多个序列之间的距离。它有

    • 30+算法
    • 纯python实现
    • 简单用法
    • 两个以上的序列比较
    • 有些算法在一个类中有多个实现
    • 可选numpy使用,以获得最高速度
    示例1:

    import textdistance
    textdistance.hamming('test', 'text')
    
    import textdistance
    
    textdistance.hamming.normalized_similarity('test', 'text')
    
    import nltk
    from nltk.translate import bleu
    from nltk.translate.bleu_score import SmoothingFunction
    smoothie = SmoothingFunction().method4
    
    C1='Text'
    C2='Best'
    
    print('BLEUscore:',bleu([C1], C2, smoothing_function=smoothie))
    
    C1='It is tough.' C2='It is rough.'
    
    BLEUscore: 0.7348889200874658
    
    C1='It is tough.' C2='It is tough.'
    
    BLEUscore: 1.0
    
    输出:

    import textdistance
    textdistance.hamming('test', 'text')
    
    import textdistance
    
    textdistance.hamming.normalized_similarity('test', 'text')
    
    import nltk
    from nltk.translate import bleu
    from nltk.translate.bleu_score import SmoothingFunction
    smoothie = SmoothingFunction().method4
    
    C1='Text'
    C2='Best'
    
    print('BLEUscore:',bleu([C1], C2, smoothing_function=smoothie))
    
    C1='It is tough.' C2='It is rough.'
    
    BLEUscore: 0.7348889200874658
    
    C1='It is tough.' C2='It is tough.'
    
    BLEUscore: 1.0
    
    一,

    示例2:

    import textdistance
    textdistance.hamming('test', 'text')
    
    import textdistance
    
    textdistance.hamming.normalized_similarity('test', 'text')
    
    import nltk
    from nltk.translate import bleu
    from nltk.translate.bleu_score import SmoothingFunction
    smoothie = SmoothingFunction().method4
    
    C1='Text'
    C2='Best'
    
    print('BLEUscore:',bleu([C1], C2, smoothing_function=smoothie))
    
    C1='It is tough.' C2='It is rough.'
    
    BLEUscore: 0.7348889200874658
    
    C1='It is tough.' C2='It is tough.'
    
    BLEUscore: 1.0
    
    输出:

    import textdistance
    textdistance.hamming('test', 'text')
    
    import textdistance
    
    textdistance.hamming.normalized_similarity('test', 'text')
    
    import nltk
    from nltk.translate import bleu
    from nltk.translate.bleu_score import SmoothingFunction
    smoothie = SmoothingFunction().method4
    
    C1='Text'
    C2='Best'
    
    print('BLEUscore:',bleu([C1], C2, smoothing_function=smoothie))
    
    C1='It is tough.' C2='It is rough.'
    
    BLEUscore: 0.7348889200874658
    
    C1='It is tough.' C2='It is tough.'
    
    BLEUscore: 1.0
    
    0.75


    谢谢,干杯

    以下是我的想法:

    导入字符串
    def匹配(a、b):
    a、 b=a.lower(),b.lower()
    错误=0
    对于string.ascii_小写形式的i:
    错误+=abs(a.计数(i)-b.计数(i))
    总计=长(a)+长(b)
    返回(总错误)/总错误
    如果名称=“\uuuuu main\uuuuuuuu”:
    打印(匹配(“苹果公司”、“苹果公司”)
    
    BLEUscore

    edit_distance('Apple', 'Appel')
    
    BLEU,或双语评估替补,是一个比较的分数 将文本翻译成一个或多个参考译文的候选译文

    完全匹配的结果为1.0分,而完全不匹配的结果为1.0分 结果得分为0.0

    虽然它是为翻译而开发的,但它可以用来评估文本 为一组自然语言处理任务生成

    代码:

    import textdistance
    textdistance.hamming('test', 'text')
    
    import textdistance
    
    textdistance.hamming.normalized_similarity('test', 'text')
    
    import nltk
    from nltk.translate import bleu
    from nltk.translate.bleu_score import SmoothingFunction
    smoothie = SmoothingFunction().method4
    
    C1='Text'
    C2='Best'
    
    print('BLEUscore:',bleu([C1], C2, smoothing_function=smoothie))
    
    C1='It is tough.' C2='It is rough.'
    
    BLEUscore: 0.7348889200874658
    
    C1='It is tough.' C2='It is tough.'
    
    BLEUscore: 1.0
    
    示例:通过更新C1和C2。

    C1='Test' C2='Test'
    
    BLEUscore: 1.0
    
    C1='Test' C2='Best'
    
    BLEUscore: 0.2326589746035907
    
    C1='Test' C2='Text'
    
    BLEUscore: 0.2866227639866161
    
    您还可以比较句子相似性:

    import textdistance
    textdistance.hamming('test', 'text')
    
    import textdistance
    
    textdistance.hamming.normalized_similarity('test', 'text')
    
    import nltk
    from nltk.translate import bleu
    from nltk.translate.bleu_score import SmoothingFunction
    smoothie = SmoothingFunction().method4
    
    C1='Text'
    C2='Best'
    
    print('BLEUscore:',bleu([C1], C2, smoothing_function=smoothie))
    
    C1='It is tough.' C2='It is rough.'
    
    BLEUscore: 0.7348889200874658
    
    C1='It is tough.' C2='It is tough.'
    
    BLEUscore: 1.0
    

    我认为“概率”在这里不太合适。在任何情况下,你要找的词都是比率,而不是概率。看看。短语是“相似性度量”,但有多个相似性度量(Jaccard、Cosine、Hamming、Levenshein等),所以你需要指定哪个。具体来说,您需要字符串之间的相似性度量@hbprotoss列出了几个。@NPE链接已断开,但相似('appel','apple')高于相似('appel','ape')您的函数将比较给定字符串与其他Sting。我想要一种返回具有最高相似性的字符串的方法ratio@SaulloCastro,
    如果self.similor(搜索字符串,item.text())>0.80:
    现在可以使用。谢谢,再见