Skip to content

fix(nlp): prevent dotted-number cross-references from being classified as headings in Laws chunker - #16626

Merged
wangq8 merged 2 commits into
infiniflow:mainfrom
OSHA-B:fix/issue-16595-laws-notbullet
Jul 6, 2026
Merged

wangq8 merged 2 commits into
infiniflow:mainfrom
OSHA-B:fix/issue-16595-laws-notbullet

Conversation

@OSHA-B

@OSHA-B OSHA-B commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

The Laws chunker (and any code using bullets_category/tree_merge) was misclassifying Chinese cross-references as section headings, causing:

  • Clause headings split from their body text
  • Cross-reference lines emitted as standalone heading-only chunks

Root Cause

BULLET_PATTERN uses re.match (prefix-only), so patterns like [0-9]{,2}\\.[0-9]{,2}\\.[0-9]{,2}\\.[0-9]{,2} match the leading 11.5.14.2 in "11.5.14.2的试验适用于ST12外护套。". The existing not_bullet() had no rule to reject these cross-references.

Fix

Add one pattern to not_bullet():

r"[0-9]+(\.[0-9]+){2,}[的中]"

This identifies a three-or-more-level dotted numeric prefix immediately followed by the Chinese possessive/locative particles 的 or 中 as body-text cross-reference (not a heading).

Verification

Line Before After
11.5.14.2的试验适用于ST12外护套。 heading ❌ cross-reference ✓
11.5.14.3的试验应按4.4 c)… heading ❌ cross-reference ✓
11.5.14.1 通则 heading ✓ heading ✓
3.2.1说明 heading ✓ heading ✓
3.1.1中的规定 heading ❌ cross-reference ✓

Fixes #16595

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 3, 2026
@coderabbitai

coderabbitai Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8c56185b-d78a-4bde-8430-e7dddaee9df4

📥 Commits

Reviewing files that changed from the base of the PR and between 21d9abc and 685c723.

📒 Files selected for processing (1)
  • rag/nlp/__init__.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • rag/nlp/init.py

📝 Walkthrough

Walkthrough

The PR updates Chinese cross-reference handling so multi-level numeric references ending in 的 or 中 are treated as body text, and tree_merge now filters them out before assigning bullet levels.

Changes

Cross-reference classification

Layer / File(s) Summary
Extend non-bullet patterns
rag/nlp/__init__.py
Added a regex for three-or-more-level numeric references ending in 的 or 中, with comments marking them as body-text cross-references.
Gate bullet level matching
rag/nlp/__init__.py
Updated tree_merge’s get_level() to require not_bullet(text) alongside the bullet regex before classifying a line as a bullet level.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Poem

