DEV Community

Teddy Zugana
Teddy Zugana

Posted on • Edited on

2 2

PHP DamerauLevenshtein ALGORITHM

/*

  • Naïve implementation of Damerau-Levenshtein distance
  • (Does not work when there are neighbouring transpositions)! */
  function DamerauLevenshtein($S1, $S2)
  {
    $L1 = strlen($S1);
    $L2 = strlen($S2);
    if ($L1==0 || $L2==0) {
        // Trivial case: one string is 0-length
        return max($L1, $L2);
    }
    else {
        // The cost of substituting the last character
        $substitutionCost = ($S1[$L1-1] != $S2[$L2-1])? 1 : 0;
        // {H1,H2} are {L1,L2} with the last character chopped off
        $H1 = substr($S1, 0, $L1-1);
        $H2 = substr($S2, 0, $L2-1);
        if ($L1>1 && $L2>1 && $S1[$L1-1]==$S2[$L2-2] && $S1[$L1-2]==$S2[$L2-1]) {
            return min (
                DamerauLevenshtein($H1, $S2) + 1,
                DamerauLevenshtein($S1, $H2) + 1,
                DamerauLevenshtein($H1, $H2) + $substitutionCost,
                DamerauLevenshtein(substr($S1, 0, $L1-2), substr($S2, 0, $L2-2)) + 1
            );
        }
        return min (
            DamerauLevenshtein($H1, $S2) + 1,
            DamerauLevenshtein($S1, $H2) + 1,
            DamerauLevenshtein($H1, $H2) + $substitutionCost
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs