Class: Bundler::SimilarityDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/similarity_detector.rb

Defined Under Namespace

Classes: SimilarityScore

Instance Method Summary collapse

Constructor Details

#initialize(corpus) ⇒ SimilarityDetector

initialize with an array of words to be matched against



6
7
8
# File 'lib/bundler/similarity_detector.rb', line 6

def initialize(corpus)
  @corpus = corpus
end

Instance Method Details

#similar_word_list(word, limit = 3) ⇒ Object

return the result of 'similar_words', concatenated into a list (eg “a, b, or c”)



18
19
20
21
22
23
24
25
# File 'lib/bundler/similarity_detector.rb', line 18

def similar_word_list(word, limit = 3)
  words = similar_words(word, limit)
  if words.length == 1
    words[0]
  elsif words.length > 1
    [words[0..-2].join(", "), words[-1]].join(" or ")
  end
end

#similar_words(word, limit = 3) ⇒ Object

return an array of words similar to 'word' from the corpus



11
12
13
14
# File 'lib/bundler/similarity_detector.rb', line 11

def similar_words(word, limit = 3)
  words_by_similarity = @corpus.map {|w| SimilarityScore.new(w, levenshtein_distance(word, w)) }
  words_by_similarity.select {|s| s.distance <= limit }.sort_by(&:distance).map(&:string)
end