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 = ''|
unless File.exists? options['dictionary']
fatal "Could not load the dictionary"
return
end
dictionary = DictionaryNode.new
File.open(options['dictionary'], 'r') do |file|
info "Reading the dictionary..."
file.each_line do |line|
next if line =~ /[A-Z]/
line.strip!
next if line.length <= 1
line.downcase!
dictionary.add_word line
end
info "Read #{dictionary.count} words."
end
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
|