1
0
mirror of https://github.com/fumiama/jieba.git synced 2026-06-09 11:00:23 +08:00
This commit is contained in:
源文雨
2022-11-30 12:18:15 +08:00
parent ab8b95ef87
commit 8bbc755ed4
48 changed files with 984 additions and 859 deletions

53
example_test.go Normal file → Executable file
View File

@@ -1,33 +1,24 @@
package jiebago_test
package jiebago
import (
"fmt"
"github.com/wangbin/jiebago"
)
func Example() {
var seg jiebago.Segmenter
var seg Segmenter
seg.LoadDictionary("dict.txt")
print := func(ch <-chan string) {
for word := range ch {
fmt.Printf(" %s /", word)
}
fmt.Println()
}
fmt.Print("【全模式】:")
print(seg.CutAll("我来到北京清华大学"))
fmt.Println(seg.CutAll("我来到北京清华大学"))
fmt.Print("【精确模式】:")
print(seg.Cut("我来到北京清华大学", false))
fmt.Println(seg.Cut("我来到北京清华大学", false))
fmt.Print("【新词识别】:")
print(seg.Cut("他来到了网易杭研大厦", true))
fmt.Println(seg.Cut("他来到了网易杭研大厦", true))
fmt.Print("【搜索引擎模式】:")
print(seg.CutForSearch("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", true))
fmt.Println(seg.CutForSearch("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", true))
// Output:
// 【全模式】: 我 / 来到 / 北京 / 清华 / 清华大学 / 华大 / 大学 /
// 【精确模式】: 我 / 来到 / 北京 / 清华大学 /
@@ -36,47 +27,41 @@ func Example() {
}
func Example_suggestFrequency() {
var seg jiebago.Segmenter
var seg Segmenter
seg.LoadDictionary("dict.txt")
print := func(ch <-chan string) {
for word := range ch {
fmt.Printf(" %s /", word)
}
fmt.Println()
}
sentence := "超敏C反应蛋白是什么"
fmt.Print("Before:")
print(seg.Cut(sentence, false))
fmt.Println(seg.Cut(sentence, false))
word := "超敏C反应蛋白"
oldFrequency, _ := seg.Frequency(word)
frequency := seg.SuggestFrequency(word)
fmt.Printf("%s current frequency: %f, suggest: %f.\n", word, oldFrequency, frequency)
seg.AddWord(word, frequency)
fmt.Print("After:")
print(seg.Cut(sentence, false))
fmt.Println(seg.Cut(sentence, false))
sentence = "如果放到post中将出错"
fmt.Print("Before:")
print(seg.Cut(sentence, false))
fmt.Println(seg.Cut(sentence, false))
word = "中将"
oldFrequency, _ = seg.Frequency(word)
frequency = seg.SuggestFrequency("中", "将")
fmt.Printf("%s current frequency: %f, suggest: %f.\n", word, oldFrequency, frequency)
seg.AddWord(word, frequency)
fmt.Print("After:")
print(seg.Cut(sentence, false))
fmt.Println(seg.Cut(sentence, false))
sentence = "今天天气不错"
fmt.Print("Before:")
print(seg.Cut(sentence, false))
fmt.Println(seg.Cut(sentence, false))
word = "今天天气"
oldFrequency, _ = seg.Frequency(word)
frequency = seg.SuggestFrequency("今天", "天气")
fmt.Printf("%s current frequency: %f, suggest: %f.\n", word, oldFrequency, frequency)
seg.AddWord(word, frequency)
fmt.Print("After:")
print(seg.Cut(sentence, false))
fmt.Println(seg.Cut(sentence, false))
// Output:
// Before: 超敏 / C / 反应 / 蛋白 / 是 / 什么 / /
// 超敏C反应蛋白 current frequency: 0.000000, suggest: 1.000000.
@@ -90,23 +75,17 @@ func Example_suggestFrequency() {
}
func Example_loadUserDictionary() {
var seg jiebago.Segmenter
var seg Segmenter
seg.LoadDictionary("dict.txt")
print := func(ch <-chan string) {
for word := range ch {
fmt.Printf(" %s /", word)
}
fmt.Println()
}
sentence := "李小福是创新办主任也是云计算方面的专家"
fmt.Print("Before:")
print(seg.Cut(sentence, true))
fmt.Println(seg.Cut(sentence, true))
seg.LoadUserDictionary("userdict.txt")
fmt.Print("After:")
print(seg.Cut(sentence, true))
fmt.Println(seg.Cut(sentence, true))
// Output:
// Before: 李小福 / 是 / 创新 / 办 / 主任 / 也 / 是 / 云 / 计算 / 方面 / 的 / 专家 /
// After: 李小福 / 是 / 创新办 / 主任 / 也 / 是 / 云计算 / 方面 / 的 / 专家 /