Gestalt pattern matching,[1] also Ratcliff/Obershelp pattern recognition,[2] is a string-matching algorithm for determining the similarity of two strings. It was developed in 1983 by John W. Ratcliff and John A. Obershelp and published in the Dr. Dobb's Journal in July 1988.[2]
The similarity of two strings S 1 {\displaystyle S_{1}} and S 2 {\displaystyle S_{2}} is determined by this formula: twice the number of matching characters K m {\displaystyle K_{m}} divided by the total number of characters of both strings. The matching characters are defined as some longest common substring[3] plus recursively the number of matching characters in the non-matching regions on both sides of the longest common substring:[2][4]
where the similarity metric can take a value between zero and one:
The value of 1 stands for the complete match of the two strings, whereas the value of 0 means there is no match and not even one common letter.
The longest common substring is WIKIM (light grey) with 5 characters. There is no further substring on the left. The non-matching substrings on the right side are EDIA and ANIA. They again have a longest common substring IA (dark gray) with length 2. The similarity metric is determined by:
WIKIM
EDIA
ANIA
IA
The Ratcliff/Obershelp matching characters can be substantially different from each longest common subsequence of the given strings. For example S 1 = q c c c c c r d d d s b b b b t e e e u {\displaystyle S_{1}=q\;ccccc\;r\;ddd\;s\;bbbb\;t\;eee\;u} and S 2 = v d d d w b b b b x e e e y c c c c c z {\displaystyle S_{2}=v\;ddd\;w\;bbbb\;x\;eee\;y\;ccccc\;z} have c c c c c {\displaystyle ccccc} as their only longest common substring, and no common characters right of its occurrence, and likewise left, leading to K m = 5 {\displaystyle K_{m}=5} . However, the longest common subsequence of S 1 {\displaystyle S_{1}} and S 2 {\displaystyle S_{2}} is ( d d d ) ( b b b b ) ( e e e ) {\displaystyle (ddd)\;(bbbb)\;(eee)} , with a total length of 10 {\displaystyle 10} .
The execution time of the algorithm is O ( n 3 ) {\displaystyle O(n^{3})} in a worst case and O ( n 2 ) {\displaystyle O(n^{2})} in an average case. By changing the computing method, the execution time can be improved significantly.[1]
The Python library implementation of the gestalt pattern matching algorithm is not commutative:[5]
For the two strings
and
the metric result for
GESTALT P
A
T
E
R
C
I
The Python difflib library, which was introduced in version 2.1,[1] implements a similar algorithm that predates the Ratcliff-Obershelp algorithm. Due to the unfavourable runtime behaviour of this similarity metric, three methods have been implemented. Two of them return an upper bound in a faster execution time.[1] The fastest variant only compares the length of the two substrings:[6]
difflib
The second upper bound calculates twice the sum of all used characters S 1 {\displaystyle S_{1}} which occur in S 2 {\displaystyle S_{2}} divided by the length of both strings but the sequence is ignored.
# Dqr Implementation in Python import collections def quick_ratio(s1: str, s2: str) -> float: """Return an upper bound on ratio() relatively quickly.""" length = len(s1) + len(s2) if not length: return 1.0 intersect = (collections.Counter(s1) & collections.Counter(s2)) matches = sum(intersect.values()) return 2.0 * matches / length
Trivially the following applies: