浏览代码

convert "References" and "Abstract" text into title format (#3615)

* convert "References" and "Abstract" text into title format

* add space when concating title texts
Tingquan Gao 8 月之前
父节点
当前提交
b502cdbcba

+ 16 - 12
paddlex/inference/pipelines/layout_parsing/result_v2.py

@@ -274,14 +274,16 @@ class LayoutParsingResultV2(BaseCVResult, HtmlMixin, XlsxMixin, MarkdownMixin):
                 )
                 )
                 return "\n".join(img_tags)
                 return "\n".join(img_tags)
 
 
-            def format_reference():
-                pattern = r"\s*\[\s*\d+\s*\]\s*"
-                res = re.sub(
-                    pattern,
-                    lambda match: "\n" + match.group(),
-                    block["reference"].replace("\n", ""),
-                )
-                return "\n" + res
+            def format_first_line(templates, format_func, spliter):
+                lines = block["block_content"].split(spliter)
+                for idx in range(len(lines)):
+                    line = lines[idx]
+                    if line.strip() == "":
+                        continue
+                    if line.lower() in templates:
+                        lines[idx] = format_func(line)
+                    break
+                return spliter.join(lines)
 
 
             def format_table():
             def format_table():
                 return "\n" + block["block_content"]
                 return "\n" + block["block_content"]
@@ -298,9 +300,9 @@ class LayoutParsingResultV2(BaseCVResult, HtmlMixin, XlsxMixin, MarkdownMixin):
                 "text": lambda: block["block_content"]
                 "text": lambda: block["block_content"]
                 .replace("-\n", " ")
                 .replace("-\n", " ")
                 .replace("\n", " "),
                 .replace("\n", " "),
-                "abstract": lambda: block["block_content"]
-                .replace("-\n", " ")
-                .replace("\n", " "),
+                "abstract": lambda: format_first_line(
+                    ["摘要", "abstract"], lambda l: f"## {l}\n", " "
+                ),
                 "content": lambda: block["block_content"]
                 "content": lambda: block["block_content"]
                 .replace("-\n", " ")
                 .replace("-\n", " ")
                 .replace("\n", " "),
                 .replace("\n", " "),
@@ -308,7 +310,9 @@ class LayoutParsingResultV2(BaseCVResult, HtmlMixin, XlsxMixin, MarkdownMixin):
                 "chart": lambda: format_image("block_image"),
                 "chart": lambda: format_image("block_image"),
                 "formula": lambda: f"$${block['block_content']}$$",
                 "formula": lambda: f"$${block['block_content']}$$",
                 "table": format_table,
                 "table": format_table,
-                "reference": lambda: block["block_content"],
+                "reference": lambda: format_first_line(
+                    ["参考文献", "references"], lambda l: f"## {l}", "\n"
+                ),
                 "algorithm": lambda: block["block_content"].strip("\n"),
                 "algorithm": lambda: block["block_content"].strip("\n"),
                 "seal": lambda: f"Words of Seals:\n{block['block_content']}",
                 "seal": lambda: f"Words of Seals:\n{block['block_content']}",
             }
             }

+ 6 - 3
paddlex/inference/pipelines/layout_parsing/utils.py

@@ -667,9 +667,12 @@ def get_single_block_parsing_res(
                     },
                     },
                 )
                 )
             else:
             else:
-                content = "".join(rec_res["rec_texts"])
-                if label != "reference":
-                    content = _process_text(content)
+                if label in ["doc_title"]:
+                    content = " ".join(rec_res["rec_texts"])
+                else:
+                    content = "".join(rec_res["rec_texts"])
+                    if label != "reference":
+                        content = _process_text(content)
                 single_block_layout_parsing_res.append(
                 single_block_layout_parsing_res.append(
                     {
                     {
                         "block_label": label,
                         "block_label": label,