Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dveselov committed Sep 10, 2016
1 parent b9958bf commit 939ba94
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
77 changes: 77 additions & 0 deletions mystem.go
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())
}
34 changes: 34 additions & 0 deletions mystem.h
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);

0 comments on commit 939ba94

Please sign in to comment.