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



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

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



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

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



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

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