Sha256: f5441ae185ef8d45e54ec6a821b70d8225f76eb988e7c98752d7c051b4c014db

Contents?: true

Size: 595 Bytes

Versions: 4

Compression:

Stored size: 595 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)

      map {|word| [Levenshtein.distance(word.to_s, target_word), word] }
        .select {|distance, _| distance <= threshold }
        .sort
        .map(&:last)
    end

    private

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
did_you_mean-0.9.10-java lib/did_you_mean/word_collection.rb
did_you_mean-0.9.10 lib/did_you_mean/word_collection.rb
did_you_mean-0.9.9-java lib/did_you_mean/word_collection.rb
did_you_mean-0.9.9 lib/did_you_mean/word_collection.rb