normalize_financial_numbers.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import re
  2. import os
  3. from pathlib import Path
  4. def normalize_financial_numbers(text: str) -> str:
  5. """
  6. 标准化财务数字:将全角字符转换为半角字符
  7. Args:
  8. text: 原始文本
  9. Returns:
  10. 标准化后的文本
  11. """
  12. if not text:
  13. return text
  14. # 定义全角到半角的映射
  15. fullwidth_to_halfwidth = {
  16. '0': '0', '1': '1', '2': '2', '3': '3', '4': '4',
  17. '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
  18. ',': ',', # 全角逗号转半角逗号
  19. '。': '.', # 全角句号转半角句号
  20. '.': '.', # 全角句点转半角句点
  21. ':': ':', # 全角冒号转半角冒号
  22. ';': ';', # 全角分号转半角分号
  23. '(': '(', # 全角左括号转半角左括号
  24. ')': ')', # 全角右括号转半角右括号
  25. '-': '-', # 全角减号转半角减号
  26. '+': '+', # 全角加号转半角加号
  27. '%': '%', # 全角百分号转半角百分号
  28. }
  29. # 第一步:执行基础字符替换
  30. normalized_text = text
  31. for fullwidth, halfwidth in fullwidth_to_halfwidth.items():
  32. normalized_text = normalized_text.replace(fullwidth, halfwidth)
  33. # 第二步:处理数字序列中的空格和分隔符
  34. # 修改正则表达式以匹配完整的数字序列,包括空格
  35. # 匹配模式:数字 + (空格? + 逗号 + 空格? + 数字)* + (空格? + 小数点 + 数字+)?
  36. number_sequence_pattern = r'(\d+(?:\s*[,,]\s*\d+)*(?:\s*[。..]\s*\d+)?)'
  37. def normalize_number_sequence(match):
  38. sequence = match.group(1)
  39. # 处理千分位分隔符周围的空格
  40. # 将 "数字 + 空格 + 逗号 + 空格 + 数字" 标准化为 "数字,数字"
  41. sequence = re.sub(r'(\d)\s*[,,]\s*(\d)', r'\1,\2', sequence)
  42. # 处理小数点周围的空格
  43. # 将 "数字 + 空格 + 小数点 + 空格 + 数字" 标准化为 "数字.数字"
  44. sequence = re.sub(r'(\d)\s*[。..]\s*(\d)', r'\1.\2', sequence)
  45. return sequence
  46. normalized_text = re.sub(number_sequence_pattern, normalize_number_sequence, normalized_text)
  47. return normalized_text
  48. def normalize_markdown_table(markdown_content: str) -> str:
  49. """
  50. 专门处理Markdown表格中的数字标准化
  51. Args:
  52. markdown_content: Markdown内容
  53. Returns:
  54. 标准化后的Markdown内容
  55. """
  56. # 使用BeautifulSoup处理HTML表格
  57. from bs4 import BeautifulSoup, Tag
  58. soup = BeautifulSoup(markdown_content, 'html.parser')
  59. tables = soup.find_all('table')
  60. for table in tables:
  61. if isinstance(table, Tag):
  62. cells = table.find_all(['td', 'th'])
  63. for cell in cells:
  64. if isinstance(cell, Tag):
  65. original_text = cell.get_text()
  66. normalized_text = normalize_financial_numbers(original_text)
  67. # 如果内容发生了变化,更新单元格内容
  68. if original_text != normalized_text:
  69. cell.string = normalized_text
  70. # 返回更新后的HTML
  71. return str(soup)
  72. def normalize_json_table(json_content: str) -> str:
  73. """
  74. 专门处理JSON格式OCR结果中表格的数字标准化
  75. Args:
  76. json_content: JSON格式的OCR结果内容
  77. Returns:
  78. 标准化后的JSON内容
  79. """
  80. """
  81. json_content 示例:
  82. [
  83. {
  84. "category": "Table",
  85. "text": "<table>...</table>"
  86. },
  87. {
  88. "category": "Text",
  89. "text": "Some other text"
  90. }
  91. ]
  92. """
  93. import json
  94. try:
  95. # 解析JSON内容
  96. data = json.loads(json_content) if isinstance(json_content, str) else json_content
  97. # 确保data是列表格式
  98. if not isinstance(data, list):
  99. return json_content
  100. # 遍历所有OCR结果项
  101. for item in data:
  102. if not isinstance(item, dict):
  103. continue
  104. # 检查是否是表格类型
  105. if item.get('category') == 'Table' and 'text' in item:
  106. table_html = item['text']
  107. # 使用BeautifulSoup处理HTML表格
  108. from bs4 import BeautifulSoup, Tag
  109. soup = BeautifulSoup(table_html, 'html.parser')
  110. tables = soup.find_all('table')
  111. for table in tables:
  112. if isinstance(table, Tag):
  113. cells = table.find_all(['td', 'th'])
  114. for cell in cells:
  115. if isinstance(cell, Tag):
  116. original_text = cell.get_text()
  117. # 应用数字标准化
  118. normalized_text = normalize_financial_numbers(original_text)
  119. # 如果内容发生了变化,更新单元格内容
  120. if original_text != normalized_text:
  121. cell.string = normalized_text
  122. # 更新item中的表格内容
  123. item['text'] = str(soup)
  124. # 同时标准化普通文本中的数字(如果需要)
  125. # elif 'text' in item:
  126. # original_text = item['text']
  127. # normalized_text = normalize_financial_numbers(original_text)
  128. # if original_text != normalized_text:
  129. # item['text'] = normalized_text
  130. # 返回标准化后的JSON字符串
  131. return json.dumps(data, ensure_ascii=False, indent=2)
  132. except json.JSONDecodeError as e:
  133. print(f"⚠️ JSON解析失败: {e}")
  134. return json_content
  135. except Exception as e:
  136. print(f"⚠️ JSON表格标准化失败: {e}")
  137. return json_content
  138. def normalize_json_file(file_path: str, output_path: str | None = None) -> str:
  139. """
  140. 标准化JSON文件中的表格数字
  141. Args:
  142. file_path: 输入JSON文件路径
  143. output_path: 输出文件路径,如果为None则覆盖原文件
  144. Returns:
  145. 标准化后的JSON内容
  146. """
  147. input_file = Path(file_path)
  148. output_file = Path(output_path) if output_path else input_file
  149. if not input_file.exists():
  150. raise FileNotFoundError(f"找不到文件: {file_path}")
  151. # 读取原始JSON文件
  152. with open(input_file, 'r', encoding='utf-8') as f:
  153. original_content = f.read()
  154. print(f"🔧 正在标准化JSON文件: {input_file.name}")
  155. # 标准化内容
  156. normalized_content = normalize_json_table(original_content)
  157. # 保存标准化后的文件
  158. with open(output_file, 'w', encoding='utf-8') as f:
  159. f.write(normalized_content)
  160. # 统计变化
  161. changes = sum(1 for o, n in zip(original_content, normalized_content) if o != n)
  162. if changes > 0:
  163. print(f"✅ 标准化了 {changes} 个字符")
  164. # 如果输出路径不同,也保存原始版本
  165. if output_path and output_path != file_path:
  166. original_backup = Path(output_path).parent / f"{Path(output_path).stem}_original.json"
  167. with open(original_backup, 'w', encoding='utf-8') as f:
  168. f.write(original_content)
  169. print(f"📄 原始版本已保存到: {original_backup}")
  170. else:
  171. print("ℹ️ 无需标准化(已是标准格式)")
  172. print(f"📄 标准化结果已保存到: {output_file}")
  173. return normalized_content
  174. if __name__ == "__main__":
  175. # 简单测试
  176. test_strings = [
  177. "28, 239, 305.48",
  178. "2023年净利润为28,239,305.48元",
  179. "总资产为1,234,567.89元",
  180. "负债总额为500,000.00元",
  181. "收入增长了10.5%,达到1,200,000元",
  182. "费用为300,000元",
  183. "利润率为15.2%",
  184. "现金流量为-50,000元",
  185. "股东权益为2,500,000.00元",
  186. "每股收益为3.25元",
  187. "市盈率为20.5倍",
  188. "营业收入为750,000元",
  189. "净资产收益率为12.3%",
  190. "总负债为1,200,000元",
  191. "流动比率为1.5倍",
  192. "速动比率为1.2倍",
  193. "资产负债率为40%",
  194. "存货周转率为6次/年",
  195. "应收账款周转率为8次/年",
  196. "固定资产周转率为2次/年",
  197. "总资产周转率为1.2次/年",
  198. "经营活动产生的现金流量净额为200,000元"
  199. ]
  200. for s in test_strings:
  201. print("原始: ", s)
  202. print("标准化: ", normalize_financial_numbers(s))
  203. print("-" * 50)