summaryrefslogtreecommitdiff
path: root/line-conf-avg
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-01-03 13:45:55 +0000
committerNick White <git@njw.name>2019-01-03 13:45:55 +0000
commit79f598fe512062d355cdd0f67f1ff3dcca0a9483 (patch)
tree1080c776dbfe29b46681b7f4e99851ff6aa80d59 /line-conf-avg
parent2dacb45ffd90828dd85a6bf0a16afecb21e0522a (diff)
Add basic working line confidence average tool (for ocropus .prob parsing)
Diffstat (limited to 'line-conf-avg')
-rw-r--r--line-conf-avg/line-conf-avg.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/line-conf-avg/line-conf-avg.go b/line-conf-avg/line-conf-avg.go
new file mode 100644
index 0000000..18421d3
--- /dev/null
+++ b/line-conf-avg/line-conf-avg.go
@@ -0,0 +1,56 @@
+package main
+
+import (
+ "bufio"
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+ "strings"
+)
+
+func main() {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, "Usage: line-conf-avg prob1 [prob2] [...]\n")
+ flag.PrintDefaults()
+ }
+ flag.Parse()
+ if flag.NArg() < 1 {
+ flag.Usage()
+ os.Exit(1)
+ }
+
+ 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)
+
+ fmt.Printf("%s: %.2f%%\n", f, avg)
+ }
+}