table_cell_matcher.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. """
  2. 表格单元格匹配器
  3. 负责将 HTML 表格单元格与 PaddleOCR bbox 进行匹配
  4. """
  5. from typing import List, Dict, Tuple, Optional
  6. from bs4 import BeautifulSoup
  7. try:
  8. from .text_matcher import TextMatcher
  9. except ImportError:
  10. from text_matcher import TextMatcher
  11. class TableCellMatcher:
  12. """表格单元格匹配器"""
  13. def __init__(self, text_matcher: TextMatcher,
  14. x_tolerance: int = 3,
  15. y_tolerance: int = 10):
  16. """
  17. Args:
  18. text_matcher: 文本匹配器
  19. x_tolerance: X轴容差(用于列边界判断)
  20. y_tolerance: Y轴容差(用于行分组)
  21. """
  22. self.text_matcher = text_matcher
  23. self.x_tolerance = x_tolerance
  24. self.y_tolerance = y_tolerance
  25. def enhance_table_html_with_bbox(self, html: str, paddle_text_boxes: List[Dict],
  26. start_pointer: int, table_bbox: Optional[List[int]] = None) -> Tuple[str, List[Dict], int]:
  27. """
  28. 为 HTML 表格添加 bbox 信息(优化版:先筛选表格区域)
  29. 策略:
  30. 1. 根据 table_bbox 筛选出表格区域内的 paddle_text_boxes
  31. 2. 将筛选后的 boxes 按行分组
  32. 3. 智能匹配 HTML 行与 paddle 行组
  33. 4. 在匹配的组内查找单元格
  34. Args:
  35. html: HTML 表格
  36. paddle_text_boxes: 全部 paddle OCR 结果
  37. start_pointer: 开始位置
  38. table_bbox: 表格边界框 [x1, y1, x2, y2]
  39. """
  40. soup = BeautifulSoup(html, 'html.parser')
  41. cells = []
  42. # 🔑 第一步:筛选表格区域内的 paddle boxes
  43. table_region_boxes, actual_table_bbox = self._filter_boxes_in_table_region(
  44. paddle_text_boxes[start_pointer:],
  45. table_bbox,
  46. html
  47. )
  48. if not table_region_boxes:
  49. print(f"⚠️ 未在表格区域找到 paddle boxes")
  50. return str(soup), cells, start_pointer
  51. print(f"📊 表格区域: {len(table_region_boxes)} 个文本框")
  52. print(f" 边界: {actual_table_bbox}")
  53. # 🔑 第二步:将表格区域的 boxes 按行分组
  54. grouped_boxes = self._group_paddle_boxes_by_rows(
  55. table_region_boxes,
  56. y_tolerance=self.y_tolerance
  57. )
  58. # 🔑 第三步:在每组内按 x 坐标排序
  59. for group in grouped_boxes:
  60. group['boxes'].sort(key=lambda x: x['bbox'][0])
  61. grouped_boxes.sort(key=lambda g: g['y_center'])
  62. print(f" 分组: {len(grouped_boxes)} 行")
  63. # 🔑 第四步:智能匹配 HTML 行与 paddle 行组
  64. html_rows = soup.find_all('tr')
  65. row_mapping = self._match_html_rows_to_paddle_groups(html_rows, grouped_boxes)
  66. print(f" HTML行: {len(html_rows)} 行")
  67. print(f" 映射: {len([v for v in row_mapping.values() if v])} 个有效映射")
  68. # 🔑 第五步:遍历 HTML 表格,使用映射关系查找
  69. for row_idx, row in enumerate(html_rows):
  70. group_indices = row_mapping.get(row_idx, [])
  71. if not group_indices:
  72. continue
  73. # 合并多个组的 boxes
  74. current_boxes = []
  75. for group_idx in group_indices:
  76. if group_idx < len(grouped_boxes):
  77. current_boxes.extend(grouped_boxes[group_idx]['boxes'])
  78. current_boxes.sort(key=lambda x: x['bbox'][0])
  79. # 🎯 关键改进:提取 HTML 单元格并预先确定列边界
  80. html_cells = row.find_all(['td', 'th'])
  81. if not html_cells:
  82. continue
  83. # 🔑 预估列边界(基于 x 坐标分布)
  84. col_boundaries = self._estimate_column_boundaries(
  85. current_boxes,
  86. len(html_cells)
  87. )
  88. print(f" 行 {row_idx + 1}: {len(html_cells)} 列,边界: {col_boundaries}")
  89. # 🎯 关键改进:顺序指针匹配
  90. box_pointer = 0 # 当前行的 boxes 指针
  91. for col_idx, cell in enumerate(html_cells):
  92. cell_text = cell.get_text(strip=True)
  93. if not cell_text:
  94. continue
  95. # 🔑 从当前指针开始匹配
  96. matched_result = self._match_cell_sequential(
  97. cell_text,
  98. current_boxes,
  99. col_boundaries,
  100. box_pointer
  101. )
  102. if matched_result:
  103. merged_bbox = matched_result['bbox']
  104. merged_text = matched_result['text']
  105. cell['data-bbox'] = f"[{merged_bbox[0]},{merged_bbox[1]},{merged_bbox[2]},{merged_bbox[3]}]"
  106. cell['data-score'] = f"{matched_result['score']:.4f}"
  107. cell['data-paddle-indices'] = str(matched_result['paddle_indices'])
  108. cells.append({
  109. 'type': 'table_cell',
  110. 'text': cell_text,
  111. 'matched_text': merged_text,
  112. 'bbox': merged_bbox,
  113. 'row': row_idx + 1,
  114. 'col': col_idx + 1,
  115. 'score': matched_result['score'],
  116. 'paddle_bbox_indices': matched_result['paddle_indices']
  117. })
  118. # 标记已使用
  119. for box in matched_result['used_boxes']:
  120. box['used'] = True
  121. # 🎯 移动指针到最后使用的 box 之后
  122. box_pointer = matched_result['last_used_index'] + 1
  123. print(f" 列 {col_idx + 1}: '{cell_text[:20]}...' 匹配 {len(matched_result['used_boxes'])} 个box (指针: {box_pointer})")
  124. # 计算新的指针位置
  125. used_count = sum(1 for box in table_region_boxes if box.get('used'))
  126. new_pointer = start_pointer + used_count
  127. print(f" 匹配: {len(cells)} 个单元格")
  128. return str(soup), cells, new_pointer
  129. def _estimate_column_boundaries(self, boxes: List[Dict],
  130. num_cols: int) -> List[Tuple[int, int]]:
  131. """
  132. 估算列边界(改进版:处理同列多文本框)
  133. Args:
  134. boxes: 当前行的所有 boxes(已按 x 排序)
  135. num_cols: HTML 表格的列数
  136. Returns:
  137. 列边界列表 [(x_start, x_end), ...]
  138. """
  139. if not boxes:
  140. return []
  141. # 🔑 关键改进:先按 x 坐标聚类(合并同列的多个文本框)
  142. x_clusters = self._cluster_boxes_by_x(boxes, x_tolerance=self.x_tolerance)
  143. print(f" X聚类: {len(boxes)} 个boxes -> {len(x_clusters)} 个列簇")
  144. # 获取所有 x 坐标范围
  145. x_min = min(cluster['x_min'] for cluster in x_clusters)
  146. x_max = max(cluster['x_max'] for cluster in x_clusters)
  147. # 🎯 策略 1: 如果聚类数量<=列数接近
  148. if len(x_clusters) <= num_cols:
  149. # 直接使用聚类边界
  150. boundaries = [(cluster['x_min'], cluster['x_max'])
  151. for cluster in x_clusters]
  152. return boundaries
  153. # 🎯 策略 2: 聚类数多于列数(某些列有多个文本簇)
  154. if len(x_clusters) > num_cols:
  155. print(f" ℹ️ 聚类数 {len(x_clusters)} > 列数 {num_cols},合并相近簇")
  156. # 合并相近的簇
  157. merged_clusters = self._merge_close_clusters(x_clusters, num_cols)
  158. boundaries = [(cluster['x_min'], cluster['x_max'])
  159. for cluster in merged_clusters]
  160. return boundaries
  161. return []
  162. def _cluster_boxes_by_x(self, boxes: List[Dict],
  163. x_tolerance: int = 3) -> List[Dict]:
  164. """
  165. 按 x 坐标聚类(合并同列的多个文本框)
  166. Args:
  167. boxes: 文本框列表
  168. x_tolerance: X坐标容忍度
  169. Returns:
  170. 聚类列表 [{'x_min': int, 'x_max': int, 'boxes': List[Dict]}, ...]
  171. """
  172. if not boxes:
  173. return []
  174. # 按左边界 x 坐标排序
  175. sorted_boxes = sorted(boxes, key=lambda b: b['bbox'][0])
  176. clusters = []
  177. current_cluster = None
  178. for box in sorted_boxes:
  179. bbox = box['bbox']
  180. x_start = bbox[0]
  181. x_end = bbox[2]
  182. if current_cluster is None:
  183. # 开始新簇
  184. current_cluster = {
  185. 'x_min': x_start,
  186. 'x_max': x_end,
  187. 'boxes': [box]
  188. }
  189. else:
  190. # 🔑 检查是否属于当前簇(修正后的逻辑)
  191. # 1. x 坐标有重叠:x_start <= current_x_max 且 x_end >= current_x_min
  192. # 2. 或者距离在容忍度内
  193. has_overlap = (x_start <= current_cluster['x_max'] and
  194. x_end >= current_cluster['x_min'])
  195. is_close = abs(x_start - current_cluster['x_max']) <= x_tolerance
  196. if has_overlap or is_close:
  197. # 合并到当前簇
  198. current_cluster['boxes'].append(box)
  199. current_cluster['x_min'] = min(current_cluster['x_min'], x_start)
  200. current_cluster['x_max'] = max(current_cluster['x_max'], x_end)
  201. else:
  202. # 保存当前簇,开始新簇
  203. clusters.append(current_cluster)
  204. current_cluster = {
  205. 'x_min': x_start,
  206. 'x_max': x_end,
  207. 'boxes': [box]
  208. }
  209. # 添加最后一簇
  210. if current_cluster:
  211. clusters.append(current_cluster)
  212. return clusters
  213. def _merge_close_clusters(self, clusters: List[Dict],
  214. target_count: int) -> List[Dict]:
  215. """
  216. 合并相近的簇,直到数量等于目标列数
  217. Args:
  218. clusters: 聚类列表
  219. target_count: 目标列数
  220. Returns:
  221. 合并后的聚类列表
  222. """
  223. if len(clusters) <= target_count:
  224. return clusters
  225. # 复制一份,避免修改原数据
  226. working_clusters = [c.copy() for c in clusters]
  227. while len(working_clusters) > target_count:
  228. # 找到距离最近的两个簇
  229. min_distance = float('inf')
  230. merge_idx = 0
  231. for i in range(len(working_clusters) - 1):
  232. distance = working_clusters[i + 1]['x_min'] - working_clusters[i]['x_max']
  233. if distance < min_distance:
  234. min_distance = distance
  235. merge_idx = i
  236. # 合并
  237. cluster1 = working_clusters[merge_idx]
  238. cluster2 = working_clusters[merge_idx + 1]
  239. merged_cluster = {
  240. 'x_min': cluster1['x_min'],
  241. 'x_max': cluster2['x_max'],
  242. 'boxes': cluster1['boxes'] + cluster2['boxes']
  243. }
  244. # 替换
  245. working_clusters[merge_idx] = merged_cluster
  246. working_clusters.pop(merge_idx + 1)
  247. return working_clusters
  248. def _get_boxes_in_column(self, boxes: List[Dict],
  249. boundaries: List[Tuple[int, int]],
  250. col_idx: int) -> List[Dict]:
  251. """
  252. 获取指定列范围内的 boxes(改进版:包含重叠)
  253. Args:
  254. boxes: 当前行的所有 boxes
  255. boundaries: 列边界
  256. col_idx: 列索引
  257. Returns:
  258. 该列的 boxes
  259. """
  260. if col_idx >= len(boundaries):
  261. return []
  262. x_start, x_end = boundaries[col_idx]
  263. col_boxes = []
  264. for box in boxes:
  265. bbox = box['bbox']
  266. box_x_start = bbox[0]
  267. box_x_end = bbox[2]
  268. # 🔑 改进:检查是否有重叠(不只是中心点)
  269. overlap = not (box_x_start > x_end or box_x_end < x_start)
  270. if overlap:
  271. col_boxes.append(box)
  272. return col_boxes
  273. def _filter_boxes_in_table_region(self, paddle_boxes: List[Dict],
  274. table_bbox: Optional[List[int]],
  275. html: str) -> Tuple[List[Dict], List[int]]:
  276. """
  277. 筛选表格区域内的 paddle boxes
  278. 策略:
  279. 1. 如果有 table_bbox,使用边界框筛选(扩展边界)
  280. 2. 如果没有 table_bbox,通过内容匹配推断区域
  281. Args:
  282. paddle_boxes: paddle OCR 结果
  283. table_bbox: 表格边界框 [x1, y1, x2, y2]
  284. html: HTML 内容(用于内容验证)
  285. Returns:
  286. (筛选后的 boxes, 实际表格边界框)
  287. """
  288. if not paddle_boxes:
  289. return [], [0, 0, 0, 0]
  290. # 🎯 策略 1: 使用提供的 table_bbox(扩展边界)
  291. if table_bbox and len(table_bbox) == 4:
  292. x1, y1, x2, y2 = table_bbox
  293. # 扩展边界(考虑边框外的文本)
  294. margin = 20
  295. expanded_bbox = [
  296. max(0, x1 - margin),
  297. max(0, y1 - margin),
  298. x2 + margin,
  299. y2 + margin
  300. ]
  301. filtered = []
  302. for box in paddle_boxes:
  303. bbox = box['bbox']
  304. box_center_x = (bbox[0] + bbox[2]) / 2
  305. box_center_y = (bbox[1] + bbox[3]) / 2
  306. # 中心点在扩展区域内
  307. if (expanded_bbox[0] <= box_center_x <= expanded_bbox[2] and
  308. expanded_bbox[1] <= box_center_y <= expanded_bbox[3]):
  309. filtered.append(box)
  310. if filtered:
  311. # 计算实际边界框
  312. actual_bbox = [
  313. min(b['bbox'][0] for b in filtered),
  314. min(b['bbox'][1] for b in filtered),
  315. max(b['bbox'][2] for b in filtered),
  316. max(b['bbox'][3] for b in filtered)
  317. ]
  318. return filtered, actual_bbox
  319. # 🎯 策略 2: 通过内容匹配推断区域
  320. print(" ℹ️ 无 table_bbox,使用内容匹配推断表格区域...")
  321. # 提取 HTML 中的所有文本
  322. from bs4 import BeautifulSoup
  323. soup = BeautifulSoup(html, 'html.parser')
  324. html_texts = set()
  325. for cell in soup.find_all(['td', 'th']):
  326. text = cell.get_text(strip=True)
  327. if text:
  328. html_texts.add(self.text_matcher.normalize_text(text))
  329. if not html_texts:
  330. return [], [0, 0, 0, 0]
  331. # 找出与 HTML 内容匹配的 boxes
  332. matched_boxes = []
  333. for box in paddle_boxes:
  334. normalized_text = self.text_matcher.normalize_text(box['text'])
  335. # 检查是否匹配
  336. if any(normalized_text in ht or ht in normalized_text
  337. for ht in html_texts):
  338. matched_boxes.append(box)
  339. if not matched_boxes:
  340. # 🔑 降级:如果精确匹配失败,使用模糊匹配
  341. print(" ℹ️ 精确匹配失败,尝试模糊匹配...")
  342. from fuzzywuzzy import fuzz
  343. for box in paddle_boxes:
  344. normalized_text = self.text_matcher.normalize_text(box['text'])
  345. for ht in html_texts:
  346. similarity = fuzz.partial_ratio(normalized_text, ht)
  347. if similarity >= 70: # 降低阈值
  348. matched_boxes.append(box)
  349. break
  350. if matched_boxes:
  351. # 计算边界框
  352. actual_bbox = [
  353. min(b['bbox'][0] for b in matched_boxes),
  354. min(b['bbox'][1] for b in matched_boxes),
  355. max(b['bbox'][2] for b in matched_boxes),
  356. max(b['bbox'][3] for b in matched_boxes)
  357. ]
  358. # 🔑 扩展边界,包含可能遗漏的文本
  359. margin = 30
  360. expanded_bbox = [
  361. max(0, actual_bbox[0] - margin),
  362. max(0, actual_bbox[1] - margin),
  363. actual_bbox[2] + margin,
  364. actual_bbox[3] + margin
  365. ]
  366. # 重新筛选(包含边界上的文本)
  367. final_filtered = []
  368. for box in paddle_boxes:
  369. bbox = box['bbox']
  370. box_center_x = (bbox[0] + bbox[2]) / 2
  371. box_center_y = (bbox[1] + bbox[3]) / 2
  372. if (expanded_bbox[0] <= box_center_x <= expanded_bbox[2] and
  373. expanded_bbox[1] <= box_center_y <= expanded_bbox[3]):
  374. final_filtered.append(box)
  375. return final_filtered, actual_bbox
  376. # 🔑 最后的降级:返回所有 boxes
  377. print(" ⚠️ 无法确定表格区域,使用所有 paddle boxes")
  378. if paddle_boxes:
  379. actual_bbox = [
  380. min(b['bbox'][0] for b in paddle_boxes),
  381. min(b['bbox'][1] for b in paddle_boxes),
  382. max(b['bbox'][2] for b in paddle_boxes),
  383. max(b['bbox'][3] for b in paddle_boxes)
  384. ]
  385. return paddle_boxes, actual_bbox
  386. return [], [0, 0, 0, 0]
  387. def _group_paddle_boxes_by_rows(self, paddle_boxes: List[Dict],
  388. y_tolerance: int = 10) -> List[Dict]:
  389. """
  390. 将 paddle_text_boxes 按 y 坐标分组(聚类)
  391. Args:
  392. paddle_boxes: Paddle OCR 文字框列表
  393. y_tolerance: Y 坐标容忍度(像素)
  394. Returns:
  395. 分组列表,每组包含 {'y_center': float, 'boxes': List[Dict]}
  396. """
  397. if not paddle_boxes:
  398. return []
  399. # 计算每个 box 的中心 y 坐标
  400. boxes_with_y = []
  401. for box in paddle_boxes:
  402. bbox = box['bbox']
  403. y_center = (bbox[1] + bbox[3]) / 2
  404. boxes_with_y.append({
  405. 'y_center': y_center,
  406. 'box': box
  407. })
  408. # 按 y 坐标排序
  409. boxes_with_y.sort(key=lambda x: x['y_center'])
  410. # 聚类
  411. groups = []
  412. current_group = None
  413. for item in boxes_with_y:
  414. if current_group is None:
  415. # 开始新组
  416. current_group = {
  417. 'y_center': item['y_center'],
  418. 'boxes': [item['box']]
  419. }
  420. else:
  421. # 检查是否属于当前组
  422. if abs(item['y_center'] - current_group['y_center']) <= y_tolerance:
  423. current_group['boxes'].append(item['box'])
  424. # 更新组的中心(使用平均值)
  425. current_group['y_center'] = sum(
  426. b['bbox'][1] + b['bbox'][3] for b in current_group['boxes']
  427. ) / (2 * len(current_group['boxes']))
  428. else:
  429. # 保存当前组,开始新组
  430. groups.append(current_group)
  431. current_group = {
  432. 'y_center': item['y_center'],
  433. 'boxes': [item['box']]
  434. }
  435. # 添加最后一组
  436. if current_group:
  437. groups.append(current_group)
  438. return groups
  439. def _match_html_rows_to_paddle_groups(self, html_rows: List,
  440. grouped_boxes: List[Dict]) -> Dict[int, List[int]]:
  441. """
  442. 智能匹配 HTML 行与 paddle 分组(改进版:更激进的多组合并)
  443. 策略:
  444. 1. 第一遍:基于内容精确匹配(允许匹配多个连续组)
  445. 2. 第二遍:将未使用的组合并到相邻已匹配的行
  446. """
  447. if not html_rows or not grouped_boxes:
  448. return {}
  449. mapping = {}
  450. # 🎯 策略 1: 数量相等,简单 1:1 映射
  451. if len(html_rows) == len(grouped_boxes):
  452. for i in range(len(html_rows)):
  453. mapping[i] = [i]
  454. return mapping
  455. # 🎯 策略 2: 第一遍 - 基于内容精确匹配(使用预处理后的组)
  456. used_groups = set()
  457. for row_idx, row in enumerate(html_rows):
  458. row_texts = [cell.get_text(strip=True) for cell in row.find_all(['td', 'th'])]
  459. row_texts = [t for t in row_texts if t]
  460. if not row_texts:
  461. mapping[row_idx] = []
  462. continue
  463. row_text_normalized = [self.text_matcher.normalize_text(t) for t in row_texts]
  464. # 🔑 关键改进:从当前位置开始,尝试匹配多个连续的**预处理组**
  465. best_groups = []
  466. best_score = 0
  467. # 找到第一个未使用的组
  468. start_group = next(
  469. (i for i in range(len(grouped_boxes)) if i not in used_groups),
  470. None
  471. )
  472. if start_group is None:
  473. mapping[row_idx] = []
  474. continue
  475. max_window = 5
  476. for group_count in range(1, max_window + 1):
  477. end_group = start_group + group_count
  478. if end_group > len(grouped_boxes):
  479. break
  480. combined_group_indices = list(range(start_group, end_group))
  481. if any(idx in used_groups for idx in combined_group_indices):
  482. continue
  483. combined_texts = []
  484. for g_idx in combined_group_indices:
  485. group_boxes = grouped_boxes[g_idx].get('boxes', [])
  486. for box in group_boxes:
  487. if box.get('used'):
  488. continue
  489. normalized_text = self.text_matcher.normalize_text(box.get('text', ''))
  490. if normalized_text:
  491. combined_texts.append(normalized_text)
  492. if not combined_texts:
  493. continue
  494. # 计算匹配度
  495. match_count = sum(1 for rt in row_text_normalized
  496. if any(rt in ct or ct in rt for ct in combined_texts))
  497. coverage = match_count / len(row_texts) if row_texts else 0
  498. if coverage > best_score:
  499. best_score = coverage
  500. best_groups = combined_group_indices
  501. if coverage == 1.0:
  502. break # 完美匹配,提前退出
  503. # 记录映射
  504. if best_groups and best_score > 0.3:
  505. mapping[row_idx] = best_groups
  506. used_groups.update(best_groups)
  507. print(f" 行 {row_idx}: 匹配组 {best_groups} (覆盖率: {best_score:.2f})")
  508. else:
  509. mapping[row_idx] = []
  510. # 🎯 策略 3: 第二遍 - 处理未使用的组(关键!)
  511. unused_groups = [i for i in range(len(grouped_boxes)) if i not in used_groups]
  512. if unused_groups:
  513. print(f" ℹ️ 发现 {len(unused_groups)} 个未匹配的 paddle 组: {unused_groups}")
  514. # 🔑 将未使用的组合并到相邻的已匹配行
  515. for unused_idx in unused_groups:
  516. # 🎯 关键改进:计算与相邻行的边界距离
  517. unused_group = grouped_boxes[unused_idx]
  518. unused_y_min = min(b['bbox'][1] for b in unused_group['boxes'])
  519. unused_y_max = max(b['bbox'][3] for b in unused_group['boxes'])
  520. # 🔑 查找上方和下方最近的已使用组
  521. above_idx = None
  522. below_idx = None
  523. above_distance = float('inf')
  524. below_distance = float('inf')
  525. # 向上查找
  526. for i in range(unused_idx - 1, -1, -1):
  527. if i in used_groups:
  528. above_idx = i
  529. # 🎯 边界距离:unused 的最小 y - above 的最大 y
  530. above_group = grouped_boxes[i]
  531. max_y_box = max(
  532. above_group['boxes'],
  533. key=lambda b: b['bbox'][3]
  534. )
  535. above_y_center = (max_y_box['bbox'][1] + max_y_box['bbox'][3]) / 2
  536. above_distance = abs(unused_y_min - above_y_center)
  537. print(f" • 组 {unused_idx} 与上方组 {i} 距离: {above_distance:.1f}px")
  538. break
  539. # 向下查找
  540. for i in range(unused_idx + 1, len(grouped_boxes)):
  541. if i in used_groups:
  542. below_idx = i
  543. # 🎯 边界距离:below 的最小 y - unused 的最大 y
  544. below_group = grouped_boxes[i]
  545. min_y_box = min(
  546. below_group['boxes'],
  547. key=lambda b: b['bbox'][1]
  548. )
  549. below_y_center = (min_y_box['bbox'][1] + min_y_box['bbox'][3]) / 2
  550. below_distance = abs(below_y_center - unused_y_max)
  551. print(f" • 组 {unused_idx} 与下方组 {i} 距离: {below_distance:.1f}px")
  552. break
  553. # 🎯 选择距离更近的一侧
  554. if above_idx is not None and below_idx is not None:
  555. # 都存在,选择距离更近的
  556. if above_distance < below_distance:
  557. closest_used_idx = above_idx
  558. merge_direction = "上方"
  559. else:
  560. closest_used_idx = below_idx
  561. merge_direction = "下方"
  562. print(f" ✓ 组 {unused_idx} 选择合并到{merge_direction}组 {closest_used_idx}")
  563. elif above_idx is not None:
  564. closest_used_idx = above_idx
  565. merge_direction = "上方"
  566. elif below_idx is not None:
  567. closest_used_idx = below_idx
  568. merge_direction = "下方"
  569. else:
  570. print(f" ⚠️ 组 {unused_idx} 无相邻已使用组,跳过")
  571. continue
  572. # 🔑 找到该组对应的 HTML 行
  573. target_html_row = None
  574. for html_row_idx, group_indices in mapping.items():
  575. if closest_used_idx in group_indices:
  576. target_html_row = html_row_idx
  577. break
  578. if target_html_row is not None:
  579. # 🎯 根据合并方向决定目标行
  580. if merge_direction == "上方":
  581. # 合并到上方对应的 HTML 行
  582. if target_html_row in mapping:
  583. if unused_idx not in mapping[target_html_row]:
  584. mapping[target_html_row].append(unused_idx)
  585. print(f" • 组 {unused_idx} 合并到 HTML 行 {target_html_row}(上方行)")
  586. else:
  587. # 合并到下方对应的 HTML 行
  588. if target_html_row in mapping:
  589. if unused_idx not in mapping[target_html_row]:
  590. mapping[target_html_row].append(unused_idx)
  591. print(f" • 组 {unused_idx} 合并到 HTML 行 {target_html_row}(下方行)")
  592. used_groups.add(unused_idx)
  593. # 🔑 策略 4: 第三遍 - 按 y 坐标排序每行的组索引
  594. for row_idx in mapping:
  595. if mapping[row_idx]:
  596. mapping[row_idx].sort(key=lambda idx: grouped_boxes[idx]['y_center'])
  597. return mapping
  598. def _preprocess_close_groups(self, grouped_boxes: List[Dict],
  599. y_gap_threshold: int = 10) -> List[List[int]]:
  600. """
  601. 🆕 预处理:将 y 间距很小的组预合并
  602. Args:
  603. grouped_boxes: 原始分组
  604. y_gap_threshold: Y 间距阈值(小于此值认为是同一行)
  605. Returns:
  606. 预处理后的组索引列表 [[0,1], [2], [3,4,5], ...]
  607. """
  608. if not grouped_boxes:
  609. return []
  610. preprocessed = []
  611. current_group = [0]
  612. for i in range(1, len(grouped_boxes)):
  613. prev_group = grouped_boxes[i - 1]
  614. curr_group = grouped_boxes[i]
  615. # 计算间距
  616. prev_y_max = max(b['bbox'][3] for b in prev_group['boxes'])
  617. curr_y_min = min(b['bbox'][1] for b in curr_group['boxes'])
  618. gap = abs(curr_y_min - prev_y_max)
  619. if gap <= y_gap_threshold:
  620. # 间距很小,合并
  621. current_group.append(i)
  622. print(f" 预合并: 组 {i-1} 和 {i} (间距: {gap}px)")
  623. else:
  624. # 间距较大,开始新组
  625. preprocessed.append(current_group)
  626. current_group = [i]
  627. # 添加最后一组
  628. if current_group:
  629. preprocessed.append(current_group)
  630. return preprocessed
  631. def _match_cell_sequential(self, cell_text: str,
  632. boxes: List[Dict],
  633. col_boundaries: List[Tuple[int, int]],
  634. start_idx: int) -> Optional[Dict]:
  635. """
  636. 🎯 顺序匹配单元格:从指定位置开始,逐步合并 boxes 直到匹配
  637. 策略:
  638. 1. 找到第一个未使用的 box
  639. 2. 尝试单个 box 精确匹配
  640. 3. 如果失败,尝试合并多个 boxes
  641. Args:
  642. cell_text: HTML 单元格文本
  643. boxes: 候选 boxes(已按 x 坐标排序)
  644. col_boundaries: 列边界列表
  645. start_idx: 起始索引
  646. Returns:
  647. {'bbox': [x1,y1,x2,y2], 'text': str, 'score': float,
  648. 'paddle_indices': [idx1, idx2], 'used_boxes': [box1, box2],
  649. 'last_used_index': int}
  650. """
  651. from fuzzywuzzy import fuzz
  652. cell_text_normalized = self.text_matcher.normalize_text(cell_text)
  653. if len(cell_text_normalized) < 2:
  654. return None
  655. # 🔑 找到第一个未使用的 box
  656. first_unused_idx = start_idx
  657. while first_unused_idx < len(boxes) and boxes[first_unused_idx].get('used'):
  658. first_unused_idx += 1
  659. if first_unused_idx >= len(boxes):
  660. return None
  661. # 🔑 策略 1: 单个 box 精确匹配
  662. for box in boxes[first_unused_idx:]:
  663. if box.get('used'):
  664. continue
  665. box_text = self.text_matcher.normalize_text(box['text'])
  666. if cell_text_normalized == box_text:
  667. return self._build_match_result([box], box['text'], 100.0, boxes.index(box))
  668. # 🔑 策略 2: 多个 boxes 合并匹配
  669. unused_boxes = [b for b in boxes if not b.get('used')]
  670. # 合并同列的 boxes 合并
  671. merged_bboxes = []
  672. for col_idx in range(len(col_boundaries)):
  673. combo_boxes = self._get_boxes_in_column(unused_boxes, col_boundaries, col_idx)
  674. if len(combo_boxes) > 0:
  675. sorted_combo = sorted(combo_boxes, key=lambda b: (b['bbox'][1], b['bbox'][0]))
  676. merged_text = ''.join([b['text'] for b in sorted_combo])
  677. merged_bboxes.append({
  678. 'text': merged_text,
  679. 'sorted_combo': sorted_combo
  680. })
  681. for box in merged_bboxes:
  682. # 1. 精确匹配
  683. merged_text_normalized = self.text_matcher.normalize_text(box['text'])
  684. if cell_text_normalized == merged_text_normalized:
  685. last_sort_idx = boxes.index(box['sorted_combo'][-1])
  686. return self._build_match_result(box['sorted_combo'], box['text'], 100.0, last_sort_idx)
  687. # 2. 子串匹配
  688. is_substring = (cell_text_normalized in merged_text_normalized or
  689. merged_text_normalized in cell_text_normalized)
  690. # 3. 模糊匹配
  691. similarity = fuzz.partial_ratio(cell_text_normalized, merged_text_normalized)
  692. # 🎯 子串匹配加分
  693. if is_substring:
  694. similarity = min(100, similarity + 10)
  695. if similarity >= self.text_matcher.similarity_threshold:
  696. print(f" ✓ 匹配成功: '{cell_text[:15]}' vs '{merged_text[:15]}' (相似度: {similarity})")
  697. return self._build_match_result(box['sorted_combo'], box['text'], similarity, start_idx)
  698. print(f" ✗ 匹配失败: '{cell_text[:15]}'")
  699. return None
  700. def _build_match_result(self, boxes: List[Dict], text: str,
  701. score: float, last_index: int) -> Dict:
  702. """构建匹配结果"""
  703. merged_bbox = [
  704. min(b['bbox'][0] for b in boxes),
  705. min(b['bbox'][1] for b in boxes),
  706. max(b['bbox'][2] for b in boxes),
  707. max(b['bbox'][3] for b in boxes)
  708. ]
  709. return {
  710. 'bbox': merged_bbox,
  711. 'text': text,
  712. 'score': score,
  713. 'paddle_indices': [b['paddle_bbox_index'] for b in boxes],
  714. 'used_boxes': boxes,
  715. 'last_used_index': last_index
  716. }