manywords 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0facce806f1a0848860e6a4979e2aaf3490670ee
4
+ data.tar.gz: 668abe4571f4572595a3c1d9eba01a945fe3a9a1
5
+ SHA512:
6
+ metadata.gz: a04a8cdf330f52f5c765610ab43debc09b320a3090297ee19f14e4552a990dc1e0071269098e3d64622c8140867640e28b809e0018af9f07799a15a3c9a70a6b
7
+ data.tar.gz: 375101bbb42c48cad6df8ca65603e86881bc35a3eec60e52f9d3cdc379870606177809daac764f835c7f969880fbe6c6d6b9825fc0f8ada4c7ae6e58cc5b712c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in manywords.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stephen H. Gerstacker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Manywords
2
+
3
+ You may be playing a word game and be stuck. Use this gem to find all of the possible words that exist with a given word combination. You can also provide a regular expression to filter you search.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'manywords'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install manywords
18
+
19
+ ## Usage
20
+
21
+ To just find all possible words, run the following:
22
+
23
+ $ manywords abcde
24
+
25
+ To filter your search, run the following:
26
+
27
+ $ manywords ^...$
28
+
29
+ By default, manywords uses the UNIX standard dictionary file, `/usr/share/dict/words`. If your platform doesn't have this file, or you would like to provide your own, use the `--dictionary` flag.
30
+
31
+ $ manywords --dictionary /path/to/dict abcde
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'lib/manywords'
7
+ t.test_files = FileList['test/lib/manywords/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
12
+
data/bin/manywords ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'manywords/command'
4
+
5
+ Manywords::Command.run
6
+
@@ -0,0 +1,74 @@
1
+ require 'manywords'
2
+ require 'methadone'
3
+
4
+ module Manywords
5
+
6
+ class Command
7
+ include Methadone::Main
8
+ include Methadone::CLILogging
9
+
10
+ def self.run
11
+
12
+ main do |letters, pattern = ''|
13
+ # Make sure the given dictionary exists
14
+ unless File.exists? options['dictionary']
15
+ $stderr.puts "Could not load the dictionary"
16
+ return
17
+ end
18
+
19
+ # Load the dictionary
20
+ dictionary = DictionaryNode.new
21
+ File.open(options['dictionary'], 'r') do |file|
22
+ puts "Reading the dictionary..."
23
+
24
+ file.each_line do |line|
25
+ # Skip proper nouns
26
+ next if line =~ /[A-Z]/
27
+
28
+ # Skip 1 letter words
29
+ line.strip!
30
+ next if line.length <= 1
31
+
32
+ # Clean up the line
33
+ line.downcase!
34
+
35
+ # Add it!
36
+ dictionary.add_word line
37
+ end
38
+
39
+ puts "Read #{dictionary.count} words."
40
+ end
41
+
42
+ # Construct the pattern if given
43
+ regex = Regexp.new(pattern)
44
+
45
+ walker = Walker.new
46
+ walker.walk letters do |word|
47
+ result = dictionary.find_word word
48
+
49
+ if result == :yes
50
+ test_word = word.is_a?(Array) ? word.join('') : word.to_s
51
+
52
+ puts test_word if regex =~ test_word
53
+ end
54
+
55
+ result
56
+ end
57
+ end
58
+
59
+ version Manywords::VERSION
60
+ description 'Determine word combinations'
61
+ arg :letters, :required
62
+ arg :pattern, :optional
63
+
64
+ options['dictionary'] = '/usr/share/dict/words'
65
+ on('-d PATH', '--dictionary', 'Dictionary')
66
+
67
+ go!
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+
@@ -0,0 +1,53 @@
1
+ module Manywords
2
+
3
+ class DictionaryNode
4
+
5
+ attr_reader :count
6
+
7
+ def initialize
8
+ @count = 0;
9
+ @letters = {}
10
+ @word = false
11
+ end
12
+
13
+ def add_word(word, idx = 0)
14
+ # If we've reached the end of the word, mark it as so
15
+ if idx == word.length
16
+ @word = true
17
+ return
18
+ end
19
+
20
+ # A word will be added, so increment
21
+ @count += 1
22
+
23
+ # Get the letter and add it to the tree if we need to
24
+ letter = word[idx]
25
+ @letters[letter] = DictionaryNode.new unless @letters.key?(letter)
26
+
27
+ # Go to the next letter
28
+ @letters[letter].add_word word, idx + 1
29
+ end
30
+
31
+ def find_word(word, idx = 0)
32
+ if !word.is_a?(String) && !word.is_a?(Array)
33
+ raise "find_word requires an Array or a String"
34
+ end
35
+
36
+ # We've reached the end, so this is either a word or a partial word
37
+ if idx == word.length
38
+ return @word ? :yes : :maybe
39
+ end
40
+
41
+ # Check if this letter is valid
42
+ letter = word[idx]
43
+ if @letters.key? letter
44
+ # Valid, so go to the next letter
45
+ @letters[letter].find_word word, idx + 1
46
+ else
47
+ :no
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,3 @@
1
+ module Manywords
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ module Manywords
2
+
3
+ class Walker
4
+
5
+ def walk(letters, &block)
6
+ list = letters.split ''
7
+ list.sort!
8
+ permutations [], list, &block
9
+ end
10
+
11
+ private
12
+
13
+ def permutations(prefix, available, &block)
14
+ # Visit the prefix
15
+ result = block.call prefix
16
+
17
+ # Do we continue from here?
18
+ if (result != :no)
19
+ available.each_with_index do |letter, idx|
20
+ next_available = available.dup
21
+ next_available.delete_at(idx)
22
+
23
+ permutations prefix + [ letter ], next_available, &block
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
data/lib/manywords.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "manywords/dictionary_node"
2
+ require "manywords/walker"
3
+ require "manywords/version"
4
+
5
+ module Manywords
6
+ # Your code goes here...
7
+ end
data/manywords.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'manywords/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "manywords"
8
+ spec.version = Manywords::VERSION
9
+ spec.authors = ["Stephen H. Gerstacker"]
10
+ spec.email = ["stephen@shortround.net"]
11
+ spec.description = %q{Discover words in random letters}
12
+ spec.summary = %q{Given a set of random letters, find all of the words in it}
13
+ spec.homepage = "http://shortround.net"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "methadone", "~> 1.2.6"
25
+
26
+ spec.required_ruby_version = '>= 2.0.0'
27
+ end
@@ -0,0 +1,70 @@
1
+ require_relative '../../test_helper'
2
+
3
+ module Manywords
4
+
5
+ describe DictionaryNode do
6
+
7
+ it "must be initially empty" do
8
+ node = DictionaryNode.new
9
+ node.count.must_equal 0
10
+ end
11
+
12
+ it "must add a word" do
13
+ node = DictionaryNode.new
14
+
15
+ node.add_word 'hello'
16
+ node.count.must_equal 1
17
+
18
+ node.add_word 'world'
19
+ node.count.must_equal 2
20
+ end
21
+
22
+ it "must return maybe if empty" do
23
+ node = DictionaryNode.new
24
+
25
+ node.find_word('').must_equal :maybe
26
+ node.find_word([]).must_equal :maybe
27
+ end
28
+
29
+ it "must verify a word is valid" do
30
+ node = DictionaryNode.new
31
+ node.add_word 'hello'
32
+
33
+ node.find_word('hello').must_equal :yes
34
+ node.find_word(%w{h e l l o}).must_equal :yes
35
+ end
36
+
37
+ it "must verify an invalid word is invalid" do
38
+ node = DictionaryNode.new
39
+ node.add_word 'hello'
40
+
41
+ node.find_word('world').must_equal :no
42
+ node.find_word(%w{w o r l d}).must_equal :no
43
+ end
44
+
45
+ it "must verify a series of letters could potentially be a word" do
46
+ node = DictionaryNode.new
47
+ node.add_word 'post'
48
+ node.add_word 'postfix'
49
+
50
+ node.find_word('p').must_equal :maybe
51
+ node.find_word('po').must_equal :maybe
52
+ node.find_word('pos').must_equal :maybe
53
+ node.find_word('post').must_equal :yes
54
+ node.find_word('postf').must_equal :maybe
55
+ node.find_word('postfi').must_equal :maybe
56
+ node.find_word('postfix').must_equal :yes
57
+
58
+ node.find_word(%w{p}).must_equal :maybe
59
+ node.find_word(%w{p o}).must_equal :maybe
60
+ node.find_word(%w{p o s}).must_equal :maybe
61
+ node.find_word(%w{p o s t}).must_equal :yes
62
+ node.find_word(%w{p o s t f}).must_equal :maybe
63
+ node.find_word(%w{p o s t f i}).must_equal :maybe
64
+ node.find_word(%w{p o s t f i x}).must_equal :yes
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
@@ -0,0 +1,10 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe Manywords do
4
+
5
+ it "must be versioned" do
6
+ Manywords::VERSION.wont_be_nil
7
+ end
8
+
9
+ end
10
+
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/manywords.rb', __FILE__)
4
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: manywords
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen H. Gerstacker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: methadone
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.6
55
+ description: Discover words in random letters
56
+ email:
57
+ - stephen@shortround.net
58
+ executables:
59
+ - manywords
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/manywords
69
+ - lib/manywords.rb
70
+ - lib/manywords/command.rb
71
+ - lib/manywords/dictionary_node.rb
72
+ - lib/manywords/version.rb
73
+ - lib/manywords/walker.rb
74
+ - manywords.gemspec
75
+ - test/lib/manywords/dictionary_node_test.rb
76
+ - test/lib/manywords/version_test.rb
77
+ - test/test_helper.rb
78
+ homepage: http://shortround.net
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: 2.0.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Given a set of random letters, find all of the words in it
102
+ test_files:
103
+ - test/lib/manywords/dictionary_node_test.rb
104
+ - test/lib/manywords/version_test.rb
105
+ - test/test_helper.rb