Extract the abstract/summary block when present.
(text: str)
| 437 | |
| 438 | |
| 439 | def extract_abstract(text: str) -> str: |
| 440 | """Extract the abstract/summary block when present.""" |
| 441 | normalized_text = clean_extracted_text(text) |
| 442 | if normalized_text: |
| 443 | lines = normalized_text.split("\n") |
| 444 | start_index = None |
| 445 | for index, line in enumerate(lines): |
| 446 | if _normalize_heading_line(line) in ABSTRACT_HEADINGS: |
| 447 | start_index = index + 1 |
| 448 | break |
| 449 | |
| 450 | if start_index is not None: |
| 451 | body_lines: List[str] = [] |
| 452 | previous_blank = False |
| 453 | for line in lines[start_index:]: |
| 454 | stripped = line.strip() |
| 455 | if not stripped: |
| 456 | if body_lines and not previous_blank: |
| 457 | body_lines.append("") |
| 458 | previous_blank = True |
| 459 | continue |
| 460 | |
| 461 | previous_blank = False |
| 462 | normalized_heading = _normalize_heading_line(stripped) |
| 463 | if body_lines and ( |
| 464 | normalized_heading in SECTION_TITLES |
| 465 | or normalized_heading in KEYWORD_HEADINGS |
| 466 | or stripped.lower().startswith("keywords:") |
| 467 | or stripped.startswith("关键词") |
| 468 | ): |
| 469 | break |
| 470 | body_lines.append(stripped) |
| 471 | |
| 472 | abstract = "\n".join(body_lines).strip() |
| 473 | if abstract: |
| 474 | return abstract |
| 475 | |
| 476 | patterns = [ |
| 477 | r"(?:^|\n)\s*(?:abstract|summary|摘要)\s*[::]?\s*\n+(?P<body>.+?)(?=\n\s*(?:keywords?|introduction|background|related work|1\.|i\.|一、)\b|\Z)", |
| 478 | r"(?:^|\n)\s*(?:abstract|summary|摘要)\s*[::]?\s*(?P<body>.+?)(?=\n\s*\n|\n\s*(?:introduction|background|1\.|i\.)\b|\Z)", |
| 479 | ] |
| 480 | for pattern in patterns: |
| 481 | match = re.search(pattern, text, re.IGNORECASE | re.DOTALL) |
| 482 | if match: |
| 483 | return match.group("body").strip() |
| 484 | return "" |
| 485 | |
| 486 | |
| 487 | def extract_text_from_pdf(pdf_path: str) -> str: |
no test coverage detected