Hunspell is the spell checker of LibreOffice, OpenOffice.org, Mozilla Firefox 3 & Thunderbird, Google Chrome, and it is also used by proprietary software packages, like macOS, InDesign, memoQ, Opera and SDL Trados.
Main features:
Hunspell is available in the default repositories of most Linux distributions, so installation won’t be a big deal.
On Arch Linux and derivatives like Antergos, Manjaro Linux, run:
$ sudo pacman -S hunspell
On Fedora:
$ sudo dnf install hunspell
On RHEL, CentOS:
$ sudo yum install epel-release
$ sudo yum install hunspell
On Debian, Ubuntu:
$ sudo apt-get install hunspell
on Mac OSX with Homebrew:
brew install hunspell
By default, Hunspell won’t have any dictionaries. To add a dictionary, for example English, just install this package – hunspell-en-us
. Similarly, to add Spanish dictionary, install hunspell-es
package.
This can also be found in the default repositories. For instance, to add English dictionary on Arch linux, run:
$ sudo pacman -S hunspell-en-us
On Debian, Ubuntu:
$ sudo apt-get install hunspell-en-us
On Fedora:
$ sudo dnf install hunspell-en-us
On RHEL/CentOS:
$ sudo yum install hunspell-en-us
on Mac OSX:
Download dictionaries from http://wordlist.aspell.net/dicts/ and put them to /Library/Spelling/
.
Once installed all dictionaries, you can ensure whether the required dictionaries are available or not using command:
$ hunspell -D
/usr/share/hunspell/en_US
...
Now that you have the english dictionary installed in your system, let's see how to spellcheck a word using PHP-Spellchecker and Hunspell.
<?php
// if you made the default hunspell installation on you local machine
$hunspell = Hunspell::create();
// or if you want to use binaries from Docker
$hunspell = new Hunspell(new CommandLine(['docker','run','--rm', '-i', 'tmaier/hunspell']);
// en_US hunspell dictionary is available
$misspellings = $hunspell->check('mispell', ['en_US'], ['from_example']);
foreach ($misspellings as $misspelling) {
$misspelling->getWord(); // 'mispell'
$misspelling->getLineNumber(); // '1'
$misspelling->getOffset(); // '0'
$misspelling->getSuggestions(); // ['misspell', ...]
$misspelling->getContext(); // ['from_example']
}
Or if you want to check a file instead:
<?php
// spellchecking a file
$misspellings = $hunspell->check(new File('path/to/file.txt'), ['en_US'], ['from_file']);
foreach ($misspellings as $misspelling) {
$misspelling->getWord();
$misspelling->getLineNumber();
$misspelling->getOffset();
$misspelling->getSuggestions();
$misspelling->getContext();
}
You can check current hunspell installation languages availability before trying to spellcheck with an unsupported language for example.
<?php
$hunspell = Hunspell::create();
$hunspell->getSupportedLanguages(); // ['en','en_US',...]
Check the tests for more examples.