Coverage for app / knowledge / passage_text.py: 97%
30 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""What stored knowledge reads as when a model reads it.
3Pages are stored as their extracted Markdown, links and all, and a page of
4legislation is mostly links: 58% of one legislation.gov.uk passage's
5characters were link targets — ``[S.I. 2026/82](https://www.legislation.gov.uk/
6id/uksi/2026/82)`` after every amendment note (scenario run 7). A model paid
7for each of them on every search, judge and answer call, and could cite none:
8a passage's citation is its page, never a link inside it. So a link keeps its
9words and loses its address (:func:`readable`). Images keep theirs: a document
10may show a picture the knowledge holds, and only an address the run saw may
11be shown (``answer_artifacts.drop_unseen_images``). Code is left as written.
13Crawled pages also opened with their navigation as extracted — "Skip to main
14content" and a run of headings with no words (the NCSC's overview page held
15nothing else). :func:`tidy_lines` drops both, at extraction
16(``web_extract.extract_markdown``) for pages crawled from now on, and in
17:func:`readable` for what is already stored.
18"""
20from __future__ import annotations
22import re
24#: Code is left as written: fences (an unterminated one runs to the end) and
25#: inline spans.
26_CODE = re.compile(r"```.*?(?:```|$)|`[^`\n]*`", re.DOTALL)
27#: A link that is not an image, its target bare (one level of parentheses
28#: inside, as Wikipedia's have) or in angle brackets, with an optional title.
29_LINK = re.compile(
30 r"(?<![!\\])\[([^\[\]\n]*)\]\(\s*"
31 r"(?:<[^<>\n]*>|[^\s()<>]*(?:\([^\s()]*\)[^\s()<>]*)*)"
32 r"""(?:\s+(?:"[^"\n]*"|'[^'\n]*'))?\s*\)"""
33)
34#: A heading with no words: navigation an extractor kept the markup of.
35_EMPTY_HEADING = re.compile(r"^[ \t]*#{1,6}[ \t]*$", re.MULTILINE)
36#: A page's skip link, alone on its line.
37_SKIP_LINK = re.compile(
38 r"^[ \t]*\[?skip to (?:the )?(?:main )?(?:content|navigation|main)\]?[ \t]*$",
39 re.IGNORECASE | re.MULTILINE,
40)
41_BLANK_RUN = re.compile(r"\n[ \t]*\n(?:[ \t]*\n)+")
44def _outside_code(text: str, repair) -> str:
45 out: list[str] = []
46 last = 0
47 for code in _CODE.finditer(text):
48 out.append(repair(text[last : code.start()]))
49 out.append(code.group(0))
50 last = code.end()
51 out.append(repair(text[last:]))
52 return "".join(out)
55def _tidy(prose: str) -> str:
56 out = _SKIP_LINK.sub("", _EMPTY_HEADING.sub("", prose))
57 return _BLANK_RUN.sub("\n\n", out)
60def _unlinked(prose: str) -> str:
61 return _LINK.sub(lambda m: m.group(1).strip(), prose)
64def tidy_lines(text: str) -> str:
65 """``text`` without its empty headings and skip links, and with no more
66 than one blank line in a row where they were. Code is left as written."""
67 if not text:
68 return text
69 return _outside_code(text, _tidy).strip()
72def readable(text: str) -> str:
73 """``text`` as a model reads it: each link its words alone, and the
74 page chrome :func:`tidy_lines` drops gone. Idempotent."""
75 if not text:
76 return text
77 return _outside_code(text, lambda prose: _tidy(_unlinked(prose))).strip()
80__all__ = ["readable", "tidy_lines"]