Convert structured recognition results to Markdown format
| 88 | |
| 89 | |
| 90 | class MarkdownConverter: |
| 91 | """Convert structured recognition results to Markdown format""" |
| 92 | |
| 93 | def __init__(self): |
| 94 | # Define heading levels for different section types |
| 95 | self.heading_levels = { |
| 96 | 'sec_0': '#', |
| 97 | 'sec_1': '##', |
| 98 | 'sec_2': '###', |
| 99 | 'sec_3': '###', |
| 100 | 'sec_4': '###', |
| 101 | 'sec_5': '###', |
| 102 | } |
| 103 | |
| 104 | # Define which labels need special handling |
| 105 | self.special_labels = { |
| 106 | 'sec_0', 'sec_1', 'sec_2', 'sec_3', 'sec_4', 'sec_5', 'list', |
| 107 | 'equ', 'tab', 'fig' |
| 108 | } |
| 109 | |
| 110 | # Define replacements for special formulas |
| 111 | self.replace_dict = { |
| 112 | '\\bm': '\mathbf ', |
| 113 | '\eqno': '\quad ', |
| 114 | '\quad': '\quad ', |
| 115 | '\leq': '\leq ', |
| 116 | '\pm': '\pm ', |
| 117 | '\\varmathbb': '\mathbb ', |
| 118 | '\in fty': '\infty', |
| 119 | '\mu': '\mu ', |
| 120 | '\cdot': '\cdot ', |
| 121 | '\langle': '\langle ', |
| 122 | '\pm': '\pm ' |
| 123 | } |
| 124 | # self.bigpattern = pattern = r"\\(big|Big|bigg|Bigg)\{(\\?[()\[\]{}]|\\langle|\\rangle)|\|\}" |
| 125 | |
| 126 | def try_remove_newline(self, text: str) -> str: |
| 127 | try: |
| 128 | # Preprocess text to handle line breaks |
| 129 | text = text.strip() |
| 130 | text = text.replace('-\n', '') |
| 131 | |
| 132 | # Handle Chinese text line breaks |
| 133 | def is_chinese(char): |
| 134 | return '\u4e00' <= char <= '\u9fff' |
| 135 | |
| 136 | lines = text.split('\n') |
| 137 | processed_lines = [] |
| 138 | |
| 139 | # Process all lines except the last one |
| 140 | for i in range(len(lines) - 1): |
| 141 | current_line = lines[i].strip() |
| 142 | next_line = lines[i + 1].strip() |
| 143 | |
| 144 | # Always add the current line, but determine if we need a newline |
| 145 | if current_line: # If current line is not empty |
| 146 | if next_line: # If next line is not empty |
| 147 | # For Chinese text handling |
no outgoing calls
no test coverage detected