ruby-dictionary provides a simple dictionary that allows for checking existence of words and finding a subset of words given a prefix.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install ruby-dictionary
A dictionary is created by passing an array of strings to the initializer.
dictionary = Dictionary.new(%w(a ab abs absolute absolutes absolutely absolve be bee been bees bend bent best))
Alternatively, words can be read in from a file (raw or gzip compressed) as well.
dictionary = Dictionary.from_file('path/to/uncompressed.txt') dictionary = Dictionary.from_file('path/to/compressed.txt.gz')
It is assumed that the file contains one word per line. However, a separator can be passed to the method as an optional second parameter if that's not the case.
dictionary = Dictionary.from_file('path/to/uncompressed.txt', ' ') dictionary = Dictionary.from_file('path/to/compressed.txt.gz', ',')
Once a dictionary is loaded, the
#exists? method can be used to determine if
a word exists.
dictionary.exists?('bees') # => true dictionary.exists?('wasps') # => false
#starting_with method returns a sorted array of all words starting with
the provided string.
dictionary.starting_with('bee') # => ["bee", "been", "bees"] dictionary.starting_with('foo') # => 
#prefixes method returns a sorted array of all the words appearing in the
beginning of the provided string.
dictionary.prefixes('abstract') # => ["a", "ab", "abs"] dictionary.prefixes('bend') # => ["be", "bend"]
By default, a new
Dictionary is case-insensitive, meaning "bee", "Bee", and
"BEE" are all considered to be the same, regardless of adding to the dictionary
or searching within it.
However, you can choose to use case-sensitive dictionary by passing an optional
true parameter to both the
dictionary = Dictionary.new(%w(Alpha Beta), true) dictionary.exists?('Alpha') # => true dictionary.exists?('alpha') # => false dictionary = Dictionary.from_file('restaurants.txt', "\n", true) dictionary.starting_with('Mc') # => ["McDonald's"] dictionary.starting_with('mc') # => 
Additionally, you can determine whether a dictionary is case-sensitive via the
dictionary = Dictionary.new() dictionary.case_sensitive? # => false dictionary = Dictionary.new(, true) dictionary.case_sensitive? # => true
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request