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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package bookpipeline
import (
"errors"
"fmt"
"io"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
)
const maxticks = 40
const goodCutoff = 70
const mediumCutoff = 65
const badCutoff = 60
const yticknum = 40
type Conf struct {
Path, Code string
Conf float64
}
type GraphConf struct {
Pgnum, Conf float64
}
func createLine(xvalues []float64, y float64, c drawing.Color) chart.ContinuousSeries {
var yvalues []float64
for range xvalues {
yvalues = append(yvalues, y)
}
return chart.ContinuousSeries{
XValues: xvalues,
YValues: yvalues,
Style: chart.Style{
StrokeColor: c,
},
}
}
func Graph(confs map[string]*Conf, bookname string, w io.Writer) error {
if len(confs) == 0 {
return errors.New("No valid confidences")
}
// Organise confs to sort them by page
var graphconf []GraphConf
for _, conf := range confs {
name := filepath.Base(conf.Path)
var numend int
numend = strings.Index(name, "_")
if numend == -1 {
numend = strings.Index(name, ".")
}
pgnum, err := strconv.ParseFloat(name[0:numend], 64)
if err != nil {
continue
}
var c GraphConf
c.Pgnum = pgnum
c.Conf = conf.Conf
graphconf = append(graphconf, c)
}
// If we failed to get any page numbers, just fake the lot
if len(graphconf) == 0 {
i := float64(1)
for _, conf := range confs {
var c GraphConf
c.Pgnum = i
c.Conf = conf.Conf
graphconf = append(graphconf, c)
i++
}
}
sort.Slice(graphconf, func(i, j int) bool { return graphconf[i].Pgnum < graphconf[j].Pgnum })
// Create main xvalues, yvalues ticks
var xvalues, yvalues []float64
var ticks []chart.Tick
var yticks []chart.Tick
tickevery := len(graphconf) / maxticks
if tickevery < 1 {
tickevery = 1
}
for i, c := range graphconf {
xvalues = append(xvalues, c.Pgnum)
yvalues = append(yvalues, c.Conf)
if i%tickevery == 0 {
ticks = append(ticks, chart.Tick{c.Pgnum, fmt.Sprintf("%.0f", c.Pgnum)})
}
}
// Make last tick the final page
final := graphconf[len(graphconf)-1]
ticks[len(ticks)-1] = chart.Tick{final.Pgnum, fmt.Sprintf("%.0f", final.Pgnum)}
for i := 1; i <= yticknum; i++ {
n := float64(i * 100) / yticknum
yticks = append(yticks, chart.Tick{n, fmt.Sprintf("%.1f", n)})
}
mainSeries := chart.ContinuousSeries{
XValues: xvalues,
YValues: yvalues,
}
// Create lines
goodCutoffSeries := createLine(xvalues, goodCutoff, chart.ColorAlternateGreen)
mediumCutoffSeries := createLine(xvalues, mediumCutoff, chart.ColorOrange)
badCutoffSeries := createLine(xvalues, badCutoff, chart.ColorRed)
// Create lines marking top and bottom 10% confidence
sort.Slice(graphconf, func(i, j int) bool { return graphconf[i].Conf < graphconf[j].Conf })
lowconf := graphconf[int(len(graphconf)/10)].Conf
highconf := graphconf[int((len(graphconf)/10)*9)].Conf
yvalues = []float64{}
for range graphconf {
yvalues = append(yvalues, lowconf)
}
minSeries := &chart.ContinuousSeries{
Style: chart.Style{
StrokeColor: chart.ColorAlternateGray,
StrokeDashArray: []float64{5.0, 5.0},
},
XValues: xvalues,
YValues: yvalues,
}
yvalues = []float64{}
for range graphconf {
yvalues = append(yvalues, highconf)
}
maxSeries := &chart.ContinuousSeries{
Style: chart.Style{
StrokeColor: chart.ColorAlternateGray,
StrokeDashArray: []float64{5.0, 5.0},
},
XValues: xvalues,
YValues: yvalues,
}
// Create annotations
var annotations []chart.Value2
for _, c := range graphconf {
if c.Conf > highconf || c.Conf < lowconf {
annotations = append(annotations, chart.Value2{Label: fmt.Sprintf("%.0f", c.Pgnum), XValue: c.Pgnum, YValue: c.Conf})
}
}
annotations = append(annotations, chart.Value2{Label: fmt.Sprintf("%.0f", lowconf), XValue: xvalues[len(xvalues)-1], YValue: lowconf})
annotations = append(annotations, chart.Value2{Label: fmt.Sprintf("%.0f", highconf), XValue: xvalues[len(xvalues)-1], YValue: highconf})
graph := chart.Chart{
Title: bookname,
Width: 3840,
Height: 2160,
XAxis: chart.XAxis{
Name: "Page number",
Range: &chart.ContinuousRange{
Min: 0.0,
},
Ticks: ticks,
},
YAxis: chart.YAxis{
Name: "Confidence",
Range: &chart.ContinuousRange{
Min: 0.0,
Max: 100.0,
},
Ticks: yticks,
},
Series: []chart.Series{
mainSeries,
minSeries,
maxSeries,
goodCutoffSeries,
mediumCutoffSeries,
badCutoffSeries,
chart.AnnotationSeries{
Annotations: annotations,
},
},
}
return graph.Render(chart.PNG, w)
}
|