Repository navigation
Return highlights in importance order - #90
Open
linhongyu510 wants to merge 1 commit into
Open
linhongyu510 wants to merge 1 commit into
linhongyu510 wants to merge 1 commit into
Conversation
Highlights.build ranks sections with textrank, filters out near-duplicates, then
truncated to topn. But the final line looked the selected uids back up against
the input:
return [text for uid, text in sections if uid in uids]
That iterates sections, so the result came back in input order and the ranking
was discarded. Both docstrings promise otherwise - build says it "uses textrank
to find sections with the highest importance", and Query.highlights says it
"Returns top ranked sections by importance".
The order is user visible. paperai query prints the highlights one per line
(query.py:247) and the report writers emit them as a Highlights section
(report/common.py:81), so the most important highlight could be shown last.
With four sections where input order differs from textrank order, build returned
['s1', 's4'] while textrank had ranked s4 first; it now returns ['s4', 's1'].
Index by a uid to text dict instead, and take topn from the ranked list. This
also drops the O(n^2) linear scan that looked up each ranked uid. The input is
materialized first since it is now iterated more than once - callers pass
dict.values() today, but the signature accepts any iterable.
ErenAta16
approved these changes
Nov 25, 2026
ErenAta16
left a comment
There was a problem hiding this comment.
I checked the ordering through Query.highlights with four sections about transit, maps and library holdings. TextRank put the network section first and the map section second. Once the similar map section was filtered out, base 1830696 returned [transport, network]. At bf5c381 the same input returned [network, transport]. The selected sections stayed the same and the most important one moved to the first position.
I also ran the three tests from this branch against both versions. Base failed the ordering assertion and raised IndexError on the single-use iterator. All three passed at the PR head.
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.
What's wrong
Highlights.builddoes three things: rank sections with textrank, dropnear-duplicates, keep the top
n. The last line then undoes the first:uidsholds the ranked order, but the return iteratessections, so thehighlights come back in input order and the ranking is thrown away.
Both docstrings promise otherwise:
Highlights.build— "uses textrank to find sections with the highestimportance across the input list"
Query.highlights(query.py:78) — "Returns top ranked sections byimportance"
Why it matters
The order is what the user sees.
paperai queryprints highlights one per line:and the report writers emit them as a
Highlightssection(
report/common.py:81). So the highest-ranked highlight could be printed last.Measured against the real class with four sections whose input order differs from
their textrank order:
s4is what textrank ranked first; it was being shown second. (s2is correctlydropped by the existing near-duplicate rule — it restates
s4.)The fix
Look the text up through a dict and take
topnfrom the ranked list:Two side effects worth calling out:
[text for u, text in sections if u == uid][0]scannedthe whole input for every ranked section; the dict removes that O(n²) scan.
dict(sections)andtextrank(sections)), so itis materialized with
list()first. Callers passdict.values()today, whichis re-iterable, but the signature accepts any iterable and a one-shot iterator
would otherwise break —
testSingleUseIterablepins that.The de-duplication rule and the
topnlimit are unchanged.Tests
New
test/python/testhighlights.py, three cases:testImportanceOrder— highlights follow textrank's relative order, and thetop-ranked section comes first. Includes a sanity assertion that the fixture
actually exercises a reordering, so the test cannot pass trivially.
testRespectsTopn— the limit still holds (passes before and after; it pinsbehaviour that must not change).
testSingleUseIterable—buildstill works when handed an iterator.Verification
The full suite is the real one — it downloads the v2.5.0 test fixtures and runs
the index/query/report paths end to end (532s locally).
Test method names were kept under pylint's 30-character limit rather than adding
a
disable, matching the existing test files.Reverse-verified: with
highlights.pyreverted and the new tests kept,testImportanceOrderandtestSingleUseIterablefail whiletestRespectsTopnstill passes.
AI disclosure
This change was prepared with AI assistance. The defect was found by reading
build's final line against its own docstring, then confirmed by running thereal class and comparing its output to
textrank's ordering; the quotedorderings, line references and test results were produced by running the code.