forked from crackcell/gonlpir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gonlpir.go
151 lines (122 loc) · 3.11 KB
/
gonlpir.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
/***************************************************************
*
* Copyright (c) 2015, Menglong TAN <[email protected]>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GPL licence
*
**************************************************************/
/**
*
*
* @file gonlpir.go
* @author Menglong TAN <[email protected]>
* @date Sat Mar 7 13:36:13 2015
*
**/
package gonlpir
/*
#cgo linux CFLAGS: -DOS_LINUX -I${SRCDIR}/deps/include
#cgo linux LDFLAGS: ${SRCDIR}/deps/lib/linux64/libNLPIR.so
#include <stdio.h>
#include <stdlib.h>
#include <gonlpir.h>
*/
import "C"
import "unsafe"
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
const (
GBK = iota // GBK简体中文
UTF8 // UTF8简体中文
BIG5 // 台湾大5码
GBK_FANTI // GBK繁体中文
UTF8_FANTI // UTF8繁体中文
)
type NLPIR struct {
init bool
dataPath string
encoding int
licence string
}
type Result struct {
Word string
Spos string
Ipos int
WordId int
WordType int
Weight int
}
func NewResult() *Result {
p := &Result{}
return p
}
func NewNLPIR(encoding int, licence string) (*NLPIR, error) {
dataPath := func() string {
_, thisSourceFilePath, _, _ := runtime.Caller(0)
return filepath.Dir(thisSourceFilePath)
} () + "/deps"
previousCurrentDir, _ := filepath.Abs(".")
os.Chdir("/tmp")
defer os.Chdir(previousCurrentDir)
p := &NLPIR{}
p.dataPath = dataPath
p.encoding = encoding
p.licence = licence
d := C.CString(dataPath)
defer C.free(unsafe.Pointer(d))
l := C.CString(licence)
defer C.free(unsafe.Pointer(l))
if ret := int(C.NLPIR_Init(d, C.int(encoding), l)); ret == 0 {
return nil, fmt.Errorf("init failed: " + C.GoString(C.NLPIR_GetLastErrorMsg()))
}
global_init = true
return p, nil
}
var global_init bool = false
func (this *NLPIR) Exit() {
if global_init {
C.NLPIR_Exit()
}
global_init = false
}
func (this *NLPIR) ParagraphProcess(paragraph string, needPosTagged bool) string {
cs := C.CString(paragraph)
defer C.free(unsafe.Pointer(cs))
r := C.NLPIR_ParagraphProcess(cs, C.int(BoolToInt(needPosTagged)))
return C.GoString(r)
}
func (this *NLPIR) ParagraphProcessA(paragraph string, useUserDict bool) []*Result {
cs := C.CString(paragraph)
defer C.free(unsafe.Pointer(cs))
n := C.int(0)
p := C.NLPIR_ParagraphProcessA(cs, &n, C.int(BoolToInt(useUserDict)))
r := (*[1 << 30]C.struct_Result)(unsafe.Pointer(p))[:n:n]
b := []byte(paragraph)
results := []*Result{}
for i := 0; i < len(r); i++ {
res := NewResult()
res.Word = string(b[r[i].start : r[i].start+r[i].length])
for j := 0; j < len(r[i].sPOS); j++ {
if r[i].sPOS[j] == 0 {
continue
}
res.Spos += string(r[i].sPOS[j])
}
res.Ipos = int(r[i].iPOS)
res.WordId = int(r[i].word_ID)
res.WordType = int(r[i].word_type)
res.Weight = int(r[i].weight)
results = append(results, res)
}
return results
}
func (this *NLPIR) ImportUserDict(fileName string, isOverwrite bool) {
cs := C.CString(fileName)
defer C.free(unsafe.Pointer(cs))
C.NLPIR_ImportUserDict(cs, C.int(BoolToInt(isOverwrite)))
}