Class: Gemfilelint::SimilarityDetector

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

Overview

Note that this used to be a part of bundler, but got removed in 4.0.0, so we have copied the implementation in here. The LICENSE is below for reference.

The MIT License

Portions copyright (c) 2010-2019 André Arko Portions copyright (c) 2009 Engine Yard

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: SimilarityScore

Instance Method Summary collapse

Constructor Details

#initialize(corpus) ⇒ SimilarityDetector

initialize with an array of words to be matched against



35
36
37
# File 'lib/gemfilelint/similarity_detector.rb', line 35

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



53
54
55
56
57
58
59
60
# File 'lib/gemfilelint/similarity_detector.rb', line 53

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



40
41
42
43
44
45
46
47
48
49
# File 'lib/gemfilelint/similarity_detector.rb', line 40

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