Sha256: d59900e7c4dd05e2d130dcb873fda170f6a94b2a800f143d8717b95bd1ef0bf1

Contents?: true

Size: 513 Bytes

Versions: 8

Compression:

Stored size: 513 Bytes

Contents

require 'did_you_mean/levenshtein'

module DidYouMean
  class WordCollection
    include Enumerable
    attr_reader :words

    def initialize(words)
      @words = words
    end

    def each(&block) words.each(&block); end

    def similar_to(target_word)
      target_word = target_word.to_s
      threshold   = threshold(target_word)

      select {|word| Levenshtein.distance(word.to_s, target_word) <= threshold }
    end

    private

    def threshold(word)
      (word.size * 0.3).ceil
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
did_you_mean-0.9.8 lib/did_you_mean/word_collection.rb
did_you_mean-0.9.7 lib/did_you_mean/word_collection.rb
did_you_mean-0.9.6-java lib/did_you_mean/word_collection.rb
did_you_mean-0.9.6 lib/did_you_mean/word_collection.rb
did_you_mean-0.9.5 lib/did_you_mean/word_collection.rb
did_you_mean-0.9.4 lib/did_you_mean/word_collection.rb
did_you_mean-0.9.2 lib/did_you_mean/word_collection.rb
did_you_mean-0.9.0 lib/did_you_mean/word_collection.rb