Skip to content

fix(clean): don't split text where an inline element ends - #370

Open
seragAhmed wants to merge 1 commit into
freelawproject:mainfrom
seragAhmed:fix/364-html-inline-token-split
Open

seragAhmed wants to merge 1 commit into
freelawproject:mainfrom
seragAhmed:fix/364-html-inline-token-split

Conversation

@seragAhmed

Copy link
Copy Markdown
Contributor

Fixes #364.

Fixes

This fixes the html cleaner putting a space wherever an inline element ends, which splits tokens and drops pin cites after linked citations (#364).

Summary

clean.html selected visible text nodes with an XPath query and returned " ".join(text), so adjacent text nodes always got a space between them, even when the markup does not separate them. <a>410 U.S. 113</a>, 115 became 410 U.S. 113 , 115, so the pin cite ended up in extra as , 115, and <em>Id</em>. at 263 became Id . at 263, which is not found at all.

This PR replaces the XPath query with one walk over the tree (lxml.etree.iterwalk) that keeps the same visible text but only puts a space where the markup separates it:

  • at the start of an element (so <em>Jin Fuey Moy</em><em>v. United States,</em> and 17<i>C.F.R.</i> still get a space);
  • around block-level elements and line breaks (p, div, br, li, td, headings, blockquote, pre, ...);
  • around footnote marks (sup, footnotemark, class="footnotemark") and class="page-label";
  • where whitespace-only text lies between.

Text that continues after the end of an inline element is joined with nothing, and so is text around hidden text (page-number, star-pagination), so can<page-number>*642</page-number>not reads cannot. Which text counts as visible is unchanged (same hidden tags, same star-pagination rule, comments and processing instructions are still skipped but their tails are kept).

With the example from the issue:

before: 'The board relied on Conn. Gen. Stat. § 10 -236b(f), and see 410 U.S. 113 , 115 (1973).'
after:  'The board relied on Conn. Gen. Stat. § 10-236b(f), and see 410 U.S. 113, 115 (1973).'

and 410 U.S. 113 now gets pin_cite='115' instead of extra=', 115'.

Effect on the benchmark sample

I ran get_citations(markup_text=..., clean_steps=["html", "inline_whitespace"]) over the one-percent CourtListener sample the benchmark uses (779 opinions, 656 with markup), once with the old cleaner and once with the new one:

  • The visible characters are identical in all 656 markup opinions (only the spaces between text nodes differ).
  • Total citations: 15,992 before, 15,984 after.
  • 127 citations gained a pin cite; 2 lost one (<i>Atwood</i>, § 44 is now read as the reference Atwood without the § 44 pin cite, the same as the plain text Atwood, § 44).
  • 10 citations are newly found: 9 Id. citations and 1 journal citation.
  • 18 citations are no longer found. 16 are (<i>Id.</i>) and <i>Id.</i>;: they were only found because of the added space, and the tokenizer does not find (Id.) or Id.; in plain text either, since ID_REGEX needs whitespace around it. One is a false positive (17 <i>A.D.</i>2<i>d</i> 225 used to match as 17 A.D. 2), and one is a reference citation (Buckner) next to a full citation of the same case.

Some citations also change their matched text, e.g. 735 A. 2d 606 becomes 735 A.2d 606 and supra becomes supra,, the same as in plain text.

The Id. cases inside parentheses or before a semicolon could be picked up by relaxing ID_REGEX's boundaries, but that changes plain-text behavior too, so I left it out of this PR.

Testing

  • New clean_text cases in tests/test_UtilsTest.py for text continuing after an inline element (</a>, 115, </a>-236b(f), <em>Id</em>., <i>L.Ed.</i>2d, nested <i><b>Id</b></i>., page-number), plus guard cases showing block elements, line breaks, table cells, list items, element starts, sup and whitespace-only text still separate words.
  • New get_citations cases in test_find_citations for the issue's pin cite (<a class="citation">410 U.S. 113</a>, 115 (1973) gives pin_cite='115') and for <em>Id</em>. at 263, run with all three tokenizers.
  • Without the change to eyecite/clean.py, the new tests fail (test_clean_text and 6 test_find_citations subtests, 2 cases x 3 tokenizers); with it, the full suite passes: python -m unittest discover -s tests -p 'test_*.py' ran 55 tests, OK.
  • pre-commit (ruff, ruff-format and the other hooks) passes on the changed files. mypy . reports the same 3 errors in eyecite/tokenizers.py with and without this change (my environment was not installed from uv.lock, so these may not show in CI).

AI Disclosure

  • No AI tools were used to create the content of this PR.
  • Parts of this PR were created with the help of an AI tool, and I have carefully reviewed all of its content and take full responsibility for it.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@CLAassistant

CLAassistant commented Nov 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

The `html` cleaner joined every visible text node with a space, so a
token that an inline element ends in the middle of came out split:
`<a>410 U.S. 113</a>, 115` became `410 U.S. 113 , 115`, which loses the
pin cite, and `<em>Id</em>. at 263` became `Id . at 263`, which is not
found at all.

Walk the tree once instead and put a space only where the markup
separates text: at the start of an element, around block-level
elements, line breaks and footnote marks, and where whitespace-only
text lies between. Text after the end of an inline element, or around
hidden text such as `<page-number>`, is joined with nothing. The set of
visible text is unchanged.

Fixes freelawproject#364
@seragAhmed
seragAhmed force-pushed the fix/364-html-inline-token-split branch from d2f8357 to a321ac4 Compare November 22, 2026 04:06

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Needs triage

Development

Successfully merging this pull request may close these issues.

clean.html puts a space inside a token wherever inline markup ends, which drops pin cites after linked citations

2 participants