### Install strsimpy Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Install the strsimpy library using pip. This is the recommended package for string similarity algorithms. ```bash pip install -U strsimpy ``` -------------------------------- ### Damerau-Levenshtein Distance Examples Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Calculate Damerau-Levenshtein distance, which includes transpositions as a valid operation. Useful for measuring string similarity where adjacent character swaps are common. ```python from strsimpy.damerau import Damerau damerau = Damerau() print(damerau.distance('ABCDEF', 'ABDCEF')) print(damerau.distance('ABCDEF', 'BACDFE')) print(damerau.distance('ABCDEF', 'ABCDE')) print(damerau.distance('ABCDEF', 'BCDEF')) print(damerau.distance('ABCDEF', 'ABCGDEF')) print(damerau.distance('ABCDEF', 'POIU')) ``` -------------------------------- ### Calculate LCS Distance and Length Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Demonstrates how to calculate the distance and length of the longest common subsequence between two strings using the LongestCommonSubsequence class. ```python from strsimpy.longest_common_subsequence import LongestCommonSubsequence lcs = LongestCommonSubsequence() print(lcs.distance('AGCAT', 'GAC')) print(lcs.length('AGCAT', 'GAC')) print(lcs.distance('AGCAT', 'AGCT')) print(lcs.length('AGCAT', 'AGCT')) ``` -------------------------------- ### Calculate Metric LCS Distance Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Shows how to compute the metric LCS distance between two strings. This metric is based on the ratio of the LCS length to the length of the longer string. ```python from strsimpy.metric_lcs import MetricLCS metric_lcs = MetricLCS() s1 = 'ABCDEFG' s2 = 'ABCDEFHJKL' # LCS: ABCDEF => length = 6 # longest = s2 => length = 10 # => 1 - 6/10 = 0.4 print(metric_lcs.distance(s1, s2)) # LCS: ABDF => length = 4 # longest = ABDEF => length = 5 # => 1 - 4 / 5 = 0.2 print(metric_lcs.distance('ABDEF', 'ABDIF')) ``` -------------------------------- ### Compute Cosine Similarity from Profiles Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Calculates the cosine similarity between two pre-computed string profiles. This is efficient for large datasets where profiles are generated once. ```python from strsimpy.cosine import Cosine cosine = Cosine(2) s0 = 'My first string' s1 = 'My other string...' p0 = cosine.get_profile(s0) p1 = cosine.get_profile(s1) print(cosine.similarity_profiles(p0, p1)) ``` -------------------------------- ### Calculate Q-Gram Distance Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Computes the Q-Gram distance between two strings, defined as the L1 norm of the difference of their profiles. This is a lower bound on Levenshtein distance and can be computed efficiently. ```python from strsimpy.qgram import QGram qgram = QGram(2) print(qgram.distance('ABCD', 'ABCE')) ``` -------------------------------- ### Calculate SIFT4 String Distance Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Use the SIFT4 class to compute the distance between two strings. The `maxoffset` parameter can be adjusted for performance tuning. ```python from strsimpy import SIFT4 s = SIFT4() # result: 11.0 s.distance('This is the first string', 'And this is another string') # 11.0 # result: 12.0 s.distance('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'Amet Lorm ispum dolor sit amet, consetetur adixxxpiscing elit.', maxoffset=10) ``` -------------------------------- ### Calculate Normalized Levenshtein Distance and Similarity Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Calculates the normalized Levenshtein distance by dividing the Levenshtein distance by the length of the longest string, resulting in a value between 0.0 and 1.0. The similarity is then computed as 1 minus the normalized distance. ```python from strsimpy.normalized_levenshtein import NormalizedLevenshtein normalized_levenshtein = NormalizedLevenshtein() print(normalized_levenshtein.distance('My string', 'My $string')) print(normalized_levenshtein.distance('My string', 'My $string')) print(normalized_levenshtein.distance('My string', 'My $string')) print(normalized_levenshtein.similarity('My string', 'My $string')) print(normalized_levenshtein.similarity('My string', 'My $string')) print(normalized_levenshtein.similarity('My string', 'My $string')) ``` -------------------------------- ### Calculate N-Gram Distance Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Computes the normalized N-Gram distance between two strings using a specified n-gram size. Useful for measuring string dissimilarity. ```python from strsimpy.ngram import NGram twogram = NGram(2) print(twogram.distance('ABCD', 'ABTUIO')) s1 = 'Adobe CreativeSuite 5 Master Collection from cheap 4zp' s2 = 'Adobe CreativeSuite 5 Master Collection from cheap d1x' fourgram = NGram(4) print(fourgram.distance(s1, s2)) ``` -------------------------------- ### Calculate Jaro-Winkler Similarity Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Computes the Jaro-Winkler similarity between two strings, suitable for short strings like names and detecting typos. The returned value is between 0.0 and 1.0. ```python from strsimpy.jaro_winkler import JaroWinkler jarowinkler = JaroWinkler() print(jarowinkler.similarity('My string', 'My tsring')) print(jarowinkler.similarity('My string', 'My ntrisg')) ``` -------------------------------- ### Weighted Levenshtein Distance Calculation Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Implement Weighted Levenshtein distance with custom costs for insertions, deletions, and substitutions. Useful for OCR or keyboard auto-correction where character similarity varies. ```python from strsimpy.weighted_levenshtein import WeightedLevenshtein def insertion_cost(char): return 1.0 def deletion_cost(char): return 1.0 def substitution_cost(char_a, char_b): if char_a == 't' and char_b == 'r': return 0.5 return 1.0 weighted_levenshtein = WeightedLevenshtein( substitution_cost_fn=substitution_cost, insertion_cost_fn=insertion_cost, deletion_cost_fn=deletion_cost) print(weighted_levenshtein.distance('String1', 'String2')) ``` -------------------------------- ### Calculate Levenshtein Distance Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Computes the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another using dynamic programming. This implementation has O(m) space and O(m.n) time complexity. ```python from strsimpy.levenshtein import Levenshtein levenshtein = Levenshtein() print(levenshtein.distance('My string', 'My $string')) print(levenshtein.distance('My string', 'My $string')) print(levenshtein.distance('My string', 'My $string')) ``` -------------------------------- ### Calculate Optimal String Alignment Distance Source: https://github.com/luozhouyang/python-string-similarity/blob/master/README.md Computes the Optimal String Alignment distance between two strings. This variant of Damerau-Levenshtein restricts edits to no more than one per substring. ```python from strsimpy.optimal_string_alignment import OptimalStringAlignment optimal_string_alignment = OptimalStringAlignment() print(optimal_string_alignment.distance('CA', 'ABC')) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.