forked from bountylabs/go-fasttext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasttext.go
145 lines (115 loc) · 2.93 KB
/
fasttext.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
package fasttext
// #cgo CXXFLAGS: -I${SRCDIR}/fasttext/include -I${SRCDIR} -std=c++17 -Ofast -fPIC -pthread -Wno-defaulted-function-deleted
// #include <stdio.h>
// #include <stdlib.h>
// #include <stdint.h>
// #include "predictions.h"
import "C"
import (
"errors"
"runtime"
"unsafe"
)
var (
ErrPredictionFailed = errors.New("prediction failed")
ErrNoPredictions = errors.New("no predictions")
)
// A model object. Effectively a wrapper
// around the C fasttext handle
type (
Model struct {
p C.FastText_Handle_t
}
ModelOpenError string
)
func (e ModelOpenError) Error() string {
return string(e)
}
// Open opens a model from a path and returns a model
// object
func Open(path string) (Model, error) {
var pinner runtime.Pinner
defer pinner.Unpin()
cStrPath := cStr(path)
pinner.Pin(cStrPath)
result := C.FastText_NewHandle(C.FastText_String_t{
data: cStrPath,
size: C.size_t(len(path)),
})
if result.status != 0 {
ch := *(**C.char)(unsafe.Pointer(&result.anon0[0]))
defer C.free(unsafe.Pointer(ch))
return Model{}, ModelOpenError(C.GoString(ch))
}
handle := *(*C.FastText_Handle_t)(unsafe.Pointer(&result.anon0[0]))
return Model{
p: handle,
}, nil
}
// Closes a model handle
func (handle *Model) Close() error {
C.FastText_DeleteHandle(handle.p)
return nil
}
func (handle *Model) MultiLinePredict(lines []string, k int32, threshoad float32) ([]Predictions, error) {
predics := make([]Predictions, 0, len(lines))
for _, line := range lines {
predictions, err := handle.Predict(line, k, threshoad)
if err != nil && errors.Is(err, ErrPredictionFailed) {
return nil, err
}
predics = append(predics, predictions)
}
if len(predics) == 0 {
return nil, ErrNoPredictions
}
return predics, nil
}
// Perform model prediction
func (handle *Model) Predict(query string, k int32, threshoad float32) (Predictions, error) {
var pinner runtime.Pinner
defer pinner.Unpin()
inputs := make([]C.FastText_PredictItem_t, k)
inputsPtr := unsafe.SliceData(inputs)
pinner.Pin(inputsPtr)
count := C.FastText_Predict(
handle.p,
C.FastText_String_t{
data: cStr(query),
size: C.size_t(len(query)),
},
C.uint32_t(k),
C.float(threshoad),
inputsPtr,
)
if count == 0 {
return nil, ErrNoPredictions
}
predictions := make(Predictions, k)
for i := int32(0); i < k; i++ {
str := C.GoStringN(inputs[i].lang.data, C.int(inputs[i].lang.size))
predictions[i] = Prediction{
Label: str,
Probability: float32(inputs[i].probability),
}
}
return predictions, nil
}
func (handle Model) Wordvec(word string) []float32 {
var pinner runtime.Pinner
defer pinner.Unpin()
strData := cStr(word)
pinner.Pin(strData)
r := C.FastText_Wordvec(
handle.p,
C.FastText_String_t{
data: strData,
size: C.size_t(len(word)),
},
)
defer C.FastText_FreeFloatVector(r)
vectors := make([]float32, r.size)
ptr := (*float32)(unsafe.Pointer(r.data))
copy(vectors, unsafe.Slice(ptr, r.size))
return vectors
}