summaryrefslogtreecommitdiff
path: root/line-conf-avg/line-conf-avg.go
blob: a25b25d1755bc7731a3f8ee87617e4df2ec1ae65 (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
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
package main

// TODO: rewrite this to use the parse/ packages

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
)

type LineDetail struct {
	Filename string
	Avgconf float64
	Filebase string
	Basename string
	Dirname string
	Fulltext string
}

type LineDetails []LineDetail

// Used by sort.Sort.
func (l LineDetails) Len() int { return len(l) }

// Used by sort.Sort.
func (l LineDetails) Less(i, j int) bool {
	return l[i].Avgconf < l[j].Avgconf
}

// Used by sort.Sort.
func (l LineDetails) Swap(i, j int) { l[i], l[j] = l[j], l[i] }

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: line-conf-avg [-html] [-nosort] prob1 [prob2] [...]\n")
		fmt.Fprintf(os.Stderr, "Prints a report of the average confidence for each line\n")
		flag.PrintDefaults()
	}
	var usehtml = flag.Bool("html", false, "output html page")
	var nosort = flag.Bool("nosort", false, "don't sort lines by confidence")
	flag.Parse()
	if flag.NArg() < 1 {
		flag.Usage()
		os.Exit(1)
	}

	lines := make(LineDetails, 0)

	for _, f := range flag.Args() {
		file, err := os.Open(f)
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()

		reader := bufio.NewReader(file)

		totalconf := float64(0)
		num := 0

		err = nil
		for err == nil {
			var line string
                        line, err = reader.ReadString('\n')
			fields := strings.Fields(line)

			if len(fields) == 2 {
				conf, converr := strconv.ParseFloat(fields[1], 64)
				if converr != nil {
					fmt.Fprintf(os.Stderr, "Error: can't convert '%s' to float (full line: %s)\n", fields[1], line)
					continue
				}
				totalconf += conf
				num += 1
			}
		}
		avg := totalconf / float64(num)

		if num == 0 || avg == 0 {
			continue
		}

		var linedetail LineDetail
		linedetail.Filename = f
		linedetail.Avgconf = avg
		linedetail.Filebase = strings.Replace(f, ".prob", "", 1)
		linedetail.Basename = filepath.Base(linedetail.Filebase)
		linedetail.Dirname = filepath.Dir(linedetail.Filebase)
		ft, ferr := ioutil.ReadFile(linedetail.Filebase + ".txt")
		if ferr != nil {
			log.Fatal(err)
		}
		linedetail.Fulltext = string(ft)
		lines = append(lines, linedetail)
	}

	if *nosort == false {
		sort.Sort(lines)
	}

	if *usehtml == false {
		for _, l := range lines {
			fmt.Printf("%s: %.2f%%\n", l.Filename, l.Avgconf)
		}
	} else {
		fmt.Printf("<!DOCTYPE html><html><head><meta charset='UTF-8'><title></title><style>td {border: 1px solid #444}</style></head><body>\n")
		fmt.Printf("<table>\n")
		for _, l := range lines {
			fmt.Printf("<tr>\n")
			fmt.Printf("<td><h1>%.4f%%</h1></td>\n", l.Avgconf)
			fmt.Printf("<td>%s</td>\n", l.Filebase)
			fmt.Printf("<td><img src='%s' /><br />%s</td>\n", l.Filebase + ".bin.png", l.Fulltext)
			fmt.Printf("</tr>\n")
		}
		fmt.Printf("</table>\n")
		fmt.Printf("</body></html>\n")
	}
}