From 675b0854c8c13c437a692be5b1e6f8df363389a5 Mon Sep 17 00:00:00 2001 From: Allen Goodman Date: Mon, 20 Jun 2011 21:24:08 -0400 Subject: [PATCH 1/2] Included recipe for matching two or more strings (i.e. calculating an edit distance). --- chapters/strings/matching-strings.md | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 chapters/strings/matching-strings.md diff --git a/chapters/strings/matching-strings.md b/chapters/strings/matching-strings.md new file mode 100644 index 0000000..7a65af3 --- /dev/null +++ b/chapters/strings/matching-strings.md @@ -0,0 +1,42 @@ +--- +layout: recipe +title: Matching Strings +chapter: Strings +--- +## Problem + +You want to match two or more strings. + +## Solution + +Calculate the edit distance, or number of operations required to transform one string into the other. + +{% highlight coffeescript %} +Levenshtein = + (str1, str2) -> + + l1 = str1.length + l2 = str2.length + + Math.max l1, l2 if Math.min l1, l2 == 0 + + i = 0; j = 0; distance = [] + + for i in [0...l1 + 1] + distance[i] = [] + distance[i][0] = i + + distance[0][j] = j for j in [0...l2 + 1] + + for i in [1...l1 + 1] + for j in [1...l2 + 1] + distance[i][j] = Math.min distance[i - 1][j] + 1, + distance[i][j - 1] + 1, + distance[i - 1][j - 1] + if str1.charAt(i - 1) == str2.charAt(j - 1) then 0 else 1 + + distance[l1][l2] +{% endhighlight %} + +## Discussion + +You can use either Hirschberg or Wagner–Fischer's algorithm to calculate a Levenshtein distance. This example uses Wagner–Fischer's algorithm. \ No newline at end of file From d1824197c303e3239d68ae28938735f65d3129d5 Mon Sep 17 00:00:00 2001 From: Allen Goodman Date: Tue, 21 Jun 2011 03:56:46 -0400 Subject: [PATCH 2/2] added newline; repositioned parentheses to make algorithm slightly more idiomatic --- chapters/strings/matching-strings.md | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/chapters/strings/matching-strings.md b/chapters/strings/matching-strings.md index 7a65af3..100aefa 100644 --- a/chapters/strings/matching-strings.md +++ b/chapters/strings/matching-strings.md @@ -12,31 +12,34 @@ You want to match two or more strings. Calculate the edit distance, or number of operations required to transform one string into the other. {% highlight coffeescript %} + Levenshtein = (str1, str2) -> - + l1 = str1.length l2 = str2.length - + Math.max l1, l2 if Math.min l1, l2 == 0 - + i = 0; j = 0; distance = [] for i in [0...l1 + 1] - distance[i] = [] - distance[i][0] = i + distance[i] = [] + distance[i][0] = i distance[0][j] = j for j in [0...l2 + 1] - + for i in [1...l1 + 1] - for j in [1...l2 + 1] - distance[i][j] = Math.min distance[i - 1][j] + 1, - distance[i][j - 1] + 1, - distance[i - 1][j - 1] + if str1.charAt(i - 1) == str2.charAt(j - 1) then 0 else 1 + for j in [1...l2 + 1] + distance[i][j] = Math.min distance[i - 1][j] + 1, + distance[i][j - 1] + 1, + distance[i - 1][j - 1] + + if (str1.charAt i - 1) == (str2.charAt j - 1) then 0 else 1 distance[l1][l2] + {% endhighlight %} ## Discussion -You can use either Hirschberg or Wagner–Fischer's algorithm to calculate a Levenshtein distance. This example uses Wagner–Fischer's algorithm. \ No newline at end of file +You can use either Hirschberg or Wagner–Fischer's algorithm to calculate a Levenshtein distance. This example uses Wagner–Fischer's algorithm.