-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenchant.go
152 lines (120 loc) · 3.3 KB
/
enchant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Package enchant provides bindings to the Enchant spell checking library.
package enchant
/*
#cgo LDFLAGS: -lenchant
#include <stdlib.h>
#include <enchant/enchant.h>
static char* getString(char **c, int i) {
return c[i];
}
*/
import "C"
import (
"fmt"
"sync"
"unsafe"
)
// Enchant is the type that contains the package internals.
type Enchant struct {
sync.Mutex
broker *C.EnchantBroker
dict *C.EnchantDict
}
// New allocates a new Enchant instance.
func New() (e *Enchant) {
return &Enchant{broker: C.enchant_broker_init()}
}
// Free frees the allocated memory related to the Enchant instance.
func (e *Enchant) Free() {
e.DictFree() // make sure dictionary is freed to prevent memory leaks.
if e.broker != nil {
C.enchant_broker_free(e.broker)
e.broker = nil
}
}
// DictFree frees the current dictionary allowing another to be loaded.
func (e *Enchant) DictFree() {
if e.broker != nil && e.dict != nil {
C.enchant_broker_free_dict(e.broker, e.dict)
e.dict = nil
}
}
// DictExists checks if a dictionary exists or not.
func (e *Enchant) DictExists(tag string) (exists bool, err error) {
if e.broker == nil {
return false, fmt.Errorf("no broker initialized")
}
cTag := C.CString(tag)
defer C.free(unsafe.Pointer(cTag))
return C.enchant_broker_dict_exists(e.broker, cTag) == 1, nil
}
// DictLoad loads a dictionary to spell check against.
func (e *Enchant) DictLoad(tag string) error {
if e.broker == nil {
return fmt.Errorf("no broker initialized")
}
if e.dict != nil {
return fmt.Errorf("a dictionary is already loaded")
}
cTag := C.CString(tag)
defer C.free(unsafe.Pointer(cTag))
e.dict = C.enchant_broker_request_dict(e.broker, cTag)
if e.dict == nil {
return fmt.Errorf("failed to load dictionary by tag: %s", tag)
}
return nil
}
// DictCheck checks if a word is found in the loaded dictionary.
func (e *Enchant) DictCheck(word string) (found bool, err error) {
if e.broker == nil {
return false, fmt.Errorf("no broker initialized")
}
if e.dict == nil {
return false, fmt.Errorf("no dictionary loaded")
}
if len(word) == 0 {
return true, nil
}
cWord := C.CString(word)
defer C.free(unsafe.Pointer(cWord))
size := uintptr(len(word))
s := (*C.ssize_t)(unsafe.Pointer(&size))
e.Lock()
status := C.enchant_dict_check(e.dict, cWord, *s)
e.Unlock()
if status < 0 {
return false, fmt.Errorf("could not check word: %s", word)
}
return status == 0, nil
}
// DictSuggest suggests spelling for a word.
func (e *Enchant) DictSuggest(word string) (suggestions []string, err error) {
if e.broker == nil {
return nil, fmt.Errorf("no broker initialized")
}
if e.dict == nil {
return nil, fmt.Errorf("no dictionary loaded")
}
if len(word) == 0 {
return nil, nil
}
cWord := C.CString(word)
defer C.free(unsafe.Pointer(cWord))
size := uintptr(len(word))
s := (*C.ssize_t)(unsafe.Pointer(&size))
// number of suggestions returned.
var suggs uintptr
ns := (*C.size_t)(unsafe.Pointer(&suggs))
e.Lock()
defer e.Unlock()
response := C.enchant_dict_suggest(e.dict, cWord, *s, ns)
if response == nil {
return nil, nil
}
defer C.enchant_dict_free_string_list(e.dict, response)
suggestions = make([]string, 0, suggs)
for i := uintptr(0); i < suggs; i++ {
suggestions = append(suggestions, C.GoString(C.getString(response, C.int(i))))
}
return suggestions, nil
}