Skip to content

Port remove_contents_table and short-line filter to GO - #17216

Closed
qinling0210 wants to merge 1 commit into
infiniflow:mainfrom
qinling0210:port_to_go
Closed

qinling0210 wants to merge 1 commit into
infiniflow:mainfrom
qinling0210:port_to_go

Conversation

@qinling0210

Copy link
Copy Markdown
Contributor

Summary

Fix the following issues:


🟡 P1 — remove_contents_table(中影响)

Python laws.py 在 tree_merge 前调用了 remove_contents_table(sections, eng)(rag/nlp/init.py:937),用于移除目录区域。

行为:检测到 "Contents"/"目录"/"TABLE OF CONTENTS" 等标记行后,删除该行及后续 128 行内匹配相同前缀的行。

为什么影响:目录条目(如"第一章"、"1.1"等)恰好匹配标题正则。不加此步骤:

  • 目录条目被当作真实标题节点
  • chunk 划分被这些假标题打断
  • 产出大量无意义的小 chunk

触发条件:文档包含目录,且目录条目格式与正文标题格式一致。

修复位置:应放在 Parser 输出后、TitleChunker 输入前。需要决定是作为 Parser 的预处理还是 TitleChunker 的预处理。


🟢 P3 — 短行过滤(低)

Python tree_merge 内部过滤:
sections = [(t, o) for t, o in sections if t and len(t.split("@")[0].strip()) > 1 and not re.match(r"[0-9]+$", t.split("@")[0].strip())]

  • 去掉空行
  • 去掉长度 ≤ 1 的行
  • 去掉纯数字行

Go TitleChunker 没有此过滤。纯数字行如 "1" 或 "2" 可能被匹配为标题(匹配 [0-9]{1,2}[. 、] 中的 [0-9]{1,2}),引入无关标题节点。

修复成本:极低,在 resolveTitleLevels 加一行过滤即可。

@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jul 22, 2026
@coderabbitai

coderabbitai Bot commented Jul 22, 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: 8988ea19-9773-4968-91f3-aecccda27744

📥 Commits

Reviewing files that changed from the base of the PR and between de1eeaf and dd3cfa4.

📒 Files selected for processing (6)
  • internal/ingestion/component/chunker/group.go
  • internal/ingestion/component/chunker/hierarchy.go
  • internal/ingestion/component/chunker/title.go
  • internal/ingestion/component/chunker/toc.go
  • internal/ingestion/component/chunker/toc_test.go
  • internal/parser/parser/pdf_postprocess.go
🚧 Files skipped from review as they are similar to previous changes (6)
  • internal/ingestion/component/chunker/group.go
  • internal/ingestion/component/chunker/hierarchy.go
  • internal/parser/parser/pdf_postprocess.go
  • internal/ingestion/component/chunker/title.go
  • internal/ingestion/component/chunker/toc_test.go
  • internal/ingestion/component/chunker/toc.go

📝 Walkthrough

Walkthrough

The chunker now removes TOC entries and short or numeric lines before heading analysis. New helpers support language-aware TOC matching, while PDF postprocessing adopts the same normalization and prefix behavior. Extensive tests cover TOC detection, filtering, language classification, and boundary cases.

Changes

TOC and heading preprocessing

Layer / File(s) Summary
TOC and line-filter helpers
internal/ingestion/component/chunker/toc.go, internal/ingestion/component/chunker/title.go, internal/ingestion/component/chunker/toc_test.go
Adds language-aware TOC removal, text normalization, prefix extraction, English classification, and short-or-numeric line filtering with extensive unit tests.
Chunker preprocessing integration
internal/ingestion/component/chunker/group.go, internal/ingestion/component/chunker/hierarchy.go
Applies TOC and short-line filtering before group and hierarchy heading processing.
PDF TOC preprocessing alignment
internal/parser/parser/pdf_postprocess.go
Normalizes TOC text and selects English or non-English prefix logic during PDF TOC removal.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ExtractedRecords
  participant TOCFilter
  participant ShortNumericFilter
  participant ChunkBuilder
  ExtractedRecords->>TOCFilter: remove TOC headers and entries
  TOCFilter->>ShortNumericFilter: pass remaining records
  ShortNumericFilter->>ChunkBuilder: pass filtered records
  ChunkBuilder->>ChunkBuilder: build group or hierarchy chunks
Loading

Poem

I’m a rabbit in the contents-page rain,
Trimming tiny numbers from the train.
TOC paths hop out of sight,
English and CJK prefixes align just right,
Clean chunks bounce through the code again! 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: porting TOC removal and short-line filtering to Go.
Description check ✅ Passed The description matches the template and gives a clear summary plus background for both TOC cleanup and short-line filtering.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@qinling0210 qinling0210 self-assigned this Jul 22, 2026

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

🧹 Nitpick comments (1)
internal/ingestion/component/chunker/group.go (1)

115-119: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Remove the laws.py / book.py migration-history breadcrumbs from the added comments. Both chunkers gained the same comment referencing the Python source files; the coding guideline asks that Go comments track the current runtime path rather than migration history. Keep a concise intent note and drop the cross-repo references.

  • internal/ingestion/component/chunker/group.go#L115-L119: trim the "Mirrors Python's remove_contents_table in laws.py / book.py" wording.
  • internal/ingestion/component/chunker/hierarchy.go#L148-L152: apply the identical trim.
🤖 Prompt for 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.

In `@internal/ingestion/component/chunker/group.go` around lines 115 - 119, Trim
the comments immediately above removeContentsTable in
internal/ingestion/component/chunker/group.go lines 115-119 and
internal/ingestion/component/chunker/hierarchy.go lines 148-152 by removing the
references to Python files and migration history, while retaining a concise note
explaining that TOC entries are removed before heading detection.

Source: Coding guidelines

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

Nitpick comments:
In `@internal/ingestion/component/chunker/group.go`:
- Around line 115-119: Trim the comments immediately above removeContentsTable
in internal/ingestion/component/chunker/group.go lines 115-119 and
internal/ingestion/component/chunker/hierarchy.go lines 148-152 by removing the
references to Python files and migration history, while retaining a concise note
explaining that TOC entries are removed before heading detection.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7602186c-b742-4a29-9cfa-f27c2f3eaf61

📥 Commits

Reviewing files that changed from the base of the PR and between b175ed5 and de1eeaf.

📒 Files selected for processing (6)
  • internal/ingestion/component/chunker/group.go
  • internal/ingestion/component/chunker/hierarchy.go
  • internal/ingestion/component/chunker/title.go
  • internal/ingestion/component/chunker/toc.go
  • internal/ingestion/component/chunker/toc_test.go
  • internal/parser/parser/pdf_postprocess.go

@qinling0210 qinling0210 added the ci Continue Integration label Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant