Repository navigation
fix(clean): don't split text where an inline element ends - #370
Open
seragAhmed wants to merge 1 commit into
Open
seragAhmed wants to merge 1 commit into
seragAhmed wants to merge 1 commit into
Conversation
1 of 2 tasks
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
force-pushed
the
fix/364-html-inline-token-split
branch
from
November 22, 2026 04:06
d2f8357 to
a321ac4
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #364.
Fixes
This fixes the
htmlcleaner putting a space wherever an inline element ends, which splits tokens and drops pin cites after linked citations (#364).Summary
clean.htmlselected 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>, 115became410 U.S. 113 , 115, so the pin cite ended up inextraas, 115, and<em>Id</em>. at 263becameId . 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:<em>Jin Fuey Moy</em><em>v. United States,</em>and17<i>C.F.R.</i>still get a space);p,div,br,li,td, headings,blockquote,pre, ...);sup,footnotemark,class="footnotemark") andclass="page-label";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), socan<page-number>*642</page-number>notreadscannot. Which text counts as visible is unchanged (same hidden tags, samestar-paginationrule, comments and processing instructions are still skipped but their tails are kept).With the example from the issue:
and
410 U.S. 113now getspin_cite='115'instead ofextra=', 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:<i>Atwood</i>, § 44is now read as the referenceAtwoodwithout the§ 44pin cite, the same as the plain textAtwood, § 44).Id.citations and 1 journal citation.(<i>Id.</i>)and<i>Id.</i>;: they were only found because of the added space, and the tokenizer does not find(Id.)orId.;in plain text either, sinceID_REGEXneeds whitespace around it. One is a false positive (17 <i>A.D.</i>2<i>d</i> 225used to match as17 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 606becomes735 A.2d 606andsuprabecomessupra,, the same as in plain text.The
Id.cases inside parentheses or before a semicolon could be picked up by relaxingID_REGEX's boundaries, but that changes plain-text behavior too, so I left it out of this PR.Testing
clean_textcases intests/test_UtilsTest.pyfor 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,supand whitespace-only text still separate words.get_citationscases intest_find_citationsfor the issue's pin cite (<a class="citation">410 U.S. 113</a>, 115 (1973)givespin_cite='115') and for<em>Id</em>. at 263, run with all three tokenizers.eyecite/clean.py, the new tests fail (test_clean_textand 6test_find_citationssubtests, 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 ineyecite/tokenizers.pywith and without this change (my environment was not installed fromuv.lock, so these may not show in CI).AI Disclosure