一、thefuzz
thefuzz包以前叫fuzzywuzzy,0.19版本開始改名為thefuzz,github地址:
GitHub - seatgeek/thefuzz: Fuzzy String Matching in Python
可以通過命令pip install thefuzz安裝此包。用法還是比較簡單的:
from thefuzz import fuzz
fuzz.ratio("test", "test!")
>>89
上面兩個(gè)字符串的相似度為89%。
二、相似度ratio的計(jì)算
我們先看看這個(gè)包下面的源碼,來查看thefuzz是怎么實(shí)現(xiàn)模糊匹配的。thefuzz源碼包的結(jié)構(gòu)如下:
先看看ratio方法源碼:
def ratio(s1, s2):
s1, s2 = utils.make_type_consistent(s1, s2)
m = SequenceMatcher(None, s1, s2)
return utils.intr(100 * m.ratio())
?可以看到,ratio方法用到了一個(gè)比較關(guān)鍵的類SequenceMatcher,但是這個(gè)類卻有可能來自兩個(gè)不同的地方。
2.1?SequenceMatcher的來源
看看fuzzy.py的頭部代碼:
import platform
import warnings
try:
#從當(dāng)前文件夾的StringMatcher中導(dǎo)入StringMatcher
from .StringMatcher import StringMatcher as SequenceMatcher
except ImportError:
if platform.python_implementation() != "PyPy":
warnings.warn('Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning')
from difflib import SequenceMatcher
#導(dǎo)入當(dāng)前文件夾的utils包,.代表當(dāng)前目錄
from . import utils
上面代碼涉及了一個(gè)導(dǎo)入問題,即先從當(dāng)前文件StringMatcher中導(dǎo)入StringMatcher,如果導(dǎo)入出現(xiàn)異常,就去difflib中導(dǎo)入SequenceMatcher。
正如上面第一張圖中看到的,當(dāng)然文件夾下面確實(shí)有一個(gè)叫StringMatcher.py的文件,也看看它前面的代碼:
from Levenshtein import *
from warnings import warn
class StringMatcher:
.............
.............
可以看出,這個(gè)StringMatcher類引用了Levenshtein包,這個(gè)包也是用來計(jì)算字符串模糊匹配的,效率上來說,有可能比difflib中的SequenceMatcher快4-10倍。
Levenshtein包是用C語言寫的,比較復(fù)雜,最初的項(xiàng)目地址:
GitHub - miohtama/python-Levenshtein: The Levenshtein Python C extension module contains functions for fast computation of Levenshtein distance and string similarity
后來這個(gè)作者沒有維護(hù)了,然后由另一個(gè)在維護(hù),項(xiàng)目的地址在這里:
GitHub - ztane/python-Levenshtein: The Levenshtein Python C extension module contains functions for fast computation of Levenshtein distance and string similarity
但是,這個(gè)頁面上,作者也說了,他也7年不維護(hù)了,現(xiàn)在沒找到新的維護(hù)者。
不管怎么說,如果要使用Levenshtein,還是可以安裝的:
pip install python-Levenshtein
總結(jié)就是,thefuzz有兩種實(shí)現(xiàn)方式,一種是依賴difflib,另一種依靠?python-Levenshtein。先看簡單的difflib。
2.2 difflib包中的SequenceMatcher
首先導(dǎo)入:
from difflib import SequenceMatcher
這個(gè)類的主要作用是計(jì)算兩個(gè)匹配字符串的相似度,如下:
s = SequenceMatcher(None, "abcde", "bcde")
s.ratio()
輸出值為0.888888。這個(gè)是怎么計(jì)算的呢?可以查看difflib.py的源代碼(我的電腦在D:\ProgramData\Miniconda3\Lib目錄下),如下:
def ratio(self):
matches = sum(triple[-1] for triple in self.get_matching_blocks())
return _calculate_ratio(matches, len(self.a) + len(self.b))
這個(gè)方法涉及到兩個(gè)比較重要的方法,一個(gè)是get_matching_blocks(),這個(gè)方法用于獲取匹配的字符塊。另一個(gè)方法_calculate_ratio,用于計(jì)算相似度,先看_calculate_ratio,代碼如下:
def _calculate_ratio(matches, length):
if length:
return 2.0 * matches / length
return 1.0
上面代碼的第三行是關(guān)鍵,matches表示的字符個(gè)數(shù),length是兩個(gè)字符串加起來的總長度。如上面的"abcde"和 "bcde",ratio的計(jì)算方法就是2*4/9,即8/9=0.888888。
再看看get_matching_blocks方法。這個(gè)方法比較復(fù)雜,我們先來看下,這個(gè)方法的用法:
s = SequenceMatcher(None, "abchde", "bcde")
print(s.get_matching_blocks())
輸出如下:
[Match(a=1, b=0, size=2), Match(a=4, b=2, size=2), Match(a=6, b=4, size=0)]?
什么意思?從方法的名字大概就能看出來,就是獲得匹配的所有字符塊。上面的代碼輸出了3個(gè)Match對象,Match(a=1, b=0, size=2)的意思是"abchde"從索引1(a=1)開始,"bcde"從索引0(b=0)開始,匹配到2(size=2)個(gè)相等字符,即“bc”。
最后一個(gè)Match(a=6, b=4, size=0)是固定的,a、b代表兩個(gè)字符串的長度,size=0固定不變。用代碼描述如下:
(len(a), len(b), 0)
但是如果前面字串符已經(jīng)匹配過,就不會(huì)再進(jìn)行匹配了,如下:
s = SequenceMatcher(None, "bc", "abchdebc")
print(s.get_matching_blocks())?
輸出:
?[Match(a=0, b=1, size=2), Match(a=2, b=8, size=0)]
即“bc”只匹配了第一次的位置,后面就算出現(xiàn)和它一樣的字符串,也不再進(jìn)行匹配。?
三、process模塊
從第一張圖中可以看到,除了fuzz.py這個(gè)文件,還有一個(gè)叫process.py的文件,process模塊常用的是從候選列表中,返回與目標(biāo)字符串最相似的一個(gè)結(jié)果。來看一個(gè)簡單的例子:
from thefuzz import fuzz,process
choices = ["hello world", "hello china", "hello beijing"]
print(process.extractOne("china",choices))
#輸出內(nèi)容
>>('hello china', 90)
正如上面代碼所示,process最常用的用法是從眾多字符串中,找到最佳匹配的字符串。
process.extractOne的格式如下:
extractOne(query, choices, processor=default_processor, scorer=default_scorer, score_cutoff=0):
"""
Args:
query: A string to match against
choices: A list or dictionary of choices, suitable for use with extract().
processor: Optional function for transforming choices before matching.
scorer: Scoring function for extract().
score_cutoff: Optional argument for score threshold. If the best
match is found, but it is not greater than this number, then
return None anyway ("not a good enough match"). Defaults to 0.
Returns:
A tuple containing a single match and its score, if a match
was found that was above score_cutoff. Otherwise, returns None.
"""
query:查詢的字符串;
choices:?待匹配的字符串列表或者字典;
processor:可選參數(shù),轉(zhuǎn)換器,在匹配前先對choices進(jìn)行轉(zhuǎn)換處理;
scorer:可選參數(shù),分?jǐn)?shù)器,用于計(jì)算分?jǐn)?shù);
score_cutoff:可選參數(shù),這個(gè)參數(shù)的作用是設(shè)置一個(gè)分?jǐn)?shù)門檻(默認(rèn)為0),如果小于這個(gè)分?jǐn)?shù),就不返回匹配的字符串,而是返回一個(gè)None。
extractOne返回的結(jié)果是一個(gè)tuple元組(最佳匹配結(jié)果,分?jǐn)?shù))。
我們比較關(guān)心的一個(gè)問題是,這個(gè)分?jǐn)?shù)是怎么計(jì)算的?看看下面例子:
from thefuzz import fuzz,process
print(fuzz.ratio("china","hello china"))
choices = ["hello world", "hello china", "hello beijing"]
print(process.extractOne("china",choices))
#輸出內(nèi)容
>>62
>>('hello china', 90)
可以看出,fuzz.ratio與process.extractOne分?jǐn)?shù)的計(jì)算方式不一樣(一個(gè)是62分,一個(gè)90分)。fuzz.ratio的計(jì)分方式,上面已經(jīng)講了,下面來看看extractOne的計(jì)分方式。
extractOne的源碼如下:
def extractOne(query, choices, processor=default_processor, scorer=default_scorer, score_cutoff=0):
best_list = extractWithoutOrder(query, choices, processor, scorer, score_cutoff)
try:
return max(best_list, key=lambda i: i[1])
except ValueError:
return None
我們剛才說了,第三個(gè)參數(shù)scorer是用于計(jì)分的,它的默認(rèn)值為default_scorer,那我們先找到這個(gè)default_scorer的值:
default_scorer = fuzz.WRatio
即默認(rèn)的計(jì)分方式為fuzz.WRatio,那么我們回到fuzz.py中,看看WRatio是做什么的?
from thefuzz import fuzz
default_scorer = fuzz.WRatio
default_scorer("china", "hello china")
#輸出
>> 90
?可以看出,WRatio的計(jì)分方式確實(shí)和上面的extractOne相同,都是90分。WRatio的計(jì)分方式比較復(fù)雜,涉及到一個(gè)權(quán)重(weight)的概念,它是基于fuzz.ratio()的基礎(chǔ)上,做了進(jìn)一步的校正。
如果我們不想采用WRatio的計(jì)分方式,或者想采用fuzz.ratio()的計(jì)分方式來提取最佳匹配結(jié)果,可以這樣:
from thefuzz import fuzz,process
print(fuzz.ratio("china","hello china"))
choices = ["hello world", "hello china", "hello beijing"]
print(process.extractOne("china",choices,scorer=fuzz.QRatio))
#輸出
>>62
>>('hello china', 62)
上面代碼的計(jì)分結(jié)果都是62分,因?yàn)閒uzz.QRatio的內(nèi)部,除了對參數(shù)進(jìn)行了一些簡單的處理以外,直接調(diào)用fuzz.ratio()方法返回了結(jié)果。所以fuzz.QRatio和fuzz.ratio()的計(jì)分方式完全相同。
fuzz.QRatio源代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-422692.html
# q is for quick
def QRatio(s1, s2, force_ascii=True, full_process=True):
if full_process:
p1 = utils.full_process(s1, force_ascii=force_ascii)
p2 = utils.full_process(s2, force_ascii=force_ascii)
else:
p1 = s1
p2 = s2
if not utils.validate_string(p1):
return 0
if not utils.validate_string(p2):
return 0
return ratio(p1, p2)
通過上面的例子可以看出,如果我們對QRatio、WRatio這些計(jì)分方式不滿意的話,完全可以自己實(shí)現(xiàn)了一個(gè)Ratio,將它做為extractOne的參數(shù),實(shí)現(xiàn)定制的返回結(jié)果。文章來源地址http://www.zghlxwxcb.cn/news/detail-422692.html
到了這里,關(guān)于python字符串模糊匹配,并計(jì)算匹配分?jǐn)?shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!