|
| 1 | +require "./string-metrics/*" |
| 2 | + |
| 3 | +# TODO: Write documentation for `String::Metrics` |
| 4 | +module StringMetrics |
| 5 | + |
| 6 | + # Gets the min edit distance between two strings. |
| 7 | + # See https://en.wikipedia.org/wiki/Levenshtein_distance |
| 8 | + # Ported from https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python |
| 9 | + def self.levenshtein(s1 : String, s2 : String) : Int |
| 10 | + return levenshtein(s2, s1) if s1.size < s2.size |
| 11 | + return s1.size if s2.size == 0 |
| 12 | + |
| 13 | + previous_row = (0..s2.size).to_a |
| 14 | + s1_chars = s1.chars |
| 15 | + s2_chars = s2.chars |
| 16 | + # it's expensive to continually recreate an array |
| 17 | + cache = [[0] * (s2.size + 1), [0] * (s2.size + 1)] |
| 18 | + s1_chars.each_with_index do |c1, i| |
| 19 | + current_row = cache[i % 2] |
| 20 | + current_row[0] = i + 1 |
| 21 | + s2_chars.each_with_index do |c2, j| |
| 22 | + insertions = previous_row.unsafe_at(j + 1) + 1 |
| 23 | + deletions = current_row.unsafe_at(j) + 1 |
| 24 | + substitutions = previous_row.unsafe_at(j) |
| 25 | + substitutions += 1 if c1 != c2 |
| 26 | + # could have minned a tuple, but this is a bit faster |
| 27 | + min = insertions |
| 28 | + min = deletions if deletions < min |
| 29 | + min = substitutions if substitutions < min |
| 30 | + # main bottleneck is here, memory writes to an already allocated address :/ |
| 31 | + current_row[j + 1] = min |
| 32 | + end |
| 33 | + previous_row = current_row |
| 34 | + end |
| 35 | + previous_row.last |
| 36 | + end |
| 37 | + |
| 38 | + def self.hamming(s1 : String, s2 : String) : Int |
| 39 | + raise ArgumentError.new("input lengths are not equal") if s1.size != s2.size |
| 40 | + (0...s2.size).sum { |i| (s1[i] != s2[i])? 1 : 0 } |
| 41 | + end |
| 42 | + |
| 43 | + # A variation of the Levenshtein distance, this counts transpositions as a single edit. |
| 44 | + # ```damerau_levenshtein("char", "hcar") == 1``` as opposed to a distance of 2 from levenshtein |
| 45 | + # on it's own |
| 46 | + # Ported from https://github.com/jamesturk/jellyfish/blob/master/jellyfish/_jellyfish.py |
| 47 | + def self.damerau_levenshtein(s1 : String, s2 : String) : Int |
| 48 | + infinite = s1.size + s2.size |
| 49 | + |
| 50 | + da = Hash(Char, Int32).new(default_value=0) |
| 51 | + |
| 52 | + # distance matrix |
| 53 | + score = (0...s1.size + 2).to_a.map {|i| [0]*(s2.size + 2)} |
| 54 | + score[0][0] = infinite |
| 55 | + |
| 56 | + (0...s1.size + 1).each do |i| |
| 57 | + score[i + 1][0] = infinite |
| 58 | + score[i + 1][1] = i |
| 59 | + end |
| 60 | + (0...s2.size + 1).each do |i| |
| 61 | + score[0][i + 1] = infinite |
| 62 | + score[1][i + 1] = i |
| 63 | + end |
| 64 | + |
| 65 | + s1_chars = s1.chars |
| 66 | + s2_chars = s2.chars |
| 67 | + |
| 68 | + (1..s1.size).each do |i| |
| 69 | + db = 0 |
| 70 | + (1..s2.size).each do |j| |
| 71 | + i1 = da[s2_chars[j-1]] |
| 72 | + j1 = db |
| 73 | + cost = 1 |
| 74 | + if s1_chars[i - 1] == s2_chars[j - 1] |
| 75 | + cost = 0 |
| 76 | + db = j |
| 77 | + end |
| 78 | + |
| 79 | + score[i + 1][j + 1] = { |
| 80 | + score[i][j] + cost, |
| 81 | + score[i+1][j] + 1, |
| 82 | + score[i][j+1] + 1, |
| 83 | + score[i1][j1] + (i - i1 - 1) + 1 + (j - j1 - 1) |
| 84 | + }.min |
| 85 | + end |
| 86 | + da[s1_chars[i - 1]] = i |
| 87 | + end |
| 88 | + score[s1.size + 1][s2.size + 1] |
| 89 | + end |
| 90 | + |
| 91 | +end |
0 commit comments