-
Notifications
You must be signed in to change notification settings - Fork 27
/
freq.go
74 lines (61 loc) · 1.46 KB
/
freq.go
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
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"sort"
"github.com/marianogappa/chart/format"
)
func preprocessFreq(isss [][]string, lineFormat format.LineFormat) ([][]float64, [][]string, format.LineFormat) {
fss := [][]float64{}
sss := [][]string{}
is := make(map[string]int)
fqs := freqs{}
for _, ss := range isss {
if len(ss) == 0 {
break //TODO this probably shouldn't happen
}
if _, ok := is[ss[0]]; !ok {
is[ss[0]] = len(fqs.fs)
fqs.fs = append(fqs.fs, freq{s: ss[0], f: 1})
} else {
fqs.fs[is[ss[0]]].f++
}
}
sort.Sort(fqs)
for i := 0; i < 9; i++ {
if i >= len(fqs.fs) {
break
}
fss = append(fss, []float64{fqs.fs[i].f})
sss = append(sss, []string{fqs.fs[i].s})
}
if len(fqs.fs) > 10 {
sum := float64(0)
for i := 9; i < len(fqs.fs); i++ {
sum += fqs.fs[i].f
}
fss = append(fss, []float64{sum})
sss = append(sss, []string{"Other"})
} else if len(fqs.fs) == 10 {
fss = append(fss, []float64{fqs.fs[9].f})
sss = append(sss, []string{fqs.fs[9].s})
}
// Updates lineFormat
lineFormat.ColTypes = append(lineFormat.ColTypes, format.Float)
lineFormat.FloatCount++
lineFormat.HasFloats = true
return fss, sss, lineFormat
}
type freq struct {
s string
f float64
}
type freqs struct {
fs []freq
}
func (f freqs) Len() int { return len(f.fs) }
func (f freqs) Swap(i, j int) { f.fs[i], f.fs[j] = f.fs[j], f.fs[i] }
func (f freqs) Less(i, j int) bool {
if f.fs[i].f == f.fs[j].f {
return i < j
}
return f.fs[i].f > f.fs[j].f
}