簡體   English   中英

如何比較 Python 中兩個字符串(英語除外)之間的相似度

[英]How to compare similarity between two strings (other than English language) in Python

我想找到兩個字符串 Example 之間的相似性

string1 = "One"
string2 = "one"

我希望答案在 0 和 1 之間。對於上面的兩個字符串,我們得到 1。現在我正在使用“Jellyfish”,這是 python 中的一個模塊,它具有 jaro_distance() function。但缺點是我' m 只能比較兩個只包含英文單詞和其他特殊字符的字符串。 但是我想比較其他語言的兩個字符串,比如旁遮普語

string1 = "ਬੁੱਧਵਾਰ"
string2 = "ਬੁੱਧਵਾ"

我嘗試了相同的 jaro_distance() function,但我得到了

>>score = jellyfish.jaro_distance(unicode(string1), unicode(string2))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)

在將它們提供給 function 之前,我嘗試對它們進行編碼和解碼。有沒有辦法將 jaro_distance() 用於其他語言,或者是否有任何其他模塊/函數可用於此? 你們能幫我嗎?

您可以使用內置模塊difflib中的SequenceMatcher

代碼示例:

import difflib

print(difflib.SequenceMatcher(None, "ਬੁੱਧਵਾਰ", "ਬੁੱਧਵਾ").ratio())

Output:

0.9230769230769231

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM