当前位置: 首页 > wzjs >正文

网站开发工程师薪酬待遇做网站好的网站建设公司

网站开发工程师薪酬待遇,做网站好的网站建设公司,北京设计工作室排名,专业做网站制作目录 前言 新增PdfNewExtractor类 替换ExtractProcessor类 最终结果 前言 dify的1.1.3版本知识库pdf解析实现使用pypdfium2提取文本,主要存在以下问题: 1. 文本提取能力有限,对表格和图片支持不足 2. 缺乏专门的中文处理优化 3. 没有文档结…

目录

前言

新增PdfNewExtractor类

替换ExtractProcessor类

最终结果


前言


dify的1.1.3版本知识库pdf解析实现使用pypdfium2提取文本,主要存在以下问题:
1. 文本提取能力有限,对表格和图片支持不足
2. 缺乏专门的中文处理优化
3. 没有文档结构分析
4. 缺少文档质量评估
建议优化方案:
1. 使用pdfplumber替代pypdfium2
2. 增加OCR支持
3. 优化中文处理逻辑
4. 添加文档结构分析
5. 实现智能表格识别
6. 增加缓存机制
7. 优化大文件处理

导入包pdfplumber和pytesseract

pip install pdfplumber
pip install pytesseract

新增PdfNewExtractor类


新增一个PdfNewExtractor处理类替代老的PdfExtractor

from collections.abc import Iterator
from typing import Optional, cast
import pdfplumber
import pytesseract
from PIL import Image
import iofrom core.rag.extractor.blob.blob import Blob
from core.rag.extractor.extractor_base import BaseExtractor
from core.rag.models.document import Document
from extensions.ext_storage import storageclass PdfNewExtractor(BaseExtractor):"""Enhanced PDF loader with improved text extraction, OCR support, and structure analysis.Args:file_path: Path to the PDF file to load.file_cache_key: Optional cache key for storing extracted text.enable_ocr: Whether to enable OCR for text extraction from images."""def __init__(self, file_path: str, file_cache_key: Optional[str] = None, enable_ocr: bool = False):"""Initialize with file path and optional settings."""self._file_path = file_pathself._file_cache_key = file_cache_keyself._enable_ocr = enable_ocrdef extract(self) -> list[Document]:"""Extract text from PDF with caching support."""plaintext_file_exists = Falseif self._file_cache_key:try:text = cast(bytes, storage.load(self._file_cache_key)).decode("utf-8")plaintext_file_exists = Truereturn [Document(page_content=text)]except FileNotFoundError:passdocuments = list(self.load())text_list = []for document in documents:text_list.append(document.page_content)text = "\n\n".join(text_list)# Save plaintext file for cachingif not plaintext_file_exists and self._file_cache_key:storage.save(self._file_cache_key, text.encode("utf-8"))return documentsdef load(self) -> Iterator[Document]:"""Lazy load PDF pages with enhanced text extraction."""blob = Blob.from_path(self._file_path)yield from self.parse(blob)def parse(self, blob: Blob) -> Iterator[Document]:"""Parse PDF with enhanced features including OCR and structure analysis."""with blob.as_bytes_io() as file_obj:with pdfplumber.open(file_obj) as pdf:for page_number, page in enumerate(pdf.pages):# Extract text with layout preservation and encoding detectioncontent = page.extract_text(layout=True)# Try to detect and fix encoding issuestry:# First try to decode as UTF-8content = content.encode('utf-8').decode('utf-8')except UnicodeError:try:# If UTF-8 fails, try GB18030 (common Chinese encoding)content = content.encode('utf-8').decode('gb18030', errors='ignore')except UnicodeError:# If all else fails, use a more lenient approachcontent = content.encode('utf-8', errors='ignore').decode('utf-8', errors='ignore')# Extract tables if presenttables = page.extract_tables()if tables:table_text = "\n\nTables:\n"for table in tables:# Convert table to text formattable_text += "\n" + "\n".join(["\t".join([str(cell) if cell else "" for cell in row]) for row in table])content += table_text# Perform OCR if enabled and text content is limited or contains potential encoding issuesif self._enable_ocr and (len(content.strip()) < 100 or any('\ufffd' in line for line in content.splitlines())):image = page.to_image()img_bytes = io.BytesIO()image.original.save(img_bytes, format='PNG')img_bytes.seek(0)pil_image = Image.open(img_bytes)# Use multiple language models and improve OCR accuracyocr_text = pytesseract.image_to_string(pil_image,lang='chi_sim+chi_tra+eng',  # Support both simplified and traditional Chineseconfig='--psm 3 --oem 3'  # Use more accurate OCR mode)if ocr_text.strip():# Clean and normalize OCR textocr_text = ocr_text.replace('\x0c', '').strip()content = f"{content}\n\nOCR Text:\n{ocr_text}"metadata = {"source": blob.source,"page": page_number,"has_tables": bool(tables)}yield Document(page_content=content, metadata=metadata)

替换ExtractProcessor类


在ExtractProcessor中把两处extractor = PdfExtractor(file_path),替换成extractor = PdfNewExtractor(file_path)。
分别在代码144行和148行

最终结果


经过测试,优化效果完美

http://www.dtcms.com/wzjs/410134.html

相关文章:

  • 网站扫二维码怎么做百度手机助手安卓版
  • 男女做暖暖网站网页推广平台
  • 网站开发什么技术路线搜索引擎优化策略
  • 企业网站建设如何去规划软文营销经典案例优秀软文
  • 网站空间1g多少钱一年市场调研报告模板范文
  • 辽宁省城乡建设网站站长工具在线查询
  • 做网站的公司叫什么问题手机百度关键词优化
  • 一个网站可以做几级链接企业网站建设方案范文
  • 河南网站建设公司百度搜索推广
  • 山东住房和城乡建设局网站首页自己怎么创建网站
  • 苏州网站开发建设公司免费搜索引擎入口
  • 网站制作哪里好专业的推广公司
  • 上海网站建设哪家公司好北京百度推广客服电话多少
  • 湖南省建设工程招标网如何seo搜索引擎优化
  • 珠海做网站设计怎么建公司网站
  • 网站怎么做json数据库59软文网
  • 佛山新网站建设哪家好下载百度app最新版并安装
  • 建设厅网站密码找回公司排名seo
  • 国土网站建设自查报告永久免费自助建站系统
  • 免费网站建站排行榜bing搜索 国内版
  • 网站被k的怎么办今日新闻最新头条10条摘抄
  • 怎么自己购买域名 建设网站网络优化工程师简历
  • 郴州专业的网站建设搜狗搜索引擎网页
  • 贵州建设厅考试网站百度怎么精准搜关键词
  • 大学 英文网站建设怎样制作一个网页
  • 浙江省住房与城乡建设部网站阿里网站seo
  • 阿里巴巴网站如何做免费推广苏州吴中区seo关键词优化排名
  • 西红门模板网站建设新人跑业务怎么找客户
  • top后缀做网站好不好短视频seo询盘获客系统
  • 做网站运用的软件网站seo教材