RAG中pdf解析的方法全览

RAG中解析PDF的方法

一 pdf格式都有哪些

1.机器生成的pdf文件,包含图像,文本,可以被编辑

2.传统扫描文档,表现为图像,不能被编辑

3.带OCR的扫描文档。可能转OCR的过程中带入了错误。

二 pdf解析全科指南
复制代码
全面指南---------用python提取PDF中各类文本内容的方法 https://www.luxiangdong.com/2023/10/05/extract/
复制代码
https://mp.weixin.qq.com/s/SjdoTv1htO6Ti98g3qgBjQ

1.pdfplumber

复制代码
https://blog.csdn.net/fuhanghang/article/details/122579548
pdfplumber的主要类和方法
pdfplumber对于表格的提取
复制代码
参考https://github.com/jsvine/pdfplumber/blob/stable/examples/notebooks/extract-table-ca-warn-report.ipynb

代码:
pdf = pdfplumber.open("../pdfs/ca-warn-report.pdf")
p0=pdf.pages[0]
im = p0.to_image()  #display 第一页
table = p0.extract_table() 抽取其中最大的表格

import pandas as pd
df = pd.DataFrame(table[1:], columns=table[0])
for column in ["Effective", "Received"]:
    df[column] = df[column].str.replace(" ", "")  使用panda来吧table抽取到的数据转成dataFrame格式

2.layout parser

layoutparser 是一个基于深度学习的文档图像分析工具包,它提供了布局检测、OCR识别、布局分析等接口,适用于处理和分析扫描文档或图像中的文字。

复制代码
https://zhuanlan.zhihu.com/p/391138225

首先,将pdf的每一页转为图像,以便对其执行OCR来提取文本块。

复制代码
pip install pdf2image
images = convert_from_bytes(open('FILE PATH', 'rb').read())  #将pdf的每一页转为图像并保存在内存中
image = np.array(image) #将图像转为像素值数据

解析pdf论文的话,使用特定模型

复制代码
参考 https://zhuanlan.zhihu.com/p/602615194

LayoutParser为常见的OCR工具提供了统一的接口,示例代码如下:

text 复制代码
ocr_agent = lp.TesseractAgent()  layourparser要与Tesseract 这个包相结合

使用代码如下

复制代码
import layoutparser as lp
image = cv2. imread (" image_file ") # load images
model = lp. Detectron2LayoutModel ("lp :// PubLayNet / faster_rcnn_R_50_FPN_3x / config ")
layout = model . detect ( image )

3.paddlepaddle的工具

https://github.com/PaddlePaddle/PaddleOCR/tree/release/2.7/ppstructure

该代码从pdf中结构出各个标题层级的关系,又在word中重建该结构。

4.其他解析pdf获得子标题的方法

复制代码
1.先ocr,再版面解析
https://www.textin.com/experience/pdf-to-word
2.用fitz提取,再写正则规则匹配
3.参考论文 
https://arxiv.org/pdf/2308.14978.pdf
4.google的document ai
5.参考如下项目
https://github.com/OKC13/General-Documents-Layout-parser
6.使用如下接口 
https://apifox.com/apidoc/shared-a55f1a3d-4871-41b7-8f1a-3af83807410b/api-120356017
相关推荐
Logintern092 小时前
【大语言模型应用】利用 DuckDuckGo 和 Tavily 搜索引擎
人工智能·搜索引擎·语言模型
user_admin_god4 小时前
OpenCode入门到入坑
java·人工智能·spring boot·语言模型
优化控制仿真模型4 小时前
2026年初中英语大纲词汇表1600词
经验分享·pdf
优化控制仿真模型5 小时前
方正小标宋简体、仿宋GB2312和楷体_GB2312办公字体安装包下载安装教程
经验分享·pdf
王莎莎-MinerU5 小时前
MinerU 生态全景:Skills、RAG、MCP、Cursor Rules
人工智能·深度学习·计算机视觉·chatgpt·pdf·软件工程
阿杰学AI7 小时前
AI核心知识125—大语言模型之 混合专家架构(简洁且通俗易懂版)
人工智能·ai·语言模型·智能路由器·aigc·moe·混合专家架构
小超同学你好7 小时前
面向 LLM 的程序设计 11:多语言与多模态下的工具描述
人工智能·语言模型
大模型最新论文速读7 小时前
VQKV:KV Cache 压缩 82% 性能几乎不降
人工智能·深度学习·算法·机器学习·自然语言处理
AI周红伟7 小时前
周红伟:RAG 与知识检索
人工智能·深度学习·机器学习·语言模型·openclaw
Ww.xh7 小时前
ESP8266连接AI大模型完整指南
人工智能·算法·语言模型