scrabblestuff 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/VERSION +1 -1
- data/assets/{wwf.txt → words.txt} +0 -0
- data/lib/scrabblestuff/solver.rb +38 -89
- data/scrabblestuff.gemspec +4 -4
- data/test/helper.rb +2 -32
- data/test/test_scrabblestuff.rb +6 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfaf3d78979001b0135400415b5affcc1f136986
|
4
|
+
data.tar.gz: 4e09e359f1f2ea9dfe3af67ee954c34d7e3e1471
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 370b744c9fee6fc2b08607cac7119e560e3dd71f26b91a3a52ddf847872a7bf0a8edeceb44ff5d1b18808ff03307d5a86e96e1794b4f0b98d3cb89e2d44d14f2
|
7
|
+
data.tar.gz: 546066d02b7a3d4038e57597dfb6f4dadfac783652b71683b5ce623abd98dd9e546693844591e6ee3a623e09e672be738f421f1c49e51a8fb840e38031d51003
|
data/README.md
CHANGED
@@ -10,6 +10,15 @@ based on the algorithm used by https://github.com/samwho/scrabble-solver
|
|
10
10
|
|
11
11
|
`gem install scrabblestuff`
|
12
12
|
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
solver = Scrabble::Solver.new
|
17
|
+
solver.load_dictionary
|
18
|
+
solver.find("astrd").sort { |x,y| x.length <=> y.length }
|
19
|
+
=> ["at", "ad", "as", "ta", "ar", "ads", "ars", "art", "tas", "tar", "tad", "sat", "sad", "rad", "rat", "ras", "rats", "rads", "sard", "drat", "star", "tads", "arts", "tars", "dart", "trad", "tsar", "darts", "drats"]
|
20
|
+
```
|
21
|
+
|
13
22
|
## Copyright
|
14
23
|
|
15
24
|
Copyright © 2014 Cody Barr. See LICENSE.md for
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
File without changes
|
data/lib/scrabblestuff/solver.rb
CHANGED
@@ -1,96 +1,45 @@
|
|
1
|
+
# trie.rb
|
2
|
+
# from https://gist.github.com/Sirupsen/6481936
|
1
3
|
module Scrabble
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
# Reads in the words.txt file and returns an array containing all of the words
|
9
|
-
# in that file.
|
10
|
-
#
|
11
|
-
# Used by other methods
|
12
|
-
|
13
|
-
def self.word_list
|
14
|
-
@word_list ||= File.open(@word_file_name) do |file|
|
15
|
-
file.readlines.map do |line|
|
16
|
-
line.chomp
|
17
|
-
end.delete_if do |line|
|
18
|
-
line.length < 2
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
@word_list.clone
|
4
|
+
class Solver
|
5
|
+
attr_accessor :word, :nodes, :used, :all
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@word, @nodes = false, {}
|
23
9
|
end
|
24
10
|
|
25
|
-
|
26
|
-
|
27
|
-
#
|
28
|
-
# Example:
|
29
|
-
#
|
30
|
-
# Scrabble::Solver.words_for "there"
|
31
|
-
# # => An array of words that the tiles t, h, e, r, e could make.
|
32
|
-
#
|
33
|
-
# Scrabble::Solver.words_for "there?"
|
34
|
-
# # => An array of words that the tiles t, h, e, r, e plus a blank tile
|
35
|
-
# # could make.
|
36
|
-
|
37
|
-
def self.words_for(letters, options = {})
|
38
|
-
# splits the letters into lowercase array of individual letters
|
39
|
-
letters = letters.downcase.split(//)
|
40
|
-
|
41
|
-
# cycles through the entire word list, returns a list of new words as long as they match the algorithm
|
42
|
-
words = word_list.keep_if do |word|
|
43
|
-
# Split the current dictionary word into an array of its letters.
|
44
|
-
word = word.split(//)
|
45
|
-
|
46
|
-
# cycles through each of the letters in our letters array
|
47
|
-
letters.each do |letter|
|
48
|
-
# if the letter is found...
|
49
|
-
unless word.index(letter).nil?
|
50
|
-
# delete the letter from the word, move to the next letter
|
51
|
-
word.delete_at word.index(letter)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
# Only return the word if there are no remaining letters
|
56
|
-
word.length == 0
|
57
|
-
end
|
58
|
-
|
59
|
-
# Filter only words that start with a specific sequence.
|
60
|
-
if options[:starts_with]
|
61
|
-
words.keep_if { |word| word.start_with? options[:starts_with] }
|
62
|
-
end
|
63
|
-
|
64
|
-
# Filter words that only end in a certain sequence.
|
65
|
-
if options[:ends_with]
|
66
|
-
words.keep_if { |word| word.end_with? options[:ends_with] }
|
67
|
-
end
|
68
|
-
|
69
|
-
# Fitler only words shorter than a given amount.
|
70
|
-
if options[:shorter_than]
|
71
|
-
words.keep_if { |word| word.length < options[:shorter_than].to_i }
|
72
|
-
end
|
73
|
-
|
74
|
-
# Filter words only longer than a given amount.
|
75
|
-
if options[:longer_than]
|
76
|
-
words.keep_if { |word| word.length > options[:longer_than].to_i }
|
77
|
-
end
|
11
|
+
def load_dictionary(word_file_name = File.dirname(__FILE__) + '/../../assets/words.txt')
|
12
|
+
words = File.read(word_file_name).split("\n").map(&:downcase)
|
78
13
|
|
79
|
-
|
80
|
-
|
81
|
-
# Generate a regex to match against the string. This regex will replace
|
82
|
-
# question marks with dots, so that they match any character. This
|
83
|
-
# gives the user a lot of power with the --contains --at combo.
|
84
|
-
regex = ''
|
85
|
-
regex += ('^' + ('.' * (options[:at].to_i - 1))) if options[:at]
|
86
|
-
regex += options[:contains].gsub('?', '.')
|
87
|
-
regex = Regexp.new regex
|
88
|
-
words.keep_if do |word|
|
89
|
-
word =~ regex
|
90
|
-
end
|
14
|
+
words.each do |word|
|
15
|
+
self.<< word.chomp
|
91
16
|
end
|
92
|
-
|
93
|
-
|
17
|
+
end
|
18
|
+
|
19
|
+
def <<(word)
|
20
|
+
node = word.each_char.inject(self) { |node, char| node.nodes[char] ||= Solver.new }
|
21
|
+
node.word = true
|
22
|
+
end
|
23
|
+
|
24
|
+
def find(letters)
|
25
|
+
@all = []
|
26
|
+
@used = frequency_map(letters)
|
27
|
+
recursive_find self, ""
|
28
|
+
@all
|
29
|
+
end
|
30
|
+
|
31
|
+
def recursive_find(root, word)
|
32
|
+
nodes.reject { |c, v| root.used[c] == 0 }.each { |char, node|
|
33
|
+
root.used[char] -= 1
|
34
|
+
node.recursive_find(root, word + char)
|
35
|
+
root.used[char] += 1
|
36
|
+
}
|
37
|
+
|
38
|
+
root.all << word if self.word
|
39
|
+
end
|
40
|
+
|
41
|
+
def frequency_map(letters)
|
42
|
+
letters.each_char.inject(Hash.new(0)) { |map, char| (map[char] += 1) && map }
|
94
43
|
end
|
95
44
|
end
|
96
|
-
end
|
45
|
+
end
|
data/scrabblestuff.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: scrabblestuff 0.
|
5
|
+
# stub: scrabblestuff 0.2.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "scrabblestuff"
|
9
|
-
s.version = "0.
|
9
|
+
s.version = "0.2.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Cody Barr"]
|
14
|
-
s.date = "2014-05-
|
14
|
+
s.date = "2014-05-31"
|
15
15
|
s.description = "scrabblestuff is a simple scrabble solver gem with a default WWF dictionary."
|
16
16
|
s.email = "cody.barr@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"README.md",
|
28
28
|
"Rakefile",
|
29
29
|
"VERSION",
|
30
|
-
"assets/
|
30
|
+
"assets/words.txt",
|
31
31
|
"lib/scrabblestuff.rb",
|
32
32
|
"lib/scrabblestuff/solver.rb",
|
33
33
|
"scrabblestuff.gemspec",
|
data/test/helper.rb
CHANGED
@@ -1,34 +1,4 @@
|
|
1
1
|
require 'simplecov'
|
2
|
+
require 'minitest/autorun'
|
2
3
|
|
3
|
-
|
4
|
-
def clean_filters
|
5
|
-
@filters = []
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
SimpleCov.configure do
|
10
|
-
clean_filters
|
11
|
-
load_adapter 'test_frameworks'
|
12
|
-
end
|
13
|
-
|
14
|
-
ENV["COVERAGE"] && SimpleCov.start do
|
15
|
-
add_filter "/.rvm/"
|
16
|
-
end
|
17
|
-
require 'rubygems'
|
18
|
-
require 'bundler'
|
19
|
-
begin
|
20
|
-
Bundler.setup(:default, :development)
|
21
|
-
rescue Bundler::BundlerError => e
|
22
|
-
$stderr.puts e.message
|
23
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
24
|
-
exit e.status_code
|
25
|
-
end
|
26
|
-
require 'test/unit'
|
27
|
-
require 'shoulda'
|
28
|
-
|
29
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
30
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
31
|
-
require 'scrabblestuff'
|
32
|
-
|
33
|
-
class Test::Unit::TestCase
|
34
|
-
end
|
4
|
+
require_relative '../lib/scrabblestuff'
|
data/test/test_scrabblestuff.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
describe ".find" do
|
4
|
+
it "should return correct results" do
|
5
|
+
solver = Scrabble::Solver.new
|
6
|
+
solver.load_dictionary
|
7
|
+
solver.find("astrd").sort { |x,y| x.length <=> y.length }.must_equal(["at", "ad", "as", "ta", "ar", "ads", "ars", "art", "tas", "tar", "tad", "sat", "sad", "rad", "rat", "ras", "rats", "rads", "sard", "drat", "star", "tads", "arts", "tars", "dart", "trad", "tsar", "darts", "drats"])
|
6
8
|
end
|
7
|
-
end
|
9
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scrabblestuff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cody Barr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: shoulda
|
@@ -96,7 +96,7 @@ files:
|
|
96
96
|
- README.md
|
97
97
|
- Rakefile
|
98
98
|
- VERSION
|
99
|
-
- assets/
|
99
|
+
- assets/words.txt
|
100
100
|
- lib/scrabblestuff.rb
|
101
101
|
- lib/scrabblestuff/solver.rb
|
102
102
|
- scrabblestuff.gemspec
|