1
0
mirror of https://github.com/fumiama/jieba.git synced 2026-06-08 02:00:24 +08:00

merge trie.go into jieba.go

This commit is contained in:
Wang Bin
2015-03-28 12:14:11 +08:00
parent 45c7854fac
commit e11060513c
2 changed files with 35 additions and 36 deletions

View File

@@ -48,6 +48,41 @@ func (rs routes) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
}
type Jieba struct {
Total float64
Freq map[string]float64
}
func (j *Jieba) AddEntry(entry *Entry) {
j.Add(entry.Word, entry.Freq)
}
func (j *Jieba) Add(word string, freq float64) {
j.Freq[word] = freq
j.Total += freq
runes := []rune(word)
for i := 0; i < len(runes); i++ {
frag := string(runes[0 : i+1])
if _, ok := j.Freq[frag]; !ok {
j.Freq[frag] = 0.0
}
}
}
// Load user specified dictionary file.
func (j *Jieba) LoadUserDict(dictFilePath string) error {
return LoadDict(j, dictFilePath, false)
}
// Set the dictionary, could be absolute path of dictionary file, or dictionary
// name in current directory. This function must be called before cut any
// sentence.
func NewJieba(dictFileName string) (*Jieba, error) {
j := &Jieba{Total: 0.0, Freq: make(map[string]float64)}
err := SetDict(j, dictFileName, false)
return j, err
}
// Build a directed acyclic graph (DAG) for sentence.
func (j *Jieba) DAG(sentence string) map[int][]int {
dag := make(map[int][]int)

36
trie.go
View File

@@ -1,36 +0,0 @@
package jiebago
type Jieba struct {
Total float64
Freq map[string]float64
}
func (j *Jieba) AddEntry(entry *Entry) {
j.Add(entry.Word, entry.Freq)
}
func (j *Jieba) Add(word string, freq float64) {
j.Freq[word] = freq
j.Total += freq
runes := []rune(word)
for i := 0; i < len(runes); i++ {
frag := string(runes[0 : i+1])
if _, ok := j.Freq[frag]; !ok {
j.Freq[frag] = 0.0
}
}
}
// Load user specified dictionary file.
func (j *Jieba) LoadUserDict(dictFilePath string) error {
return LoadDict(j, dictFilePath, false)
}
// Set the dictionary, could be absolute path of dictionary file, or dictionary
// name in current directory. This function must be called before cut any
// sentence.
func NewJieba(dictFileName string) (*Jieba, error) {
j := &Jieba{Total: 0.0, Freq: make(map[string]float64)}
err := SetDict(j, dictFileName, false)
return j, err
}