improvise 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d23a1bbb23c53484630f7e37c9eaf98aefffef7
4
- data.tar.gz: 8017aec4d0fa52f0dcda9a65fc8ea17e1dbb573d
3
+ metadata.gz: 20e2150a56f14065cefb3d37dfb58694a4f39d9f
4
+ data.tar.gz: 1aefacfe9d5a94cc298e405f4f88f0725714621a
5
5
  SHA512:
6
- metadata.gz: b9e9f8471cd08b819667c12dcfe45ef24ca94d2ba9f1ec9180cca1d8e3d8f4c83e6071333c2ca01c3a1b9eeaeb368d8a24a864eef9cf566c11c0bdb08121b88f
7
- data.tar.gz: b8d199c5a1f6465e713ae5fadc6ec813cf0527ba7027c13758ce00ae6241c9631f42357b59b6aaec652543beae552a99a57cc7dd47ccd89a02acfd27dbc491db
6
+ metadata.gz: afa09f4a97bd02eaff58a966769026eda56625f3b27241fbf49035c87771e106f0dd826f186290a771f7bd56b85124cfdbb957f9b5c019845c0f074249214bae
7
+ data.tar.gz: e66451c405c93f8285ae65b649bda0fd4301d583676c20732fa0b6fe06dc79b859901df38647184061e7819b1a9ae094a12f939a403a837dde97d86ac671f870
data/README.md CHANGED
@@ -27,10 +27,21 @@ require 'improvise'
27
27
  ```
28
28
 
29
29
  ### Learn
30
- Create a new dictionary with depth 3 and learn from a list of existing words:
30
+ Create a new dictionary with depth 3:
31
31
 
32
32
  ```ruby
33
33
  dict = Improvise::ForwardDictionary.new(3)
