Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: optimize character processing in span_pre_proc.py for improved spacing and content generation

myhloli7f365ce96bba
diff --git a/mineru/utils/span_pre_proc.py b/mineru/utils/span_pre_proc.py
index de74d52f..9ba76f9a 100644
--- a/mineru/utils/span_pre_proc.py
+++ b/mineru/utils/span_pre_proc.py
@@ -216,28 +216,32 @@ def calculate_char_in_span(char_bbox, span_bbox, char, span_height_ratio=Span_He
def chars_to_content(span):
# 检查span中的char是否为空
- if len(span['chars']) == 0:
- pass
- else:
+ if len(span['chars']) != 0:
# 给chars按char_idx排序
- span['chars'] = sorted(span['chars'], key=lambda x: x['char_idx'])
+ chars = sorted(span['chars'], key=lambda x: x['char_idx'])
# Calculate the width of each character
- char_widths = [char['bbox'][2] - char['bbox'][0] for char in span['chars']]
+ char_widths = [char['bbox'][2] - char['bbox'][0] for char in chars]
# Calculate the median width
median_width = statistics.median(char_widths)
- content = ''
- for char in span['chars']:
+ parts = []
+ for idx, char1 in enumerate(chars):
+ char2 = chars[idx + 1] if idx + 1 < len(chars) else None
# 如果下一个char的x0和上一个char的x1距离超过0.25个字符宽度,则需要在中间插入一个空格
- char1 = char
- char2 = span['chars'][span['chars'].index(char) + 1] if span['chars'].index(char) + 1 < len(span['chars']) else None
- if char2 and char2['bbox'][0] - char1['bbox'][2] > median_width * 0.25 and char['char'] != ' ' and char2['char'] != ' ':
- content += f"{char['char']} "
+ if (
+ char2
+ and char2['bbox'][0] - char1['bbox'][2] > median_width * 0.25
+ and char1['char'] != ' '
+ and char2['char'] != ' '
+ ):
+ parts.append(char1['char'])
+ parts.append(' ')
else:
- content += char['char']
+ parts.append(char1['char'])
+ content = ''.join(parts)
content = __replace_unicode(content)
content = __replace_ligatures(content)
content = __replace_ligatures(content)