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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main

// The cross-check report: on stderr and in the database as text, and in the
// EPUB as its last chapter ("Prüfbericht").

import (
	"fmt"
	"strings"
)

type report struct {
	p     *pipeline
	pages []*page
	warns []string
}

func buildReport(p *pipeline, pages []*page) *report {
	return &report{p: p, pages: pages, warns: p.warns}
}

func pageName(pg *page) string {
	side := map[string]string{"L": "left", "R": "right", "S": "single"}[pg.Side]
	return fmt.Sprintf("p. %-6s (scan %d %s)", pg.Label, pg.Scan, side)
}

func (r *report) text() string {
	var b strings.Builder
	b.WriteString("\n== cross-check ==\n")
	fmt.Fprintf(&b, "%-28s %-14s %6s %6s %5s %5s %5s %5s\n", "page", "kind", "agree", "words", "susp", "ins", "fixed", "conf")
	for _, pg := range r.pages {
		if pg.Merge == nil {
			fmt.Fprintf(&b, "%-28s %-14s %s\n", pageName(pg), pg.Kind, map[bool]string{true: "shown as image", false: "left out"}[pg.Image != nil])
			continue
		}
		m := pg.Merge
		flag := ""
		if m.Flagged {
			flag = "  FLAGGED"
		}
		att := ""
		if m.Attempt > 1 {
			att = " (2nd reading)"
		}
		fmt.Fprintf(&b, "%-28s %-14s %5.1f%% %6d %5d %5d %5d %5d%s%s\n", pageName(pg), pg.Kind,
			100*m.Score, m.Words, m.Suspects, m.Inserted, m.Decided, m.Confirmed, flag, att)
	}
	for _, pg := range r.pages {
		m := pg.Merge
		if m == nil {
			continue
		}
		var lines []string
		if pg.NumNote != "" {
			lines = append(lines, "page number: "+pg.NumNote)
		}
		lines = append(lines, m.LongRuns...)
		for _, b := range m.Blocks {
			for _, t := range b.Toks {
				switch {
				case t.Class == "decided" && t.Was != "":
					lines = append(lines, fmt.Sprintf("fixed: %q (Gemini: %q)", t.surface(), t.Was))
				case t.Class == "decided":
					lines = append(lines, fmt.Sprintf("fixed: %q (layer and OCR agree)", t.surface()))
				case t.Class == "suspect" && t.Other != "":
					lines = append(lines, fmt.Sprintf("suspect: %q (OCR: %q)", t.surface(), t.Other))
				}
			}
		}
		if len(m.Ignored) > 0 {
			lines = append(lines, fmt.Sprintf("OCR noise left out: %s", strings.Join(m.Ignored, " · ")))
		}
		if len(lines) == 0 {
			continue
		}
		fmt.Fprintf(&b, "\n%s\n", strings.TrimSpace(pageName(pg)))
		for _, l := range lines {
			fmt.Fprintf(&b, "  %s\n", l)
		}
	}
	if len(r.warns) > 0 {
		b.WriteString("\nwarnings:\n")
		for _, w := range r.warns {
			fmt.Fprintf(&b, "  %s\n", w)
		}
	}
	return b.String()
}

func (r *report) xhtml(href func(*page) string) string {
	var b strings.Builder
	b.WriteString("<section class=\"report\">\n<h1>Prüfbericht</h1>\n")
	b.WriteString("<p>Jede Seite wurde zweimal gelesen: von einem Sprachmodell (Gemini), dessen Text dieses Buch ist, und von einer OCR (tesseract)")
	if r.p.info.hasTextLayer() {
		b.WriteString(", dazu die Textebene der PDF")
	}
	b.WriteString(". Wo die Lesungen voneinander abweichen, wurde ein zweites Modell (Jev) mit dem Bildausschnitt des Wortes gefragt, welche Lesung gedruckt ist. Übereinstimmung: Anteil der Wörter, die bestätigt sind.</p>\n")
	b.WriteString("<table class=\"report\">\n<tr><th>Seite</th><th>Übereinst.</th><th>unsicher</th><th>ergänzt</th><th>korrigiert</th></tr>\n")
	for _, pg := range r.pages {
		if pg.Merge == nil {
			continue
		}
		m := pg.Merge
		label := esc(pg.Label)
		if h := href(pg); h != "" {
			label = fmt.Sprintf("<a href=\"%s\">%s</a>", h, label)
		}
		if m.Flagged {
			label = "<span class=\"flag\">⚠</span> " + label
		}
		fmt.Fprintf(&b, "<tr><td>%s</td><td>%.1f %%</td><td>%d</td><td>%d</td><td>%d</td></tr>\n",
			label, 100*m.Score, m.Suspects, m.Inserted, m.Decided)
	}
	b.WriteString("</table>\n")
	for _, pg := range r.pages {
		m := pg.Merge
		if m == nil {
			continue
		}
		var items []string
		if pg.NumNote != "" {
			items = append(items, "Seitenzahl: "+esc(pg.NumNote))
		}
		for _, l := range m.LongRuns {
			items = append(items, esc(l))
		}
		for _, bl := range m.Blocks {
			for _, t := range bl.Toks {
				switch {
				case t.Class == "decided" && t.Was != "":
					items = append(items, fmt.Sprintf("korrigiert: <span class=\"decided\">%s</span> (Gemini: %s)", esc(t.surface()), esc(t.Was)))
				case t.Class == "suspect" && t.Other != "":
					items = append(items, fmt.Sprintf("unsicher: <span class=\"suspect\">%s</span> (OCR: %s)", esc(t.surface()), esc(t.Other)))
				}
			}
		}
		if len(items) == 0 {
			continue
		}
		label := esc(pg.Label)
		if h := href(pg); h != "" {
			label = fmt.Sprintf("<a href=\"%s\">%s</a>", h, label)
		}
		fmt.Fprintf(&b, "<h2>Seite %s</h2>\n<ul>\n", label)
		for _, it := range items {
			fmt.Fprintf(&b, "<li>%s</li>\n", it)
		}
		b.WriteString("</ul>\n")
	}
	b.WriteString("</section>\n")
	return b.String()
}