近期因爲數據庫中保存的一些相似小區名稱,街道名稱存在簡寫,錯別字等不規範的現象,須要將不規範的書寫進行糾錯改正。在進行糾錯的過程當中用到了【編輯距離】的計算方式來與對照表進行精確匹配。python
1.Levenshtein距離是一種計算兩個字符串間的差別程度的字符串度量(string metric)。咱們能夠認爲Levenshtein距離就是從一個字符串修改到另外一個字符串時,其中編輯單個字符(好比修改、插入、刪除)所須要的最少次數。git
2.jaro距離數據庫
3.jaro-winkler距離app
注:其中的類似度 = 1 - 距離spa
因爲jaro的distance中存在局部可視窗口的概念,即便有相同的子串出現,可是超過可視窗口的長度依舊不會計算,可是業務的數據大多數帶有寫比較長的前綴,就會影響最終匹配的準確度,因此將可視窗口的長度放大至比較字符串的最長串的長度,因此將包中的部分源碼修改,python代碼以下:code
def count_matches(s1, s2, len1, len2): assert len1 and len1 <= len2 # search_range = max(len2//2-1, 0) # print ("search_range",search_range) search_range = len2 num_matches = 0 flags1 = [0] * len1 flags2 = [0] * len2 for i, char in enumerate(s1): lolim = max(i - search_range, 0) hilim = min(i + search_range, len2 - 1) for j in range(lolim, hilim + 1): if not flags2[j] and char == s2[j]: flags1[i] = flags2[j] = 1 # where_matched[i] = j num_matches += 1 break return num_matches, flags1, flags2 # , where_matched def count_half_transpositions(s1, s2, flags1, flags2): half_transposes = 0 k = 0 for i, flag in enumerate(flags1): if not flag: continue while not flags2[k]: k += 1 if s1[i] != s2[k]: half_transposes += 1 k += 1 return half_transposes def count_typos(s1, s2, flags1, flags2, typo_table): assert 0 in flags1 typo_score = 0 for i, flag1 in enumerate(flags1): if flag1: continue # Iterate through unmatched chars row = s1[i] if row not in typo_table: # If we don't have a similarity mapping for the char, continue continue typo_row = typo_table[row] for j, flag2 in enumerate(flags2): if flag2: continue col = s2[j] if col not in typo_row: continue # print 'Similarity!', row, col typo_score += typo_row[col] flags2[j] = 2 break return typo_score, flags2 def fn_jaro(len1, len2, num_matches, half_transposes, typo_score, typo_scale): if not len1: if not len2: return 1.0 return 0.0 if not num_matches: return 0.0 similar = (typo_score / typo_scale) + num_matches weight = (similar / len1 + similar / len2 + (num_matches - half_transposes // 2) / num_matches) return weight / 3 def string_metrics(s1, s2, typo_table=None, typo_scale=1, boost_threshold=None, pre_len=0, pre_scale=0, longer_prob=False): len1 = len(s1) len2 = len(s2) if len2 < len1: s1, s2 = s2, s1 len1, len2 = len2, len1 assert len1 <= len2 if not (len1 and len2): return len1, len2, 0, 0, 0, 0, False num_matches, flags1, flags2 = count_matches(s1, s2, len1, len2) # If no characters in common - return if not num_matches: return len1, len2, 0, 0, 0, 0, False half_transposes = count_half_transpositions(s1, s2, flags1, flags2) # adjust for similarities in non-matched characters typo_score = 0 if typo_table and len1 > num_matches: typo_score, flags2 = count_typos(s1, s2, flags1, flags2, typo_table) if not boost_threshold: return len1, len2, num_matches, half_transposes, typo_score, 0, 0 pre_matches = 0 adjust_long = False weight_typo = fn_jaro(len1, len2, num_matches, half_transposes, typo_score, typo_scale) # Continue to boost the weight if the strings are similar if weight_typo > boost_threshold: # Adjust for having up to first 'pre_len' chars (not digits) in common limit = min(len1, pre_len) while pre_matches < limit: char1 = s1[pre_matches] if not (char1.isalpha() and char1 == s2[pre_matches]): break pre_matches += 1 if longer_prob: cond = len1 > pre_len cond = cond and num_matches > pre_matches + 1 cond = cond and 2 * num_matches >= len1 + pre_matches cond = cond and s1[0].isalpha() if cond: adjust_long = True return (len1, len2, num_matches, half_transposes, typo_score, pre_matches, adjust_long) def metric_jaro(string1, string2): "The standard, basic Jaro string metric." ans = string_metrics(string1, string2) len1, len2, num_matches, half_transposes = ans[:4] assert ans[4:] == (0, 0, False) return fn_jaro(len1, len2, num_matches, half_transposes, 0, 1) def metric_jaro_score(s1,s2): return metric_jaro(s1,s2) print (metric_jaro_score("賽鼎線世紀明珠45號","世紀明珠45號"))