Improvise
Improvise generates random words by learning from existing text.
Installation
Add this line to your application's Gemfile:
gem 'improvise'
And then execute:
$ bundle
Or install it yourself as:
$ gem install improvise
Usage
Require the 'improvise' gem:
require 'improvise'
Learn
Create a new dictionary with depth 3:
dict = Improvise::ForwardDictionary.new(3)
Or create a reversed dictionary with depth 3:
dict = Improvise::ReverseDictionary.new(3)
Learn from a list of existing words:
dict.learn!([
'ruby',
'sapphire',
'pearl',
'diamond',
'amethyst',
'topaz',
'emerald'
])
Generate
Generate a 6-letter word:
dict.generate_word(6)
Generate a 7-letter word, starting with 'top':
dict.generate_word(7, 'top')
Store
Write a dictionary in Ruby's Marshal format to a file called 'dict.bin':
dict_file = File.open('dict.bin', 'w')
Improvise::IO::DictionaryWriter.write(dict_file, dict)
Write a dictionary in Ruby's Marshal format, gzipped, to a file called 'dict.bin.gz':
dict_file = File.open('dict.bin.gz', 'w')
Improvise::IO::DictionaryWriter.write(dict_file, dict, gzip: true)
Write a dictionary in json format, gzipped, to a file called 'dict.json.gz':
dict_file = File.open('dict.json.gz', 'w')
Improvise::IO::DictionaryWriter.write(dict_file, dict, format: :json, gzip: true)
Note: writing a dictionary closes the passed IO object.
Load
Restore a dictionary from a file called 'dict.bin' in Ruby's Marshal format:
dict_file = File.open('dict.bin', 'r')
dict = Improvise::IO::DictionaryReader.read(dict_file)
Restore a dictionary from a file called 'dict.bin.gz' in Ruby's Marshal format, gzipped:
dict_file = File.open('dict.bin.gz', 'r')
dict = Improvise::IO::DictionaryReader.read(dict_file, gzip: true)
Restore a dictionary from a file called 'dict.json.gz' in json format, gzipped:
dict_file = File.open('dict.json.gz', 'r')
dict = Improvise::IO::DictionaryReader.read(dict_file, format: :json, gzip: true)
Note: reading a dictionary closes the passed IO object.
Command-line tool
A command-line interface to the library has been provided under the name 'improvise'.
Run improvise --help
to get a list of available commands.
Contributing
- Fork
- Branch (
git checkout -b my-new-feature
) - Commit (
git commit -am 'Added some feature'
) - Push (
git push origin my-new-feature
) - Pull request