Skip to content

文档分类、阅读顺序与PDF:练习答案 ​

参考实现 ​

复用正文blocks,显式类型优于猜测宽度:

python
page = [{'x':20,'y':0,'text':'总标题','kind':'heading'}] + [dict(b,kind='body') for b in blocks]
head = [b for b in page if b['kind']=='heading']
body = sorted((b for b in page if b['kind']=='body'), key=lambda b:(b['x']>=200,b['y']))
texts = [b['text'] for b in head+body]
assert texts == ['总标题','左栏第一段','左栏第二段','右栏第一段','右栏第二段']

练习假设已经完成标题分类,不能据此宣称能识别任意PDF。分类证据应来自样式、宽度与布局,仍需原图核对。

返回题目。

链路变体 ​

将生成表头的 'Price' 改成 'Amount',期望表格也改为 Amount;其它价格值不应改变。接在完整代码末尾运行:

python
from PIL import Image
image = Image.open('scan.png')
image.resize((image.width//2,image.height//2)).save('low.png')
low,_ = ocr('low.png')
print('low resolution OCR',low)
assert isinstance(low,list) or low is None
status={'requires_render':not bool(spa.main.get_text(strip=True))}
assert status['requires_render'] is True

比较框坐标前先转换到同一个 PDF 点坐标系;不能用两个分辨率下的原始像素位置直接判断版面变化。置信度只用于分流,需用人工转写的测试集测错误率。

用心记录每一步成长