Class: Manywords::Command

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Methadone::Main
Defined in:
lib/manywords/command.rb

Class Method Summary collapse

Class Method Details

.runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/manywords/command.rb', line 10

def self.run

  main do |letters, pattern = ''|
    # Make sure the given dictionary exists
    unless File.exists? options['dictionary']
      fatal "Could not load the dictionary"
      return
    end

    # Load the dictionary
    dictionary = DictionaryNode.new
    File.open(options['dictionary'], 'r') do |file|
      info "Reading the dictionary..."

      file.each_line do |line|
        # Skip proper nouns
        next if line =~ /[A-Z]/

        # Skip 1 letter words
        line.strip!
        next if line.length <= 1

        # Clean up the line
        line.downcase!

        # Add it!
        dictionary.add_word line
      end

      info "Read #{dictionary.count} words."
    end

    # Construct the pattern if given
    regex = Regexp.new(pattern)

    walker = Walker.new
    walker.walk letters do |word|
      result = dictionary.find_word word

      if result == :yes
        test_word = word.is_a?(Array) ? word.join('') : word.to_s

        puts test_word if regex =~ test_word
      end

      result
    end
  end

  version     Manywords::VERSION
  description 'Determine word combinations given a series of letters and an optional regular expression to filter.'

  arg         :letters, :required
  arg         :pattern, :optional

  options['dictionary'] = '/usr/share/dict/words'
  on('-d PATH', '--dictionary', 'Dictionary')

  go!
end