summaryrefslogtreecommitdiff
path: root/parse/prob/prob.go
blob: 6781a1a62a14fe7b898d3274e0000aa804254e3a (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
package prob

import (
	"bufio"
	"io/ioutil"
	"path/filepath"
	"strconv"
	"strings"

	"git.rescribe.xyz/testingtools/parse"
)

// TODO: probably switch to just relying on io.Reader
func getLineAvg(r *bufio.Reader) (float64, error) {
	var err error

	totalconf := float64(0)
	num := 0

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

		if len(fields) == 2 {
			conf, converr := strconv.ParseFloat(fields[1], 64)
			if converr != nil {
				continue
			}
			totalconf += conf
			num += 1
		}
	}
	if num <= 0 {
		return 0, nil
	}
	avg := totalconf / float64(num)
	return avg, nil
}

// TODO: probably switch to just relying on io.Reader
// Note this only processes one line at a time
func GetLineDetails(name string, r *bufio.Reader) (parse.LineDetails, error) {
	var line parse.LineDetail
	lines := make(parse.LineDetails, 0)

	avg, err := getLineAvg(r)
	if err != nil {
		return lines, err
	}

	filebase := strings.Replace(name, ".prob", "", 1)

	txt, err := ioutil.ReadFile(filebase + ".txt")
	if err != nil {
		return lines, err
	}

	line.Name = filepath.Base(filebase)
	line.Avgconf = avg
	line.Text = string(txt)
	line.OcrName = filepath.Dir(filebase)

	var imgfn parse.ImgPath
	imgfn.Path = filebase + ".bin.png"
	line.Img = imgfn

	lines = append(lines, line)

	return lines, nil
}