Gestalt pattern matching

From testwiki
Jump to navigation Jump to search

Template:Short description 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]

Algorithm

The similarity of two strings S1 and S2 is determined by this formula: twice the number of matching characters Km 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]

Dro=2Km|S1|+|S2|

where the similarity metric can take a value between zero and one:

0Dro1

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

S1 W I K I M E D I A
S2 W I K I M A N I A

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:

2Km|S1|+|S2|=2(|“WIKIM”|+|“IA”|)|S1|+|S2|=2(5+2)9+9=1418=0.7

Properties

The Ratcliff/Obershelp matching characters can be substantially different from each longest common subsequence of the given strings. For example S1=qcccccrdddsbbbbteeeu and S2=vdddwbbbbxeeeycccccz have ccccc as their only longest common substring, and no common characters right of its occurrence, and likewise left, leading to Km=5. However, the longest common subsequence of S1 and S2 is (ddd)(bbbb)(eee), with a total length of 10.

Complexity

The execution time of the algorithm is O(n3) in a worst case and O(n2) in an average case. By changing the computing method, the execution time can be improved significantly.[1]

Commutative property

The Python library implementation of the gestalt pattern matching algorithm is not commutative:[5]

Dro(S1,S2)Dro(S2,S1).
Sample

For the two strings

S1=GESTALT PATTERN MATCHING

and

S2=GESTALT PRACTICE

the metric result for

Dro(S1,S2) is 2440 with the substrings GESTALT P, A, T, E and for
Dro(S2,S1) the metric is 2640 with the substrings GESTALT P, R, A, C, I.Template:Why

Applications

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]

Drqr=2min(|S1|,|S2|)|S1|+|S2|,

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

Dqr=2|{|S1|}{|S2|}||S1|+|S2|Template:Clarify
# 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:

0DroDqrDrqr1 and
0Km|{|S1|}{|S2|}|min(|S1|,|S2|)|S1|+|S2|2.

References

  1. 1.0 1.1 1.2 1.3 difflib — Helpers for computing deltas inside the Python documentation
  2. 2.0 2.1 2.2 National Institute of Standards and Technology Ratcliff/Obershelp pattern recognition
  3. While two strings may have several longest common substrings, Ratcliff (1988) apparently assumes that there is only one.
  4. Ilya Ilyankou: Comparison of Jaro-Winkler and Ratcliff/Obershelp algorithms in spell check, May 2014 (PDF)
  5. How does Pythons SequenceMatcher work? at stackoverflow.com
  6. Borrowed from Python 3.7.0, difflib.py Lines 38–41 and 676–686

Further reading

See also

Template:Strings