mindoc/vendor/github.com/huichen/sego/utils.go
2017-04-21 18:20:35 +08:00

94 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sego
import (
"bytes"
"fmt"
)
// 输出分词结果为字符串
//
// 有两种输出模式,以"中华人民共和国"为例
//
// 普通模式searchMode=false输出一个分词"中华人民共和国/ns "
// 搜索模式searchMode=true 输出普通模式的再细致切分:
// "中华/nz 人民/n 共和/nz 共和国/ns 人民共和国/nt 中华人民共和国/ns "
//
// 搜索模式主要用于给搜索引擎提供尽可能多的关键字详情请见Token结构体的注释。
func SegmentsToString(segs []Segment, searchMode bool) (output string) {
if searchMode {
for _, seg := range segs {
output += tokenToString(seg.token)
}
} else {
for _, seg := range segs {
output += fmt.Sprintf(
"%s/%s ", textSliceToString(seg.token.text), seg.token.pos)
}
}
return
}
func tokenToString(token *Token) (output string) {
for _, s := range token.segments {
output += tokenToString(s.token)
}
output += fmt.Sprintf("%s/%s ", textSliceToString(token.text), token.pos)
return
}
// 输出分词结果到一个字符串slice
//
// 有两种输出模式,以"中华人民共和国"为例
//
// 普通模式searchMode=false输出一个分词"[中华人民共和国]"
// 搜索模式searchMode=true 输出普通模式的再细致切分:
// "[中华 人民 共和 共和国 人民共和国 中华人民共和国]"
//
// 搜索模式主要用于给搜索引擎提供尽可能多的关键字详情请见Token结构体的注释。
func SegmentsToSlice(segs []Segment, searchMode bool) (output []string) {
if searchMode {
for _, seg := range segs {
output = append(output, tokenToSlice(seg.token)...)
}
} else {
for _, seg := range segs {
output = append(output, seg.token.Text())
}
}
return
}
func tokenToSlice(token *Token) (output []string) {
for _, s := range token.segments {
output = append(output, tokenToSlice(s.token)...)
}
output = append(output, textSliceToString(token.text))
return output
}
// 将多个字元拼接一个字符串输出
func textSliceToString(text []Text) string {
var output string
for _, word := range text {
output += string(word)
}
return output
}
// 返回多个字元的字节总长度
func textSliceByteLength(text []Text) (length int) {
for _, word := range text {
length += len(word)
}
return
}
func textSliceToBytes(text []Text) []byte {
var buf bytes.Buffer
for _, word := range text {
buf.Write(word)
}
return buf.Bytes()
}