Normalize PDF text so downstream regexes behave predictably.
(text: str)
| 139 | |
| 140 | |
| 141 | def clean_extracted_text(text: str) -> str: |
| 142 | """Normalize PDF text so downstream regexes behave predictably.""" |
| 143 | if not text: |
| 144 | return "" |
| 145 | |
| 146 | text = ( |
| 147 | text.replace("\r\n", "\n") |
| 148 | .replace("\r", "\n") |
| 149 | .replace("\u200b", "") |
| 150 | .replace("\u200c", "") |
| 151 | .replace("\u200d", "") |
| 152 | .replace("\ufeff", "") |
| 153 | ) |
| 154 | |
| 155 | cleaned_lines: List[str] = [] |
| 156 | previous_blank = False |
| 157 | for raw_line in text.split("\n"): |
| 158 | line = re.sub(r"[ \t]+", " ", raw_line).strip() |
| 159 | if not line: |
| 160 | if not previous_blank: |
| 161 | cleaned_lines.append("") |
| 162 | previous_blank = True |
| 163 | continue |
| 164 | cleaned_lines.append(line) |
| 165 | previous_blank = False |
| 166 | |
| 167 | return "\n".join(cleaned_lines).strip() |
| 168 | |
| 169 | |
| 170 | def _normalize_heading_line(line: str) -> str: |
no outgoing calls
no test coverage detected