Skip to content

Commit

Permalink
NewJieba(...string)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyiwu committed Apr 14, 2016
1 parent df4bc42 commit c00c9e7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## next version

+ NewJieba(...string) support variable arguments
+ removed example/ and write Example in `*_test.go`
+ add some kind of Benchmark, Testing

## v0.12.0

Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions extractor_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package gojieba

import (
"fmt"
"strings"
"testing"
)

func ExampleExtract() {
x := NewExtractor(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORDS_PATH)
defer x.Free()

s := "我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。"
words := x.Extract(s, 5)
fmt.Println(s)
fmt.Println("关键词抽取:", strings.Join(words, "/"))

// Output:
// 我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。
// 关键词抽取: CEO/升职/加薪/手扶拖拉机/巅峰
}

func TestExtractor(t *testing.T) {
x := NewExtractor("./dict/jieba.dict.utf8", "./dict/hmm_model.utf8", "./dict/user.dict.utf8", "./dict/idf.utf8", "./dict/stop_words.utf8")
defer x.Free()
Expand Down
16 changes: 14 additions & 2 deletions jieba.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,31 @@ type Jieba struct {
jieba C.Jieba
}

func NewJieba(dict_path, hmm_path, user_dict_path string) *Jieba {
func NewJieba(paths ...string) *Jieba {
dict_path, hmm_path, user_dict_path := getDictPaths(paths...)
dpath := C.CString(dict_path)
defer C.free(unsafe.Pointer(dpath))
hpath := C.CString(hmm_path)
defer C.free(unsafe.Pointer(hpath))
upath := C.CString(user_dict_path)
defer C.free(unsafe.Pointer(upath))

return &Jieba{
C.NewJieba(dpath, hpath, upath),
}
}

func getDictPaths(args ...string) (string, string, string) {
dicts := [3]string{
DICT_PATH,
HMM_PATH,
USER_DICT_PATH,
}
for i := 0; i < len(args) && i < len(dicts); i++ {
dicts[i] = args[i]
}
return dicts[0], dicts[1], dicts[2]
}

func (x *Jieba) Free() {
C.FreeJieba(x.jieba)
}
Expand Down
23 changes: 6 additions & 17 deletions jieba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ func ExampleJieba() {
var s string
var words []string
use_hmm := true
x := NewJieba(DICT_PATH, HMM_PATH, USER_DICT_PATH)
//equals with x := NewJieba(DICT_PATH, HMM_PATH, USER_DICT_PATH)
x := NewJieba()
defer x.Free()

s = "我来到北京清华大学"
Expand Down Expand Up @@ -50,22 +51,9 @@ func ExampleJieba() {
// 词性标注: 长春市/ns,长春/ns,药店/n
}

func ExampleExtract() {
x := NewExtractor(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORDS_PATH)
defer x.Free()

s := "我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。"
words := x.Extract(s, 5)
fmt.Println(s)
fmt.Println("关键词抽取:", strings.Join(words, "/"))

// Output:
// 我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。
// 关键词抽取: CEO/升职/加薪/手扶拖拉机/巅峰
}

func TestJieba(t *testing.T) {
x := NewJieba(DICT_PATH, HMM_PATH, USER_DICT_PATH)
//equals with x := NewJieba(DICT_PATH, HMM_PATH, USER_DICT_PATH)
x := NewJieba()
defer x.Free()
var s string
var expected string
Expand Down Expand Up @@ -116,7 +104,8 @@ func TestJieba(t *testing.T) {
}

func BenchmarkJieba(b *testing.B) {
x := NewJieba(DICT_PATH, HMM_PATH, USER_DICT_PATH)
//equals with x := NewJieba(DICT_PATH, HMM_PATH, USER_DICT_PATH)
x := NewJieba()
s := "小明硕士毕业于中国科学院计算所,后在日本京都大学深造"
defer x.Free()
b.ResetTimer()
Expand Down

0 comments on commit c00c9e7

Please sign in to comment.