python相似性檢測的安裝包__python

來源:互聯網
上載者:User
安裝python-Levenshtein模組

pip install python-Levenshtein

使用python-Levenshtein模組

import Levenshtein

演算法說明

1). Levenshtein.hamming(str1, str2)
計算漢明距離。要求str1和str2必須長度一致。是描述兩個等長字串之間對應 位置上不同字元的個數。

2). Levenshtein.distance(str1, str2)
計算編輯距離(也稱為 Levenshtein距離)。是描述由一個字串轉化成另一個字串最少的操作次數,在其中的操作包括插入、刪除、替換。
演算法實現參考動態規劃整理。

3). Levenshtein.ratio(str1, str2)
計算萊文斯坦比。計算公式r = (sum - ldist) / sum, 其中sum是指str1 和 str2 字串的長度總和,ldist是 類編輯距離
注意 :這裡的類編輯距離不是2中所說的編輯距離,2中三種操作中每個操作+1,而在此處,刪除、插入依然+1,但是替換+2
這樣設計的目的:ratio('a', 'c'),sum=2, 按2中計算為(2-1)/2 = 0.5,’a','c'沒有重合,顯然不合算,但是替換操作+2,就可以解決這個問題。

4). Levenshtein.jaro(s1 , s2 )
計算jaro距離,

其中的 m 為s1 , s2的匹配長度,當某位置的認為匹配當該位置字元相同,或者在不超過

t是調換次數的一半

5.) Levenshtein.jaro_winkler(s 1 , s 2 )
計算 Jaro–Winkler距離:
 

import Levenshtein 報錯:ImportError: No module named Levenshtein

於是去: python-Levenshtein 下載源碼進行安裝(在 http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-levenshtein其實也有編譯好的exe),第一次安裝的時候報錯:error: Unable to find vcvarsall.bat ,但其實我是裝了VS2010的,所以執行如下步驟正常安裝:

1.設定環境變數,執行:

SET VS90COMNTOOLS=%VS100COMNTOOLS%

2.再去安裝:

setup.py install

就可以正常,編譯,安裝了。

$ python>>> import Levenshtein>>> help(Levenshtein.ratio)ratio(...)    Compute similarity of two strings.    ratio(string1, string2)    The similarity is a number between 0 and 1, it's usually equal or    somewhat higher than difflib.SequenceMatcher.ratio(), becuase it's    based on real minimal edit distance.    Examples:    >>> ratio('Hello world!', 'Holly grail!')    0.58333333333333337    >>> ratio('Brian', 'Jesus')    0.0>>> help(Levenshtein.distance)distance(...)    Compute absolute Levenshtein distance of two strings.    distance(string1, string2)    Examples (it's hard to spell Levenshtein correctly):    >>> distance('Levenshtein', 'Lenvinsten')    4    >>> distance('Levenshtein', 'Levensthein')    2    >>> distance('Levenshtein', 'Levenshten')    1    >>> distance('Levenshtein', 'Levenshtein')    0

 
difflib 庫 

>>> import difflib>>> difflib.SequenceMatcher(None, 'abcde', 'abcde').ratio()1.0>>> difflib.SequenceMatcher(None, 'abcde', 'zbcde').ratio()0.80000000000000004>>> difflib.SequenceMatcher(None, 'abcde', 'zyzzy').ratio()0.0

 

FuzzyWuzzy

git clone git://github.com/seatgeek/fuzzywuzzy.git fuzzywuzzycd fuzzywuzzypython setup.py install>>> from fuzzywuzzy import fuzz>>> from fuzzywuzzy import processSimple Ratio>>> fuzz.ratio("this is a test", "this is a test!")    96Partial Ratio>>> fuzz.partial_ratio("this is a test", "this is a test!")    100Token Sort Ratio>>> fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")    90>>> fuzz.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")    100Token Set Ratio>>> fuzz.token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")    84>>> fuzz.token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")    100
gitclone git://github.com/seatgeek/fuzzywuzzy.git fuzzywuzzycdfuzzywuzzypythonsetup.pyinstall >>> fromfuzzywuzzyimportfuzz>>> fromfuzzywuzzyimportprocess SimpleRatio>>> fuzz.ratio("this is a test", "this is a test!")    96 PartialRatio>>> fuzz.partial_ratio("this is a test", "this is a test!")    100 TokenSortRatio>>> fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")    90>>> fuzz.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")    100 TokenSetRatio>>> fuzz.token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")    84>>> fuzz.token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")    100

google-diff-match-patch

import diff match patch textA = "the cat in the red hat" textB = "the feline in the blue hat"

dmp = diff match patch.diff match patch()  #create a diff match patch object diffs = dmp.diff main(textA, textB)   # All 'diff' jobs start with invoking diff main()

d value = dmp.diff levenshtein(diffs) print d_value

maxLenth = max(len(textA), len(textB)) print float(d_value)/float(maxLenth)

similarity = (1 - float(d_value)/float(maxLenth)) * 100 print similarity

importdiff_match_patchtextA = "the cat in the red hat"textB = "the feline in the blue hat" dmp = diff_match_patch.diff_match_patch()  #create a diff_match_patch objectdiffs = dmp.diff_main(textA, textB)   # All 'diff' jobs start with invoking diff_main() d_value = dmp.diff_levenshtein(diffs)printd_value maxLenth = max(len(textA), len(textB))printfloat(d_value)/float(maxLenth) similarity = (1 - float(d_value)/float(maxLenth)) * 100printsimilarity
title2
第二種方法安裝 參考部落格:http://blog.csdn.net/TH_NUM/article/details/77095177 安裝pip install python-Levenshtein,出現錯誤:Microsoft Visual C++ 14.0 is required 出現錯誤主要是因為直接使用 pip install 【第三方庫名】 安裝自己需要的第三方庫。 解決辦法: 一定要安裝和自己windows版本和python版本對應的第三方庫
在這裡下載需要的第三方庫:http://www.lfd.uci.edu/~gohlke/pythonlibs
安裝步驟: 首先:在網站上下載對於版本的 python_Levenshtein-0.12.0-cp36-cp36m-win_amd64.whl 然後:然後在控制台上切換到置放位置後輸入pip install python_Levenshtein-0.12.0-cp36-cp36m-win_amd64.whl 就完成了。。。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.