docloc 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docloc-0.1.0/.gitignore +9 -0
- docloc-0.1.0/.workflow/docloc-ci.yml +31 -0
- docloc-0.1.0/LICENSE +21 -0
- docloc-0.1.0/PKG-INFO +195 -0
- docloc-0.1.0/README.md +152 -0
- docloc-0.1.0/pyproject.toml +79 -0
- docloc-0.1.0/scripts/verify_release.py +212 -0
- docloc-0.1.0/src/docloc/__init__.py +175 -0
- docloc-0.1.0/src/docloc/__main__.py +8 -0
- docloc-0.1.0/src/docloc/chunking.py +379 -0
- docloc-0.1.0/src/docloc/cli.py +185 -0
- docloc-0.1.0/src/docloc/errors.py +35 -0
- docloc-0.1.0/src/docloc/models.py +359 -0
- docloc-0.1.0/src/docloc/parsers/__init__.py +24 -0
- docloc-0.1.0/src/docloc/parsers/base.py +87 -0
- docloc-0.1.0/src/docloc/parsers/docx.py +133 -0
- docloc-0.1.0/src/docloc/parsers/pdf.py +176 -0
- docloc-0.1.0/src/docloc/parsers/registry.py +129 -0
- docloc-0.1.0/src/docloc/parsers/text.py +168 -0
- docloc-0.1.0/src/docloc/parsers/xlsx.py +125 -0
- docloc-0.1.0/src/docloc/preview.py +363 -0
- docloc-0.1.0/src/docloc/py.typed +0 -0
- docloc-0.1.0/tests/test_chunking.py +199 -0
- docloc-0.1.0/tests/test_docloc_parsers.py +324 -0
- docloc-0.1.0/tests/test_optional_deps_and_cli.py +179 -0
- docloc-0.1.0/tests/test_preview.py +167 -0
docloc-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
version: '1.0'
|
|
2
|
+
name: docloc-ci
|
|
3
|
+
displayName: docloc CI & Release Verification
|
|
4
|
+
triggers:
|
|
5
|
+
push:
|
|
6
|
+
branches:
|
|
7
|
+
precise:
|
|
8
|
+
- main
|
|
9
|
+
pr:
|
|
10
|
+
branches:
|
|
11
|
+
precise:
|
|
12
|
+
- main
|
|
13
|
+
stages:
|
|
14
|
+
- name: verify
|
|
15
|
+
displayName: Verify & Package
|
|
16
|
+
strategy: naturally
|
|
17
|
+
trigger: auto
|
|
18
|
+
steps:
|
|
19
|
+
- step: build@python
|
|
20
|
+
name: verify_docloc
|
|
21
|
+
displayName: Verify docloc across Python 3.11 & 3.12
|
|
22
|
+
pythonVersion: '3.9'
|
|
23
|
+
commands:
|
|
24
|
+
- set -euo pipefail
|
|
25
|
+
- pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade uv
|
|
26
|
+
- export UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple"
|
|
27
|
+
- uv run --python 3.12 scripts/verify_release.py --python 3.11 --python 3.12 --out-dir dist
|
|
28
|
+
artifacts:
|
|
29
|
+
- name: docloc-dist
|
|
30
|
+
path:
|
|
31
|
+
- ./dist
|
docloc-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fanyixu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
docloc-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: docloc
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Document parsing and structure-aware chunking with original source locators
|
|
5
|
+
Project-URL: Homepage, https://gitee.com/fan-yixu/docloc
|
|
6
|
+
Project-URL: Repository, https://gitee.com/fan-yixu/docloc.git
|
|
7
|
+
Project-URL: Issues, https://gitee.com/fan-yixu/docloc/issues
|
|
8
|
+
Author-email: Fanyixu <fanmm666@qq.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: chunking,document-parsing,docx,locator,markdown,pdf,provenance,rag,xlsx
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Topic :: Text Processing
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Provides-Extra: all
|
|
25
|
+
Requires-Dist: openpyxl<4,>=3.1; extra == 'all'
|
|
26
|
+
Requires-Dist: pypdf<7,>=4.2; extra == 'all'
|
|
27
|
+
Requires-Dist: python-docx<2,>=1.1; extra == 'all'
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: mypy<2,>=1.11; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest<9,>=8.2; extra == 'dev'
|
|
31
|
+
Requires-Dist: ruff<1,>=0.5; extra == 'dev'
|
|
32
|
+
Requires-Dist: twine<7,>=6.1; extra == 'dev'
|
|
33
|
+
Provides-Extra: docx
|
|
34
|
+
Requires-Dist: python-docx<2,>=1.1; extra == 'docx'
|
|
35
|
+
Provides-Extra: office
|
|
36
|
+
Requires-Dist: openpyxl<4,>=3.1; extra == 'office'
|
|
37
|
+
Requires-Dist: python-docx<2,>=1.1; extra == 'office'
|
|
38
|
+
Provides-Extra: pdf
|
|
39
|
+
Requires-Dist: pypdf<7,>=4.2; extra == 'pdf'
|
|
40
|
+
Provides-Extra: xlsx
|
|
41
|
+
Requires-Dist: openpyxl<4,>=3.1; extra == 'xlsx'
|
|
42
|
+
Description-Content-Type: text/markdown
|
|
43
|
+
|
|
44
|
+
# docloc
|
|
45
|
+
|
|
46
|
+
`docloc` 是一个**零核心依赖**的通用 Python 文档解析与切块库,统一读取 Markdown、TXT、DOCX、XLSX 和 PDF,并为每个文本块保留可验证的真实原文位置(行号、页码、段落/表格序号、工作表与单元格范围),同时服务**全文搜索、文档预览和 RAG**。
|
|
47
|
+
|
|
48
|
+
## 核心特性
|
|
49
|
+
|
|
50
|
+
- **统一解析、按需安装**:核心仅依赖 Python 标准库(开箱支持 `.md` / `.markdown` / `.txt`),办公与 PDF 格式依赖通过可选 extras 按需安装。
|
|
51
|
+
- **真实原文坐标(绝不编造位置)**:每个块携带结构化 `Locator`(行号、页码、段落索引、表格索引、Sheet 与精确单元格范围及块内字符偏移)。
|
|
52
|
+
- **结构感知切块**:永不跨源块边界切分;中文按单字计数、`ERR-404` / `v1.2.3` / `src/app.py` 等技术标识符不被拦腰切断;自动过滤 PDF 目录点线等低信息噪声块。
|
|
53
|
+
- **表格携带表头**:支持逐行(`table_mode="row"`)与按 token 预算合并多行(`table_mode="budget"`)两种表格切块模式,每块自动重复表头且保留 Excel 真实行区间坐标(如 `A1:D1,A5:D5`)。
|
|
54
|
+
- **溯源校验与阅读预览**:`verify_locator()` 校验文本块与坐标的真实一致性;`build_reading_projection()` + `extract_context_window()` 为文档预览和引用悬停提供有界上下文窗口与高亮区间。
|
|
55
|
+
- **诚实失败与可扩展**:加密/损坏/纯扫描件 PDF 不会静默返回空成功,而是抛出带 `ErrorCode` 的 `ParseError`;通过 `OCRHook` 与 `ParserRegistry.register()` 可扩展 OCR、旧版 `.doc`/`.xls` 或自定义格式。
|
|
56
|
+
|
|
57
|
+
## 安装
|
|
58
|
+
|
|
59
|
+
要求 Python `>=3.11`。从仓库根目录安装本地包。
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
# 核心安装:零第三方依赖(支持 Markdown / TXT、切块、溯源校验、预览窗口)
|
|
63
|
+
pip install ./packages/docloc
|
|
64
|
+
|
|
65
|
+
# 按需安装办公与 PDF 解析依赖
|
|
66
|
+
pip install "./packages/docloc[docx]" # Word (.docx)
|
|
67
|
+
pip install "./packages/docloc[xlsx]" # Excel (.xlsx)
|
|
68
|
+
pip install "./packages/docloc[pdf]" # PDF 文本层 (.pdf)
|
|
69
|
+
pip install "./packages/docloc[office]" # DOCX + XLSX
|
|
70
|
+
pip install "./packages/docloc[all]" # 全部可选格式
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## 快速上手
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from docloc import (
|
|
77
|
+
build_reading_projection,
|
|
78
|
+
chunk_document,
|
|
79
|
+
extract_context_window,
|
|
80
|
+
parse_and_chunk,
|
|
81
|
+
parse_file,
|
|
82
|
+
verify_locator,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
# 1. 解析单个文件,获得带真实坐标的结构化块
|
|
86
|
+
doc = parse_file("docs/guide.md")
|
|
87
|
+
|
|
88
|
+
# 2. 结构感知切块(可选 table_mode="row" 或 "budget")
|
|
89
|
+
chunks = chunk_document(doc, target_tokens=300, overlap_tokens=50)
|
|
90
|
+
for chunk in chunks[:3]:
|
|
91
|
+
print(chunk.locator.format_label(), chunk.text)
|
|
92
|
+
|
|
93
|
+
# 3. 一步完成解析与切块
|
|
94
|
+
doc, chunks = parse_and_chunk("reports/metrics.xlsx", target_tokens=300, overlap_tokens=50)
|
|
95
|
+
|
|
96
|
+
# 4. 溯源证明:校验块内容哈希、字符偏移/行号/表格范围是否真实匹配原文档
|
|
97
|
+
verified_locator = verify_locator(chunks[0], doc)
|
|
98
|
+
|
|
99
|
+
# 5. 文档预览:构建阅读投影并提取引用前后有界上下文窗口
|
|
100
|
+
projection = build_reading_projection(doc)
|
|
101
|
+
ctx = extract_context_window(projection, chunks[0], before=4, after=8)
|
|
102
|
+
for win in ctx.windows:
|
|
103
|
+
for line in win.lines:
|
|
104
|
+
mark = "*" if line.cited else " "
|
|
105
|
+
print(f"{mark} {line.line:>4}: {line.text}")
|
|
106
|
+
|
|
107
|
+
# 6. 全文搜索 / Embedding:获取附带章节标题的检索文本(不篡改 chunk.text 原文)
|
|
108
|
+
text_for_index = chunks[0].search_text(include_heading=True)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Locator 坐标规范
|
|
112
|
+
|
|
113
|
+
| 源格式 | `LocatorKind` | 关键字段 | `format_label()` 示例 |
|
|
114
|
+
| --- | --- | --- | --- |
|
|
115
|
+
| Markdown | `markdown_lines` | `start_line`, `end_line`, `heading`, `char_start`, `char_end` | `L12-L24` |
|
|
116
|
+
| TXT | `text_lines` | `start_line`, `end_line`, `char_start`, `char_end` | `L1-L8` |
|
|
117
|
+
| DOCX 段落 | `word_paragraph` | `paragraph`(0-based), `heading`, `char_start`, `char_end` | `¶3` |
|
|
118
|
+
| DOCX 表格 | `word_table` | `table`(0-based), `heading` | `Table 1` |
|
|
119
|
+
| XLSX 工作表 | `excel_range` | `sheet`, `cell_range`, `heading` | `参数!A1:D1,A5:D5` |
|
|
120
|
+
| PDF 页面 | `pdf_page` | `page`(1-based), `char_start`, `char_end` | `p.5` |
|
|
121
|
+
|
|
122
|
+
## 错误码(`ErrorCode`)
|
|
123
|
+
|
|
124
|
+
所有解析失败统一抛出 `docloc.ParseError`,携带 `.code: ErrorCode` 与 `.message: str`:
|
|
125
|
+
|
|
126
|
+
| 错误码 | 含义 |
|
|
127
|
+
| --- | --- |
|
|
128
|
+
| `FILE_TOO_LARGE` | 文件体积超过 `ParseOptions.max_bytes` |
|
|
129
|
+
| `UNSUPPORTED_FORMAT` | 扩展名未注册解析器 |
|
|
130
|
+
| `UNSUPPORTED_ENCODING` | 文本编码无法通过 BOM 或回退链解码 |
|
|
131
|
+
| `PARSER_UNAVAILABLE` | 可选依赖未安装,错误消息会指出对应的 extra |
|
|
132
|
+
| `CORRUPT_DOCUMENT` | 文件结构损坏无法打开 |
|
|
133
|
+
| `ENCRYPTED_DOCUMENT` | PDF 已加密且密码无效 |
|
|
134
|
+
| `OCR_REQUIRED` | PDF 全部页面均无文本层且未提供 `OCRHook` |
|
|
135
|
+
| `OCR_FAILED` | 自定义 `OCRHook` 执行失败或未识别出任何文本 |
|
|
136
|
+
|
|
137
|
+
## 扩展点
|
|
138
|
+
|
|
139
|
+
### 接入 `OCRHook`(扫描版 PDF)
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from docloc import Locator, LocatorKind, ParseOptions, ParsedBlock, parse_file
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class MyOCRHook:
|
|
146
|
+
def extract_pages(
|
|
147
|
+
self, data: bytes, page_numbers: list[int], options: ParseOptions
|
|
148
|
+
) -> list[ParsedBlock]:
|
|
149
|
+
return [
|
|
150
|
+
ParsedBlock(
|
|
151
|
+
text=run_my_ocr(data, page),
|
|
152
|
+
locator=Locator(kind=LocatorKind.PDF_PAGE, page=page),
|
|
153
|
+
)
|
|
154
|
+
for page in page_numbers
|
|
155
|
+
]
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
doc = parse_file("scanned.pdf", ocr_hook=MyOCRHook())
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### 通过 `ParserRegistry.register()` 扩展自定义格式
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
from docloc import Locator, LocatorKind, ParseOptions, ParsedBlock, ParsedDocument, ParserCapability, ParserRegistry
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class CsvParser:
|
|
168
|
+
extensions = ("csv",)
|
|
169
|
+
|
|
170
|
+
def check_capability(self, kind: str) -> ParserCapability:
|
|
171
|
+
return ParserCapability(kind=kind, available=True)
|
|
172
|
+
|
|
173
|
+
def parse(self, data: bytes, options: ParseOptions, *, kind: str, source_name: str = "") -> ParsedDocument:
|
|
174
|
+
text = data.decode("utf-8")
|
|
175
|
+
return ParsedDocument(
|
|
176
|
+
blocks=[ParsedBlock(text=text, locator=Locator(kind=LocatorKind.TEXT_LINES, start_line=1, end_line=len(text.splitlines()) or 1))],
|
|
177
|
+
source_name=source_name,
|
|
178
|
+
format=kind,
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
registry = ParserRegistry()
|
|
183
|
+
registry.register(CsvParser())
|
|
184
|
+
doc = registry.parse_file("items.csv")
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## CLI
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
# 查看当前环境各格式可用性与缺失依赖提示
|
|
191
|
+
docloc capabilities
|
|
192
|
+
|
|
193
|
+
# 解析并切块,按 JSONL 输出(每行一个带 locator 的 chunk)
|
|
194
|
+
docloc parse guide.md --chunk --target 300 --overlap 50 --jsonl
|
|
195
|
+
```
|
docloc-0.1.0/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# docloc
|
|
2
|
+
|
|
3
|
+
`docloc` 是一个**零核心依赖**的通用 Python 文档解析与切块库,统一读取 Markdown、TXT、DOCX、XLSX 和 PDF,并为每个文本块保留可验证的真实原文位置(行号、页码、段落/表格序号、工作表与单元格范围),同时服务**全文搜索、文档预览和 RAG**。
|
|
4
|
+
|
|
5
|
+
## 核心特性
|
|
6
|
+
|
|
7
|
+
- **统一解析、按需安装**:核心仅依赖 Python 标准库(开箱支持 `.md` / `.markdown` / `.txt`),办公与 PDF 格式依赖通过可选 extras 按需安装。
|
|
8
|
+
- **真实原文坐标(绝不编造位置)**:每个块携带结构化 `Locator`(行号、页码、段落索引、表格索引、Sheet 与精确单元格范围及块内字符偏移)。
|
|
9
|
+
- **结构感知切块**:永不跨源块边界切分;中文按单字计数、`ERR-404` / `v1.2.3` / `src/app.py` 等技术标识符不被拦腰切断;自动过滤 PDF 目录点线等低信息噪声块。
|
|
10
|
+
- **表格携带表头**:支持逐行(`table_mode="row"`)与按 token 预算合并多行(`table_mode="budget"`)两种表格切块模式,每块自动重复表头且保留 Excel 真实行区间坐标(如 `A1:D1,A5:D5`)。
|
|
11
|
+
- **溯源校验与阅读预览**:`verify_locator()` 校验文本块与坐标的真实一致性;`build_reading_projection()` + `extract_context_window()` 为文档预览和引用悬停提供有界上下文窗口与高亮区间。
|
|
12
|
+
- **诚实失败与可扩展**:加密/损坏/纯扫描件 PDF 不会静默返回空成功,而是抛出带 `ErrorCode` 的 `ParseError`;通过 `OCRHook` 与 `ParserRegistry.register()` 可扩展 OCR、旧版 `.doc`/`.xls` 或自定义格式。
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
要求 Python `>=3.11`。从仓库根目录安装本地包。
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# 核心安装:零第三方依赖(支持 Markdown / TXT、切块、溯源校验、预览窗口)
|
|
20
|
+
pip install ./packages/docloc
|
|
21
|
+
|
|
22
|
+
# 按需安装办公与 PDF 解析依赖
|
|
23
|
+
pip install "./packages/docloc[docx]" # Word (.docx)
|
|
24
|
+
pip install "./packages/docloc[xlsx]" # Excel (.xlsx)
|
|
25
|
+
pip install "./packages/docloc[pdf]" # PDF 文本层 (.pdf)
|
|
26
|
+
pip install "./packages/docloc[office]" # DOCX + XLSX
|
|
27
|
+
pip install "./packages/docloc[all]" # 全部可选格式
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 快速上手
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from docloc import (
|
|
34
|
+
build_reading_projection,
|
|
35
|
+
chunk_document,
|
|
36
|
+
extract_context_window,
|
|
37
|
+
parse_and_chunk,
|
|
38
|
+
parse_file,
|
|
39
|
+
verify_locator,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# 1. 解析单个文件,获得带真实坐标的结构化块
|
|
43
|
+
doc = parse_file("docs/guide.md")
|
|
44
|
+
|
|
45
|
+
# 2. 结构感知切块(可选 table_mode="row" 或 "budget")
|
|
46
|
+
chunks = chunk_document(doc, target_tokens=300, overlap_tokens=50)
|
|
47
|
+
for chunk in chunks[:3]:
|
|
48
|
+
print(chunk.locator.format_label(), chunk.text)
|
|
49
|
+
|
|
50
|
+
# 3. 一步完成解析与切块
|
|
51
|
+
doc, chunks = parse_and_chunk("reports/metrics.xlsx", target_tokens=300, overlap_tokens=50)
|
|
52
|
+
|
|
53
|
+
# 4. 溯源证明:校验块内容哈希、字符偏移/行号/表格范围是否真实匹配原文档
|
|
54
|
+
verified_locator = verify_locator(chunks[0], doc)
|
|
55
|
+
|
|
56
|
+
# 5. 文档预览:构建阅读投影并提取引用前后有界上下文窗口
|
|
57
|
+
projection = build_reading_projection(doc)
|
|
58
|
+
ctx = extract_context_window(projection, chunks[0], before=4, after=8)
|
|
59
|
+
for win in ctx.windows:
|
|
60
|
+
for line in win.lines:
|
|
61
|
+
mark = "*" if line.cited else " "
|
|
62
|
+
print(f"{mark} {line.line:>4}: {line.text}")
|
|
63
|
+
|
|
64
|
+
# 6. 全文搜索 / Embedding:获取附带章节标题的检索文本(不篡改 chunk.text 原文)
|
|
65
|
+
text_for_index = chunks[0].search_text(include_heading=True)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Locator 坐标规范
|
|
69
|
+
|
|
70
|
+
| 源格式 | `LocatorKind` | 关键字段 | `format_label()` 示例 |
|
|
71
|
+
| --- | --- | --- | --- |
|
|
72
|
+
| Markdown | `markdown_lines` | `start_line`, `end_line`, `heading`, `char_start`, `char_end` | `L12-L24` |
|
|
73
|
+
| TXT | `text_lines` | `start_line`, `end_line`, `char_start`, `char_end` | `L1-L8` |
|
|
74
|
+
| DOCX 段落 | `word_paragraph` | `paragraph`(0-based), `heading`, `char_start`, `char_end` | `¶3` |
|
|
75
|
+
| DOCX 表格 | `word_table` | `table`(0-based), `heading` | `Table 1` |
|
|
76
|
+
| XLSX 工作表 | `excel_range` | `sheet`, `cell_range`, `heading` | `参数!A1:D1,A5:D5` |
|
|
77
|
+
| PDF 页面 | `pdf_page` | `page`(1-based), `char_start`, `char_end` | `p.5` |
|
|
78
|
+
|
|
79
|
+
## 错误码(`ErrorCode`)
|
|
80
|
+
|
|
81
|
+
所有解析失败统一抛出 `docloc.ParseError`,携带 `.code: ErrorCode` 与 `.message: str`:
|
|
82
|
+
|
|
83
|
+
| 错误码 | 含义 |
|
|
84
|
+
| --- | --- |
|
|
85
|
+
| `FILE_TOO_LARGE` | 文件体积超过 `ParseOptions.max_bytes` |
|
|
86
|
+
| `UNSUPPORTED_FORMAT` | 扩展名未注册解析器 |
|
|
87
|
+
| `UNSUPPORTED_ENCODING` | 文本编码无法通过 BOM 或回退链解码 |
|
|
88
|
+
| `PARSER_UNAVAILABLE` | 可选依赖未安装,错误消息会指出对应的 extra |
|
|
89
|
+
| `CORRUPT_DOCUMENT` | 文件结构损坏无法打开 |
|
|
90
|
+
| `ENCRYPTED_DOCUMENT` | PDF 已加密且密码无效 |
|
|
91
|
+
| `OCR_REQUIRED` | PDF 全部页面均无文本层且未提供 `OCRHook` |
|
|
92
|
+
| `OCR_FAILED` | 自定义 `OCRHook` 执行失败或未识别出任何文本 |
|
|
93
|
+
|
|
94
|
+
## 扩展点
|
|
95
|
+
|
|
96
|
+
### 接入 `OCRHook`(扫描版 PDF)
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from docloc import Locator, LocatorKind, ParseOptions, ParsedBlock, parse_file
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class MyOCRHook:
|
|
103
|
+
def extract_pages(
|
|
104
|
+
self, data: bytes, page_numbers: list[int], options: ParseOptions
|
|
105
|
+
) -> list[ParsedBlock]:
|
|
106
|
+
return [
|
|
107
|
+
ParsedBlock(
|
|
108
|
+
text=run_my_ocr(data, page),
|
|
109
|
+
locator=Locator(kind=LocatorKind.PDF_PAGE, page=page),
|
|
110
|
+
)
|
|
111
|
+
for page in page_numbers
|
|
112
|
+
]
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
doc = parse_file("scanned.pdf", ocr_hook=MyOCRHook())
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 通过 `ParserRegistry.register()` 扩展自定义格式
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from docloc import Locator, LocatorKind, ParseOptions, ParsedBlock, ParsedDocument, ParserCapability, ParserRegistry
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class CsvParser:
|
|
125
|
+
extensions = ("csv",)
|
|
126
|
+
|
|
127
|
+
def check_capability(self, kind: str) -> ParserCapability:
|
|
128
|
+
return ParserCapability(kind=kind, available=True)
|
|
129
|
+
|
|
130
|
+
def parse(self, data: bytes, options: ParseOptions, *, kind: str, source_name: str = "") -> ParsedDocument:
|
|
131
|
+
text = data.decode("utf-8")
|
|
132
|
+
return ParsedDocument(
|
|
133
|
+
blocks=[ParsedBlock(text=text, locator=Locator(kind=LocatorKind.TEXT_LINES, start_line=1, end_line=len(text.splitlines()) or 1))],
|
|
134
|
+
source_name=source_name,
|
|
135
|
+
format=kind,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
registry = ParserRegistry()
|
|
140
|
+
registry.register(CsvParser())
|
|
141
|
+
doc = registry.parse_file("items.csv")
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## CLI
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
# 查看当前环境各格式可用性与缺失依赖提示
|
|
148
|
+
docloc capabilities
|
|
149
|
+
|
|
150
|
+
# 解析并切块,按 JSONL 输出(每行一个带 locator 的 chunk)
|
|
151
|
+
docloc parse guide.md --chunk --target 300 --overlap 50 --jsonl
|
|
152
|
+
```
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "docloc"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Document parsing and structure-aware chunking with original source locators"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
authors = [{ name = "Fanyixu", email = "fanmm666@qq.com" }]
|
|
10
|
+
keywords = [
|
|
11
|
+
"document-parsing",
|
|
12
|
+
"chunking",
|
|
13
|
+
"rag",
|
|
14
|
+
"provenance",
|
|
15
|
+
"locator",
|
|
16
|
+
"markdown",
|
|
17
|
+
"pdf",
|
|
18
|
+
"docx",
|
|
19
|
+
"xlsx",
|
|
20
|
+
]
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Development Status :: 4 - Beta",
|
|
23
|
+
"Intended Audience :: Developers",
|
|
24
|
+
"Operating System :: OS Independent",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Programming Language :: Python :: 3.13",
|
|
29
|
+
"Programming Language :: Python :: 3.14",
|
|
30
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
31
|
+
"Topic :: Text Processing",
|
|
32
|
+
"Typing :: Typed",
|
|
33
|
+
]
|
|
34
|
+
dependencies = []
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://gitee.com/fan-yixu/docloc"
|
|
38
|
+
Repository = "https://gitee.com/fan-yixu/docloc.git"
|
|
39
|
+
Issues = "https://gitee.com/fan-yixu/docloc/issues"
|
|
40
|
+
|
|
41
|
+
[project.optional-dependencies]
|
|
42
|
+
docx = ["python-docx>=1.1,<2"]
|
|
43
|
+
xlsx = ["openpyxl>=3.1,<4"]
|
|
44
|
+
pdf = ["pypdf>=4.2,<7"]
|
|
45
|
+
office = ["python-docx>=1.1,<2", "openpyxl>=3.1,<4"]
|
|
46
|
+
all = ["python-docx>=1.1,<2", "openpyxl>=3.1,<4", "pypdf>=4.2,<7"]
|
|
47
|
+
dev = ["mypy>=1.11,<2", "pytest>=8.2,<9", "ruff>=0.5,<1", "twine>=6.1,<7"]
|
|
48
|
+
|
|
49
|
+
[project.scripts]
|
|
50
|
+
docloc = "docloc.cli:main"
|
|
51
|
+
|
|
52
|
+
[build-system]
|
|
53
|
+
requires = ["hatchling>=1.27"]
|
|
54
|
+
build-backend = "hatchling.build"
|
|
55
|
+
|
|
56
|
+
[tool.hatch.build.targets.wheel]
|
|
57
|
+
packages = ["src/docloc"]
|
|
58
|
+
|
|
59
|
+
[tool.pytest.ini_options]
|
|
60
|
+
minversion = "8.0"
|
|
61
|
+
testpaths = ["tests"]
|
|
62
|
+
addopts = "-ra"
|
|
63
|
+
|
|
64
|
+
[tool.ruff]
|
|
65
|
+
line-length = 100
|
|
66
|
+
target-version = "py311"
|
|
67
|
+
src = ["src", "tests"]
|
|
68
|
+
|
|
69
|
+
[tool.ruff.lint]
|
|
70
|
+
select = ["E", "F", "I", "UP", "B"]
|
|
71
|
+
|
|
72
|
+
[tool.mypy]
|
|
73
|
+
python_version = "3.11"
|
|
74
|
+
strict = true
|
|
75
|
+
warn_unused_configs = true
|
|
76
|
+
|
|
77
|
+
[[tool.mypy.overrides]]
|
|
78
|
+
module = ["docx.*", "openpyxl.*", "pypdf.*"]
|
|
79
|
+
ignore_missing_imports = true
|