Skip to content

Return highlights in importance order - #90

Open
linhongyu510 wants to merge 1 commit into
neuml:masterfrom
linhongyu510:fix/highlights-importance-order
Open

linhongyu510 wants to merge 1 commit into
neuml:masterfrom
linhongyu510:fix/highlights-importance-order

Conversation

@linhongyu510

Copy link
Copy Markdown

What's wrong

Highlights.build does three things: rank sections with textrank, drop
near-duplicates, keep the top n. The last line then undoes the first:

uids = [uid for uid, _ in results][:topn]

# Get related text for each match
return [text for uid, text in sections if uid in uids]

uids holds the ranked order, but the return iterates sections, so the
highlights come back in input order and the ranking is thrown away.

Both docstrings promise otherwise:

  • Highlights.build — "uses textrank to find sections with the highest
    importance across the input list"
  • Query.highlights (query.py:78) — "Returns top ranked sections by
    importance
    "

Why it matters

The order is what the user sees. paperai query prints highlights one per line:

for highlight in highlights:
    console.print(f"[bright_blue] - {Query.text(highlight)}[/bright_blue]", ...)

and the report writers emit them as a Highlights section
(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:

textrank order: ['s4', 's2', 's1', 's3']
build returned: ['s1', 's4']      # before
build returned: ['s4', 's1']      # after

s4 is what textrank ranked first; it was being shown second. (s2 is correctly
dropped by the existing near-duplicate rule — it restates s4.)

The fix

Look the text up through a dict and take topn from the ranked list:

lookup = dict(sections)
...
return [lookup[uid] for uid, _ in results[:topn]]

Two side effects worth calling out:

  • The old per-iteration [text for u, text in sections if u == uid][0] scanned
    the whole input for every ranked section; the dict removes that O(n²) scan.
  • The input is now read twice (dict(sections) and textrank(sections)), so it
    is materialized with list() first. Callers pass dict.values() today, which
    is re-iterable, but the signature accepts any iterable and a one-shot iterator
    would otherwise break — testSingleUseIterable pins that.

The de-duplication rule and the topn limit are unchanged.

Tests

New test/python/testhighlights.py, three cases:

  • testImportanceOrder — highlights follow textrank's relative order, and the
    top-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 pins
    behaviour that must not change).
  • testSingleUseIterable — build still works when handed an iterator.

Verification

unittest discover -p testhighlights.py                    3 tests, OK
make data && unittest discover -s ./test/python           26 tests, OK
black 24.10.0 --check --line-length 150                   2 files unchanged
pylint 3.3.1 (repo's pre-commit flags + .pylintrc)        10.00/10

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.py reverted and the new tests kept,
testImportanceOrder and testSingleUseIterable fail while testRespectsTopn
still 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 the
real class and comparing its output to textrank's ordering; the quoted
orderings, line references and test results were produced by running the code.

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 ErenAta16 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.

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants