Skip to content

mamantoha/crystal-hunspell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crystal-hunspell

CI GitHub release License

Crystal bindings for Hunspell.

Installation

Before installing crystal-hunspell ensure you have hunspell already installed:

  1. Add the dependency to your shard.yml:

    dependencies:
      hunspell:
        github: mamantoha/crystal-hunspell
  2. Run shards install

Usage

The library has been tested with Hunspell version 1.7.2.

require "hunspell"

Open a dictionary:

hunspell = Hunspell.new("/usr/share/hunspell/en_US.aff", "/usr/share/hunspell/en_US.dic")

or

hunspell = Hunspell.new("en_US")

Ensure to close Hunspell instance after using.

hunspell.close

Below are some simple examples for how to use the repository.

Spelling

It's a simple task to ask if a particular word is in the dictionary.

hunspell.spell?("correct")
# => true

hunspell.spell?("incorect")
# => false

This will only ever return true or false, and won't give suggestions about why it might be wrong. It also depends on your choice of dictionary.

Suggestions

If you want to get a suggestion from Hunspell, it can provide a corrected label given a basestring input.

hunspell.suggest("arbitrage")
# => ["arbitrage", "arbitrages", "arbitrager", "arbitraged", "arbitrate"]

Suffix Match

hunspell.suffix_suggest("do")
# => ["doing", "doth", "doer", "dos", "do's", "doings", "doers"]

Stemming

The module can also stem words, providing the stems for pluralization and other inflections.

hunsell.stem("fishing")
# => ["fishing", "fish"]

Analyze

Like stemming but return morphological analysis of the input instead.

hunspell.analyze("permanently")
# => [" st:permanent fl:Y"]

Bulk Requests

You can also request bulk actions against Hunspell. Currently suggest, suffix_suggest, stem, and analyze are bulk requestable.

hunspell.bulk_suggest(["correct", "incorect"])
# => {"correct"  => ["correct", "corrects", "cor rect", "cor-rect"],
#     "incorect" => ["incorrect", "correction", "corrector", "injector", "correct"]}

hunspell.bulk_suffix_suggest(["cat", "do"])
# => {"cat" => ["cats", "cat's"],
#     "do"  => ["doing", "doth", "doer", "dos", "do's", "doings", "doers"]}

hunspell.bulk_stem(["stems", "currencies"])
# => {"stems" => ["stem"], "currencies" => ["currency"]}

hunspell.bulk_analyze(["dog", "permanently"])
# => {"dog" => [" st:dog"], "permanently" => [" st:permanent fl:Y"]}

Development

sudo apt install libclang-dev libhunspell-dev

Generate new bindings for Hunspell

crystal ./lib/crystal_lib/src/main.cr src/hunspell/lib_hunspell.cr.in > src/hunspell/lib_hunspell.cr

Contributing

  1. Fork it (https://github.com/mamantoha/crystal-hunspell/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors