diff --git a/analyse/idf.go b/analyse/idf.go index c054f5e..37e50eb 100644 --- a/analyse/idf.go +++ b/analyse/idf.go @@ -38,7 +38,7 @@ func (i *Idf) loadDictionary(fileName string) error { return dictionary.LoadDictionary(i, fileName) } -func (i Idf) Frequency(key string) (float64, bool) { +func (i *Idf) Frequency(key string) (float64, bool) { i.RLock() freq, ok := i.freqMap[key] i.RUnlock() diff --git a/analyse/stopwords.go b/analyse/stopwords.go index ac2f598..81012f7 100644 --- a/analyse/stopwords.go +++ b/analyse/stopwords.go @@ -57,7 +57,7 @@ func NewStopWord() *StopWord { return s } -func (s StopWord) IsStopWord(word string) bool { +func (s *StopWord) IsStopWord(word string) bool { s.RLock() _, ok := s.stopWordMap[word] s.RUnlock() diff --git a/dictionary.go b/dictionary.go index 2cc8bb6..d8805b8 100644 --- a/dictionary.go +++ b/dictionary.go @@ -7,12 +7,14 @@ import ( "github.com/wangbin/jiebago/dictionary" ) +// A Dictionary represents a thread-safe dictionary used for word segmentation. type Dictionary struct { total, logTotal float64 freqMap map[string]float64 sync.RWMutex } +// Load loads all tokens from channel func (d *Dictionary) Load(ch <-chan dictionary.Token) { d.Lock() for token := range ch { @@ -22,6 +24,7 @@ func (d *Dictionary) Load(ch <-chan dictionary.Token) { d.updateLogTotal() } +// AddToken adds one token func (d *Dictionary) AddToken(token dictionary.Token) { d.Lock() d.addToken(token) @@ -46,7 +49,8 @@ func (d *Dictionary) updateLogTotal() { d.logTotal = math.Log(d.total) } -func (d Dictionary) Frequency(key string) (float64, bool) { +// Frequency returns the frequency of give word, if not found, the second result is false +func (d *Dictionary) Frequency(key string) (float64, bool) { d.RLock() freq, ok := d.freqMap[key] d.RUnlock() diff --git a/posseg/dictionary.go b/posseg/dictionary.go index 2618464..e4fee3b 100644 --- a/posseg/dictionary.go +++ b/posseg/dictionary.go @@ -50,14 +50,14 @@ func (d *Dictionary) updateLogTotal() { d.logTotal = math.Log(d.total) } -func (d Dictionary) Frequency(key string) (float64, bool) { +func (d *Dictionary) Frequency(key string) (float64, bool) { d.RLock() freq, ok := d.freqMap[key] d.RUnlock() return freq, ok } -func (d Dictionary) Pos(key string) (string, bool) { +func (d *Dictionary) Pos(key string) (string, bool) { d.RLock() pos, ok := d.posMap[key] d.RUnlock()