enchant

package module
v0.0.0-...-f5e1f01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2014 License: MIT Imports: 2 Imported by: 1

README

Go-Enchant

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 {
		enchant.LoadDict("en_GB")

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

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

		// now let's get some suggestions for "wollo":
		fmt.Println(enchant.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

Documentation

Overview

Package Enchant provides simplified bindings to Enchant spell checking library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enchant

type Enchant struct {
	// contains filtered or unexported fields
}

Enchant is a type that encapsulates Enchant internals

func NewEnchant

func NewEnchant() (e *Enchant, err error)

NewEnchant creates a new Enchant instance for access to the rest of the Enchant API.

The returned value is an Enchant struct.

Example usage:

		enchant, err := enchant.NewEnchant()
		if err != nil {
			panic("Enchant error: " + err.Error())
		}
		defer enchant.Free()
     fmt.Println(enchant.DictExists("zh"))

Because the Enchant package is a binding to Enchant C library, memory allocated by the NewEnchant() call has to be disposed explicitly. This is why the above example contains a deferred call to Free().

func (*Enchant) Check

func (e *Enchant) Check(word string) bool

Check whether a given word is in the currently loaded dictionary. This wraps enchant_dict_check. It returns a boolean value: true if the word is in the dictionary, false otherwise.

func (*Enchant) DictExists

func (e *Enchant) DictExists(name string) bool

DictExists wraps enchant_broker_dict_exists. It takes a language code name, such as "en_GB", as string argument, and it returns whether or not such a dictionary is installed on the system.

func (*Enchant) Free

func (e *Enchant) Free()

Free frees the Enchant broker and dictionary, and needs to be called when use of the library is no longer needed to prevent memory leaks.

func (*Enchant) LoadDict

func (e *Enchant) LoadDict(name string)

LoadDict wraps enchant_broker_request_dict, and adds the loaded dictionary to the Enchant instance. It takes a language code name, such as "en_GB", as string argument, and it returns a EnchantDict representation of this dictionary.

func (*Enchant) Suggest

func (e *Enchant) Suggest(word string) (suggestions []string)

Suggest words based on the given word. This is a wrapper for enchant_dict_suggest. It returns a slice of suggestion strings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL