Method 2
import Levenshtein
def get_equal_rate(str1, str2):
return Levenshtein.ratio(str1, str2)
Neither of the following two methods works well :
difflib.SequenceMatcher(None, str1, str2).quick_ratio() Low precision
difflib.SequenceMatcher(None, str1, str2).ratio() Precision high point
Strictly compare whether the corresponding position characters are equal , If there's a dislocation , You can't use
def compare_str(aaa,bbb):
if len(bbb)>len(aaa):
aaa=aaa.ljust(len(bbb)," ")
elif len(bbb) < len(aaa):
bbb=bbb.ljust(len(aaa), " ")
error_count=0
for index, data in enumerate(aaa):
if index>= len(bbb):
error_count+=1
else:
if data!=bbb[index]:
error_count+=1
return error_count
import difflib
url1="http://service.library.mtime.com/Movie.api?Ajax_CallBack=true&Ajax_CallBackType=Mtime.Library.Services&Ajax_CallBackMethod=GetMovieOverviewRating&Ajax_CrossDomain=1&Ajax_RequestUrl=http%3A%2F%2Fmovie.mtime.com%2F232316%2F&t=20185259461767462&Ajax_CallBackArgument0=232316"
url2="http://service.library.mtime.com/Movie.api?Ajax_CallBack=true&Ajax_CallBackType=Mtime.Library.Services&Ajax_CallBackMethod=GetMovieOverViewRating&Ajax_CrossDomain=1&Ajax_RequestUrl=http%3A%2F%2Fmovie.mtime.com%2F233465%2F&t=2018525956832821&Ajax_CallBackArgument0=233465"
d=difflib.Differ()
diff=d.compare(url1.splitlines(),url2.splitlines())
print '\n'.join(list(diff))
Compare the similarity of two strings :
import difflib
# The method of judging similarity , Yes difflib library
def get_equal_rate_1(str1, str2):
return difflib.SequenceMatcher(None, str1, str2).quick_ratio()