Gestalt Pattern Matching


Gestalt Pattern Matching, also Ratcliff/Obershelp Pattern Recognition, 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.

Algorithm

The similarity of two string and is determined by the formula, calculating twice the number of matching characters divided by the total number of characters of both strings. The matching characters are defined as the longest common substring plus recursively the number of matching characters in the non-matching regions on both sides of the LCS:
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.

Sample

The longest common substring is WIKIM with 5 characters. There is no further substring on left. The non-matching substrings on right side are EDIA and ANIA. They again have a longest common substring IA with length 2.
The similarity metric is determined by:

Properties

Complexity

The execution time of the algorithm is O in worst case and in average case. By changing the computing method the execution time can be improved significantly.

Commutative property

It can be shown, that Gestalt Pattern Matching Algorithm is not commutative:
;Sample
For the two strings
and
the metric result for

Applications

The algorithm became a basis of the Python difflib library, which was introduced in version 2.1. Due to the unfavourable runtime behaviour of this similarity metric, three methods have been implemented. Two of them returns an upper bound in a faster execution time. The fastest variant only compares the length of the two substrings:

  1. Drqr Implementation in Python
def real_quick_ratio -> float:
"""Return an upper bound on ratio very quickly."""
l1, l2 = len, len
length = l1 + l2
if not length:
return 1.0
return 2.0 * min / length

The second upper bound calculates twice the sum of all used characters which occur in divided by the length of both strings but the sequence is ignored.

  1. Dqr Implementation in Python
def quick_ratio -> float:
"""Return an upper bound on ratio relatively quickly."""
length = len + len
if not length:
return 1.0
intersect = collections.Counter & collections.Counter
matches = sum)
return 2.0 * matches / length

Trivially the following applies:

Complexity

The execution time of this particular Python implementation was stated as in worst case and in best case.