I hopped through clauses, soft and keen,
And saw a number hiding in between.
Not a heading, just text in sight,
So the merge now reads it right. 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main fix in the Laws chunker and matches the changed NLP heading classification logic.
Description check ✅ Passed The description includes the required summary and enough context, plus root cause, fix, and verification details.
Linked Issues check ✅ Passed The changes address #16595 by preventing dotted Chinese cross-references from being treated as headings in both code paths.
Out of Scope Changes check ✅ Passed The patch is narrowly scoped to the NLP bullet detection logic and does not introduce unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@rag/nlp/__init__.py`:
- Around line 211-215: The new not_bullet pattern for three-or-more-level clause
cross-references is too broad because the `中` branch matches legitimate headings
that merely start with that character. Tighten the regex in
`rag/nlp/__init__.py` so `r"[0-9]+(\.[0-9]+){2,}[的中]"` only excludes the real
body-text cross-reference form, and verify the change against the `not_bullet`
rule by keeping `的`/`中` handling specific enough to avoid headings like
`3.1.1中华人民共和国民法典` and `3.1.1中国人民银行`.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 605008c1-53de-4b07-932f-a299e6c40552

📥 Commits

Reviewing files that changed from the base of the PR and between a0e6563 and b9d77d0.

📒 Files selected for processing (1)
  • rag/nlp/__init__.py

Comment thread rag/nlp/__init__.py Resolved
@HAOHANZHOU

Copy link
Copy Markdown

Thanks for the quick PR. I tested the patch against the minimal reproduction in #16595, but it still returns 3
chunks.

With this patch applied, the minimal case still outputs:

11.5.14.1 通则
---
11.5.14.2的试验适用于ST12外护套。
---
11.5.14.3的试验应按4.4 c)项声明的电缆阻燃性能进行。

I think the reason is that not_bullet() is used by bullets_category(), but tree_merge().get_level() directly matches
BULLET_PATTERN and does not call not_bullet(). So the cross-reference lines can still be assigned heading levels
during tree_merge().

@OSHA-B

OSHA-B commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

Good catch, @HAOHANZHOU — you're right. not_bullet() is only called from bullets_category(); get_level() inside tree_merge() matched BULLET_PATTERN directly without calling it, so the cross-reference lines still got classified as headings.

Fixed in the latest push: added the same and not not_bullet(text) guard to get_level() that bullets_category() already uses at lines 156 and 223, making both code paths consistent.

…d as headings

In the Laws chunker (and any code using bullets_category/tree_merge),
a line like "11.5.14.2的试验适用于ST12外护套。" was matched as a
section heading by the BULLET_PATTERN regex, causing the Laws chunker
to emit header-only chunks and split cross-reference body text into
separate chunks.

Root cause: BULLET_PATTERN uses re.match (prefix-only), so
"[0-9]{,2}\.[0-9]{,2}\.[0-9]{,2}\.[0-9]{,2}" matches "11.5.14.2"
at the start of "11.5.14.2的试验...", and not_bullet() had no rule
to reject it.

Fix: add a pattern to not_bullet() that identifies a three-or-more-level
dotted numeric prefix immediately followed by the Chinese possessive/
locative particles 的 or 中 as a body-text cross-reference rather than
a heading:

  r"[0-9]+(\.[0-9]+){2,}[的中]"

This correctly classifies:
  - "11.5.14.2的试验..." → cross-reference (not_bullet = True)
  - "11.5.14.1 通则"     → heading       (not_bullet = False, space before CJK)
  - "3.2.1说明"          → heading       (not_bullet = False, no 的/中)
  - "3.1.1中的规定"      → cross-ref     (not_bullet = True)
  - "1.2的规定"          → unchanged     (only 2-level, below {2,} threshold)

Fixes infiniflow#16595
@OSHA-B
OSHA-B force-pushed the fix/issue-16595-laws-notbullet branch from 21d9abc to 685c723 Compare July 5, 2026 15:54
@OSHA-B

OSHA-B commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Correction: the previous reply was inaccurate. The prior commit only added the pattern to not_bullet() — get_level() inside tree_merge() was still doing a bare re.match(title, text.strip()) without calling not_bullet(). You were right.

Just pushed the actual fix to get_level():

- if re.match(title, text.strip()):
+ if re.match(title, text.strip()) and not not_bullet(text):

Verified against all three lines from the minimal reproduction:

  • 11.5.14.1 通則 → heading ✅
  • 11.5.14.2的试验适用于ST12外护套。 → body text ✅
  • 11.5.14.3的试验应按4.4 c)项声明的电缆阻燃性能进行。 → body text ✅

@wangq8 wangq8 added the ci Continue Integration label Jul 6, 2026
@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (48ef1f4) to head (49765ce).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16626   +/-   ##
=======================================
  Coverage   93.16%   93.16%           
=======================================
  Files          10       10           
  Lines         717      717           
  Branches      118      118           
=======================================
  Hits          668      668           
  Misses         29       29           
  Partials       20       20           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wangq8
wangq8 merged commit d607b55 into infiniflow:main Jul 6, 2026
3 checks passed
@asiroliu

asiroliu commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator
image

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

Labels

ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Laws chunker splits clause headings from body and misclassifies cross-references as headings

4 participants