summaryrefslogtreecommitdiff
path: root/go/mixed-server.go
blob: 44dfcdc860fe507211417ec67d86b7a4058d90a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
 
import (
"sort"
"sync"
)
 
type wordsByCount struct {
words *map[string]uint
keys  []string
}
 
func newWordsByCount(w *map[string]uint) wordsByCount {
keys := make([]string, len(*w))
i := 0
for w, _ := range *w {
keys[i] = w
i++
}
 
return wordsByCount{words: w, keys: keys}
}
func (wbc wordsByCount) Len() int { return len(*wbc.words) }
func (wbc wordsByCount) Swap(i, j int) {
wbc.keys[i], wbc.keys[j] = wbc.keys[j], wbc.keys[i]
}
func (wbc wordsByCount) Less(i, j int) bool {
return (*wbc.words)[wbc.keys[i]] > (*wbc.words)[wbc.keys[j]]
}
func (wbc wordsByCount) MostCommonWords() []string {
sort.Sort(wbc)
mcw := make([]string, len(*wbc.words))
for i, p := range wbc.keys {
mcw[i] = p
}
return mcw
}
 
type Model struct {
lock  sync.RWMutex
words map[string]uint
}
 
func NewModel() Model {
return Model{words: make(map[string]uint)}
}
 
func (m *Model) AddWords(words []string) {
m.lock.Lock()
defer m.lock.Unlock()
for _, word := range words {
v, _ := m.words[word]
m.words[word] = v + 1
}
}
 
func (m *Model) GetMostCommonPairs(how_many uint) []string {
m.lock.RLock()
defer m.lock.RUnlock()
wbc := newWordsByCount(&m.words)
mcw := wbc.MostCommonWords()
return mcw[:how_many]
}