diff --git a/jieba.go b/jieba.go index fbc2e83..fe9b2cc 100644 --- a/jieba.go +++ b/jieba.go @@ -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) diff --git a/trie.go b/trie.go deleted file mode 100644 index 0477455..0000000 --- a/trie.go +++ /dev/null @@ -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 -}