-
Notifications
You must be signed in to change notification settings - Fork 3
/
libvarnam.go
201 lines (149 loc) · 4.75 KB
/
libvarnam.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package libvarnam
// #cgo pkg-config: varnam
// #include <varnam.h>
import "C"
import "fmt"
// Varnam app binding.
type Varnam struct {
handle *C.varnam
}
// VarnamError satisfies error interface.
type VarnamError struct {
errorCode int
message string
}
// SchemeDetails returns language and other changes.
type SchemeDetails struct {
LangCode string
Identifier string
DisplayName string
Author string
CompiledDate string
IsStable bool
}
// CorpusDetails returns corpus details.
type CorpusDetails struct {
WordsCount int `json:"wordsCount"`
}
// LearnStatus .
type LearnStatus struct {
TotalWords int
Failed int
}
func (e *VarnamError) Error() string {
return e.message
}
// GetSuggestionsFilePath returns suggestions.
func (v *Varnam) GetSuggestionsFilePath() string {
return C.GoString(C.varnam_get_suggestions_file(v.handle))
}
// GetCorpusDetails will return corpus details.
func (v *Varnam) GetCorpusDetails() (*CorpusDetails, error) {
var details *C.vcorpus_details
rc := C.varnam_get_corpus_details(v.handle, &details)
if rc != C.VARNAM_SUCCESS {
errorCode := (int)(rc)
return nil, &VarnamError{errorCode: errorCode, message: v.getVarnamError(errorCode)}
}
return &CorpusDetails{WordsCount: int(details.wordsCount)}, nil
}
// Transliterate given string to corresponding language.
func (v *Varnam) Transliterate(text string) ([]string, error) {
var va *C.varray
rc := C.varnam_transliterate(v.handle, C.CString(text), &va)
if rc != C.VARNAM_SUCCESS {
errorCode := (int)(rc)
return nil, &VarnamError{errorCode: errorCode, message: v.getVarnamError(errorCode)}
}
var (
i C.int
words []string
)
for i = 0; i < C.varray_length(va); i++ {
word := (*C.vword)(C.varray_get(va, i))
words = append(words, C.GoString(word.text))
}
return words, nil
}
// ReverseTransliterate given string.
func (v *Varnam) ReverseTransliterate(text string) (string, error) {
var output *C.char
rc := C.varnam_reverse_transliterate(v.handle, C.CString(text), &output)
if rc != C.VARNAM_SUCCESS {
errorCode := (int)(rc)
return "", &VarnamError{errorCode: errorCode, message: v.getVarnamError(errorCode)}
}
return C.GoString(output), nil
}
// LearnFromFile learns from file from the given filepath.
func (v *Varnam) LearnFromFile(filePath string) (*LearnStatus, error) {
var status C.vlearn_status
rc := C.varnam_learn_from_file(v.handle, C.CString(filePath), &status, nil, nil)
if rc != C.VARNAM_SUCCESS {
errorCode := (int)(rc)
return nil, &VarnamError{errorCode: errorCode, message: v.getVarnamError(errorCode)}
}
return &LearnStatus{TotalWords: int(status.total_words), Failed: int(status.failed)}, nil
}
// Init initializes varnam bindings.
func Init(schemeIdentifier string) (*Varnam, error) {
var (
v *C.varnam
msg *C.char
)
rc := C.varnam_init_from_id(C.CString(schemeIdentifier), &v, &msg)
if rc != C.VARNAM_SUCCESS {
return nil, &VarnamError{errorCode: (int)(rc), message: C.GoString(msg)}
}
return &Varnam{handle: v}, nil
}
// GetAllSchemeDetails returns all scheme related details.
func GetAllSchemeDetails() []*SchemeDetails {
allHandles := C.varnam_get_all_handles()
if allHandles == nil {
return []*SchemeDetails{}
}
var schemeDetails []*SchemeDetails
length := int(C.varray_length(allHandles))
for i := 0; i < length; i++ {
var detail *C.vscheme_details
handle := (*C.varnam)(C.varray_get(allHandles, C.int(i)))
rc := C.varnam_get_scheme_details(handle, &detail)
if rc != C.VARNAM_SUCCESS {
return []*SchemeDetails{}
}
schemeDetails = append(schemeDetails, &SchemeDetails{
LangCode: C.GoString(detail.langCode), Identifier: C.GoString(detail.identifier),
DisplayName: C.GoString(detail.displayName), Author: C.GoString(detail.author),
CompiledDate: C.GoString(detail.compiledDate), IsStable: detail.isStable > 0})
C.varnam_destroy(handle)
}
return schemeDetails
}
// Learn from given input text.
func (v *Varnam) Learn(text string) error {
rc := C.varnam_learn(v.handle, C.CString(text))
if rc != 0 {
errorCode := (int)(rc)
return &VarnamError{errorCode: errorCode, message: v.getVarnamError(errorCode)}
}
return nil
}
func (v *Varnam) getVarnamError(errorCode int) string {
errormessage := C.varnam_get_last_error(v.handle)
varnamErrorMsg := C.GoString(errormessage)
return fmt.Sprintf("%d:%s", errorCode, varnamErrorMsg)
}
// Destroy closes handle.
func (v *Varnam) Destroy() {
C.varnam_destroy(v.handle)
}
// Train methods adds a word with pattern, eg: pattern=firefox, word=ഫയർഫോക്സ്
func (v *Varnam) Train(pattern, word string) error {
rc := C.varnam_train(v.handle, C.CString(pattern), C.CString(word))
if rc != 0 {
errorCode := (int)(rc)
return &VarnamError{errorCode: errorCode, message: v.getVarnamError(errorCode)}
}
return nil
}