Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions segmenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ func (seg *Segmenter) Dictionary() *Dictionary {
// 分词文本 频率 词性
func (seg *Segmenter) LoadDictionary(files string) {
seg.dict = NewDictionary()
for _, file := range strings.Split(files, ",") {
log.Printf("载入sego词典 %s", file)
dictFile, err := os.Open(file)

// 处理字典文件
processDictFile := func(fileName string) {
dictFile, err := os.Open(fileName)
defer dictFile.Close()
if err != nil {
log.Fatalf("无法载入字典文件 \"%s\" \n", file)
log.Fatalf("无法载入字典文件 \"%s\" \n", fileName)
}

reader := bufio.NewReader(dictFile)
Expand Down Expand Up @@ -91,6 +92,12 @@ func (seg *Segmenter) LoadDictionary(files string) {
}
}

for _, file := range strings.Split(files, ",") {
log.Printf("载入sego词典 %s", file)
// 处理多个字典文件时,避免file对象生命周期延长的问题
processDictFile(file)
}

// 计算每个分词的路径值,路径值含义见Token结构体的注释
logTotalFrequency := float32(math.Log2(float64(seg.dict.totalFrequency)))
for i := range seg.dict.tokens {
Expand Down