Module: Crawler::Utils

Defined in:
lib/crawler/utils.rb

Class Method Summary collapse

Class Method Details

.levenshtein_score(string_1, string_2) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/crawler/utils.rb', line 10

def self.levenshtein_score(string_1, string_2)
  string_1_transliterated = transliterate(string_1)
  string_2_transliterated = transliterate(string_2)
  levenshtein_distance = Levenshtein.distance(string_1_transliterated, string_2_transliterated)
  max_size = [string_1_transliterated.size, string_2_transliterated.size].max.to_f

  (max_size - levenshtein_distance) / max_size
end

.transliterate(string) ⇒ Object



6
7
8
# File 'lib/crawler/utils.rb', line 6

def self.transliterate(string)
  ActiveSupport::Inflector.transliterate(string.gsub(/[:\-.,!?]/, ' ').strip.gsub(/\s+/, ' '), nil).downcase
end