4 Комити 68843fb9bf ... ff828e2d08

Аутор SHA1 Порука Датум
  zhch158_admin ff828e2d08 feat: 在解析和处理OCR数据时添加匹配文本字段 пре 1 недеља
  zhch158_admin c3f6a15418 feat: 增强内容渲染和搜索功能,优化高亮显示逻辑 пре 1 недеља
  zhch158_admin bec2397a73 Refactor code structure for improved readability and maintainability пре 1 недеља
  zhch158_admin 136730b453 feat: 添加 TableCellMatcher 到模块导出列表 пре 1 недеља
5 измењених фајлова са 264 додато и 1897 уклоњено
  1. 2 0
      merger/__init__.py
  2. 0 495
      merger/data_processor_v1.py
  3. 0 1324
      merger/data_processor_v2.py
  4. 260 78
      ocr_validator_layout.py
  5. 2 0
      ocr_validator_utils.py

+ 2 - 0
merger/__init__.py

@@ -9,6 +9,7 @@ from .bbox_extractor import BBoxExtractor
 from .data_processor import DataProcessor
 from .markdown_generator import MarkdownGenerator
 from .unified_output_converter import UnifiedOutputConverter
+from .table_cell_matcher import TableCellMatcher
 
 __all__ = [
     'MinerUPaddleOCRMerger',
@@ -18,4 +19,5 @@ __all__ = [
     'DataProcessor',
     'MarkdownGenerator',
     'UnifiedOutputConverter',
+    'TableCellMatcher',
 ]

+ 0 - 495
merger/data_processor_v1.py

@@ -1,495 +0,0 @@
-"""
-数据处理模块
-负责处理 MinerU/PaddleOCR_VL/DotsOCR 数据,添加 bbox 信息
-"""
-from typing import List, Dict, Tuple
-from bs4 import BeautifulSoup
-
-try:
-    from .text_matcher import TextMatcher
-    from .bbox_extractor import BBoxExtractor
-except ImportError:
-    from text_matcher import TextMatcher
-    from bbox_extractor import BBoxExtractor
-
-
-class DataProcessor:
-    """数据处理器"""
-    
-    def __init__(self, text_matcher: TextMatcher, look_ahead_window: int = 10):
-        """
-        Args:
-            text_matcher: 文本匹配器
-            look_ahead_window: 向前查找窗口
-        """
-        self.text_matcher = text_matcher
-        self.look_ahead_window = look_ahead_window
-    
-    def process_mineru_data(self, mineru_data: List[Dict], 
-                           paddle_text_boxes: List[Dict]) -> List[Dict]:
-        """
-        处理 MinerU 数据,添加 bbox 信息
-        
-        Args:
-            mineru_data: MinerU 数据
-            paddle_text_boxes: PaddleOCR 文字框列表
-        
-        Returns:
-            合并后的数据, table cell使用paddle的bbox,其他类型只是移动指针,bbox还是沿用minerU的bbox
-        """
-        merged_data = []
-        paddle_pointer = 0
-        last_matched_index = 0
-
-        # 按 bbox 排序
-        mineru_data.sort(
-            key=lambda x: (x['bbox'][1], x['bbox'][0]) 
-            if 'bbox' in x else (float('inf'), float('inf'))
-        )
-
-        for item in mineru_data:
-            item_type = item.get('type', '')
-            
-            if item_type == 'table':
-                merged_item, paddle_pointer = self._process_table(
-                    item, paddle_text_boxes, paddle_pointer
-                )
-                merged_data.append(merged_item)
-            
-            elif item_type in ['text', 'title']:
-                merged_item, paddle_pointer, last_matched_index = self._process_text(
-                    item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            elif item_type == 'list':
-                merged_item, paddle_pointer, last_matched_index = self._process_list(
-                    item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            else:
-                merged_data.append(item.copy())
-        
-        return merged_data
-    
-    def process_dotsocr_data(self, dotsocr_data: List[Dict],
-                            paddle_text_boxes: List[Dict]) -> List[Dict]:
-        """
-        🎯 处理 DotsOCR 数据,转换为 MinerU 格式并添加 bbox 信息
-        
-        Args:
-            dotsocr_data: DotsOCR 数据
-            paddle_text_boxes: PaddleOCR 文字框列表
-        
-        Returns:
-            MinerU 格式的合并数据
-        """
-        merged_data = []
-        paddle_pointer = 0
-        last_matched_index = 0
-        
-        # 按 bbox 排序
-        dotsocr_data.sort(
-            key=lambda x: (x['bbox'][1], x['bbox'][0])
-            if 'bbox' in x else (float('inf'), float('inf'))
-        )
-        
-        for item in dotsocr_data:
-            # 🎯 转换为 MinerU 格式
-            mineru_item = self._convert_dotsocr_to_mineru(item)
-            category = mineru_item.get('type', '')
-            
-            # 🎯 根据类型处理
-            if category.lower() == 'table':
-                merged_item, paddle_pointer = self._process_dotsocr_table(
-                    mineru_item, paddle_text_boxes, paddle_pointer
-                )
-                merged_data.append(merged_item)
-            
-            elif category.lower() in ['text', 'title', 'header', 'footer']:
-                merged_item, paddle_pointer, last_matched_index = self._process_text(
-                    mineru_item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            elif category.lower() == 'list':
-                merged_item, paddle_pointer, last_matched_index = self._process_list(
-                    mineru_item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            else:
-                # Page-header, Page-footer, Picture 等
-                merged_data.append(mineru_item)
-        
-        return merged_data
-    
-    def _convert_dotsocr_to_mineru(self, dotsocr_item: Dict) -> Dict:
-        """
-        🎯 将 DotsOCR 格式转换为 MinerU 格式
-        
-        DotsOCR:
-        {
-            "category": "Table",
-            "bbox": [x1, y1, x2, y2],
-            "text": "..."
-        }
-        
-        MinerU:
-        {
-            "type": "table",
-            "bbox": [x1, y1, x2, y2],
-            "table_body": "...",
-            "page_idx": 0
-        }
-        """
-        category = dotsocr_item.get('category', '')
-        
-        # 🎯 Category 映射
-        category_map = {
-            'Page-header': 'header',
-            'Page-footer': 'footer',
-            'Picture': 'image',
-            'Figure': 'image',
-            'Section-header': 'title',
-            'Table': 'table',
-            'Text': 'text',
-            'Title': 'title',
-            'List': 'list',
-            'Caption': 'title'
-        }
-        
-        mineru_type = category_map.get(category, 'text')
-        
-        # 🎯 基础转换
-        mineru_item = {
-            'type': mineru_type,
-            'bbox': dotsocr_item.get('bbox', []),
-            'page_idx': 0  # DotsOCR 默认单页
-        }
-        
-        # 🎯 处理文本内容
-        text = dotsocr_item.get('text', '')
-        
-        if mineru_type == 'table':
-            # 表格:text -> table_body
-            mineru_item['table_body'] = text
-        else:
-            # 其他类型:保持 text
-            mineru_item['text'] = text
-            
-            # 标题级别
-            if category == 'Section-header':
-                mineru_item['text_level'] = 1
-        
-        return mineru_item
-    
-    def _process_dotsocr_table(self, item: Dict, paddle_text_boxes: List[Dict],
-                              start_pointer: int) -> Tuple[Dict, int]:
-        """
-        🎯 处理 DotsOCR 表格(已转换为 MinerU 格式)
-        
-        DotsOCR 的表格 HTML 已经在 text 字段中,需要转移到 table_body
-        """
-        merged_item = item.copy()
-        table_html = item.get('table_body', '')
-        
-        if not table_html:
-            return merged_item, start_pointer
-        
-        # 🎯 复用表格处理逻辑
-        enhanced_html, cells, new_pointer = self._enhance_table_html_with_bbox(
-            table_html, paddle_text_boxes, start_pointer
-        )
-        
-        merged_item['table_body'] = enhanced_html
-        merged_item['table_body_with_bbox'] = enhanced_html
-        merged_item['bbox_mapping'] = 'merged_from_paddle_ocr'
-        merged_item['table_cells'] = cells if cells else []
-        
-        return merged_item, new_pointer
-    
-    def process_paddleocr_vl_data(self, paddleocr_vl_data: Dict,
-                                  paddle_text_boxes: List[Dict]) -> List[Dict]:
-        """
-        处理 PaddleOCR_VL 数据,添加 bbox 信息
-        
-        Args:
-            paddleocr_vl_data: PaddleOCR_VL 数据 (JSON 对象)
-            paddle_text_boxes: PaddleOCR 文字框列表
-        
-        Returns:
-            🎯 MinerU 格式的合并数据(统一输出格式)
-        """
-        merged_data = []
-        paddle_pointer = 0
-        last_matched_index = 0
-        
-        # 🎯 获取旋转角度和原始图像尺寸
-        rotation_angle = self._get_rotation_angle_from_vl(paddleocr_vl_data)
-        orig_image_size = None
-        
-        if rotation_angle != 0:
-            orig_image_size = self._get_original_image_size_from_vl(paddleocr_vl_data)
-            print(f"🔄 PaddleOCR_VL 检测到旋转角度: {rotation_angle}°")
-            print(f"📐 原始图像尺寸: {orig_image_size[0]} x {orig_image_size[1]}")
-        
-        # 提取 parsing_res_list
-        parsing_res_list = paddleocr_vl_data.get('parsing_res_list', [])
-        
-        # 按 bbox 排序
-        parsing_res_list.sort(
-            key=lambda x: (x['block_bbox'][1], x['block_bbox'][0])
-            if 'block_bbox' in x else (float('inf'), float('inf'))
-        )
-        
-        for item in parsing_res_list:
-            # 🎯 先转换 bbox 坐标(如果需要)
-            if rotation_angle != 0 and orig_image_size:
-                item = self._transform_vl_block_bbox(item, rotation_angle, orig_image_size)
-            
-            # 🎯 统一转换为 MinerU 格式
-            mineru_item = self._convert_paddleocr_vl_to_mineru(item)
-            item_type = mineru_item.get('type', '')
-            
-            # 🎯 根据类型处理(复用 MinerU 的通用方法)
-            if item_type == 'table':
-                merged_item, paddle_pointer = self._process_table(
-                    mineru_item, paddle_text_boxes, paddle_pointer
-                )
-                merged_data.append(merged_item)
-            
-            elif item_type in ['text', 'title', 'header', 'footer', 'equation']:
-                merged_item, paddle_pointer, last_matched_index = self._process_text(
-                    mineru_item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            elif item_type == 'list':
-                merged_item, paddle_pointer, last_matched_index = self._process_list(
-                    mineru_item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            else:
-                # 其他类型(image 等)直接添加
-                merged_data.append(mineru_item)
-        
-        return merged_data
-    
-    def _get_rotation_angle_from_vl(self, paddleocr_vl_data: Dict) -> float:
-        """从 PaddleOCR_VL 数据中获取旋转角度"""
-        return BBoxExtractor._get_rotation_angle(paddleocr_vl_data)
-    
-    def _get_original_image_size_from_vl(self, paddleocr_vl_data: Dict) -> tuple:
-        """从 PaddleOCR_VL 数据中获取原始图像尺寸"""
-        return BBoxExtractor._get_original_image_size(paddleocr_vl_data)
-    
-    def _transform_vl_block_bbox(self, item: Dict, angle: float, 
-                                 orig_image_size: tuple) -> Dict:
-        """
-        转换 PaddleOCR_VL 的 block_bbox 坐标
-        
-        Args:
-            item: PaddleOCR_VL 的 block 数据
-            angle: 旋转角度
-            orig_image_size: 原始图像尺寸
-        
-        Returns:
-            转换后的 block 数据
-        """
-        transformed_item = item.copy()
-        
-        if 'block_bbox' not in item:
-            return transformed_item
-        
-        block_bbox = item['block_bbox']
-        if len(block_bbox) < 4:
-            return transformed_item
-        
-        # block_bbox 格式: [x1, y1, x2, y2]
-        # 转换为 poly 格式进行旋转
-        poly = [
-            [block_bbox[0], block_bbox[1]],  # 左上
-            [block_bbox[2], block_bbox[1]],  # 右上
-            [block_bbox[2], block_bbox[3]],  # 右下
-            [block_bbox[0], block_bbox[3]]   # 左下
-        ]
-        
-        # 🎯 使用 BBoxExtractor 的坐标转换方法
-        transformed_poly = BBoxExtractor._inverse_rotate_coordinates(
-            poly, angle, orig_image_size
-        )
-        
-        # 转换回 bbox 格式
-        xs = [p[0] for p in transformed_poly]
-        ys = [p[1] for p in transformed_poly]
-        transformed_bbox = [min(xs), min(ys), max(xs), max(ys)]
-        
-        transformed_item['block_bbox'] = transformed_bbox
-        
-        return transformed_item
-    
-    def _convert_paddleocr_vl_to_mineru(self, paddleocr_vl_item: Dict) -> Dict:
-        """
-        🎯 将 PaddleOCR_VL 格式转换为 MinerU 格式
-        
-        基于 PP-DocLayout_plus-L 的 20 种类别
-        """
-        block_label = paddleocr_vl_item.get('block_label', '')
-        
-        # 🎯 PP-DocLayout_plus-L 类别映射(共 20 种)
-        label_map = {
-            # 标题类(3种)
-            'paragraph_title': 'title',
-            'doc_title': 'title',
-            'figure_table_chart_title': 'title',
-            
-            # 文本类(9种)
-            'text': 'text',
-            'number': 'text',
-            'content': 'text',
-            'abstract': 'text',
-            'footnote': 'text',
-            'aside_text': 'text',
-            'algorithm': 'text',
-            'reference': 'text',
-            'reference_content': 'text',
-            
-            # 页眉页脚(2种)
-            'header': 'header',
-            'footer': 'footer',
-            
-            # 表格(1种)
-            'table': 'table',
-            
-            # 图片/图表(3种)
-            'image': 'image',
-            'chart': 'image',
-            'seal': 'image',
-            
-            # 公式(2种)
-            'formula': 'equation',
-            'formula_number': 'equation'
-        }
-        
-        mineru_type = label_map.get(block_label, 'text')
-        
-        mineru_item = {
-            'type': mineru_type,
-            'bbox': paddleocr_vl_item.get('block_bbox', []),
-            'page_idx': 0
-        }
-        
-        content = paddleocr_vl_item.get('block_content', '')
-        
-        if mineru_type == 'table':
-            mineru_item['table_body'] = content
-        else:
-            mineru_item['text'] = content
-            
-            # 标题级别
-            if block_label == 'doc_title':
-                mineru_item['text_level'] = 1
-            elif block_label == 'paragraph_title':
-                mineru_item['text_level'] = 2
-            elif block_label == 'figure_table_chart_title':
-                mineru_item['text_level'] = 3
-        
-        return mineru_item
-    
-    def _process_table(self, item: Dict, paddle_text_boxes: List[Dict],
-                      start_pointer: int) -> Tuple[Dict, int]:
-        """处理 MinerU 表格"""
-        merged_item = item.copy()
-        table_html = item.get('table_body', '')
-        
-        enhanced_html, cells, new_pointer = self._enhance_table_html_with_bbox(
-            table_html, paddle_text_boxes, start_pointer
-        )
-        
-        merged_item['table_body'] = enhanced_html
-        merged_item['table_body_with_bbox'] = enhanced_html
-        merged_item['bbox_mapping'] = 'merged_from_paddle_ocr'
-        merged_item['table_cells'] = cells if cells else []
-        
-        return merged_item, new_pointer
-    
-    def _process_text(self, item: Dict, paddle_text_boxes: List[Dict],
-                     paddle_pointer: int, last_matched_index: int) -> Tuple[Dict, int, int]:
-        """处理文本"""
-        merged_item = item.copy()
-        text = item.get('text', '')
-        
-        matched_bbox, paddle_pointer, last_matched_index = \
-            self.text_matcher.find_matching_bbox(
-                text, paddle_text_boxes, paddle_pointer, last_matched_index,
-                self.look_ahead_window
-            )
-        
-        if matched_bbox:
-            matched_bbox['used'] = True
-        
-        return merged_item, paddle_pointer, last_matched_index
-    
-    def _process_list(self, item: Dict, paddle_text_boxes: List[Dict],
-                     paddle_pointer: int, last_matched_index: int) -> Tuple[Dict, int, int]:
-        """处理列表"""
-        merged_item = item.copy()
-        list_items = item.get('list_items', [])
-        
-        for list_item in list_items:
-            matched_bbox, paddle_pointer, last_matched_index = \
-                self.text_matcher.find_matching_bbox(
-                    list_item, paddle_text_boxes, paddle_pointer, last_matched_index,
-                    self.look_ahead_window
-                )
-            
-            if matched_bbox:
-                matched_bbox['used'] = True
-        
-        return merged_item, paddle_pointer, last_matched_index
-    
-    def _enhance_table_html_with_bbox(self, html: str, paddle_text_boxes: List[Dict],
-                                      start_pointer: int) -> Tuple[str, List[Dict], int]:
-        """为 HTML 表格添加 bbox 信息"""
-        soup = BeautifulSoup(html, 'html.parser')
-        current_pointer = start_pointer
-        last_matched_index = start_pointer
-        cells = []
-
-        for row_idx, row in enumerate(soup.find_all('tr')):
-            for col_idx, cell in enumerate(row.find_all(['td', 'th'])):
-                cell_text = cell.get_text(strip=True)
-                
-                if not cell_text:
-                    continue
-                
-                matched_bbox, current_pointer, last_matched_index = \
-                    self.text_matcher.find_matching_bbox(
-                        cell_text, paddle_text_boxes, current_pointer, 
-                        last_matched_index, self.look_ahead_window
-                    )
-                
-                if matched_bbox:
-                    bbox = matched_bbox['bbox']
-                    cell['data-bbox'] = f"[{bbox[0]},{bbox[1]},{bbox[2]},{bbox[3]}]"
-                    cell['data-score'] = f"{matched_bbox['score']:.4f}"
-                    cell['data-paddle-index'] = str(matched_bbox['paddle_bbox_index'])
-
-                    # ✅ 完整记录单元格信息
-                    cells.append({
-                        'type': 'table_cell',
-                        'text': cell_text,
-                        'bbox': bbox,
-                        'row': row_idx + 1,
-                        'col': col_idx + 1,
-                        'score': matched_bbox['score'],
-                        'paddle_bbox_index': matched_bbox['paddle_bbox_index']
-                    })
-                    
-                    matched_bbox['used'] = True
-                # ✅ 如果匹配失败,不应该添加到 cells 中
-    
-        return str(soup), cells, current_pointer

+ 0 - 1324
merger/data_processor_v2.py

@@ -1,1324 +0,0 @@
-"""
-数据处理模块
-负责处理 MinerU/PaddleOCR_VL/DotsOCR 数据,添加 bbox 信息
-"""
-from typing import List, Dict, Tuple, Optional
-from bs4 import BeautifulSoup
-
-try:
-    from .text_matcher import TextMatcher
-    from .bbox_extractor import BBoxExtractor
-except ImportError:
-    from text_matcher import TextMatcher
-    from bbox_extractor import BBoxExtractor
-
-
-class DataProcessor:
-    """数据处理器"""
-    
-    def __init__(self, text_matcher: TextMatcher, look_ahead_window: int = 10):
-        """
-        Args:
-            text_matcher: 文本匹配器
-            look_ahead_window: 向前查找窗口
-        """
-        self.text_matcher = text_matcher
-        self.look_ahead_window = look_ahead_window
-    
-    def process_mineru_data(self, mineru_data: List[Dict], 
-                           paddle_text_boxes: List[Dict]) -> List[Dict]:
-        """
-        处理 MinerU 数据,添加 bbox 信息
-        
-        Args:
-            mineru_data: MinerU 数据
-            paddle_text_boxes: PaddleOCR 文字框列表
-        
-        Returns:
-            合并后的数据, table cell使用paddle的bbox,其他类型只是移动指针,bbox还是沿用minerU的bbox
-        """
-        merged_data = []
-        paddle_pointer = 0
-        last_matched_index = 0
-
-        # 按 bbox 排序
-        mineru_data.sort(
-            key=lambda x: (x['bbox'][1], x['bbox'][0]) 
-            if 'bbox' in x else (float('inf'), float('inf'))
-        )
-
-        for item in mineru_data:
-            item_type = item.get('type', '')
-            
-            if item_type == 'table':
-                merged_item, paddle_pointer = self._process_table(
-                    item, paddle_text_boxes, paddle_pointer
-                )
-                merged_data.append(merged_item)
-            
-            elif item_type in ['text', 'title']:
-                merged_item, paddle_pointer, last_matched_index = self._process_text(
-                    item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            elif item_type == 'list':
-                merged_item, paddle_pointer, last_matched_index = self._process_list(
-                    item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            else:
-                merged_data.append(item.copy())
-        
-        return merged_data
-    
-    def process_dotsocr_data(self, dotsocr_data: List[Dict],
-                            paddle_text_boxes: List[Dict]) -> List[Dict]:
-        """
-        🎯 处理 DotsOCR 数据,转换为 MinerU 格式并添加 bbox 信息
-        
-        Args:
-            dotsocr_data: DotsOCR 数据
-            paddle_text_boxes: PaddleOCR 文字框列表
-        
-        Returns:
-            MinerU 格式的合并数据
-        """
-        merged_data = []
-        paddle_pointer = 0
-        last_matched_index = 0
-        
-        # 按 bbox 排序
-        dotsocr_data.sort(
-            key=lambda x: (x['bbox'][1], x['bbox'][0])
-            if 'bbox' in x else (float('inf'), float('inf'))
-        )
-        
-        for item in dotsocr_data:
-            # 🎯 转换为 MinerU 格式
-            mineru_item = self._convert_dotsocr_to_mineru(item)
-            category = mineru_item.get('type', '')
-            
-            # 🎯 根据类型处理
-            if category.lower() == 'table':
-                merged_item, paddle_pointer = self._process_table(
-                    mineru_item, paddle_text_boxes, paddle_pointer
-                )
-                merged_data.append(merged_item)
-            
-            elif category.lower() in ['text', 'title', 'header', 'footer']:
-                merged_item, paddle_pointer, last_matched_index = self._process_text(
-                    mineru_item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            elif category.lower() == 'list':
-                merged_item, paddle_pointer, last_matched_index = self._process_list(
-                    mineru_item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            else:
-                # Page-header, Page-footer, Picture 等
-                merged_data.append(mineru_item)
-        
-        return merged_data
-    
-    def _convert_dotsocr_to_mineru(self, dotsocr_item: Dict) -> Dict:
-        """
-        🎯 将 DotsOCR 格式转换为 MinerU 格式
-        
-        DotsOCR:
-        {
-            "category": "Table",
-            "bbox": [x1, y1, x2, y2],
-            "text": "..."
-        }
-        
-        MinerU:
-        {
-            "type": "table",
-            "bbox": [x1, y1, x2, y2],
-            "table_body": "...",
-            "page_idx": 0
-        }
-        """
-        category = dotsocr_item.get('category', '')
-        
-        # 🎯 Category 映射
-        category_map = {
-            'Page-header': 'header',
-            'Page-footer': 'footer',
-            'Picture': 'image',
-            'Figure': 'image',
-            'Section-header': 'title',
-            'Table': 'table',
-            'Text': 'text',
-            'Title': 'title',
-            'List': 'list',
-            'Caption': 'title'
-        }
-        
-        mineru_type = category_map.get(category, 'text')
-        
-        # 🎯 基础转换
-        mineru_item = {
-            'type': mineru_type,
-            'bbox': dotsocr_item.get('bbox', []),
-            'page_idx': 0  # DotsOCR 默认单页
-        }
-        
-        # 🎯 处理文本内容
-        text = dotsocr_item.get('text', '')
-        
-        if mineru_type == 'table':
-            # 表格:text -> table_body
-            mineru_item['table_body'] = text
-        else:
-            # 其他类型:保持 text
-            mineru_item['text'] = text
-            
-            # 标题级别
-            if category == 'Section-header':
-                mineru_item['text_level'] = 1
-        
-        return mineru_item
-    
-    def process_paddleocr_vl_data(self, paddleocr_vl_data: Dict,
-                                  paddle_text_boxes: List[Dict]) -> List[Dict]:
-        """
-        处理 PaddleOCR_VL 数据,添加 bbox 信息
-        
-        Args:
-            paddleocr_vl_data: PaddleOCR_VL 数据 (JSON 对象)
-            paddle_text_boxes: PaddleOCR 文字框列表
-        
-        Returns:
-            🎯 MinerU 格式的合并数据(统一输出格式)
-        """
-        merged_data = []
-        paddle_pointer = 0
-        last_matched_index = 0
-        
-        # 🎯 获取旋转角度和原始图像尺寸
-        rotation_angle = self._get_rotation_angle_from_vl(paddleocr_vl_data)
-        orig_image_size = None
-        
-        if rotation_angle != 0:
-            orig_image_size = self._get_original_image_size_from_vl(paddleocr_vl_data)
-            print(f"🔄 PaddleOCR_VL 检测到旋转角度: {rotation_angle}°")
-            print(f"📐 原始图像尺寸: {orig_image_size[0]} x {orig_image_size[1]}")
-        
-        # 提取 parsing_res_list
-        parsing_res_list = paddleocr_vl_data.get('parsing_res_list', [])
-        
-        # 按 bbox 排序
-        parsing_res_list.sort(
-            key=lambda x: (x['block_bbox'][1], x['block_bbox'][0])
-            if 'block_bbox' in x else (float('inf'), float('inf'))
-        )
-        
-        for item in parsing_res_list:
-            # 🎯 先转换 bbox 坐标(如果需要)
-            if rotation_angle != 0 and orig_image_size:
-                item = self._transform_vl_block_bbox(item, rotation_angle, orig_image_size)
-            
-            # 🎯 统一转换为 MinerU 格式
-            mineru_item = self._convert_paddleocr_vl_to_mineru(item)
-            item_type = mineru_item.get('type', '')
-            
-            # 🎯 根据类型处理(复用 MinerU 的通用方法)
-            if item_type == 'table':
-                merged_item, paddle_pointer = self._process_table(
-                    mineru_item, paddle_text_boxes, paddle_pointer
-                )
-                merged_data.append(merged_item)
-            
-            elif item_type in ['text', 'title', 'header', 'footer', 'equation']:
-                merged_item, paddle_pointer, last_matched_index = self._process_text(
-                    mineru_item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            elif item_type == 'list':
-                merged_item, paddle_pointer, last_matched_index = self._process_list(
-                    mineru_item, paddle_text_boxes, paddle_pointer, last_matched_index
-                )
-                merged_data.append(merged_item)
-            
-            else:
-                # 其他类型(image 等)直接添加
-                merged_data.append(mineru_item)
-        
-        return merged_data
-    
-    def _get_rotation_angle_from_vl(self, paddleocr_vl_data: Dict) -> float:
-        """从 PaddleOCR_VL 数据中获取旋转角度"""
-        return BBoxExtractor._get_rotation_angle(paddleocr_vl_data)
-    
-    def _get_original_image_size_from_vl(self, paddleocr_vl_data: Dict) -> tuple:
-        """从 PaddleOCR_VL 数据中获取原始图像尺寸"""
-        return BBoxExtractor._get_original_image_size(paddleocr_vl_data)
-    
-    def _transform_vl_block_bbox(self, item: Dict, angle: float, 
-                                 orig_image_size: tuple) -> Dict:
-        """
-        转换 PaddleOCR_VL 的 block_bbox 坐标
-        
-        Args:
-            item: PaddleOCR_VL 的 block 数据
-            angle: 旋转角度
-            orig_image_size: 原始图像尺寸
-        
-        Returns:
-            转换后的 block 数据
-        """
-        transformed_item = item.copy()
-        
-        if 'block_bbox' not in item:
-            return transformed_item
-        
-        block_bbox = item['block_bbox']
-        if len(block_bbox) < 4:
-            return transformed_item
-        
-        # block_bbox 格式: [x1, y1, x2, y2]
-        # 转换为 poly 格式进行旋转
-        poly = [
-            [block_bbox[0], block_bbox[1]],  # 左上
-            [block_bbox[2], block_bbox[1]],  # 右上
-            [block_bbox[2], block_bbox[3]],  # 右下
-            [block_bbox[0], block_bbox[3]]   # 左下
-        ]
-        
-        # 🎯 使用 BBoxExtractor 的坐标转换方法
-        transformed_poly = BBoxExtractor._inverse_rotate_coordinates(
-            poly, angle, orig_image_size
-        )
-        
-        # 转换回 bbox 格式
-        xs = [p[0] for p in transformed_poly]
-        ys = [p[1] for p in transformed_poly]
-        transformed_bbox = [min(xs), min(ys), max(xs), max(ys)]
-        
-        transformed_item['block_bbox'] = transformed_bbox
-        
-        return transformed_item
-    
-    def _convert_paddleocr_vl_to_mineru(self, paddleocr_vl_item: Dict) -> Dict:
-        """
-        🎯 将 PaddleOCR_VL 格式转换为 MinerU 格式
-        
-        基于 PP-DocLayout_plus-L 的 20 种类别
-        """
-        block_label = paddleocr_vl_item.get('block_label', '')
-        
-        # 🎯 PP-DocLayout_plus-L 类别映射(共 20 种)
-        label_map = {
-            # 标题类(3种)
-            'paragraph_title': 'title',
-            'doc_title': 'title',
-            'figure_table_chart_title': 'title',
-            
-            # 文本类(9种)
-            'text': 'text',
-            'number': 'text',
-            'content': 'text',
-            'abstract': 'text',
-            'footnote': 'text',
-            'aside_text': 'text',
-            'algorithm': 'text',
-            'reference': 'text',
-            'reference_content': 'text',
-            
-            # 页眉页脚(2种)
-            'header': 'header',
-            'footer': 'footer',
-            
-            # 表格(1种)
-            'table': 'table',
-            
-            # 图片/图表(3种)
-            'image': 'image',
-            'chart': 'image',
-            'seal': 'image',
-            
-            # 公式(2种)
-            'formula': 'equation',
-            'formula_number': 'equation'
-        }
-        
-        mineru_type = label_map.get(block_label, 'text')
-        
-        mineru_item = {
-            'type': mineru_type,
-            'bbox': paddleocr_vl_item.get('block_bbox', []),
-            'page_idx': 0
-        }
-        
-        content = paddleocr_vl_item.get('block_content', '')
-        
-        if mineru_type == 'table':
-            mineru_item['table_body'] = content
-        else:
-            mineru_item['text'] = content
-            
-            # 标题级别
-            if block_label == 'doc_title':
-                mineru_item['text_level'] = 1
-            elif block_label == 'paragraph_title':
-                mineru_item['text_level'] = 2
-            elif block_label == 'figure_table_chart_title':
-                mineru_item['text_level'] = 3
-        
-        return mineru_item
-    
-    def _process_table(self, item: Dict, paddle_text_boxes: List[Dict],
-                  start_pointer: int) -> Tuple[Dict, int]:
-        """
-        处理表格类型(MinerU 格式)
-        
-        策略:
-        - 解析 HTML 表格
-        - 为每个单元格匹配 PaddleOCR 的 bbox
-        - 返回处理后的表格和新指针位置
-        """
-        table_body = item.get('table_body', '')
-        
-        if not table_body:
-            print(f"⚠️ 表格内容为空,跳过")
-            return item, start_pointer
-        
-        try:
-            # 🔑 传入 table_bbox 用于筛选
-            table_bbox = item.get('bbox')  # MinerU 提供的表格边界
-            
-            enhanced_html, cells, new_pointer = self._enhance_table_html_with_bbox(
-                table_body,
-                paddle_text_boxes,
-                start_pointer,
-                table_bbox  # ✅ 传入边界框
-            )
-            
-            # 更新 item
-            item['table_body'] = enhanced_html
-            item['table_cells'] = cells
-            
-            # 统计信息
-            matched_count = len(cells)
-            total_cells = len(BeautifulSoup(table_body, 'html.parser').find_all(['td', 'th']))
-            
-            print(f"   表格单元格: {matched_count}/{total_cells} 匹配")
-            
-            return item, new_pointer
-            
-        except Exception as e:
-            print(f"⚠️ 表格处理失败: {e}")
-            import traceback
-            traceback.print_exc()
-            return item, start_pointer
-        
-    def _process_text(self, item: Dict, paddle_text_boxes: List[Dict],
-                     paddle_pointer: int, last_matched_index: int) -> Tuple[Dict, int, int]:
-        """处理文本"""
-        merged_item = item.copy()
-        text = item.get('text', '')
-        
-        matched_bbox, paddle_pointer, last_matched_index = \
-            self.text_matcher.find_matching_bbox(
-                text, paddle_text_boxes, paddle_pointer, last_matched_index,
-                self.look_ahead_window
-            )
-        
-        if matched_bbox:
-            matched_bbox['used'] = True
-        
-        return merged_item, paddle_pointer, last_matched_index
-    
-    def _process_list(self, item: Dict, paddle_text_boxes: List[Dict],
-                     paddle_pointer: int, last_matched_index: int) -> Tuple[Dict, int, int]:
-        """处理列表"""
-        merged_item = item.copy()
-        list_items = item.get('list_items', [])
-        
-        for list_item in list_items:
-            matched_bbox, paddle_pointer, last_matched_index = \
-                self.text_matcher.find_matching_bbox(
-                    list_item, paddle_text_boxes, paddle_pointer, last_matched_index,
-                    self.look_ahead_window
-                )
-            
-            if matched_bbox:
-                matched_bbox['used'] = True
-        
-        return merged_item, paddle_pointer, last_matched_index
-    
-    def _enhance_table_html_with_bbox(self, html: str, paddle_text_boxes: List[Dict],
-                                  start_pointer: int, table_bbox: Optional[List[int]] = None) -> Tuple[str, List[Dict], int]:
-        """
-        为 HTML 表格添加 bbox 信息(优化版:先筛选表格区域)
-        
-        策略:
-        1. 根据 table_bbox 筛选出表格区域内的 paddle_text_boxes
-        2. 将筛选后的 boxes 按行分组
-        3. 智能匹配 HTML 行与 paddle 行组
-        4. 在匹配的组内查找单元格
-    
-        Args:
-            html: HTML 表格
-            paddle_text_boxes: 全部 paddle OCR 结果
-            start_pointer: 开始位置
-            table_bbox: 表格边界框 [x1, y1, x2, y2]
-        """
-        soup = BeautifulSoup(html, 'html.parser')
-        cells = []
-        
-        # 🔑 第一步:筛选表格区域内的 paddle boxes
-        table_region_boxes, actual_table_bbox = self._filter_boxes_in_table_region(
-            paddle_text_boxes[start_pointer:],
-            table_bbox,
-            html
-        )
-        
-        if not table_region_boxes:
-            print(f"⚠️ 未在表格区域找到 paddle boxes")
-            return str(soup), cells, start_pointer
-        
-        print(f"📊 表格区域: {len(table_region_boxes)} 个文本框")
-        print(f"   边界: {actual_table_bbox}")
-        
-        # 🔑 第二步:将表格区域的 boxes 按行分组
-        grouped_boxes = self._group_paddle_boxes_by_rows(
-            table_region_boxes,
-            y_tolerance=20
-        )
-        
-        # 🔑 第三步:在每组内按 x 坐标排序
-        for group in grouped_boxes:
-            group['boxes'].sort(key=lambda x: x['bbox'][0])
-        
-        grouped_boxes.sort(key=lambda g: g['y_center'])
-        
-        print(f"   分组: {len(grouped_boxes)} 行")
-        
-        # 🔑 第四步:智能匹配 HTML 行与 paddle 行组
-        html_rows = soup.find_all('tr')
-        row_mapping = self._match_html_rows_to_paddle_groups(html_rows, grouped_boxes)
-        
-        print(f"   HTML行: {len(html_rows)} 行")
-        print(f"   映射: {len([v for v in row_mapping.values() if v])} 个有效映射")
-        
-        # 🔑 第五步:遍历 HTML 表格,使用映射关系查找
-        for row_idx, row in enumerate(html_rows):
-            group_indices = row_mapping.get(row_idx, [])
-            
-            if not group_indices:
-                continue
-            
-            # 合并多个组的 boxes
-            current_boxes = []
-            for group_idx in group_indices:
-                if group_idx < len(grouped_boxes):
-                    current_boxes.extend(grouped_boxes[group_idx]['boxes'])
-            
-            current_boxes.sort(key=lambda x: x['bbox'][0])
-            
-            # 🎯 关键改进:提取 HTML 单元格并预先确定列边界
-            html_cells = row.find_all(['td', 'th'])
-            
-            if not html_cells:
-                continue
-            
-            # 🔑 预估列边界(基于 x 坐标分布)
-            col_boundaries = self._estimate_column_boundaries(
-                current_boxes, 
-                len(html_cells)
-            )
-            
-            print(f"   行 {row_idx + 1}: {len(html_cells)} 列,边界: {col_boundaries}")
-            
-            # 🔑 按列分配 boxes
-            for col_idx, cell in enumerate(html_cells):
-                cell_text = cell.get_text(strip=True)
-                
-                if not cell_text:
-                    continue
-                
-                # 🎯 获取该列范围内的所有 boxes
-                col_boxes = self._get_boxes_in_column(
-                    current_boxes,
-                    col_boundaries,
-                    col_idx
-                )
-                
-                if not col_boxes:
-                    continue
-                
-                # 🎯 尝试匹配并合并
-                matched_result = self._match_and_merge_boxes_for_cell(
-                    cell_text,
-                    col_boxes
-                )
-                
-                if matched_result:
-                    merged_bbox = matched_result['bbox']
-                    merged_text = matched_result['text']
-                    
-                    cell['data-bbox'] = f"[{merged_bbox[0]},{merged_bbox[1]},{merged_bbox[2]},{merged_bbox[3]}]"
-                    cell['data-score'] = f"{matched_result['score']:.4f}"
-                    cell['data-paddle-indices'] = str(matched_result['paddle_indices'])
-                    
-                    cells.append({
-                        'type': 'table_cell',
-                        'text': cell_text,
-                        'matched_text': merged_text,
-                        'bbox': merged_bbox,
-                        'row': row_idx + 1,
-                        'col': col_idx + 1,
-                        'score': matched_result['score'],
-                        'paddle_bbox_indices': matched_result['paddle_indices']
-                    })
-                    
-                    # 标记已使用
-                    for box in matched_result['used_boxes']:
-                        box['used'] = True
-        
-        # 计算新的指针位置
-        used_count = sum(1 for box in table_region_boxes if box.get('used'))
-        new_pointer = start_pointer + used_count
-        
-        print(f"   匹配: {len(cells)} 个单元格")
-        
-        return str(soup), cells, new_pointer
-
-    def _estimate_column_boundaries(self, boxes: List[Dict], 
-                                    num_cols: int) -> List[Tuple[int, int]]:
-        """
-        估算列边界(改进版:处理同列多文本框)
-        
-        Args:
-            boxes: 当前行的所有 boxes(已按 x 排序)
-            num_cols: HTML 表格的列数
-        
-        Returns:
-            列边界列表 [(x_start, x_end), ...]
-        """
-        if not boxes:
-            return []
-        
-        # 🔑 关键改进:先按 x 坐标聚类(合并同列的多个文本框)
-        x_clusters = self._cluster_boxes_by_x(boxes, x_tolerance=10)
-        
-        print(f"      X聚类: {len(boxes)} 个boxes -> {len(x_clusters)} 个列簇")
-        
-        # 获取所有 x 坐标范围
-        x_min = min(cluster['x_min'] for cluster in x_clusters)
-        x_max = max(cluster['x_max'] for cluster in x_clusters)
-        
-        # 🎯 策略 1: 如果聚类数量与列数接近
-        if len(x_clusters) == num_cols:
-            # 直接使用聚类边界
-            boundaries = [(cluster['x_min'], cluster['x_max']) 
-                        for cluster in x_clusters]
-            return boundaries
-        
-        # # 🎯 策略 2: 聚类数少于列数(某些列没有文本)
-        # if len(x_clusters) < num_cols:
-        #     print(f"      ⚠️ 聚类数 {len(x_clusters)} < 列数 {num_cols},使用均分策略")
-            
-        #     # 使用聚类的间隙来推断缺失的列边界
-        #     cluster_centers = [(c['x_min'] + c['x_max']) / 2 for c in x_clusters]
-            
-        #     # 计算平均列宽
-        #     if len(cluster_centers) > 1:
-        #         avg_gap = (x_max - x_min) / (num_cols - 1)
-        #     else:
-        #         avg_gap = 100  # 默认列宽
-            
-        #     # 生成边界
-        #     boundaries = []
-        #     prev_x = x_min
-            
-        #     for i in range(num_cols):
-        #         if i < len(x_clusters):
-        #             # 使用实际聚类
-        #             boundaries.append((x_clusters[i]['x_min'], x_clusters[i]['x_max']))
-        #             prev_x = x_clusters[i]['x_max']
-        #         else:
-        #             # 推断缺失列
-        #             next_x = prev_x + avg_gap
-        #             boundaries.append((prev_x, next_x))
-        #             prev_x = next_x
-            
-        #     return boundaries
-        
-        # 🎯 策略 3: 聚类数多于列数(某些列有多个文本簇)
-        if len(x_clusters) > num_cols:
-            print(f"      ℹ️ 聚类数 {len(x_clusters)} > 列数 {num_cols},合并相近簇")
-            
-            # 合并相近的簇
-            merged_clusters = self._merge_close_clusters(x_clusters, num_cols)
-            
-            boundaries = [(cluster['x_min'], cluster['x_max']) 
-                        for cluster in merged_clusters]
-            return boundaries
-        
-        return []
-
-
-    def _cluster_boxes_by_x(self, boxes: List[Dict], 
-                        x_tolerance: int = 50) -> List[Dict]:
-        """
-        按 x 坐标聚类(合并同列的多个文本框)
-        
-        Args:
-            boxes: 文本框列表
-            x_tolerance: X坐标容忍度
-        
-        Returns:
-            聚类列表 [{'x_min': int, 'x_max': int, 'boxes': List[Dict]}, ...]
-        """
-        if not boxes:
-            return []
-        
-        # 按左边界 x 坐标排序
-        sorted_boxes = sorted(boxes, key=lambda b: b['bbox'][0])
-        
-        clusters = []
-        current_cluster = None
-        
-        for box in sorted_boxes:
-            bbox = box['bbox']
-            x_start = bbox[0]
-            x_end = bbox[2]
-            
-            if current_cluster is None:
-                # 开始新簇
-                current_cluster = {
-                    'x_min': x_start,
-                    'x_max': x_end,
-                    'boxes': [box]
-                }
-            else:
-                # 检查是否属于当前簇
-                # 🔑 条件:x 坐标重叠或接近
-                overlap = not (x_start > current_cluster['x_max'] + x_tolerance or
-                            x_end < current_cluster['x_min'] - x_tolerance)
-                
-                if overlap or abs(x_start - current_cluster['x_min']) <= x_tolerance:
-                    # 合并到当前簇
-                    current_cluster['boxes'].append(box)
-                    current_cluster['x_min'] = min(current_cluster['x_min'], x_start)
-                    current_cluster['x_max'] = max(current_cluster['x_max'], x_end)
-                else:
-                    # 保存当前簇,开始新簇
-                    clusters.append(current_cluster)
-                    current_cluster = {
-                        'x_min': x_start,
-                        'x_max': x_end,
-                        'boxes': [box]
-                    }
-        
-        # 添加最后一簇
-        if current_cluster:
-            clusters.append(current_cluster)
-        
-        return clusters
-
-
-    def _merge_close_clusters(self, clusters: List[Dict], 
-                            target_count: int) -> List[Dict]:
-        """
-        合并相近的簇,直到数量等于目标列数
-        
-        Args:
-            clusters: 聚类列表
-            target_count: 目标列数
-        
-        Returns:
-            合并后的聚类列表
-        """
-        if len(clusters) <= target_count:
-            return clusters
-        
-        # 复制一份,避免修改原数据
-        working_clusters = [c.copy() for c in clusters]
-        
-        while len(working_clusters) > target_count:
-            # 找到距离最近的两个簇
-            min_distance = float('inf')
-            merge_idx = 0
-            
-            for i in range(len(working_clusters) - 1):
-                distance = working_clusters[i + 1]['x_min'] - working_clusters[i]['x_max']
-                if distance < min_distance:
-                    min_distance = distance
-                    merge_idx = i
-            
-            # 合并
-            cluster1 = working_clusters[merge_idx]
-            cluster2 = working_clusters[merge_idx + 1]
-            
-            merged_cluster = {
-                'x_min': cluster1['x_min'],
-                'x_max': cluster2['x_max'],
-                'boxes': cluster1['boxes'] + cluster2['boxes']
-            }
-            
-            # 替换
-            working_clusters[merge_idx] = merged_cluster
-            working_clusters.pop(merge_idx + 1)
-        
-        return working_clusters
-
-
-    def _get_boxes_in_column(self, boxes: List[Dict], 
-                            boundaries: List[Tuple[int, int]],
-                            col_idx: int) -> List[Dict]:
-        """
-        获取指定列范围内的 boxes(改进版:包含重叠)
-        
-        Args:
-            boxes: 当前行的所有 boxes
-            boundaries: 列边界
-            col_idx: 列索引
-        
-        Returns:
-            该列的 boxes
-        """
-        if col_idx >= len(boundaries):
-            return []
-        
-        x_start, x_end = boundaries[col_idx]
-        
-        col_boxes = []
-        for box in boxes:
-            bbox = box['bbox']
-            box_x_start = bbox[0]
-            box_x_end = bbox[2]
-            
-            # 🔑 改进:检查是否有重叠(不只是中心点)
-            overlap = not (box_x_start > x_end or box_x_end < x_start)
-            
-            if overlap:
-                col_boxes.append(box)
-        
-        return col_boxes
-
-
-    def _match_and_merge_boxes_for_cell(self, cell_text: str, 
-                                    col_boxes: List[Dict]) -> Optional[Dict]:
-        """
-        匹配并合并单元格的多个 boxes
-        
-        策略:
-        1. 尝试单个 box 精确匹配
-        2. 如果失败,尝试合并多个 boxes
-        
-        Args:
-            cell_text: HTML 单元格文本
-            col_boxes: 该列的候选 boxes
-        
-        Returns:
-            {'bbox': [x1,y1,x2,y2], 'text': str, 'score': float, 
-            'paddle_indices': [idx1, idx2], 'used_boxes': [box1, box2]}
-        """
-        from fuzzywuzzy import fuzz
-        
-        cell_text_normalized = self.text_matcher.normalize_text(cell_text)
-        
-        if len(cell_text_normalized) < 2:
-            return None
-        
-        # 🔑 策略 1: 单个 box 精确匹配
-        for box in col_boxes:
-            if box.get('used'):
-                continue
-            
-            box_text = self.text_matcher.normalize_text(box['text'])
-            
-            if cell_text_normalized == box_text:
-                return {
-                    'bbox': box['bbox'],
-                    'text': box['text'],
-                    'score': box['score'],
-                    'paddle_indices': [box['paddle_bbox_index']],
-                    'used_boxes': [box]
-                }
-        
-        # 🔑 策略 2: 多个 boxes 合并匹配
-        unused_boxes = [b for b in col_boxes if not b.get('used')]
-        
-        if not unused_boxes:
-            return None
-        
-        # 尝试不同的组合长度
-        for combo_len in range(1, min(len(unused_boxes) + 1, 5)):
-            # 按 y 坐标排序(上到下)
-            sorted_boxes = sorted(unused_boxes, key=lambda b: b['bbox'][1])
-            
-            # 滑动窗口
-            for start_idx in range(len(sorted_boxes) - combo_len + 1):
-                combo_boxes = sorted_boxes[start_idx:start_idx + combo_len]
-                
-                # 合并文本
-                merged_text = ''.join([b['text'] for b in combo_boxes])
-                merged_text_normalized = self.text_matcher.normalize_text(merged_text)
-                
-                # 计算相似度
-                similarity = fuzz.partial_ratio(cell_text_normalized, merged_text_normalized)
-                
-                if similarity >= 85:  # 高阈值
-                    # 合并 bbox
-                    merged_bbox = [
-                        min(b['bbox'][0] for b in combo_boxes),
-                        min(b['bbox'][1] for b in combo_boxes),
-                        max(b['bbox'][2] for b in combo_boxes),
-                        max(b['bbox'][3] for b in combo_boxes)
-                    ]
-                    
-                    return {
-                        'bbox': merged_bbox,
-                        'text': merged_text,
-                        'score': sum(b['score'] for b in combo_boxes) / len(combo_boxes),
-                        'paddle_indices': [b['paddle_bbox_index'] for b in combo_boxes],
-                        'used_boxes': combo_boxes
-                    }
-        
-        # 🔑 策略 3: 降级匹配(单个最佳)
-        best_box = None
-        best_score = 0
-        
-        for box in unused_boxes:
-            box_text = self.text_matcher.normalize_text(box['text'])
-            score = fuzz.partial_ratio(cell_text_normalized, box_text)
-            
-            if score > best_score:
-                best_score = score
-                best_box = box
-        
-        if best_box and best_score >= 70:
-            return {
-                'bbox': best_box['bbox'],
-                'text': best_box['text'],
-                'score': best_box['score'],
-                'paddle_indices': [best_box['paddle_bbox_index']],
-                'used_boxes': [best_box]
-            }
-        
-        return None
-
-    def _filter_boxes_in_table_region(self, paddle_boxes: List[Dict],
-                                  table_bbox: Optional[List[int]],
-                                  html: str) -> Tuple[List[Dict], List[int]]:
-        """
-        筛选表格区域内的 paddle boxes
-    
-        策略:
-        1. 如果有 table_bbox,使用边界框筛选(扩展边界)
-        2. 如果没有 table_bbox,通过内容匹配推断区域
-    
-        Args:
-            paddle_boxes: paddle OCR 结果
-            table_bbox: 表格边界框 [x1, y1, x2, y2]
-            html: HTML 内容(用于内容验证)
-    
-        Returns:
-            (筛选后的 boxes, 实际表格边界框)
-        """
-        if not paddle_boxes:
-            return [], [0, 0, 0, 0]
-        
-        # 🎯 策略 1: 使用提供的 table_bbox(扩展边界)
-        if table_bbox and len(table_bbox) == 4:
-            x1, y1, x2, y2 = table_bbox
-            
-            # 扩展边界(考虑边框外的文本)
-            margin = 20
-            expanded_bbox = [
-                max(0, x1 - margin),
-                max(0, y1 - margin),
-                x2 + margin,
-                y2 + margin
-            ]
-            
-            filtered = []
-            for box in paddle_boxes:
-                bbox = box['bbox']
-                box_center_x = (bbox[0] + bbox[2]) / 2
-                box_center_y = (bbox[1] + bbox[3]) / 2
-                
-                # 中心点在扩展区域内
-                if (expanded_bbox[0] <= box_center_x <= expanded_bbox[2] and
-                    expanded_bbox[1] <= box_center_y <= expanded_bbox[3]):
-                    filtered.append(box)
-            
-            if filtered:
-                # 计算实际边界框
-                actual_bbox = [
-                    min(b['bbox'][0] for b in filtered),
-                    min(b['bbox'][1] for b in filtered),
-                    max(b['bbox'][2] for b in filtered),
-                    max(b['bbox'][3] for b in filtered)
-                ]
-                return filtered, actual_bbox
-        
-        # 🎯 策略 2: 通过内容匹配推断区域
-        print("   ℹ️ 无 table_bbox,使用内容匹配推断表格区域...")
-        
-        # 提取 HTML 中的所有文本
-        from bs4 import BeautifulSoup
-        soup = BeautifulSoup(html, 'html.parser')
-        html_texts = set()
-        for cell in soup.find_all(['td', 'th']):
-            text = cell.get_text(strip=True)
-            if text:
-                html_texts.add(self.text_matcher.normalize_text(text))
-        
-        if not html_texts:
-            return [], [0, 0, 0, 0]
-        
-        # 找出与 HTML 内容匹配的 boxes
-        matched_boxes = []
-        for box in paddle_boxes:
-            normalized_text = self.text_matcher.normalize_text(box['text'])
-            
-            # 检查是否匹配
-            if any(normalized_text in ht or ht in normalized_text 
-                   for ht in html_texts):
-                matched_boxes.append(box)
-        
-        if not matched_boxes:
-            # 🔑 降级:如果精确匹配失败,使用模糊匹配
-            print("   ℹ️ 精确匹配失败,尝试模糊匹配...")
-            
-            from fuzzywuzzy import fuzz
-            for box in paddle_boxes:
-                normalized_text = self.text_matcher.normalize_text(box['text'])
-                
-                for ht in html_texts:
-                    similarity = fuzz.partial_ratio(normalized_text, ht)
-                    if similarity >= 70:  # 降低阈值
-                        matched_boxes.append(box)
-                        break
-    
-        if matched_boxes:
-            # 计算边界框
-            actual_bbox = [
-                min(b['bbox'][0] for b in matched_boxes),
-                min(b['bbox'][1] for b in matched_boxes),
-                max(b['bbox'][2] for b in matched_boxes),
-                max(b['bbox'][3] for b in matched_boxes)
-            ]
-            
-            # 🔑 扩展边界,包含可能遗漏的文本
-            margin = 30
-            expanded_bbox = [
-                max(0, actual_bbox[0] - margin),
-                max(0, actual_bbox[1] - margin),
-                actual_bbox[2] + margin,
-                actual_bbox[3] + margin
-            ]
-            
-            # 重新筛选(包含边界上的文本)
-            final_filtered = []
-            for box in paddle_boxes:
-                bbox = box['bbox']
-                box_center_x = (bbox[0] + bbox[2]) / 2
-                box_center_y = (bbox[1] + bbox[3]) / 2
-                
-                if (expanded_bbox[0] <= box_center_x <= expanded_bbox[2] and
-                    expanded_bbox[1] <= box_center_y <= expanded_bbox[3]):
-                    final_filtered.append(box)
-            
-            return final_filtered, actual_bbox
-        
-        # 🔑 最后的降级:返回所有 boxes
-        print("   ⚠️ 无法确定表格区域,使用所有 paddle boxes")
-        if paddle_boxes:
-            actual_bbox = [
-                min(b['bbox'][0] for b in paddle_boxes),
-                min(b['bbox'][1] for b in paddle_boxes),
-                max(b['bbox'][2] for b in paddle_boxes),
-                max(b['bbox'][3] for b in paddle_boxes)
-            ]
-            return paddle_boxes, actual_bbox
-        
-        return [], [0, 0, 0, 0]
-
-    def _group_paddle_boxes_by_rows(self, paddle_boxes: List[Dict], 
-                                    y_tolerance: int = 20) -> List[Dict]:
-        """
-        将 paddle_text_boxes 按 y 坐标分组(聚类)
-        
-        Args:
-            paddle_boxes: Paddle OCR 文字框列表
-            y_tolerance: Y 坐标容忍度(像素)
-        
-        Returns:
-            分组列表,每组包含 {'y_center': float, 'boxes': List[Dict]}
-        """
-        if not paddle_boxes:
-            return []
-        
-        # 计算每个 box 的中心 y 坐标
-        boxes_with_y = []
-        for box in paddle_boxes:
-            bbox = box['bbox']
-            y_center = (bbox[1] + bbox[3]) / 2
-            boxes_with_y.append({
-                'y_center': y_center,
-                'box': box
-            })
-        
-        # 按 y 坐标排序
-        boxes_with_y.sort(key=lambda x: x['y_center'])
-        
-        # 聚类
-        groups = []
-        current_group = None
-        
-        for item in boxes_with_y:
-            if current_group is None:
-                # 开始新组
-                current_group = {
-                    'y_center': item['y_center'],
-                    'boxes': [item['box']]
-                }
-            else:
-                # 检查是否属于当前组
-                if abs(item['y_center'] - current_group['y_center']) <= y_tolerance:
-                    current_group['boxes'].append(item['box'])
-                    # 更新组的中心(使用平均值)
-                    current_group['y_center'] = sum(
-                        b['bbox'][1] + b['bbox'][3] for b in current_group['boxes']
-                    ) / (2 * len(current_group['boxes']))
-                else:
-                    # 保存当前组,开始新组
-                    groups.append(current_group)
-                    current_group = {
-                        'y_center': item['y_center'],
-                        'boxes': [item['box']]
-                    }
-        
-        # 添加最后一组
-        if current_group:
-            groups.append(current_group)
-        
-        return groups
-
-
-    def _find_best_match_in_group(self, target_text: str, boxes: List[Dict],
-                                start_idx: int = 0) -> Optional[Dict]:
-        """
-        在给定的 boxes 列表中查找最佳匹配(已按 x 坐标排序)
-        
-        Args:
-            target_text: 目标文本
-            boxes: 候选 boxes(已排序)
-            start_idx: 起始索引
-        
-        Returns:
-            最佳匹配的 box 或 None
-        """
-        target_text = self.text_matcher.normalize_text(target_text)
-        
-        if len(target_text) < 2:
-            return None
-        
-        best_match = None
-        best_score = 0
-        
-        # 优先从 start_idx 开始查找
-        search_range = list(range(start_idx, len(boxes))) + list(range(0, start_idx))
-        
-        for idx in search_range:
-            box = boxes[idx]
-            
-            if box.get('used'):
-                continue
-            
-            box_text = self.text_matcher.normalize_text(box['text'])
-            
-            # 精确匹配
-            if target_text == box_text:
-                return box
-            
-            # 长度比例检查
-            length_ratio = min(len(target_text), len(box_text)) / max(len(target_text), len(box_text))
-            if length_ratio < 0.35:
-                continue
-            
-            # 子串检查
-            shorter = target_text if len(target_text) < len(box_text) else box_text
-            longer = box_text if len(target_text) < len(box_text) else target_text
-            is_substring = shorter in longer
-            
-            # 计算相似度
-            from fuzzywuzzy import fuzz
-            partial_ratio = fuzz.partial_ratio(target_text, box_text)
-            if is_substring:
-                partial_ratio += 10
-            
-            if partial_ratio >= self.text_matcher.similarity_threshold:
-                if partial_ratio > best_score:
-                    best_score = partial_ratio
-                    best_match = box
-        
-        return best_match
-
-    def _match_html_rows_to_paddle_groups(self, html_rows: List, 
-                                      grouped_boxes: List[Dict]) -> Dict[int, List[int]]:
-        """
-        智能匹配 HTML 行与 paddle 分组(改进版:处理跨行文本)
-    
-        策略:
-        1. 第一遍:基于内容精确匹配
-        2. 第二遍:将未使用的组合并到相邻已匹配的行
-        """
-        if not html_rows or not grouped_boxes:
-            return {}
-        
-        mapping = {}
-        
-        # 🎯 策略 1: 数量相等,简单 1:1 映射
-        if len(html_rows) == len(grouped_boxes):
-            for i in range(len(html_rows)):
-                mapping[i] = [i]
-            return mapping
-        
-        # 🎯 策略 2: 第一遍 - 基于内容精确匹配
-        used_groups = set()
-        
-        for row_idx, row in enumerate(html_rows):
-            row_texts = [cell.get_text(strip=True) for cell in row.find_all(['td', 'th'])]
-            row_texts = [t for t in row_texts if t]
-            
-            if not row_texts:
-                mapping[row_idx] = []
-                continue
-            
-            row_text_normalized = [self.text_matcher.normalize_text(t) for t in row_texts]
-            
-            # 查找最匹配的 paddle 组
-            best_groups = []
-            best_score = 0
-            
-            # 尝试匹配单个组
-            for group_idx, group in enumerate(grouped_boxes):
-                if group_idx in used_groups:
-                    continue
-                
-                group_texts = [self.text_matcher.normalize_text(b['text']) 
-                              for b in group['boxes'] if not b.get('used')]
-                
-                match_count = sum(1 for rt in row_text_normalized 
-                                if any(rt in gt or gt in rt for gt in group_texts))
-                
-                coverage = match_count / len(row_texts) if row_texts else 0
-                
-                if coverage > best_score:
-                    best_score = coverage
-                    best_groups = [group_idx]
-            
-            # 🔑 如果单组匹配度不高,尝试匹配多个连续组
-            if best_score < 0.5:
-                # 从当前位置向后查找
-                start_group = min([g for g in range(len(grouped_boxes)) if g not in used_groups], 
-                                default=0)
-                combined_texts = []
-                combined_groups = []
-                
-                for group_idx in range(start_group, min(start_group + 5, len(grouped_boxes))):
-                    if group_idx in used_groups:
-                        continue
-                    
-                    combined_groups.append(group_idx)
-                    combined_texts.extend([
-                        self.text_matcher.normalize_text(b['text'])
-                        for b in grouped_boxes[group_idx]['boxes']
-                        if not b.get('used')
-                    ])
-                    
-                    match_count = sum(1 for rt in row_text_normalized
-                                    if any(rt in gt or gt in rt for gt in combined_texts))
-                    coverage = match_count / len(row_texts) if row_texts else 0
-                    
-                    if coverage > best_score:
-                        best_score = coverage
-                        best_groups = combined_groups.copy()
-        
-            # 记录映射
-            if best_groups and best_score > 0.3:
-                mapping[row_idx] = best_groups
-                used_groups.update(best_groups)
-            else:
-                # 降级策略:位置推测
-                estimated_group = min(row_idx, len(grouped_boxes) - 1)
-                if estimated_group not in used_groups:
-                    mapping[row_idx] = [estimated_group]
-                    used_groups.add(estimated_group)
-                else:
-                    mapping[row_idx] = []
-        
-        # 🎯 策略 3: 第二遍 - 处理未使用的组(关键!)
-        unused_groups = [i for i in range(len(grouped_boxes)) if i not in used_groups]
-        
-        if unused_groups:
-            print(f"   ℹ️ 发现 {len(unused_groups)} 个未匹配的 paddle 组: {unused_groups}")
-            
-            # 🔑 将未使用的组合并到相邻的已匹配行
-            for unused_idx in unused_groups:
-                # 策略:合并到最近的上方或下方已匹配行
-                
-                # 1. 查找该组的 y 坐标
-                unused_y = grouped_boxes[unused_idx]['y_center']
-                
-                # 2. 找到最近的已使用组
-                closest_used_idx = None
-                min_distance = float('inf')
-                
-                for used_idx in sorted(used_groups):
-                    distance = abs(grouped_boxes[used_idx]['y_center'] - unused_y)
-                    if distance < min_distance:
-                        min_distance = distance
-                        closest_used_idx = used_idx
-                
-                if closest_used_idx is not None:
-                    # 3. 找到该组对应的 HTML 行
-                    target_html_row = None
-                    for html_row_idx, group_indices in mapping.items():
-                        if closest_used_idx in group_indices:
-                            target_html_row = html_row_idx
-                            break
-                    
-                    if target_html_row is not None:
-                        # 4. 判断合并方向(基于 y 坐标)
-                        if unused_y < grouped_boxes[closest_used_idx]['y_center']:
-                            # 未使用组在上方,可能是上一行的跨列文本
-                            if target_html_row > 0:
-                                # 合并到上一行
-                                if target_html_row - 1 in mapping:
-                                    if unused_idx not in mapping[target_html_row - 1]:
-                                        mapping[target_html_row - 1].append(unused_idx)
-                                        print(f"      • 组 {unused_idx} 合并到 HTML 行 {target_html_row - 1}(上方)")
-                            else:
-                                # 合并到当前行
-                                if unused_idx not in mapping[target_html_row]:
-                                    mapping[target_html_row].append(unused_idx)
-                                    print(f"      • 组 {unused_idx} 合并到 HTML 行 {target_html_row}(当前)")
-                        else:
-                            # 未使用组在下方,可能是当前行的跨列文本
-                            if unused_idx not in mapping[target_html_row]:
-                                mapping[target_html_row].append(unused_idx)
-                                print(f"      • 组 {unused_idx} 合并到 HTML 行 {target_html_row}(下方)")
-                
-                used_groups.add(unused_idx)
-        
-        # 🔑 策略 4: 第三遍 - 按 y 坐标排序每行的组索引
-        for row_idx in mapping:
-            if mapping[row_idx]:
-                mapping[row_idx].sort(key=lambda idx: grouped_boxes[idx]['y_center'])
-        
-        return mapping

+ 260 - 78
ocr_validator_layout.py

@@ -178,15 +178,28 @@ class OCRLayoutManager:
             st.error(f"❌ 图像加载失败: {e}")
             return None
 
-    def render_content_by_mode(self, content: str, render_mode: str, font_size: int, container_height: int, layout_type: str):
-        """根据渲染模式显示内容 - 增强版本"""
+    def render_content_by_mode(self, content: str, render_mode: str, font_size: int, 
+                          container_height: int, layout_type: str, 
+                          highlight_config: Optional[Dict] = None):
+        """
+        根据渲染模式显示内容 - 增强版本
+        
+        Args:
+            content: 要渲染的内容
+            render_mode: 渲染模式
+            font_size: 字体大小
+            container_height: 容器高度
+            layout_type: 布局类型
+            highlight_config: 高亮配置 {'has_bbox': bool, 'match_type': str}
+        """
         if content is None or render_mode is None:
             return
             
         if render_mode == "HTML渲染":
-            # 增强的HTML渲染样式,支持横向滚动
+            # 🎯 构建样式 - 包含基础样式和高亮样式
             content_style = f"""
             <style>
+            /* ========== 基础容器样式 ========== */
             .{layout_type}-content-display {{
                 height: {container_height}px;
                 overflow-x: auto;
@@ -201,12 +214,12 @@ class OCRLayoutManager:
                 max-width: 100%;
             }}
             
+            /* ========== 表格样式 ========== */
             .{layout_type}-content-display table {{
-                width: 100%;  /* 修改:从100%改为auto,让表格自适应内容 */
+                width: 100%;
                 border-collapse: collapse;
                 margin: 10px 0;
-                white-space: nowrap;  /* 修改:允许文字换行 */
-                /* table-layout: auto; *?  /* 新增:自动表格布局 */
+                white-space: nowrap;
             }}
             
             .{layout_type}-content-display th,
@@ -214,11 +227,10 @@ class OCRLayoutManager:
                 border: 1px solid #ddd;
                 padding: 8px;
                 text-align: left;
-                /* 移除:min-width固定限制 */
-                max-width: 300px;     /* 新增:设置最大宽度避免过宽 */
-                word-wrap: break-word; /* 新增:长单词自动换行 */
-                word-break: break-all; /* 新增:允许在任意字符间换行 */
-                vertical-align: top;   /* 新增:顶部对齐 */
+                max-width: 300px;
+                word-wrap: break-word;
+                word-break: break-all;
+                vertical-align: top;
             }}
             
             .{layout_type}-content-display th {{
@@ -226,30 +238,31 @@ class OCRLayoutManager:
                 position: sticky;
                 top: 0;
                 z-index: 1;
-                font-weight: bold;    /* 新增:表头加粗 */
+                font-weight: bold;
             }}
             
-            /* 新增:针对数字列的特殊处理 */
+            /* 数字列右对齐 */
             .{layout_type}-content-display td.number {{
                 text-align: right;
                 white-space: nowrap;
                 font-family: 'Monaco', 'Menlo', monospace;
             }}
             
-            /* 新增:针对短文本列的处理 */
+            /* 短文本列不换行 */
             .{layout_type}-content-display td.short-text {{
                 white-space: nowrap;
                 min-width: 80px;
             }}
             
+            /* ========== 图片样式 ========== */
             .{layout_type}-content-display img {{
                 max-width: 100%;
                 height: auto;
                 border-radius: 4px;
                 margin: 10px 0;
             }}
-        
-            /* 新增:响应式表格 */
+            
+            /* ========== 响应式设计 ========== */
             @media (max-width: 768px) {{
                 .{layout_type}-content-display table {{
                     font-size: {max(font_size-2, 8)}px;
@@ -260,23 +273,54 @@ class OCRLayoutManager:
                     max-width: 150px;
                 }}
             }}
-
-            .highlight-text {{
-                background-color: #ffeb3b !important;
+            
+            /* ========== 高亮文本样式 ========== */
+            .{layout_type}-content-display .highlight-text {{
                 padding: 2px 4px;
                 border-radius: 3px;
                 cursor: pointer;
-                color: #333333 !important;
+                font-weight: 500;
+                transition: all 0.2s ease;
+            }}
+            
+            .{layout_type}-content-display .highlight-text:hover {{
+                opacity: 0.8;
+                transform: scale(1.02);
             }}
             
-            .selected-highlight {{
+            /* 🎯 精确匹配且有框 - 绿色 */
+            .{layout_type}-content-display .highlight-text.selected-highlight {{
                 background-color: #4caf50 !important;
                 color: white !important;
+                border: 1px solid #2e7d32 !important;
+            }}
+            
+            /* 🎯 OCR匹配 - 蓝色 */
+            .{layout_type}-content-display .highlight-text.ocr-match {{
+                background-color: #2196f3 !important;
+                color: white !important;
+                border: 1px solid #1565c0 !important;
+            }}
+            
+            /* 🎯 无边界框 - 橙色虚线 */
+            .{layout_type}-content-display .highlight-text.no-bbox {{
+                background-color: #ff9800 !important;
+                color: white !important;
+                border: 1px dashed #f57c00 !important;
+            }}
+            
+            /* 🎯 默认高亮 - 黄色 */
+            .{layout_type}-content-display .highlight-text.default {{
+                background-color: #ffeb3b !important;
+                color: #333333 !important;
+                border: 1px solid #fbc02d !important;
             }}
             </style>
             """
+            
             st.markdown(content_style, unsafe_allow_html=True)
-            st.markdown(f'<div class="{layout_type}-content-display">{content}</div>', unsafe_allow_html=True)
+            st.markdown(f'<div class="{layout_type}-content-display">{content}</div>', 
+                       unsafe_allow_html=True)
             
         elif render_mode == "Markdown渲染":
             converted_content = convert_html_table_to_markdown(content)
@@ -296,26 +340,25 @@ class OCRLayoutManager:
                 key=f"{layout_type}_text_area"
             )
 
+
     def create_compact_layout(self, config: Dict):
-        """创建紧凑的对比布局"""
-        # 主要内容区域
+        """创建紧凑的对比布局 - 增强搜索功能"""
         layout = config['styles']['layout']
         font_size = config['styles'].get('font_size', 10)
         container_height = layout.get('default_height', 600)
         zoom_level = layout.get('default_zoom', 1.0)
         layout_type = "compact"
 
-        left_col, right_col = st.columns([layout['content_width'], layout['sidebar_width']], vertical_alignment='top', border=True)
+        left_col, right_col = st.columns([layout['content_width'], layout['sidebar_width']], 
+                                         vertical_alignment='top', border=True)
 
         with left_col:
-            # 快速定位文本选择器 - 增强版(搜索+下拉)
             if self.validator.text_bbox_mapping:
                 # 搜索输入框
                 search_col, select_col = st.columns([1, 2])
                 
-                # 初始化session state
                 if "compact_search_query" not in st.session_state:
-                    st.session_state.compact_search_query = None
+                    st.session_state.compact_search_query = ""
                 
                 with search_col:
                     search_query = st.text_input(
@@ -325,38 +368,89 @@ class OCRLayoutManager:
                         key=f"{layout_type}_search_input",
                         label_visibility="collapsed"
                     )
-                    # 更新session state
                     st.session_state.compact_search_query = search_query
                 
-                # 构建选项列表
+                # 🎯 增强搜索逻辑:构建选项列表
                 text_options = ["请选择文本..."]
                 text_display = ["请选择文本..."]
+                match_info = [None]  # 记录匹配信息
                 
                 for text, info_list in self.validator.text_bbox_mapping.items():
-                    # 如果有搜索条件,进行过滤
+                    # 🔑 关键改进:同时搜索 text 和 matched_text
                     if search_query and search_query.strip():
-                        if search_query.lower() not in text.lower():
-                            continue  # 跳过不匹配的项
+                        query_lower = search_query.lower()
+                        
+                        # 1. 检查原始文本
+                        text_match = query_lower in text.lower()
+                        
+                        # 2. 检查 matched_text(OCR识别文本)
+                        matched_text_match = False
+                        matched_text = None
+                        if info_list and isinstance(info_list[0], dict):
+                            matched_text = info_list[0].get('matched_text', '')
+                            matched_text_match = query_lower in matched_text.lower() if matched_text else False
+                        
+                        # 如果都不匹配,跳过
+                        if not text_match and not matched_text_match:
+                            continue
+                        
+                        # 记录匹配类型
+                        if text_match:
+                            match_type = "exact"
+                            match_source = text
+                        else:
+                            match_type = "ocr"
+                            match_source = matched_text
+                    else:
+                        match_type = None
+                        match_source = text
                     
                     text_options.append(text)
                     
-                    # 检查是否是表格单元格
+                    # 🎯 构建显示文本(带匹配提示)
                     if info_list and isinstance(info_list[0], dict):
                         first_info = info_list[0]
+                        
+                        # 检查是否有 bbox
+                        has_bbox = 'bbox' in first_info and first_info['bbox']
+                        
+                        # 表格单元格显示
                         if 'row' in first_info and 'col' in first_info:
                             display_text = f"[R{first_info['row']},C{first_info['col']}] {text}"
-                            if len(display_text) > 47:
-                                display_text = display_text[:44] + "..."
                         else:
-                            display_text = text[:47] + "..." if len(text) > 50 else text
-                    else:
-                        display_text = text[:47] + "..." if len(text) > 50 else text
+                            display_text = text
+                        
+                        # 🎯 添加匹配提示
+                        if match_type == "ocr":
+                            display_text = f"🔍 {display_text} (OCR: {match_source[:20]}...)"
+                        elif not has_bbox:
+                            display_text = f"⚠️ {display_text} (无框)"
                         
+                        # 截断过长文本
+                        if len(display_text) > 60:
+                            display_text = display_text[:57] + "..."
+                    else:
+                        display_text = text[:57] + "..." if len(text) > 60 else text
+                    
                     text_display.append(display_text)
+                    match_info.append({
+                        'type': match_type,
+                        'source': match_source,
+                        'has_bbox': has_bbox if info_list else False
+                    })
                 
-                # 显示匹配数量
+                # 🎯 显示搜索统计
                 if search_query and search_query.strip():
-                    st.caption(f"找到 {len(text_options)-1} 个匹配项")
+                    ocr_matches = sum(1 for m in match_info[1:] if m and m['type'] == 'ocr')
+                    no_bbox_count = sum(1 for m in match_info[1:] if m and not m['has_bbox'])
+                    
+                    stat_parts = [f"找到 {len(text_options)-1} 个匹配项"]
+                    if ocr_matches > 0:
+                        stat_parts.append(f"🔍 {ocr_matches} 个OCR匹配")
+                    if no_bbox_count > 0:
+                        stat_parts.append(f"⚠️ {no_bbox_count} 个无框")
+                    
+                    st.caption(" | ".join(stat_parts))
                 
                 # 确定默认选中的索引
                 default_index = 0
@@ -373,28 +467,78 @@ class OCRLayoutManager:
                         key=f"{layout_type}_quick_text_selector"
                     )
                 
+                # 🎯 显示匹配详情
                 if selected_index > 0:
                     st.session_state.selected_text = text_options[selected_index]
-        
-            # 处理并显示OCR内容 - 只高亮选中的文本
+                    
+                    # 获取匹配信息
+                    selected_match_info = match_info[selected_index]
+                    if selected_match_info:
+                        if selected_match_info['type'] == 'ocr':
+                            st.info(f"🔍 **OCR识别文本匹配**: `{selected_match_info['source']}`")
+                        elif not selected_match_info['has_bbox']:
+                            st.warning(f"⚠️ **未找到边界框**: 文本在MD中存在,但没有对应的坐标信息")
+            
+            # 🎯 增强高亮显示逻辑
             if self.validator.md_content:
                 highlighted_content = self.validator.md_content
                 
-                # 只高亮选中的文本
                 if st.session_state.selected_text:
                     selected_text = st.session_state.selected_text
+                    
+                    # 获取匹配信息
+                    info_list = self.validator.text_bbox_mapping.get(selected_text, [])
+                    has_bbox = False
+                    matched_text = None
+                    match_type = None
+                    
+                    if info_list and isinstance(info_list[0], dict):
+                        has_bbox = 'bbox' in info_list[0] and info_list[0]['bbox']
+                        matched_text = info_list[0].get('matched_text', '')
+                        
+                        # 🔑 判断匹配类型
+                        if matched_text and matched_text != selected_text:
+                            match_type = "ocr"
+                        elif has_bbox:
+                            match_type = "exact"
+                        else:
+                            match_type = "no_bbox"
+                    
+                    # 🎯 应用高亮
                     if len(selected_text) > 2:
-                        highlighted_content = highlighted_content.replace(
-                            selected_text,
-                            f'<span class="highlight-text selected-highlight" title="{selected_text}">{selected_text}</span>'
-                        )
+                        # 1. 高亮原始文本
+                        if selected_text in highlighted_content:
+                            if match_type == "exact":
+                                highlight_class = "highlight-text selected-highlight"
+                            elif match_type == "no_bbox":
+                                highlight_class = "highlight-text no-bbox"
+                            else:
+                                highlight_class = "highlight-text default"
+                            
+                            highlighted_content = highlighted_content.replace(
+                                selected_text,
+                                f'<span class="{highlight_class}" title="{selected_text}">{selected_text}</span>'
+                            )
+                        
+                        # 2. 如果有 matched_text 且不同,也高亮
+                        if matched_text and matched_text != selected_text and matched_text in highlighted_content:
+                            highlighted_content = highlighted_content.replace(
+                                matched_text,
+                                f'<span class="highlight-text ocr-match" title="OCR: {matched_text}">{matched_text}</span>'
+                            )
                 
-                self.render_content_by_mode(highlighted_content, "HTML渲染", font_size, container_height, layout_type)
-        
+                # 🎯 调用渲染方法(样式已内置)
+                self.render_content_by_mode(
+                    highlighted_content, 
+                    "HTML渲染", 
+                    font_size, 
+                    container_height, 
+                    layout_type
+                )
+    
         with right_col:
-            # 修复的对齐图片显示
             self.create_aligned_image_display(zoom_level, "compact")
-    
+
     def create_aligned_image_display(self, zoom_level: float = 1.0, layout_type: str = "aligned"):
         """创建响应式图片显示"""
     
@@ -453,6 +597,7 @@ class OCRLayoutManager:
         if image:
             try:
                 resized_image, all_boxes, selected_boxes = self.zoom_image(image, self.zoom_level)
+                
                 # 创建交互式图片
                 fig = self.create_resized_interactive_plot(resized_image, selected_boxes, self.zoom_level, all_boxes)
 
@@ -518,44 +663,81 @@ class OCRLayoutManager:
 
         return resized_image, all_boxes, selected_boxes
 
-    def _add_bboxes_to_plot(self, fig: go.Figure, bboxes: List[List[int]], image_height: int, 
-                           line_color: str = "blue", line_width: int = 1, 
-                           fill_color: str = "rgba(0, 100, 200, 0.2)"):
+    def _add_bboxes_to_plot_batch(self, fig: go.Figure, bboxes: List[List[int]], 
+                                image_height: int, 
+                                line_color: str = "blue", 
+                                line_width: int = 1, 
+                                fill_color: str = "rgba(0, 100, 200, 0.2)"):
         """
-        在plotly图表上添加边界框
-        
-        Args:
-            fig: plotly图表对象
-            bboxes: 边界框列表,每个bbox格式为[x1, y1, x2, y2]
-            image_height: 图片高度,用于Y轴坐标转换
-            line_color: 边框线颜色
-            line_width: 边框线宽度
-            fill_color: 填充颜色(RGBA格式)
+        批量添加边界框(性能优化版)
         """
         if not bboxes or len(bboxes) == 0:
             return
-            
+        
+        # 🎯 关键优化:构建 shapes 列表,一次性添加
+        shapes = []
         for bbox in bboxes:
             if len(bbox) < 4:
                 continue
-                
+            
             x1, y1, x2, y2 = bbox[:4]
             
-            # 转换为Plotly坐标系(翻转Y轴)
-            # JSON格式: 原点在左上角, y向下增加
-            # Plotly格式: 原点在左下角, y向上增加
+            # 转换坐标
             plot_x1 = x1
             plot_x2 = x2
-            plot_y1 = image_height - y2  # JSON的y2(底部) -> Plotly的底部
-            plot_y2 = image_height - y1  # JSON的y1(顶部) -> Plotly的顶部
+            plot_y1 = image_height - y2
+            plot_y2 = image_height - y1
             
-            fig.add_shape(
+            shapes.append(dict(
                 type="rect",
                 x0=plot_x1, y0=plot_y1,
                 x1=plot_x2, y1=plot_y2,
                 line=dict(color=line_color, width=line_width),
                 fillcolor=fill_color,
-            )
+            ))
+        
+        # 🎯 一次性更新所有形状
+        fig.update_layout(shapes=fig.layout.shapes + tuple(shapes))
+
+    def _add_bboxes_as_scatter(self, fig: go.Figure, bboxes: List[List[int]], 
+                          image_height: int,
+                          line_color: str = "blue", 
+                          line_width: int = 1,
+                          name: str = "boxes"):
+        """
+        使用 Scatter 绘制边界框(极致性能优化)
+        """
+        if not bboxes or len(bboxes) == 0:
+            return
+        
+        # 🎯 收集所有矩形的边框线坐标
+        x_coords = []
+        y_coords = []
+        
+        for bbox in bboxes:
+            if len(bbox) < 4:
+                continue
+            
+            x1, y1, x2, y2 = bbox[:4]
+            
+            # 转换坐标
+            plot_y1 = image_height - y2
+            plot_y2 = image_height - y1
+            
+            # 绘制矩形:5个点(闭合)
+            x_coords.extend([x1, x2, x2, x1, x1, None])  # None用于断开线段
+            y_coords.extend([plot_y1, plot_y1, plot_y2, plot_y2, plot_y1, None])
+        
+        # 🎯 一次性添加所有边框
+        fig.add_trace(go.Scatter(
+            x=x_coords,
+            y=y_coords,
+            mode='lines',
+            line=dict(color=line_color, width=line_width),
+            name=name,
+            showlegend=False,
+            hoverinfo='skip'
+        ))
 
     def create_resized_interactive_plot(self, image: Image.Image, selected_boxes: List[List[int]], 
                                        zoom_level: float, all_boxes: List[List[int]]) -> go.Figure:
@@ -579,23 +761,23 @@ class OCRLayoutManager:
         
         # 显示所有bbox(淡蓝色)
         if all_boxes:
-            self._add_bboxes_to_plot(
+            self._add_bboxes_as_scatter(
                 fig=fig,
                 bboxes=all_boxes,
                 image_height=image.height,
-                line_color="blue",
+                line_color="rgba(0, 100, 200, 0.8)",
                 line_width=1,
-                fill_color="rgba(0, 100, 200, 0.2)"
+                name="all_boxes"
             )
 
         # 高亮显示选中的bbox(红色)
         if selected_boxes:
-            self._add_bboxes_to_plot(
+            self._add_bboxes_to_plot_batch(
                 fig=fig,
                 bboxes=selected_boxes,
                 image_height=image.height,
                 line_color="red",
-                line_width=3,
+                line_width=1,
                 fill_color="rgba(255, 0, 0, 0.3)"
             )
     

+ 2 - 0
ocr_validator_utils.py

@@ -344,6 +344,7 @@ def parse_mineru_data(data: List, config: Dict, tool_name="mineru") -> List[Dict
                 if cell_text and cell_bbox and len(cell_bbox) >= 4:
                     parsed_data.append({
                         'text': str(cell_text).strip(),
+                        'matched_text': cell.get('matched_text', ''),
                         'bbox': cell_bbox[:4],
                         'row': cell.get('row', -1),
                         'col': cell.get('col', -1),
@@ -671,6 +672,7 @@ def process_ocr_data(ocr_data: List, config: Dict) -> Dict[str, List]:
                 if text not in text_bbox_mapping:
                     text_bbox_mapping[text] = []
                 text_bbox_mapping[text].append({
+                    'matched_text': item.get('matched_text', ''),
                     'bbox': bbox,
                     'category': item.get('category', 'Text'),
                     'index': i,