From 632a149df0196f0f057fa4d552aa28d22901bcda Mon Sep 17 00:00:00 2001 From: Nick White Date: Wed, 22 Jan 2020 16:17:05 +0000 Subject: Add GetWordConfs function to hocr pkg --- pkg/hocr/hocr.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/hocr/hocr.go b/pkg/hocr/hocr.go index c3c88b3..fa8ae8e 100644 --- a/pkg/hocr/hocr.go +++ b/pkg/hocr/hocr.go @@ -127,3 +127,32 @@ func GetAvgConf(hocrfn string) (float64, error) { } return total / num, nil } + +// GetWordConfs is a utility function that parses a hocr +// file and returns an array containing the confidences +// of each word therein. +func GetWordConfs(hocrfn string) ([]float64, error) { + var confs []float64 + + file, err := ioutil.ReadFile(hocrfn) + if err != nil { + return confs, err + } + + h, err := Parse(file) + if err != nil { + return confs, err + } + + for _, l := range h.Lines { + for _, w := range l.Words { + c, err := wordConf(w.Title) + if err != nil { + return confs, err + } + confs = append(confs, c) + } + } + + return confs, nil +} -- cgit v1.2.1-24-ge1ad