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

// Settling a disagreement between readings by showing Jev the printed word.
//
// This is the use Jev's documentation recommends for extraction: the
// candidates come from elsewhere — Gemini, tesseract, the text layer — and
// Jev only picks the one the image supports. Every question is asked twice,
// with the options in opposite orders, because Jev leans towards the first
// option; an answer counts only if both orders give it, confidently.

import (
	"context"
	"fmt"
	"strings"
)

const decideVersion = "decide-1"

// decideMinConf is the confidence both orders must reach. With two readings
// and "neither", 0.8 means the chosen option has at least 0.87 probability.
const decideMinConf = 0.8

// decideBatch is how many words go into one request (each asked twice).
const decideBatch = 16

type decision struct {
	Pick int     `json:"pick"` // index into Readings; -1: neither
	Sure bool    `json:"sure"`
	Conf float64 `json:"conf"`
}

func decideQuestion(ref string, readings []string, rev bool) jevQuestion {
	opts := make([]option, 0, len(readings)+1)
	for i, r := range readings {
		opts = append(opts, option{fmt.Sprintf("r%d", i), fmt.Sprintf("The printed word reads exactly: %s", r)})
	}
	opts = append(opts, option{"neither", "None of these: the printed word is spelled differently from every option above, or cannot be read."})
	q := choice(fmt.Sprintf(
		"This image is cut from a scanned printed book page: %s. "+
			"Which option spells the word in the middle of the image exactly as it is printed? "+
			"Compare letter by letter: umlauts (ä ö ü), ß versus ss, capital letters and every single letter count. "+
			"Ignore punctuation and line-break hyphens.", ref), opts...)
	if rev {
		q = reversed(q)
	}
	return q
}

func (c *jevClient) decide(ctx context.Context, qs []question) ([]decision, string, error) {
	out := make([]decision, len(qs))
	model := ""
	for start := 0; start < len(qs); start += decideBatch {
		end := min(start+decideBatch, len(qs))
		questions := map[string]jevQuestion{}
		var imgs []jevImage
		for i := start; i < end; i++ {
			ref := fmt.Sprintf("word-crop-%d-5e0a3c", i)
			imgs = append(imgs, jevImage{ref, qs[i].Crop})
			questions[fmt.Sprintf("q%d", i)] = decideQuestion(ref, qs[i].Readings, false)
			questions[fmt.Sprintf("q%dr", i)] = decideQuestion(ref, qs[i].Readings, true)
		}
		r, err := c.ask(ctx, map[string]string{"task": "Reading single words in a scanned book."}, questions, imgs)
		if err != nil {
			return nil, "", err
		}
		model = r.Model
		for i := start; i < end; i++ {
			a, b := r.Answers[fmt.Sprintf("q%d", i)], r.Answers[fmt.Sprintf("q%dr", i)]
			pick := -1
			if strings.HasPrefix(a.Choice, "r") {
				fmt.Sscanf(a.Choice, "r%d", &pick)
			}
			out[i] = decision{
				Pick: pick,
				Sure: a.Choice == b.Choice && a.Confidence >= decideMinConf && b.Confidence >= decideMinConf && pick >= 0,
				Conf: min(a.Confidence, b.Confidence),
			}
		}
	}
	return out, model, nil
}