From cffd64caa40ad1a68e860f157c22d5a89524c310 Mon Sep 17 00:00:00 2001 From: Stephen Masters Date: Thu, 19 Nov 2026 16:58:53 +0000 Subject: [PATCH] fix(harvest): keep text inside formatting templates The transcript cleaning stripped every template whole. A centred heading, a signature set to the right, a spaced-out name, a drop capital and, in the encyclopedia, headwords and bibliography authors went with their wrappers: 1,382 words on 480 pages sampled across every harvest, and 181 on the evaluation pages (brief 023). Ten templates that only format printed text now leave their text behind. Those that render as a block take a line of their own, since transcribers write them hard against the next word. Abovyan's four volumes join PRINTED_ANGLE_BRACKETS. The edition's own editorial principles put the editors' additions in angle brackets, and the fold had turned them into guillemets, in one line into a bracket paired with a guillemet. TRANSCRIPT_CLEANING is 4: text saved by cleaning 3 has lost both for good, so every harvest needs --refresh-stale before the next crop harvest. Co-authored-by: Yvette Mankerian Co-Authored-By: Claude Fable 5.2 --- .claude/skills/tetrak-hy-training/SKILL.md | 18 +++++ src/tetrak_hy_trainer/wikisource.py | 93 +++++++++++++++++++++- tests/test_wikisource.py | 79 ++++++++++++++++++ 3 files changed, 187 insertions(+), 3 deletions(-) diff --git a/.claude/skills/tetrak-hy-training/SKILL.md b/.claude/skills/tetrak-hy-training/SKILL.md index fdee355..27fa811 100644 --- a/.claude/skills/tetrak-hy-training/SKILL.md +++ b/.claude/skills/tetrak-hy-training/SKILL.md @@ -75,6 +75,24 @@ engine's mistakes. (2026-10-08) the template stripper took the whole page with the wrapper and every poetry index harvested as empty, silently. Harvests taken before then have no verse: `--refresh-stale` refetches those pages. +- **Formatting templates carry print.** Until cleaning 4 (2026-11-19) every + template was stripped whole, and a centred heading, a signature set to the + right, a spaced-out name, a drop capital and, in the encyclopedia, many + headwords and every bibliography's authors went with their wrappers: 1,382 + words on 480 sampled pages, 98 of the Aram Manukyan evaluation set's + (brief 023). `_TEXT_TEMPLATES` in `wikisource.py` lists the ten that are + kept, each seen carrying print; add a new one there with a test, and say + whether it sets its text on a line of its own. **Every harvest taken + before then is stale**: run `--refresh-stale` on each directory before the + next crop harvest, which refuses stale pages. To find such templates in a + new source, read a few pages' raw wikitext (`action=raw`) beside the + cleaned text. +- **A critical edition's angle brackets are print.** `PRINTED_ANGLE_BRACKETS` + lists the sources whose `<…>` are the editor's own (a supplied ending, an + expanded abbreviation) and must not be folded to guillemets. Abovyan's + four volumes joined at cleaning 4; the edition states the convention in + its editorial principles (volume 2, page 7). Check a new critical edition + against a scan before harvesting it, because the fold cannot be undone. - **`--image-width 3840`** matches the evaluation scans, so crops carry the same detail per character as the material the model is scored on. - Re-runs are incremental. Adding `--images` to a harvest taken text-only tops diff --git a/src/tetrak_hy_trainer/wikisource.py b/src/tetrak_hy_trainer/wikisource.py index ab8d4d1..65bd973 100644 --- a/src/tetrak_hy_trainer/wikisource.py +++ b/src/tetrak_hy_trainer/wikisource.py @@ -211,16 +211,81 @@ def page_image_url(self, file_title: str, page_number: int, width: int = 2048) - # wrapper, and every poetry index harvested as empty: Varoujan, Terian, # Charents vols. 2-3, Tsaturyan, Toranian's verse. Keep the poem's text. _POEM_TEMPLATE = re.compile(r"\{\{[^{}<]*(.*?)[^{}]*\}\}", re.DOTALL) +# Templates that only format text the page prints. Everything else is +# stripped whole, which is right for a line number or a rule and wrong for +# these: until cleaning 4 a centred heading, a signature set to the right, +# a spaced-out name and a drop capital all went with their wrapper. On the +# evaluation pages that was 181 Armenian words, 98 of them in the Aram +# Manukyan documents, where every heading is centred (brief 023). Found by +# tallying the templates on 480 pages across every harvest; a template is +# listed only once it has been seen carrying print. +# Կենտրոն, կ, ct centred text +# Աջաթև text set to the right: signatures, datelines +# լայն letter-spaced text +# x-larger, xx-larger a larger size: headings and drop capitals +# ՀՍՀ հեղ the author's signature under an encyclopedia entry +# գբս, գբվ a word broken across two pages: the first argument is +# the part this page prints, the second the whole word +# Names are matched without regard to case, as MediaWiki matches the first +# letter. The printed text is always the first unnamed argument. +# +# The first five set their text on a line of its own, whatever the +# wikitext does: transcribers write "մեջ։{{ՀՍՀ հեղ|Վ. Մաղալյան}}" and +# "{{Աջաթև|14 Սեպտեմբեր}}Երէկ գիշեր" with nothing between, and unwrapped +# in place those would weld two words into one. The others run on in the +# line: a drop capital is the first letter of its word. +_BLOCK_TEXT_TEMPLATES = frozenset({"կենտրոն", "կ", "ct", "աջաթև", "հսհ հեղ"}) +_INLINE_TEXT_TEMPLATES = frozenset({"լայն", "x-larger", "xx-larger", "գբս", "գբվ"}) +_TEXT_TEMPLATES = _BLOCK_TEXT_TEMPLATES | _INLINE_TEXT_TEMPLATES +# Stands for the line break around a block template's text until every +# template is unwrapped; never occurs in wikitext. +_LINE_EDGE = "\x00" +_LINE_EDGES = re.compile(rf"\n?(?:[ \t]*{_LINE_EDGE}[ \t]*)+\n?") +_NAMED_ARGUMENT = re.compile(r"\s*[\w-]+\s*=") _WIKILINK = re.compile(r"\[\[(?:[^|\]]*\|)?([^\]]*)\]\]") _EMPHASIS = re.compile(r"'{2,}") +def _template_arguments(body: str) -> list[str]: + """Split a template's body on the pipes that separate its arguments. + + A pipe inside a ``[[link|label]]`` belongs to the link. + """ + arguments, depth, start = [], 0, 0 + for position, character in enumerate(body): + if body.startswith("[[", position): + depth += 1 + elif body.startswith("]]", position): + depth = max(depth - 1, 0) + elif character == "|" and depth == 0: + arguments.append(body[start:position]) + start = position + 1 + arguments.append(body[start:]) + return arguments + + +def _printed_text(match: re.Match[str]) -> str: + """What an innermost template leaves behind: its text, or nothing.""" + name, *arguments = _template_arguments(match.group()[2:-2]) + name = name.strip().lower() + if name not in _TEXT_TEMPLATES: + return "" + for argument in arguments: + if not _NAMED_ARGUMENT.match(argument): + if name in _BLOCK_TEXT_TEMPLATES and argument.strip(): + return f"{_LINE_EDGE}{argument}{_LINE_EDGE}" + return argument + return "" + + def clean_wikitext(text: str, index_title: str = "") -> str: """Reduce a transcription page's wikitext to its plain page text. Strips the ProofreadPage ```` header/footer (quality tag and running header), ``
`` transclusion markers, references, - templates, HTML tags and wiki markup, keeping link display text. The + templates, HTML tags and wiki markup, keeping link display text and + the text inside the templates that only format it (centred, set right, + spaced, enlarged: ``_TEXT_TEMPLATES``). The result is the page's prose — suitable as corpus text for synthesis and as the transcript side of a page/scan pair. @@ -236,7 +301,8 @@ def clean_wikitext(text: str, index_title: str = "") -> str: text = _REF.sub("", text) text = _POEM_TEMPLATE.sub(r"\1", text) while _TEMPLATE.search(text): - text = _TEMPLATE.sub("", text) + text = _TEMPLATE.sub(_printed_text, text) + text = _LINE_EDGES.sub("\n", text) text = _WIKILINK.sub(r"\1", text) text = _HTML_TAG.sub("", text) text = _EMPHASIS.sub("", text) @@ -369,11 +435,28 @@ def _armenian_full_stops(text: str) -> str: # guillemet, and folding would teach the model the wrong shape for 787 # printed brackets. Each checked against its scans: Tumanyan pages 522, # 552, 630 and 637; Baronian page 694. +# +# Abovyan's complete works joined in brief 023. The edition says so itself: +# rule 9 of its editorial principles (volume 2, page 7) puts in angle +# brackets the completed part of a word the author left short, the +# editors' other additions and notes on illegible words. Volume 1 page 45 +# prints a refrain's supplied endings that way ("սիրե <լւոյ>"), volume 6 +# page 123 an expanded abbreviation ("գերմ<անացւոց>"), and all four +# volumes type their quotations as « ». Folded, page 45 read +# "սիրե«լւոյ»" and, where the regex judged one bracket print and its +# partner not, "գե<րիս քոյ»". PRINTED_ANGLE_BRACKETS = frozenset( { "Ինդեքս:Թումանյանի ԵԼԺ հ5.djvu", "Ինդեքս:Hagop Baronian, Collected works, vol. 10 " "(Հակոբ Պարոնյան, Երկերի ժողովածու, հատոր 10-րդ).djvu", + "Ինդեքս:Խաչատուր Աբովյան, Երկերի լիակատար ժողովածու, հատոր 1 " + "(Khachatur Abovyan, Collective works, volume 1).djvu", + "Ինդեքս:Խաչատուր Աբովյան, Երկերի լիակատար ժողովածու, հատոր 2 " + "(Khachatur Abovyan, Collective works, volume 2).djvu", + "Ինդեքս:Խաչատուր Աբովյան, Երկերի լիակատար ժողովածու, հատոր 3 " + "(Khachatur Abovyan, Collective works, volume 3).djvu", + "Ինդեքս:Խաչատուր Աբովյան, Երկեր, հատոր 6.djvu", } ) @@ -389,7 +472,11 @@ def _armenian_full_stops(text: str) -> str: # 3 verse inside a {{Poemx|…}} wrapper is kept (brief 014). # Cleaning 1 and 2 stripped it with the template, so a poetry page # saved earlier is empty or missing its verse. -TRANSCRIPT_CLEANING = 3 +# 4 text inside the formatting templates is kept (_TEXT_TEMPLATES), and +# Abovyan's four volumes keep their printed angle brackets (brief +# 023). Cleaning 3 dropped every centred heading and folded those +# brackets to guillemets, neither of which can be put back. +TRANSCRIPT_CLEANING = 4 def cleaning_of(entry: dict) -> int: diff --git a/tests/test_wikisource.py b/tests/test_wikisource.py index 210d0c1..d6f80da 100644 --- a/tests/test_wikisource.py +++ b/tests/test_wikisource.py @@ -66,6 +66,66 @@ def test_keeps_verse_wrapped_in_a_poem_template(self) -> None: "Որ ոտքի միակ ոստումով", ] + def test_keeps_the_text_inside_formatting_templates(self) -> None: + # Abovyan volume 1, page 271: the heading is centred and bold, and + # until cleaning 4 it left with its template. + page = ( + "{{poemx||{{Կենտրոն|'''ՍՈՒԳ ԱՐԱՐԵԱԼ Ի ՄԵԾԱՆՈՒՆ\n" + "ՄԱՀՈՒԱՆ ՍԻՐՈՒՀԻՈՅ ՄԱՐԻԱՆԷԻ։'''}}\n" + "{{Տող|1}}Առի՞ց սկիզբն կոծել զմահ քո դառնագին,}}" + ) + assert clean_wikitext(page).splitlines() == [ + "ՍՈՒԳ ԱՐԱՐԵԱԼ Ի ՄԵԾԱՆՈՒՆ", + "ՄԱՀՈՒԱՆ ՍԻՐՈՒՀԻՈՅ ՄԱՐԻԱՆԷԻ։", + "Առի՞ց սկիզբն կոծել զմահ քո դառնագին,", + ] + + def test_formatting_templates_of_every_kind_keep_their_text(self) -> None: + assert clean_wikitext("{{Աջաթև|Արճակի գաւառապետ՝ Շիրին}}") == "Արճակի գաւառապետ՝ Շիրին" + assert clean_wikitext("{{լայն|Նադիրով}} եկաւ") == "Նադիրով եկաւ" + assert clean_wikitext("{{Լայն|Փավստոս Բուզանդ}}") == "Փավստոս Բուզանդ" + # A drop capital belongs to its word. + assert clean_wikitext("{{x-larger|Չ}}որս տարերք") == "Չորս տարերք" + assert clean_wikitext("{{ՀՍՀ հեղ|Հ․ Պողոսյան}}") == "Հ․ Պողոսյան" + assert clean_wikitext("{{կ|'''Ը․'''}}") == "Ը․" + + def test_block_templates_take_a_line_of_their_own(self) -> None: + # Written with nothing between them and the text they follow or + # lead; unwrapped in place they would weld two words into one. + assert clean_wikitext("մեջ։{{ՀՍՀ հեղ|Վ. Մաղալյան}}\nՄԱԽՄՈՒՐՈՎԱ Տատյանա").splitlines() == [ + "մեջ։", + "Վ. Մաղալյան", + "ՄԱԽՄՈՒՐՈՎԱ Տատյանա", + ] + assert clean_wikitext("{{Աջաթև|14 Սեպտեմբեր}}Երէկ գիշեր").splitlines() == [ + "14 Սեպտեմբեր", + "Երէկ գիշեր", + ] + # Already on its own line: no blank line is added around it. + assert clean_wikitext("ա\n{{Կենտրոն|ՏԱՂ}}\nբ") == "ա\nՏԱՂ\nբ" + # Nested blocks are one line, not three. + assert clean_wikitext("ա {{Կենտրոն|{{կ|ՏԱՂ}}}} բ") == "ա\nՏԱՂ\nբ" + + def test_a_named_argument_is_not_the_text(self) -> None: + assert clean_wikitext("{{ct|f=120%|t=1|''ՍԵԲԱՍՏԱՑԻ ՄՈՒՐԱԴ''}}") == "ՍԵԲԱՍՏԱՑԻ ՄՈՒՐԱԴ" + assert clean_wikitext("ա {{աջաթև|}} բ") == "ա բ" + + def test_a_word_broken_across_pages_keeps_the_part_this_page_prints(self) -> None: + assert clean_wikitext("այր {{գբս|նշանա|նշանաւոր}}") == "այր նշանա" + assert clean_wikitext("{{գբվ|տանք|աշխատանք}} ու գործ") == "տանք ու գործ" + + def test_a_link_inside_a_formatting_template_keeps_its_label(self) -> None: + assert clean_wikitext("{{Կենտրոն|[[Հեղինակ:Խաչատուր Աբովյան|Խ. ԱԲՈՎՅԱՆ]]}}") == "Խ. ԱԲՈՎՅԱՆ" + + def test_formatting_inside_formatting(self) -> None: + assert clean_wikitext("{{Կենտրոն|{{x-larger|ՏԱՂ ՉՈՐՍ ՏԱՐԵՐԱՑ}}}}") == "ՏԱՂ ՉՈՐՍ ՏԱՐԵՐԱՑ" + # ...and an unknown template inside a known one still goes. + assert clean_wikitext("{{Կենտրոն|ՏԱՂ{{rule|4em}}}}") == "ՏԱՂ" + + def test_line_numbers_and_unknown_templates_still_go(self) -> None: + assert clean_wikitext("{{Տող|150}}Ձայնք, որ մեղմէին") == "Ձայնք, որ մեղմէին" + assert clean_wikitext("ա {{Միջգրություն|լայնք=380px|տուփ}} բ") == "ա բ" + def test_keeps_wikilink_display_text(self) -> None: assert clean_wikitext("[[Թիրախ|ցուցադրվող]] տեքստ") == "ցուցադրվող տեքստ" assert clean_wikitext("[[Պարզ հղում]]") == "Պարզ հղում" @@ -359,6 +419,25 @@ def test_a_critical_edition_keeps_its_editorial_brackets(self) -> None: assert wikisource.normalise_transcript(text, "Ինդեքս:Թումանյանի ԵԼԺ հ5.djvu") == text assert "«ԻՈՐԴ»" in wikisource.normalise_transcript(text) # any other source + def test_abovyan_keeps_the_brackets_its_edition_prints(self) -> None: + """Volume 1, page 45: the editor supplies a refrain's endings in + angle brackets. Folded, one line came out with a bracket and a + guillemet for a pair.""" + title = ( + "Ինդեքս:Խաչատուր Աբովյան, Երկերի լիակատար ժողովածու, հատոր 1 " + "(Khachatur Abovyan, Collective works, volume 1).djvu" + ) + text = "սէր իմ սիրե <լւոյ>\nսէր իմ գե<րիս քոյ>" + assert wikisource.normalise_transcript(text, title) == text + assert "«" in wikisource.normalise_transcript(text) # any other source + for volume in ( + "հատոր 2 (Khachatur Abovyan, Collective works, volume 2)", + "հատոր 3 (Khachatur Abovyan, Collective works, volume 3)", + ): + other = f"Ինդեքս:Խաչատուր Աբովյան, Երկերի լիակատար ժողովածու, {volume}.djvu" + assert other in wikisource.PRINTED_ANGLE_BRACKETS + assert "Ինդեքս:Խաչատուր Աբովյան, Երկեր, հատոր 6.djvu" in wikisource.PRINTED_ANGLE_BRACKETS + def test_labels_can_keep_their_angle_brackets(self) -> None: assert wikisource.normalise_transcript("<Ազգ>", angle_brackets_printed=True) == "<Ազգ>"