-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmystem.go
174 lines (143 loc) · 3.72 KB
/
mystem.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
package mystem
/*
#cgo LDFLAGS: -lmystem_c_binding
#include "mystem.h"
char* get_flex_gram_by_id(char** grammemes, int id) {
return grammemes[id];
};
*/
import "C"
import (
"encoding/binary"
"unsafe"
)
func stringToSymbols(word string) []C.ushort {
var (
symbols []C.ushort
)
for _, char := range word {
symbols = append(symbols, C.ushort(char))
}
return symbols
}
func symbolsToString(symbols *C.TSymbol, length C.int) string {
var (
runes []rune
)
bytes := C.GoBytes(unsafe.Pointer(symbols), length*2)
for len(bytes) != 0 {
letter := binary.LittleEndian.Uint16(bytes[:2])
runes = append(runes, rune(letter))
bytes = bytes[2:]
}
return string(runes)
}
func decodeGrammemes(grammemes []byte) []int {
var (
result []int
)
for _, grammeme := range grammemes {
result = append(result, int(grammeme))
}
return result
}
func decodeGrammemesArray(grams **C.char, count int) [][]int {
var (
grammemes [][]int
)
for i := 0; i < count; i++ {
currentRawGramSet := []byte(C.GoString(C.get_flex_gram_by_id(grams, C.int(i))))
grammemes = append(grammemes, decodeGrammemes(currentRawGramSet))
}
return grammemes
}
type Analyses struct {
handle unsafe.Pointer
}
func NewAnalyses(word string) *Analyses {
cWord := stringToSymbols(word)
analyses := new(Analyses)
analyses.handle = C.MystemAnalyze((*C.TSymbol)(unsafe.Pointer(&cWord[0])), C.int(len(cWord)))
return analyses
}
func (analyses *Analyses) Count() int {
length := C.MystemAnalysesCount(analyses.handle)
return (int)(length)
}
func (analyses *Analyses) Close() {
C.MystemDeleteAnalyses(analyses.handle)
}
func (analyses *Analyses) GetLemma(i int) *Lemma {
lemma := new(Lemma)
handle := C.MystemLemma(unsafe.Pointer(analyses.handle), (C.int)(i))
lemma.handle = handle
return lemma
}
type Lemma struct {
handle unsafe.Pointer
}
func (lemma *Lemma) TextLength() C.int {
return C.MystemLemmaTextLen(lemma.handle)
}
func (lemma *Lemma) Text() string {
return symbolsToString(C.MystemLemmaText(lemma.handle), lemma.TextLength())
}
func (lemma *Lemma) FormLength() C.int {
return C.MystemLemmaFormLen(lemma.handle)
}
func (lemma *Lemma) Form() string {
return symbolsToString(C.MystemLemmaForm(lemma.handle), lemma.FormLength())
}
func (lemma *Lemma) Quality() int {
return int(C.MystemLemmaQuality(lemma.handle))
}
func (lemma *Lemma) StemGram() []int {
rawGrammemes := []byte(C.GoString(C.MystemLemmaStemGram(lemma.handle)))
return decodeGrammemes(rawGrammemes)
}
func (lemma *Lemma) FlexGramNum() int {
return int(C.MystemLemmaFlexGramNum(lemma.handle))
}
func (lemma *Lemma) FlexGram() [][]int {
rawGram := C.MystemLemmaFlexGram(lemma.handle)
return decodeGrammemesArray(rawGram, lemma.FlexGramNum())
}
func (lemma *Lemma) GenerateForms() *Forms {
forms := new(Forms)
forms.handle = C.MystemGenerate(lemma.handle)
return forms
}
type Forms struct {
handle unsafe.Pointer
}
type Form struct {
handle unsafe.Pointer
}
func (forms *Forms) Count() int {
return int(C.MystemFormsCount(forms.handle))
}
func (forms *Forms) Close() {
C.MystemDeleteForms(forms.handle)
}
func (forms *Forms) Get(id int) *Form {
form := new(Form)
form.handle = C.MystemForm(forms.handle, C.int(id))
return form
}
func (form *Form) TextLength() C.int {
return C.MystemFormTextLen(form.handle)
}
func (form *Form) Text() string {
return symbolsToString(C.MystemFormText(form.handle), form.TextLength())
}
func (form *Form) StemGram() []int {
rawGrammemes := []byte(C.GoString(C.MystemFormStemGram(form.handle)))
return decodeGrammemes(rawGrammemes)
}
func (form *Form) FlexGramNum() int {
return int(C.MystemFormFlexGramNum(form.handle))
}
func (form *Form) FlexGram() [][]int {
rawGram := C.MystemFormFlexGram(form.handle)
return decodeGrammemesArray(rawGram, form.FlexGramNum())
}