34
+ ```
35
+
36
+ Or create a reversed dictionary with depth 3:
37
+
38
+ ```ruby
39
+ dict = Improvise::ReverseDictionary.new(3)
40
+ ```
41
+
42
+ Learn from a list of existing words:
43
+
44
+ ```ruby
34
45
  dict.learn!([
35
46
  'ruby',
36
47
  'sapphire',
@@ -17,6 +17,7 @@ desc 'Create or update a dictionary based on an input file'
17
17
  arg_name 'input dictionary'
18
18
  command :learn do |c|
19
19
  c.flag [:d, :depth], :desc => 'The depth to use when building the dictionary', :default_value => 2, :type => Integer
20
+ c.switch [:r, :reverse], :desc => 'Build a reverse dictionary', :negatable => false
20
21
 
21
22
  c.action do |global_options, options, args|
22
23
  if args.length < 1
@@ -28,6 +29,7 @@ command :learn do |c|
28
29
  input_filename = args[0]
29
30
  dictionary_filename = args[1]
30
31
  depth = options[:depth]
32
+ reverse = options[:reverse]
31
33
  format = global_options[:format]
32
34
  gzip = global_options[:gzip]
33
35
 
@@ -38,7 +40,11 @@ command :learn do |c|
38
40
  if File.exists?(dictionary_filename)
39
41
  dictionary = Improvise::IO::DictionaryReader.read(File.open(dictionary_filename, 'r'), format: format, gzip: gzip)
40
42
  else
41
- dictionary = Improvise::ForwardDictionary.new(depth)
43
+ if reverse
44
+ dictionary = Improvise::ReverseDictionary.new(depth)
45
+ else
46
+ dictionary = Improvise::ForwardDictionary.new(depth)
47
+ end
42
48
  end
43
49
 
44
50
  dictionary.learn!(words)
@@ -1,14 +1,12 @@
1
- require 'improvise/version'
2
-
3
1
  Gem::Specification.new do |s|
4
2
  s.specification_version = 2 if s.respond_to? :specification_version=
5
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
4
  s.rubygems_version = '1.3.5'
7
5
 
8
6
  s.name = 'improvise'
9
- s.version = Improvise::VERSION
7
+ s.version = '0.2.0'
10
8
  s.platform = Gem::Platform::RUBY
11
- s.date = '2014-05-20'
9
+ s.date = '2014-05-21'
12
10
  s.authors = ['Sander Kleykens']
13
11
  s.email = ['sander@kleykens.com']
14
12
  s.homepage = 'http://github.com/SanderKleykens/improvise'
@@ -51,12 +49,15 @@ Gem::Specification.new do |s|
51
49
  lib/improvise/io/dictionaryreader.rb
52
50
  lib/improvise/io/dictionarywriter.rb
53
51
  lib/improvise/io/io.rb
52
+ lib/improvise/reversedictionary.rb
53
+ lib/improvise/util/stringutils.rb
54
54
  lib/improvise/version.rb
55
55
  spec/improvise/dictionary_spec.rb
56
56
  spec/improvise/dictionarytree_spec.rb
57
57
  spec/improvise/forwarddictionary_spec.rb
58
58
  spec/improvise/io/dictionaryreader_spec.rb
59
59
  spec/improvise/io/dictionarywriter_spec.rb
60
+ spec/improvise/reversedictionary_spec.rb
60
61
  spec/spec_helper.rb
61
62
  ]
62
63
  # = MANIFEST =
@@ -1,3 +1,4 @@
1
1
  require 'improvise/version'
2
2
  require 'improvise/forwarddictionary'
3
+ require 'improvise/reversedictionary'
3
4
  require 'improvise/io/io'
@@ -8,24 +8,24 @@ module Improvise
8
8
  @root = root || Tree::TreeNode.new('root')
9
9
  end
10
10
 
11
- def add_entry!(prefix, suffix)
12
- prefix_node = DictionaryTree.add_node!(@root, prefix)
13
- prefix_node.content += 1
11
+ def add_entry!(key, value)
12
+ key_node = DictionaryTree.add_node!(@root, key)
13
+ key_node.content += 1
14
14
 
15
- suffix_node = DictionaryTree.add_node!(prefix_node, suffix)
16
- suffix_node.content += 1
15
+ value_node = DictionaryTree.add_node!(key_node, value)
16
+ value_node.content += 1
17
17
  end
18
18
 
19
- def random_prefix
19
+ def random_key
20
20
  DictionaryTree.new_pickup(@root).pick
21
21
  end
22
22
 
23
- def random_suffix(prefix)
24
- if prefix.nil?
23
+ def random_value(key)
24
+ if key.nil?
25
25
  return nil
26
26
  end
27
27
 
28
- node = @root[prefix]
28
+ node = @root[key]
29
29
 
30
30
  if node.nil?
31
31
  return nil
@@ -1,4 +1,5 @@
1
1
  require 'improvise/dictionary'
2
+ require 'improvise/util/stringutils'
2
3
 
3
4
  module Improvise
4
5
  class ForwardDictionary < Dictionary
@@ -7,21 +8,20 @@ module Improvise
7
8
  end
8
9
 
9
10
  def generate_word(length, root=nil)
10
- word = root || @tree.random_prefix
11
- word = word.slice(0..(length - 1))
11
+ word = root || @tree.random_key
12
12
 
13
13
  while word.length < length do
14
- suffix = @tree.random_suffix(word[[-@depth, 0].max..-1])
15
- word += suffix || @tree.random_prefix
16
- word = word.slice(0..(length - 1))
14
+ suffix = @tree.random_value(word[[-@depth, 0].max..-1])
15
+ word += suffix || @tree.random_key
17
16
  end
18
17
 
19
- word
18
+ Improvise::Util::StringUtils.truncate(word, length, omission: '')
20
19
  end
21
20
 
22
21
  def learn!(words)
23
22
  [*words].each do |word|
24
- @tree.add_entry!('', word.slice(0..(@depth - 1)))
23
+ start_of_word = word.slice(0..(@depth - 1))
24
+ @tree.add_entry!('', start_of_word)
25
25
 
26
26
  word.split('').each_cons(@depth + 1) do |characters|
27
27
  prefix = characters[0..-2].join('')
@@ -0,0 +1,35 @@
1
+ require 'improvise/dictionary'
2
+ require 'improvise/util/stringutils'
3
+
4
+ module Improvise
5
+ class ReverseDictionary < Dictionary
6
+ def initialize(depth=2, tree=nil)
7
+ super(depth, tree)
8
+ end
9
+
10
+ def generate_word(length, root=nil)
11
+ word = root || @tree.random_key
12
+
13
+ while word.length < length do
14
+ prefix = @tree.random_value(word[[-@depth, 0].max..-1])
15
+ word = (prefix || @tree.random_key) + word
16
+ end
17
+
18
+ Improvise::Util::StringUtils.truncate(word.reverse, length, omission: '').reverse
19
+ end
20
+
21
+ def learn!(words)
22
+ [*words].each do |word|
23
+ start_of_word = word.reverse.slice(0..(@depth - 1)).reverse
24
+ @tree.add_entry!('', start_of_word)
25
+
26
+ word.split('').reverse.each_cons(@depth + 1) do |characters|
27
+ suffix = characters[0..(@depth - 1)].reverse.join('')
28
+ prefix = characters[depth]
29
+
30
+ @tree.add_entry!(suffix, prefix)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,39 @@
1
+ module Improvise
2
+ module Util
3
+ module StringUtils
4
+ # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
5
+ #
6
+ # truncate('Once upon a time in a world far far away', 27)
7
+ # # => "Once upon a time in a wo..."
8
+ #
9
+ # Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
10
+ #
11
+ # truncate('Once upon a time in a world far far away', 27, separator: ' ')
12
+ # # => "Once upon a time in a..."
13
+ #
14
+ # truncate('Once upon a time in a world far far away', 27, separator: /\s/)
15
+ # # => "Once upon a time in a..."
16
+ #
17
+ # The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
18
+ # for a total length not exceeding <tt>length</tt>:
19
+ #
20
+ # truncate('And they found that many people were sleeping better.', 25, omission: '... (continued)')
21
+ # # => "And they f... (continued)"
22
+ #
23
+ # Adapted from Ruby on Rails (released under the MIT license)
24
+ def self.truncate(string, truncate_at, options = {})
25
+ return string.dup unless string.length > truncate_at
26
+
27
+ options[:omission] ||= '...'
28
+ length_with_room_for_omission = truncate_at - options[:omission].length
29
+ stop = if options[:separator]
30
+ string.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
31
+ else
32
+ length_with_room_for_omission
33
+ end
34
+
35
+ "#{string[0...stop]}#{options[:omission]}"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module Improvise
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,37 +1,37 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Improvise::DictionaryTree do
4
- PREFIX = 'te'
5
- SUFFIX = 's'
4
+ KEY = 'te'
5
+ VALUE = 's'
6
6
 
7
7
  before do
8
8
  @empty_tree = Improvise::DictionaryTree.new
9
9
  end
10
10
 
11
- it "should return nil as random prefix if the tree is empty" do
12
- expect(@empty_tree.random_prefix).to eq(nil)
11
+ it "should return nil as random key if the tree is empty" do
12
+ expect(@empty_tree.random_key).to eq(nil)
13
13
  end
14
14
 
15
- it "should return nil as random suffix if the prefix hasn't been found" do
16
- expect(@empty_tree.random_suffix('test')).to eq(nil)
15
+ it "should return nil as random value if the prefix hasn't been found" do
16
+ expect(@empty_tree.random_value('test')).to eq(nil)
17
17
  end
18
18
 
19
- it "should return nil as random suffix if the prefix is nil" do
20
- expect(@empty_tree.random_suffix(nil)).to eq(nil)
19
+ it "should return nil as random value if the prefix is nil" do
20
+ expect(@empty_tree.random_value(nil)).to eq(nil)
21
21
  end
22
22
 
23
- it "should return '#{PREFIX}'" do
23
+ it "should return '#{KEY}'" do
24
24
  tree = Improvise::DictionaryTree.new
25
- tree.add_entry!(PREFIX, SUFFIX)
25
+ tree.add_entry!(KEY, VALUE)
26
26
 
27
- expect(tree.random_prefix).to eq(PREFIX)
27
+ expect(tree.random_key).to eq(KEY)
28
28
  end
29
29
 
30
- it "should return '#{SUFFIX}'" do
30
+ it "should return '#{VALUE}'" do
31
31
  tree = Improvise::DictionaryTree.new
32
- tree.add_entry!(PREFIX, SUFFIX)
32
+ tree.add_entry!(KEY, VALUE)
33
33
  tree.add_entry!('aa', 's')
34
34
 
35
- expect(tree.random_suffix(PREFIX)).to eq(SUFFIX)
35
+ expect(tree.random_value(KEY)).to eq(VALUE)
36
36
  end
37
37
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Improvise::ReverseDictionary do
4
+ before do
5
+ @dict1 = Improvise::ReverseDictionary.new(1)
6
+ @dict2 = Improvise::ReverseDictionary.new
7
+ end
8
+
9
+ it "should have depth 1" do
10
+ expect(@dict1.depth).to eq(1)
11
+ end
12
+
13
+ it "should have depth 2" do
14
+ expect(@dict2.depth).to eq(2)
15
+ end
16
+
17
+ it "should generate 'te' using depth 1" do
18
+ @dict1.learn!('te')
19
+ expect(@dict1.generate_word(2, 'e')).to eq('te')
20
+ end
21
+
22
+ it "should generate 'te' using depth 2" do
23
+ @dict2.learn!('te')
24
+ expect(@dict2.generate_word(2)).to eq('te')
25
+ end
26
+
27
+ it "should generate 'tes' using depth 2" do
28
+ @dict2.learn!('tes')
29
+ expect(@dict2.generate_word(3, 'es')).to eq('tes')
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: improvise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sander Kleykens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-20 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gli
@@ -157,12 +157,15 @@ files:
157
157
  - lib/improvise/io/dictionaryreader.rb
158
158
  - lib/improvise/io/dictionarywriter.rb
159
159
  - lib/improvise/io/io.rb
160
+ - lib/improvise/reversedictionary.rb
161
+ - lib/improvise/util/stringutils.rb
160
162
  - lib/improvise/version.rb
161
163
  - spec/improvise/dictionary_spec.rb
162
164
  - spec/improvise/dictionarytree_spec.rb
163
165
  - spec/improvise/forwarddictionary_spec.rb
164
166
  - spec/improvise/io/dictionaryreader_spec.rb
165
167
  - spec/improvise/io/dictionarywriter_spec.rb
168
+ - spec/improvise/reversedictionary_spec.rb
166
169
  - spec/spec_helper.rb
167
170
  homepage: http://github.com/SanderKleykens/improvise
168
171
  licenses: