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



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

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”)



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

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



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

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