Module: ODDB::Util::NGramSimilarity

Defined in:
src/util/ngram_similarity.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) compare(str1, str2, n = 5)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'src/util/ngram_similarity.rb', line 7

def self.compare(str1, str2, n=5)
  str1 = u(str1).downcase.gsub(/[\s,.\-\/]+/, '')
  str2 = u(str2).downcase.gsub(/[\s,.\-\/]+/, '')
  if(str1.length < str2.length)
    str1, str2 = str2, str1
  end
  parts = [ str1.length - n, 0 ].max + 1
  count = 0
  parts.times { |idx|
    if(str2.include? str1[idx, n])
      count += 1
    end
  }
  count.to_f / parts
end