word-guesser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module WordGuesser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,42 @@
1
+ module WordGuesser
2
+ class WordCollection
3
+ include Enumerable
4
+
5
+ def initialize base_word, words
6
+ @base_word = base_word
7
+ @words = words
8
+ end
9
+
10
+ def guessed_letter_frequency
11
+ frequencies = {}
12
+ original_letters = @base_word.tr('_', '')
13
+ @words.each do |word|
14
+ word.tr(original_letters, '').each_char do |char|
15
+ if frequencies.has_key? char
16
+ frequencies[char] += 1
17
+ else
18
+ frequencies[char] = 1
19
+ end
20
+ end
21
+ end
22
+
23
+ frequencies
24
+ end
25
+
26
+ def in_dictionary
27
+ WordCollection.new(@base_word, @words & Dictionary.words)
28
+ end
29
+
30
+ def each
31
+ @words.each {|word| yield word }
32
+ end
33
+
34
+ def size
35
+ @words.size
36
+ end
37
+
38
+ def to_s
39
+ @words.to_a.join ', '
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,48 @@
1
+ require 'set'
2
+
3
+ module WordGuesser
4
+ class WordGuesser
5
+ def initialize base_word, letters_left
6
+ @base_word = base_word.downcase
7
+ original_letters = @base_word.tr('_', '').split('')
8
+ letters_left = letters_left.uniq if letters_left.respond_to? :uniq
9
+ @letters_left = letters_left.map {|l| l.downcase } - original_letters
10
+ end
11
+
12
+ def real_combinations
13
+ words = []
14
+ letters_group = "[#{Regexp.quote(@letters_left.join)}]"
15
+ Dictionary.words.grep(/^#{@base_word.gsub(/_/, letters_group)}$/) do |word|
16
+ words << word
17
+ end
18
+
19
+ WordCollection.new @base_word, words
20
+ end
21
+
22
+ def all_combinations
23
+ words = []
24
+ if @base_word.include? '_'
25
+ ->(memo, groups, letters, this, &block) do
26
+ groups = groups.clone
27
+ current_group = groups.delete_at 0
28
+ memo = memo + current_group
29
+ if groups.any?
30
+ letters.each do |letter|
31
+ this.call(memo + letter, groups, letters, this, &block)
32
+ end
33
+ else
34
+ block.call memo
35
+ end
36
+ end.tap do |inject_letters|
37
+ inject_letters.call "", @base_word.split('_', -1), @letters_left, inject_letters do |word|
38
+ words << word
39
+ end
40
+ end
41
+ else
42
+ words << @base_word
43
+ end
44
+
45
+ WordCollection.new @base_word, words
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ require 'word_guesser'
2
+
3
+ RSpec.configure do |config|
4
+ config.color_enabled = true
5
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe WordGuesser::WordCollection do
4
+ let(:word_collection) { described_class.new base_word, words }
5
+ let(:base_word) { stub }
6
+ let(:words) { stub Set }
7
+
8
+ describe "#in_dictionary" do
9
+ subject { word_collection.in_dictionary }
10
+ let(:words) { %w[foo bar baz goodbye whirl] }
11
+ let(:dictionary) { %w[foo bar hello world] }
12
+
13
+ before { WordGuesser::Dictionary.stub(:words).and_return dictionary }
14
+
15
+ its(:class) { should == WordGuesser::WordCollection }
16
+ it { subject.instance_variable_get(:@base_word).should == base_word }
17
+ it do
18
+ collection_words = subject.instance_variable_get(:@words).to_set
19
+ collection_words.subset?(words.to_set).should be_true
20
+ collection_words.subset?(dictionary.to_set).should be_true
21
+ (dictionary.to_set + words.to_set).superset?(collection_words).should be_true
22
+ end
23
+ end
24
+
25
+ describe "#guessed_letter_frequency" do
26
+ subject { word_collection.guessed_letter_frequency }
27
+ let(:base_word) { "h___o" }
28
+ let(:words) { %w[hallo helio hello hillo hippo hullo hydro] }
29
+
30
+ it do
31
+ should == { 'l' => 9, 'i' => 3, 'e' => 2, 'p' => 2, 'r' => 1,
32
+ 'u' => 1, 'y' => 1, 'd' => 1, 'a' => 1 }
33
+ end
34
+ end
35
+
36
+ describe "#size" do
37
+ subject { word_collection.size }
38
+ let(:word_collection) { described_class.new(stub, words) }
39
+ let(:size) { double }
40
+
41
+ it do
42
+ words.should_receive(:size).and_return size
43
+ should == size
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe WordGuesser::WordGuesser do
4
+ let(:word_guesser) { described_class.new base_word, letters }
5
+
6
+ describe "#all_combinations" do
7
+ subject { word_guesser.all_combinations }
8
+ let(:letters) { ['a', 'r'] }
9
+
10
+ context "when there is a _ at the start" do
11
+ let(:base_word) { "_oo" }
12
+ its(:to_set) { should == ["aoo", "roo"].to_set }
13
+ end
14
+
15
+ context "when there is a _ in the middle" do
16
+ let(:base_word) { "f_o" }
17
+ its(:to_set) { should == ["fao", "fro"].to_set }
18
+ end
19
+
20
+ context "when there is a _ at the end" do
21
+ let(:base_word) { "fo_" }
22
+ its(:to_set) { should == ["foa", "for"].to_set }
23
+ end
24
+
25
+ context "when there is no _" do
26
+ let(:base_word) { "foo" }
27
+ its(:to_a) { should == [base_word] }
28
+ end
29
+ end
30
+
31
+ describe "#real_combinations" do
32
+ subject { word_guesser.real_combinations }
33
+ let(:base_word) { "b__"}
34
+ let(:letters) { [*('a'..'z')] }
35
+ let(:dictionary) { %w[foo bar baz biz hello world] }
36
+
37
+ before { WordGuesser::Dictionary.stub(:words).and_return dictionary }
38
+
39
+ its(:class) { should == WordGuesser::WordCollection }
40
+ it { subject.instance_variable_get(:@base_word).should == base_word }
41
+ it do
42
+ subject.instance_variable_get(:@words).to_set.should == Set.new(%w[bar baz biz])
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "word_guesser/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "word-guesser"
7
+ s.version = WordGuesser::VERSION
8
+ s.authors = ["Andrew Marshall"]
9
+ s.email = ["andrew@johnandrewmarshall.com"]
10
+ s.homepage = "http://johnandrewmarshall.com/projects/word-guesser"
11
+ s.summary = %q{A word guesser}
12
+ s.description = %q{A simple app to guess possible words given known letters}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "rspec"
20
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: word-guesser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Marshall
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70255280143020 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70255280143020
25
+ description: A simple app to guess possible words given known letters
26
+ email:
27
+ - andrew@johnandrewmarshall.com
28
+ executables:
29
+ - word-guesser
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .travis.yml
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.markdown
38
+ - Rakefile
39
+ - bin/word-guesser
40
+ - lib/word_guesser.rb
41
+ - lib/word_guesser/dictionary.rb
42
+ - lib/word_guesser/enable1.txt
43
+ - lib/word_guesser/version.rb
44
+ - lib/word_guesser/word_collection.rb
45
+ - lib/word_guesser/word_guesser.rb
46
+ - spec/spec_helper.rb
47
+ - spec/word_collection_spec.rb
48
+ - spec/word_guesser_spec.rb
49
+ - word-guesser.gemspec
50
+ homepage: http://johnandrewmarshall.com/projects/word-guesser
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.16
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A word guesser
74
+ test_files:
75
+ - spec/spec_helper.rb
76
+ - spec/word_collection_spec.rb
77
+ - spec/word_guesser_spec.rb