Skip to content

taruti/enchant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Go-Enchant

This is a fork of https://github.com/hermanschaaf/enchant with a different API for using multiple dictionaries at a time.

Go-Enchant provides bindings for the C Enchant Spellcheck Library in Go. Instead of offering direct mappings to the Enchant functions, it abstracts away some complexity and makes it easier to do resource management in Go.

Installation

First off, you will need to have the libenchant development files installed, along with any dictionaries you might want to use (aspell, hunspell, etc):

sudo aptitude install libenchant-dev

Then install this package with go get:

go get github.com/hermanschaaf/enchant

Usage

Basic usage is illustrated by the following example program:

package main

import (
	"fmt"
	"github.com/hermanschaaf/enchant"
)

func main() {
	// create a new enchant instance
	enchant, err := enchant.NewEnchant()
	if err != nil {
		panic("Enchant error: " + err.Error())
	}

	// defer freeing memory to the end of this program
	defer enchant.Free()

	// check whether a certain dictionary exists on the system
	has_en := enchant.DictExists("en_GB")

	// load the english dictionary:
	if has_en {
		d, err := enchant.LoadDict("en_GB")
		if err != nil {
		   panic("Enchant error: " + err.Error())
		}
		defer enchant.FreeDict(d)

		// see if a word is in the dictionary:
		fmt.Println("hallo:", d.Check("hallo"))

		// and one that won't be in there:
		fmt.Println("wollo:", d.Check("wollo"))

		// now let's get some suggestions for "wollo":
		fmt.Println(d.Suggest("wollo"))
	}
}
  1. First, we create a new Enchant instance using the NewEnchant function.

  2. We defer a call to enchant.Free() to free memory allocation when our program ends. Free() handles the freeing of both the Enchant broker and loaded dictionaries.

  3. Next, we check whether a certain dictionary is installed on the system using a call to enc.DictExists().

  4. We know the dictionary exists now, so we load it into our Enchant instance with a call to LoadDict().

  5. Now we are free to make any calls to Enchant that we want. We call Check, which returns whether the given word is contained in the dictionary or not. We expect "hallo" to be in the dictionary, and "wollo" not to be. Indeed, our program output confirms this:

hallo: true
wollo: false
[Rollo woolly hollow follow woollen would worldly]

The final line is the result of our call to enc.Suggest("wollo"): it returns a slice of spelling suggestions for the given word.

Documentation

Full documentation can be found at godoc.org/github.com/hermanschaaf/enchant

About

Go enchant binding for spell checking

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages