-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package mystem | ||
|
||
/* | ||
#cgo LDFLAGS: -lmystem_c_binding | ||
#include "mystem.h" | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
import "encoding/binary" | ||
|
||
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) | ||
} | ||
|
||
type Analyses struct { | ||
handle unsafe.Pointer | ||
} | ||
|
||
type Lemma struct { | ||
handle unsafe.Pointer | ||
} | ||
|
||
func NewAnalyses(word string) *Analyses { | ||
cWord := StringToSymbols(word) | ||
cWordLength := len(cWord) | ||
analyses := new(Analyses) | ||
handle := C.MystemAnalyze((*C.TSymbol)(unsafe.Pointer(&cWord[0])), C.int(cWordLength)) | ||
analyses.handle = handle | ||
return analyses | ||
} | ||
|
||
func (analyses *Analyses) Length() 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 | ||
} | ||
|
||
func (lemma *Lemma) TextLength() C.int { | ||
return C.MystemLemmaTextLen(lemma.handle) | ||
} | ||
|
||
func (lemma *Lemma) Text() string { | ||
return SymbolsToString(C.MystemLemmaText(lemma.handle), lemma.TextLength()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
typedef void MystemAnalysesHandle; | ||
typedef void MystemLemmaHandle; | ||
typedef void MystemFormsHandle; | ||
typedef void MystemFormHandle; | ||
typedef unsigned short int TSymbol; | ||
|
||
MystemAnalysesHandle* MystemAnalyze(TSymbol* word, int len); | ||
void MystemDeleteAnalyses(MystemAnalysesHandle* analyses); | ||
int MystemAnalysesCount(MystemAnalysesHandle* analyses); | ||
|
||
MystemLemmaHandle* MystemLemma(MystemAnalysesHandle* analyses, int i); | ||
|
||
TSymbol* MystemLemmaText(MystemLemmaHandle* lemma); | ||
int MystemLemmaTextLen(MystemLemmaHandle* lemma); | ||
TSymbol* MystemLemmaForm(MystemLemmaHandle* lemma); | ||
int MystemLemmaFormLen(MystemLemmaHandle* lemma); | ||
int MystemLemmaQuality(MystemLemmaHandle* lemma); | ||
char* MystemLemmaStemGram(MystemLemmaHandle* lemma); | ||
char** MystemLemmaFlexGram(MystemLemmaHandle* lemma); | ||
int MystemLemmaFlexGramNum(MystemLemmaHandle* lemma); | ||
int MystemLemmaFlexLen(MystemLemmaHandle* lemma); | ||
int MystemLemmaRuleId(MystemLemmaHandle* lemma); | ||
|
||
MystemFormsHandle* MystemGenerate(MystemLemmaHandle* lemma); | ||
void MystemDeleteForms(MystemFormsHandle* forms); | ||
int MystemFormsCount(MystemFormsHandle* forms); | ||
|
||
MystemFormHandle* MystemForm(MystemFormsHandle* forms, int i); | ||
|
||
TSymbol* MystemFormText(MystemFormHandle* form); | ||
int MystemFormTextLen(MystemFormHandle* form); | ||
char* MystemFormStemGram(MystemFormHandle* form); | ||
char** MystemFormFlexGram(MystemFormHandle* form); | ||
int MystemFormFlexGramNum(MystemFormHandle* form); |