Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion playground/shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,21 @@
};
}

// Shapes that are a letter or a digit depending on the task: a vertical
// line (l, I, 1), a ring (O, o, 0) and a Z (Z, z, 2).
const TWINS = new Set(["l", "I", "1", "O", "o", "0", "Z", "z"]);
// Characters written together are read together: next to a digit that is
// clearly a digit, a twin is read as a digit too, and next to a clear
// letter as a letter. "12" drawn on the Letters task used to read l, 2.
function agree(groups, parts, opts) {
const clear = parts.filter((p) => p && p.label && !TWINS.has(p.label));
const digits = clear.some((p) => p.task === "symbols");
const letters = clear.some((p) => p.task === "chars");
if (digits === letters) return parts;
const task = digits ? "symbols" : "chars";
return parts.map((p, i) => (p && p.label && TWINS.has(p.label) && p.task !== task ? recogniseOne(groups[i], { ...opts, task }) : p));
}

// strokes: arrays of {x, y}. opts.size: the drawing area's height in
// pixels. opts.task: "chars" or "symbols". Returns one character, or, for
// characters written side by side, { sequence: [one per character] }.
Expand All @@ -486,7 +501,7 @@
const { groups, gap } = characters(clean, opts.size || 170);
if (groups.length < 2 || opts.ranking) return recogniseOne(clean, opts);
const whole = recogniseOne(clean, opts);
const parts = groups.map((g) => recogniseOne(g, opts));
const parts = agree(groups, groups.map((g) => recogniseOne(g, opts)), opts);
const allRead = parts.every((p) => p && p.label);
// A clear gap means separate characters. A narrow one (a sloppy H whose
// bar misses a stem) is one character if it reads as one.
Expand Down
33 changes: 33 additions & 0 deletions tests/test_playground_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,39 @@ def test_two_letters_side_by_side(pair):
assert read.count(pair) >= 7, read


def test_the_reported_12_on_the_letters_task_reads_1_2():
# Reported: drawn on the Letters task, the 1 was read as the letter l.
got = recognise([{"strokes": _reported("12"), "task": "chars", "size": 215}])
assert [c["label"] for c in got[0]["sequence"]] == ["1", "2"]


@pytest.mark.parametrize(
"first,second,task,want",
[
# A clear digit makes its neighbour a digit: O is 0 next to a 2.
("2", "O", "chars", "20"),
# A clear letter makes a vertical line the letter l, even on numbers.
("b", "l", "symbols", "bl"),
],
)
def test_characters_written_together_are_read_together(first, second, task, want):
rng = random.Random(want + task)
line = {"l": [path((0.5, 0.02), (0.52, 0.98))]}
sketches = {**SKETCHES, **MULTI, **line}
cases = []
for _ in range(8):
a = draw(sketches[first], rng, height=90, left=40)
right = max(p["x"] for st in a for p in st) + 18
cases.append(
{
"strokes": a + draw(sketches[second], rng, height=90, left=right),
"task": task,
}
)
read = ["".join(c["label"] or "?" for c in g["sequence"]) for g in recognise(cases)]
assert read.count(want) >= 7, read


def test_one_character_with_a_gap_between_its_strokes_stays_one():
# An H whose bar stops short of both stems, and a = and a 梅.
rng = random.Random(9)
Expand Down
Loading