Sha256: 500f56443dc9cd09bb3d1d76678578cb1c627b3a87ce85a5e9e9f1739ce796bd

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

class WordsMatrix::Service
  attr_reader :matrix, :dictionary, :words

  def initialize(config={})
    config = WordsMatrix::Config::DEFAULTS.merge(config) { |key, default, local|
      local || default
    }

    validate_options!(config)
    @matrix = WordsMatrix::Matrix.new(config[:n].to_i, config[:min_length].to_i)
    @dictionary = WordsMatrix::Dictionary.new(config[:min_length].to_i, config[:n].to_i, config[:dict_path])

    @words = []
  end

  def find_words
    @matrix.tokens.each_pair do |letter, tokens|
      next unless @dictionary.words.has_key?(letter)
      words = @dictionary.words[letter].keys

      tokens.each do |token|
        words_found = words.select { |word| token =~ /^#{word}.*/ }

        @words += words_found.map { |word| @dictionary.words[letter][word] }
      end
    end
    @words.uniq!
  end

  def to_s
    words_found = @words.any? ? @words.join("\n") : "none :("
    ["Matrix:",
      matrix.to_s,
      "",
      "Words found:",
      words_found].join("\n")
  end

  private
  def validate_options!(config)
    raise ArgumentError, "matrix size should be a positive integer" if config[:n].to_i < 1
    raise ArgumentError, "min word length should be a positive integer and less than matrix size" unless (0..config[:n].to_i).include?(config[:min_length].to_i)
    raise ArgumentError, "dictionary path is not a valid file path" unless File.readable?(config[:dict_path])
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
words_matrix-0.0.1 lib/words_matrix/service.rb