From 94b0293019880708d30961b145eb939d58cf8a87 Mon Sep 17 00:00:00 2001 From: yanyiwu Date: Mon, 4 Apr 2016 10:38:51 +0800 Subject: [PATCH] get dict path by env var GOPATH --- README.md | 8 ++------ README_EN.md | 3 +-- example/bleve/bleve_gojieba_demo.go | 14 +++++++++++--- example/demo.go | 16 ++++++++++------ 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 1b61f53..003f371 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,9 @@ go get github.com/yanyiwu/gojieba See Details in [example/demo.go](example/demo.go) ``` -cd example -go run demo.go +go run example/demo.go ``` -之所以需要先 cd 到 example 目录下,是因为 demo.go 里面有写死的字典相对路径。 - 输出结果: ``` @@ -54,8 +51,7 @@ DemoExtract ## Bleve 中文分词插件用法 ``` -cd example/bleve -go run bleve_gojieba_demo.go +go run example/bleve/bleve_gojieba_demo.go ``` See Details in [example/bleve/bleve_gojieba_demo.go](example/bleve/bleve_gojieba_demo.go) diff --git a/README_EN.md b/README_EN.md index dd4eb7b..9eb1330 100644 --- a/README_EN.md +++ b/README_EN.md @@ -21,8 +21,7 @@ go get github.com/yanyiwu/gojieba See example in [example/demo.go](example/demo.go) ``` -cd example -go run demo.go +go run example/demo.go ``` Output Result: diff --git a/example/bleve/bleve_gojieba_demo.go b/example/bleve/bleve_gojieba_demo.go index dae06d4..e131c91 100644 --- a/example/bleve/bleve_gojieba_demo.go +++ b/example/bleve/bleve_gojieba_demo.go @@ -4,11 +4,19 @@ import ( "encoding/json" "fmt" "os" + "path" "github.com/blevesearch/bleve" _ "github.com/yanyiwu/gojieba/bleve" ) +var ( + DICT_DIR = path.Join(os.Getenv("GOPATH"), "src/github.com/yanyiwu/gojieba/dict") + DICT_PATH = path.Join(DICT_DIR, "jieba.dict.utf8") + HMM_PATH = path.Join(DICT_DIR, "hmm_model.utf8") + USER_DICT_PATH = path.Join(DICT_DIR, "user.dict.utf8") +) + func main() { messages := []struct { Id string @@ -38,9 +46,9 @@ func main() { err := indexMapping.AddCustomTokenizer("gojieba", map[string]interface{}{ - "dictpath": "../../dict/jieba.dict.utf8", - "hmmpath": "../../dict/hmm_model.utf8", - "userdictpath": "../../dict/user.dict.utf8", + "dictpath": DICT_PATH, + "hmmpath": HMM_PATH, + "userdictpath": USER_DICT_PATH, "type": "gojieba", }, ) diff --git a/example/demo.go b/example/demo.go index 5b9719a..fb0920e 100644 --- a/example/demo.go +++ b/example/demo.go @@ -2,20 +2,24 @@ package main import ( "fmt" + "os" + "path" "strings" "github.com/yanyiwu/gojieba" ) -const ( - DICT_PATH = "../dict/jieba.dict.utf8" - HMM_PATH = "../dict/hmm_model.utf8" - USER_DICT_PATH = "../dict/user.dict.utf8" - IDF_PATH = "../dict/idf.utf8" - STOP_WORDS_PATH = "../dict/stop_words.utf8" +var ( + DICT_DIR = path.Join(os.Getenv("GOPATH"), "src/github.com/yanyiwu/gojieba/dict") + DICT_PATH = path.Join(DICT_DIR, "jieba.dict.utf8") + HMM_PATH = path.Join(DICT_DIR, "hmm_model.utf8") + USER_DICT_PATH = path.Join(DICT_DIR, "user.dict.utf8") + IDF_PATH = path.Join(DICT_DIR, "idf.utf8") + STOP_WORDS_PATH = path.Join(DICT_DIR, "stop_words.utf8") ) func DemoJieba() { + println(DICT_DIR) fmt.Println("DemoJieba") var words []string use_hmm := true