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
package main

// Tesseract: the second, independent reading of every page. It is worse than
// Gemini at nearly everything except the one thing that matters here — it
// cannot invent a sentence, skip a line because it seemed redundant, or
// modernise a spelling. Where the two agree, the text is as good as either;
// where they do not, align.go has something to look at, and tesseract's word
// boxes say where on the page to look.

import (
	"context"
	"image"
	"strconv"
	"strings"
)

type ocrWord struct {
	Text  string          `json:"t"`
	Conf  float64         `json:"c"`
	Box   image.Rectangle `json:"b"`
	Block int             `json:"bl"`
	Par   int             `json:"p"`
	Line  int             `json:"l"`
}

// tesseractLangs is what tesseract is told to expect, main language first.
// French and English always come along: scholarly German quotes both.
func tesseractLangs(main string) string {
	switch main {
	case "eng":
		return "eng+deu+fra"
	case "fra":
		return "fra+deu+eng"
	}
	return "deu+fra+eng"
}

func runTesseract(ctx context.Context, png []byte, langs string) ([]ocrWord, error) {
	out, err := run(ctx, png, "tesseract", "stdin", "stdout", "-l", langs, "--psm", "3", "tsv")
	if err != nil {
		return nil, err
	}
	return parseTSV(string(out)), nil
}

// parseTSV reads tesseract's TSV output: one row per element, level 5 being
// a word.
func parseTSV(s string) []ocrWord {
	var words []ocrWord
	for i, l := range strings.Split(s, "\n") {
		if i == 0 {
			continue
		}
		f := strings.Split(l, "\t")
		if len(f) < 12 || f[0] != "5" {
			continue
		}
		text := strings.TrimSpace(f[11])
		if text == "" {
			continue
		}
		n := func(i int) int { v, _ := strconv.Atoi(f[i]); return v }
		conf, _ := strconv.ParseFloat(f[10], 64)
		words = append(words, ocrWord{
			Text:  text,
			Conf:  conf,
			Box:   image.Rect(n(6), n(7), n(6)+n(8), n(7)+n(9)),
			Block: n(2), Par: n(3), Line: n(4),
		})
	}
	return words
}

// sameLine says whether two OCR words were read as part of one line.
func sameLine(a, b ocrWord) bool {
	return a.Block == b.Block && a.Par == b.Par && a.Line == b.Line
}