summaryrefslogtreecommitdiff
path: root/lib/prob/prob.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-01-25 09:55:55 +0000
committerNick White <git@njw.name>2019-01-25 09:55:55 +0000
commit30c088b90e7b6a25d93cbdad7564ff063e62afd3 (patch)
treec47d7bc086a076cfe5e702628c4e5e3b1eab1aa5 /lib/prob/prob.go
parent1c17766952bdcd6f7d31d0fa1a2e504b1aa4f14a (diff)
Reorganisation and cleanup
Diffstat (limited to 'lib/prob/prob.go')
-rw-r--r--lib/prob/prob.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/prob/prob.go b/lib/prob/prob.go
new file mode 100644
index 0000000..0299a96
--- /dev/null
+++ b/lib/prob/prob.go
@@ -0,0 +1,69 @@
+package prob
+
+import (
+ "io/ioutil"
+ "path/filepath"
+ "strconv"
+ "strings"
+
+ "git.rescribe.xyz/testingtools/lib/line"
+)
+
+func getLineAvg(f string) (float64, error) {
+ totalconf := float64(0)
+ num := 0
+
+ prob, err := ioutil.ReadFile(f)
+ if err != nil {
+ return 0, err
+ }
+
+ for _, l := range strings.Split(string(prob), "\n") {
+ fields := strings.Fields(l)
+
+ if len(fields) == 2 {
+ conf, err := strconv.ParseFloat(fields[1], 64)
+ if err != nil {
+ continue
+ }
+ totalconf += conf
+ num += 1
+ }
+ }
+ if num <= 0 {
+ return 0, nil
+ }
+ avg := totalconf / float64(num)
+ return avg, nil
+}
+
+// Note this only processes one line at a time
+func GetLineDetails(probfn string) (line.Details, error) {
+ var l line.Detail
+ lines := make(line.Details, 0)
+
+ avg, err := getLineAvg(probfn)
+ if err != nil {
+ return lines, err
+ }
+
+ filebase := strings.Replace(probfn, ".prob", "", 1)
+
+ txt, err := ioutil.ReadFile(filebase + ".txt")
+ if err != nil {
+ return lines, err
+ }
+
+ l.Name = filepath.Base(filebase)
+ l.Avgconf = avg
+ l.Text = string(txt)
+ l.OcrName = filepath.Dir(filebase)
+
+ var imgfn line.ImgPath
+ imgfn.Path = filebase + ".bin.png"
+ l.Img = imgfn
+
+ lines = append(lines, l)
+
+ return lines, nil
+}