Skip to content

add show pos for word #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Dictionary struct {
total, logTotal float64
freqMap map[string]float64
posMap map[string]string
sync.RWMutex
}

Expand Down Expand Up @@ -43,6 +44,8 @@ func (d *Dictionary) addToken(token dictionary.Token) {
d.freqMap[frag] = 0.0
}
}

d.posMap[token.Text()] = token.Pos()
}

func (d *Dictionary) updateLogTotal() {
Expand All @@ -57,6 +60,13 @@ func (d *Dictionary) Frequency(key string) (float64, bool) {
return freq, ok
}

func (d *Dictionary) Pos(key string) (string, bool) {
d.RLock()
pos, ok := d.posMap[key]
d.RUnlock()
return pos, ok
}

func (d *Dictionary) loadDictionary(fileName string) error {
return dictionary.LoadDictionary(d, fileName)
}
7 changes: 6 additions & 1 deletion jieba.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (seg *Segmenter) Frequency(word string) (float64, bool) {
return seg.dict.Frequency(word)
}

// Pos returns a word's part_of_speech and existence
func (seg *Segmenter) Pos(word string) (string, bool) {
return seg.dict.Pos(word)
}

// AddWord adds a new word with frequency to dictionary
func (seg *Segmenter) AddWord(word string, frequency float64) {
seg.dict.AddToken(dictionary.NewToken(word, frequency, ""))
Expand Down Expand Up @@ -94,7 +99,7 @@ func (seg *Segmenter) SuggestFrequency(words ...string) float64 {
// LoadDictionary loads dictionary from given file name. Everytime
// LoadDictionary is called, previously loaded dictionary will be cleard.
func (seg *Segmenter) LoadDictionary(fileName string) error {
seg.dict = &Dictionary{freqMap: make(map[string]float64)}
seg.dict = &Dictionary{freqMap: make(map[string]float64), posMap: make(map[string]string)}
return seg.dict.loadDictionary(fileName)
}

Expand Down
Loading