1
0
mirror of https://github.com/fumiama/jieba.git synced 2026-06-27 15:40:32 +08:00

pass tests & update readme

This commit is contained in:
源文雨
2022-11-30 16:27:07 +08:00
parent 6982ead703
commit 1803c768ee
4 changed files with 56 additions and 60 deletions

View File

@@ -1,19 +1,18 @@
#结巴分词 Go 语言版jieba #结巴分词 Go 语言版jieba
[![GoDoc](https://godoc.org/github.com/fumiama/jieba?status.svg)](https://godoc.org/github.com/fumiama/jieba)
[![Build Status](https://travis-ci.org/wangbin/jieba.png?branch=master)](https://travis-ci.org/wangbin/jieba) [![GoDoc](https://godoc.org/github.com/fumiama/jieba?status.svg)](https://godoc.org/github.com/fumiama/jieba) [结巴分词](https://github.com/fxsjy/jieba) 是由 [@fxsjy](https://github.com/fxsjy) 使用 Python 编写的中文分词组件,本仓库是结巴分词的 Golang 语言实现,修改于[jiebago](https://github.com/wangbin/jiebago),优化了速度与性能,增加了从`fs.File`加载字典等功能。
[结巴分词](https://github.com/fxsjy/jieba) 是由 [@fxsjy](https://github.com/fxsjy) 使用 Python 编写的中文分词组件Iiebago 是结巴分词的 Golang 语言实现。
## 安装
```
go get github.com/fumiama/jieba/...
```
## 使用 ## 使用
```
go get -d github.com/fumiama/jieba
```
## 示例
``` ```
package main package main
@@ -23,53 +22,47 @@ import (
"github.com/fumiama/jieba" "github.com/fumiama/jieba"
) )
var seg jieba.Segmenter func main() {
seg, err := LoadDictionaryAt("dict.txt")
if err != nil {
panic(err)
}
func init() { fmt.Print("【全模式】:")
seg.LoadDictionary("dict.txt") fmt.Println(seg.CutAll("我来到北京清华大学"))
}
func print(ch <-chan string) { fmt.Print("【精确模式】:")
for word := range ch { fmt.Println(seg.Cut("我来到北京清华大学", false))
fmt.Printf(" %s /", word)
}
fmt.Println()
}
func Example() { fmt.Print("【新词识别】:")
fmt.Print("【全模式】:") fmt.Println(seg.Cut("他来到了网易杭研大厦", true))
print(seg.CutAll("我来到北京清华大学"))
fmt.Print("【精确模式】:") fmt.Print("【搜索引擎模式】:")
print(seg.Cut("我来到北京清华大学", false)) fmt.Println(seg.CutForSearch("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", true))
fmt.Print("【新词识别】:")
print(seg.Cut("他来到了网易杭研大厦", true))
fmt.Print("【搜索引擎模式】:")
print(seg.CutForSearch("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", true))
} }
``` ```
输出结果: 输出结果:
``` ```
【全模式】: / 来到 / 北京 / 清华 / 清华大学 / 华大 / 大学 / 【全模式】:[我 来到 北京 清华 清华大学 华大 大学]
【精确模式】:[我 来到 北京 清华大学]
精确模式】: 我 / 来到 / 北京 / 清华大学 / 新词识别】:[他 来到 了 网易 杭研 大厦]
【搜索引擎模式】:[小明 硕士 毕业 于 中国 科学 学院 科学院 中国科学院 计算 计算所 后 在 日本 京都 大学 日本京都大学 深造]
【新词识别】: 他 / 来到 / 了 / 网易 / 杭研 / 大厦 /
【搜索引擎模式】: 小明 / 硕士 / 毕业 / 于 / 中国 / 科学 / 学院 / 科学院 / 中国科学院 / 计算 / 计算所 / / 后 / 在 / 日本 / 京都 / 大学 / 日本京都大学 / 深造 /
``` ```
更多信息请参考[文档](https://godoc.org/github.com/fumiama/jieba)。 更多信息请参考[文档](https://godoc.org/github.com/fumiama/jieba)。
## 分词速度 ## 分词速度
```c
- 2MB / Second in Full Mode goos: darwin
- 700KB / Second in Default Mode goarch: amd64
- Test Env: AMD Phenom(tm) II X6 1055T CPU @ 2.8GHz; 《金庸全集》 pkg: github.com/fumiama/jieba
cpu: Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
## 许可证 BenchmarkCutNoHMM-8 50101 22889 ns/op 4.67 MB/s 24492 B/op 148 allocs/op
BenchmarkCut-8 47473 25152 ns/op 4.25 MB/s 31310 B/op 185 allocs/op
MIT: http://wangbin.mit-license.org BenchmarkCutAll-8 81760 14286 ns/op 7.49 MB/s 22746 B/op 75 allocs/op
BenchmarkCutForSearchNoHMM-8 49009 24371 ns/op 4.39 MB/s 26421 B/op 157 allocs/op
BenchmarkCutForSearch-8 44643 26597 ns/op 4.02 MB/s 33224 B/op 194 allocs/op
PASS
ok github.com/fumiama/jieba 8.769s
```

View File

@@ -32,9 +32,6 @@ func worker() {
} }
func Example_parallelCut() { func Example_parallelCut() {
// Set the number of goroutines
runtime.GOMAXPROCS(numThreads)
// open file for segmentation // open file for segmentation
file, err := os.Open("README.md") file, err := os.Open("README.md")
if err != nil { if err != nil {
@@ -83,6 +80,7 @@ func Example_parallelCut() {
} }
t1 := time.Now() t1 := time.Now()
close(result)
// Write the segments into a file for verify // Write the segments into a file for verify
outputFile, _ := os.OpenFile("parallelCut.log", os.O_CREATE|os.O_WRONLY, 0600) outputFile, _ := os.OpenFile("parallelCut.log", os.O_CREATE|os.O_WRONLY, 0600)

View File

@@ -22,10 +22,10 @@ func Example() {
fmt.Print("【搜索引擎模式】:") fmt.Print("【搜索引擎模式】:")
fmt.Println(seg.CutForSearch("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", true)) fmt.Println(seg.CutForSearch("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", true))
// Output: // Output:
// 【全模式】: / 来到 / 北京 / 清华 / 清华大学 / 华大 / 大学 / // 【全模式】:[我 来到 北京 清华 清华大学 华大 大学]
// 【精确模式】: / 来到 / 北京 / 清华大学 / // 【精确模式】:[来到 北京 清华大学]
// 【新词识别】: 他 / 来到 / 了 / 网易 / 杭研 / 大厦 / // 【新词识别】:[他 来到 了 网易 杭研 大厦]
// 【搜索引擎模式】: 小明 / 硕士 / 毕业 / 于 / 中国 / 科学 / 学院 / 科学院 / 中国科学院 / 计算 / 计算所 / / 后 / 在 / 日本 / 京都 / 大学 / 日本京都大学 / 深造 / // 【搜索引擎模式】:[小明 硕士 毕业 于 中国 科学 学院 科学院 中国科学院 计算 计算所 后 在 日本 京都 大学 日本京都大学 深造]
} }
func Example_suggestFrequency() { func Example_suggestFrequency() {
@@ -67,15 +67,15 @@ func Example_suggestFrequency() {
fmt.Print("After:") fmt.Print("After:")
fmt.Println(seg.Cut(sentence, false)) fmt.Println(seg.Cut(sentence, false))
// Output: // Output:
// Before: 超敏 / C / 反应 / 蛋白 / 是 / 什么 / / // Before:[超敏 C 反应 蛋白 是 什么 ]
// 超敏C反应蛋白 current frequency: 0.000000, suggest: 1.000000. // 超敏C反应蛋白 current frequency: 0.000000, suggest: 1.000000.
// After: 超敏C反应蛋白 / 是 / 什么 / / // After:[超敏C反应蛋白 是 什么 ]
// Before: 如果 / 放到 / post / 中将 / 出错 / // Before:[如果 放到 post 中将 出错]
// 中将 current frequency: 763.000000, suggest: 494.000000. // 中将 current frequency: 763.000000, suggest: 494.000000.
// After: 如果 / 放到 / post // 将 / 出错 / // After:[如果 放到 post 中 出错]
// Before: 今天天气 / 不错 / // Before:[今天天气 不错]
// 今天天气 current frequency: 3.000000, suggest: 0.000000. // 今天天气 current frequency: 3.000000, suggest: 0.000000.
// After: 今天 / 天气 / 不错 / // After:[今天 天气 不错]
} }
func Example_loadUserDictionary() { func Example_loadUserDictionary() {
@@ -93,6 +93,6 @@ func Example_loadUserDictionary() {
fmt.Print("After:") fmt.Print("After:")
fmt.Println(seg.Cut(sentence, true)) fmt.Println(seg.Cut(sentence, true))
// Output: // Output:
// Before: 李小福 / / 创新 / 办 / 主任 / 也 / 是 / 云 / 计算 / 方面 / 的 / 专家 / // Before:[李小福 是 创新 办 主任 也 是 云 计算 方面 专家]
// After: 李小福 / 是 / 创新办 / 主任 / 也 / / 云计算 / 方面 / 的 / 专家 / // After:[李小福 创新办 主任 是 云计算 方面 的 专家]
} }

View File

@@ -791,6 +791,7 @@ func TestLoadUserDictionary(t *testing.T) {
func BenchmarkCutNoHMM(b *testing.B) { func BenchmarkCutNoHMM(b *testing.B) {
sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作" sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
b.SetBytes(int64(len(sentence)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
seg.Cut(sentence, false) seg.Cut(sentence, false)
@@ -799,6 +800,7 @@ func BenchmarkCutNoHMM(b *testing.B) {
func BenchmarkCut(b *testing.B) { func BenchmarkCut(b *testing.B) {
sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作" sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
b.SetBytes(int64(len(sentence)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
seg.Cut(sentence, true) seg.Cut(sentence, true)
@@ -807,6 +809,7 @@ func BenchmarkCut(b *testing.B) {
func BenchmarkCutAll(b *testing.B) { func BenchmarkCutAll(b *testing.B) {
sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作" sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
b.SetBytes(int64(len(sentence)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
seg.CutAll(sentence) seg.CutAll(sentence)
@@ -815,6 +818,7 @@ func BenchmarkCutAll(b *testing.B) {
func BenchmarkCutForSearchNoHMM(b *testing.B) { func BenchmarkCutForSearchNoHMM(b *testing.B) {
sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作" sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
b.SetBytes(int64(len(sentence)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
seg.CutForSearch(sentence, false) seg.CutForSearch(sentence, false)
@@ -823,6 +827,7 @@ func BenchmarkCutForSearchNoHMM(b *testing.B) {
func BenchmarkCutForSearch(b *testing.B) { func BenchmarkCutForSearch(b *testing.B) {
sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作" sentence := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
b.SetBytes(int64(len(sentence)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
seg.CutForSearch(sentence, true) seg.CutForSearch(sentence, true)