Skip to content

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

refactor: improve CJK language handling and hyphen management in text processing

myhloli0dd4c4c4e4c6
diff --git a/mineru/backend/pipeline/pipeline_middle_json_mkcontent.py b/mineru/backend/pipeline/pipeline_middle_json_mkcontent.py
index ace57862..8a80b0da 100644
--- a/mineru/backend/pipeline/pipeline_middle_json_mkcontent.py
+++ b/mineru/backend/pipeline/pipeline_middle_json_mkcontent.py
@@ -139,10 +139,15 @@ def merge_para_with_text(para_block):
para_text += content
continue
- langs = ['zh', 'ja', 'ko']
+ # 定义CJK语言集合(中日韩)
+ cjk_langs = {'zh', 'ja', 'ko'}
# logger.info(f'block_lang: {block_lang}, content: {content}')
- if block_lang in langs: # 中文/日语/韩文语境下,换行不需要空格分隔,但是如果是行内公式结尾,还是要加空格
- if j == len(line['spans']) - 1 and span_type not in [ContentType.INLINE_EQUATION]:
+
+ # 判断是否为行末span
+ is_last_span = j == len(line['spans']) - 1
+
+ if block_lang in cjk_langs: # 中文/日语/韩文语境下,换行不需要空格分隔,但是如果是行内公式结尾,还是要加空格
+ if is_last_span and span_type not in [ContentType.INLINE_EQUATION]:
para_text += content
else:
para_text += f'{content} '
@@ -151,21 +156,21 @@ def merge_para_with_text(para_block):
if span_type in [ContentType.TEXT, ContentType.INLINE_EQUATION]:
# 如果span是line的最后一个且末尾带有-连字符,那么末尾不应该加空格,同时应该把-删除
if (
- j == len(line['spans']) - 1
+ is_last_span
and span_type == ContentType.TEXT
and is_hyphen_at_line_end(content)
):
- # 如果下一行的第一个span是大写字母开头,不删除连字符
+ # 如果下一行的第一个span是小写字母开头,删除连字符
if (
i + 1 < len(para_block['lines'])
- and para_block['lines'][i + 1]['spans']
- and para_block['lines'][i + 1]['spans'][0]['type'] == ContentType.TEXT
- and para_block['lines'][i + 1]['spans'][0]['content']
- and para_block['lines'][i + 1]['spans'][0]['content'][0].isupper()
+ and para_block['lines'][i + 1].get('spans')
+ and para_block['lines'][i + 1]['spans'][0].get('type') == ContentType.TEXT
+ and para_block['lines'][i + 1]['spans'][0].get('content', '')
+ and para_block['lines'][i + 1]['spans'][0]['content'][0].islower()
):
- para_text += content
- else:
para_text += content[:-1]
+ else: # 如果没有下一行,或者下一行的第一个span不是小写字母开头,则保留连字符但不加空格
+ para_text += content
else: # 西方文本语境下 content间需要空格分隔
para_text += f'{content} '
else:
diff --git a/mineru/backend/vlm/vlm_middle_json_mkcontent.py b/mineru/backend/vlm/vlm_middle_json_mkcontent.py
index 379d8e07..c081cd6f 100644
--- a/mineru/backend/vlm/vlm_middle_json_mkcontent.py
+++ b/mineru/backend/vlm/vlm_middle_json_mkcontent.py
@@ -46,14 +46,7 @@ def merge_para_with_text(para_block, formula_enable=True, img_buket_path=''):
else:
if span.get('image_path', ''):
content = f"![]({img_buket_path}/{span['image_path']})"
- # if content:
- # if span_type in [ContentType.TEXT, ContentType.INLINE_EQUATION]:
- # if j == len(line['spans']) - 1:
- # para_text += content
- # else:
- # para_text += f'{content} '
- # elif span_type == ContentType.INTERLINE_EQUATION:
- # para_text += content
+
content = content.strip()
if content:
@@ -61,10 +54,15 @@ def merge_para_with_text(para_block, formula_enable=True, img_buket_path=''):
para_text += content
continue
- langs = ['zh', 'ja', 'ko']
+ # 定义CJK语言集合(中日韩)
+ cjk_langs = {'zh', 'ja', 'ko'}
# logger.info(f'block_lang: {block_lang}, content: {content}')
- if block_lang in langs: # 中文/日语/韩文语境下,换行不需要空格分隔,但是如果是行内公式结尾,还是要加空格
- if j == len(line['spans']) - 1 and span_type not in [ContentType.INLINE_EQUATION]:
+
+ # 判断是否为行末span
+ is_last_span = j == len(line['spans']) - 1
+
+ if block_lang in cjk_langs: # 中文/日语/韩文语境下,换行不需要空格分隔,但是如果是行内公式结尾,还是要加空格
+ if is_last_span and span_type != ContentType.INLINE_EQUATION:
para_text += content
else:
para_text += f'{content} '
@@ -73,16 +71,16 @@ def merge_para_with_text(para_block, formula_enable=True, img_buket_path=''):
if span_type in [ContentType.TEXT, ContentType.INLINE_EQUATION]:
# 如果span是line的最后一个且末尾带有-连字符,那么末尾不应该加空格,同时应该把-删除
if (
- j == len(line['spans']) - 1
+ is_last_span
and span_type == ContentType.TEXT
and is_hyphen_at_line_end(content)
):
# 如果下一行的第一个span是小写字母开头,删除连字符
if (
i+1 < len(para_block['lines'])
- and para_block['lines'][i + 1]['spans']
- and para_block['lines'][i + 1]['spans'][0]['type'] == ContentType.TEXT
- and para_block['lines'][i + 1]['spans'][0]['content']
+ and para_block['lines'][i + 1].get('spans')
+ and para_block['lines'][i + 1]['spans'][0].get('type') == ContentType.TEXT
+ and para_block['lines'][i + 1]['spans'][0].get('content', '')
and para_block['lines'][i + 1]['spans'][0]['content'][0].islower()
):
para_text += content[:-1]
@@ -537,7 +535,7 @@ def merge_para_with_text_v2(para_block):
para_content = []
para_type = para_block['type']
- for line in para_block['lines']:
+ for i, line in enumerate(para_block['lines']):
for j, span in enumerate(line['spans']):
span_type = span['type']
if span.get("content", '').strip():
@@ -551,20 +549,35 @@ def merge_para_with_text_v2(para_block):
if span_type in [
ContentTypeV2.SPAN_TEXT,
]:
- langs = ['zh', 'ja', 'ko']
+ # 定义CJK语言集合(中日韩)
+ cjk_langs = {'zh', 'ja', 'ko'}
# logger.info(f'block_lang: {block_lang}, content: {content}')
- if block_lang in langs: # 中文/日语/韩文语境下,换行不需要空格分隔,但是如果是行内公式结尾,还是要加空格
- if j == len(line['spans']) - 1:
+
+ # 判断是否为行末span
+ is_last_span = j == len(line['spans']) - 1
+
+ if block_lang in cjk_langs: # 中文/日语/韩文语境下,换行不需要空格分隔,但是如果是行内公式结尾,还是要加空格
+ if is_last_span:
span_content = span['content']
else:
span_content = f"{span['content']} "
else:
# 如果span是line的最后一个且末尾带有-连字符,那么末尾不应该加空格,同时应该把-删除
if (
- j == len(line['spans']) - 1
+ is_last_span
and is_hyphen_at_line_end(span['content'])
):
- span_content = span['content'][:-1]
+ # 如果下一行的第一个span是小写字母开头,删除连字符
+ if (
+ i + 1 < len(para_block['lines'])
+ and para_block['lines'][i + 1].get('spans')
+ and para_block['lines'][i + 1]['spans'][0].get('type') == ContentType.TEXT
+ and para_block['lines'][i + 1]['spans'][0].get('content', '')
+ and para_block['lines'][i + 1]['spans'][0]['content'][0].islower()
+ ):
+ span_content = span['content'][:-1]
+ else: # 如果没有下一行,或者下一行的第一个span不是小写字母开头,则保留连字符但不加空格
+ span_content = span['content']
else:
# 西方文本语境下content间需要空格分隔
span_content = f"{span['content']} "