matcher_utils.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import copy
  15. import re
  16. def deal_isolate_span(thead_part):
  17. """
  18. Deal with isolate span cases in this function.
  19. It causes by wrong prediction in structure recognition model.
  20. eg. predict <td rowspan="2"></td> to <td></td> rowspan="2"></b></td>.
  21. :param thead_part:
  22. :return:
  23. """
  24. # 1. find out isolate span tokens.
  25. isolate_pattern = (
  26. r"<td></td> rowspan='(\d)+' colspan='(\d)+'></b></td>|"
  27. r"<td></td> colspan='(\d)+' rowspan='(\d)+'></b></td>|"
  28. r"<td></td> rowspan='(\d)+'></b></td>|"
  29. r"<td></td> colspan='(\d)+'></b></td>"
  30. )
  31. isolate_iter = re.finditer(isolate_pattern, thead_part)
  32. isolate_list = [i.group() for i in isolate_iter]
  33. # 2. find out span number, by step 1 result.
  34. span_pattern = (
  35. r" rowspan='(\d)+' colspan='(\d)+'|"
  36. r" colspan='(\d)+' rowspan='(\d)+'|"
  37. r" rowspan='(\d)+'|"
  38. r" colspan='(\d)+'"
  39. )
  40. corrected_list = []
  41. for isolate_item in isolate_list:
  42. span_part = re.search(span_pattern, isolate_item)
  43. spanStr_in_isolateItem = span_part.group()
  44. # 3. merge the span number into the span token format string.
  45. if spanStr_in_isolateItem is not None:
  46. corrected_item = f"<td{spanStr_in_isolateItem}></td>"
  47. corrected_list.append(corrected_item)
  48. else:
  49. corrected_list.append(None)
  50. # 4. replace original isolated token.
  51. for corrected_item, isolate_item in zip(corrected_list, isolate_list):
  52. if corrected_item is not None:
  53. thead_part = thead_part.replace(isolate_item, corrected_item)
  54. else:
  55. pass
  56. return thead_part
  57. def deal_duplicate_bb(thead_part):
  58. """
  59. Deal duplicate <b> or </b> after replace.
  60. Keep one <b></b> in a <td></td> token.
  61. :param thead_part:
  62. :return:
  63. """
  64. # 1. find out <td></td> in <thead></thead>.
  65. td_pattern = (
  66. r"<td rowspan='(\d)+' colspan='(\d)+'>(.+?)</td>|"
  67. r"<td colspan='(\d)+' rowspan='(\d)+'>(.+?)</td>|"
  68. r"<td rowspan='(\d)+'>(.+?)</td>|"
  69. r"<td colspan='(\d)+'>(.+?)</td>|"
  70. r"<td>(.*?)</td>"
  71. )
  72. td_iter = re.finditer(td_pattern, thead_part)
  73. td_list = [t.group() for t in td_iter]
  74. # 2. is multiply <b></b> in <td></td> or not?
  75. new_td_list = []
  76. for td_item in td_list:
  77. if td_item.count("<b>") > 1 or td_item.count("</b>") > 1:
  78. # multiply <b></b> in <td></td> case.
  79. # 1. remove all <b></b>
  80. td_item = td_item.replace("<b>", "").replace("</b>", "")
  81. # 2. replace <tb> -> <tb><b>, </tb> -> </b></tb>.
  82. td_item = td_item.replace("<td>", "<td><b>").replace("</td>", "</b></td>")
  83. new_td_list.append(td_item)
  84. else:
  85. new_td_list.append(td_item)
  86. # 3. replace original thead part.
  87. for td_item, new_td_item in zip(td_list, new_td_list):
  88. thead_part = thead_part.replace(td_item, new_td_item)
  89. return thead_part
  90. def deal_bb(result_token):
  91. """
  92. In our opinion, <b></b> always occurs in <thead></thead> text's context.
  93. This function will find out all tokens in <thead></thead> and insert <b></b> by manual.
  94. :param result_token:
  95. :return:
  96. """
  97. # find out <thead></thead> parts.
  98. thead_pattern = "<thead>(.*?)</thead>"
  99. if re.search(thead_pattern, result_token) is None:
  100. return result_token
  101. thead_part = re.search(thead_pattern, result_token).group()
  102. origin_thead_part = copy.deepcopy(thead_part)
  103. # check "rowspan" or "colspan" occur in <thead></thead> parts or not .
  104. span_pattern = r"<td rowspan='(\d)+' colspan='(\d)+'>|<td colspan='(\d)+' rowspan='(\d)+'>|<td rowspan='(\d)+'>|<td colspan='(\d)+'>"
  105. span_iter = re.finditer(span_pattern, thead_part)
  106. span_list = [s.group() for s in span_iter]
  107. has_span_in_head = True if len(span_list) > 0 else False
  108. if not has_span_in_head:
  109. # <thead></thead> not include "rowspan" or "colspan" branch 1.
  110. # 1. replace <td> to <td><b>, and </td> to </b></td>
  111. # 2. it is possible to predict text include <b> or </b> by Text-line recognition,
  112. # so we replace <b><b> to <b>, and </b></b> to </b>
  113. thead_part = (
  114. thead_part.replace("<td>", "<td><b>")
  115. .replace("</td>", "</b></td>")
  116. .replace("<b><b>", "<b>")
  117. .replace("</b></b>", "</b>")
  118. )
  119. else:
  120. # <thead></thead> include "rowspan" or "colspan" branch 2.
  121. # Firstly, we deal rowspan or colspan cases.
  122. # 1. replace > to ><b>
  123. # 2. replace </td> to </b></td>
  124. # 3. it is possible to predict text include <b> or </b> by Text-line recognition,
  125. # so we replace <b><b> to <b>, and </b><b> to </b>
  126. # Secondly, deal ordinary cases like branch 1
  127. # replace ">" to "<b>"
  128. replaced_span_list = []
  129. for sp in span_list:
  130. replaced_span_list.append(sp.replace(">", "><b>"))
  131. for sp, rsp in zip(span_list, replaced_span_list):
  132. thead_part = thead_part.replace(sp, rsp)
  133. # replace "</td>" to "</b></td>"
  134. thead_part = thead_part.replace("</td>", "</b></td>")
  135. # remove duplicated <b> by re.sub
  136. mb_pattern = "(<b>)+"
  137. single_b_string = "<b>"
  138. thead_part = re.sub(mb_pattern, single_b_string, thead_part)
  139. mgb_pattern = "(</b>)+"
  140. single_gb_string = "</b>"
  141. thead_part = re.sub(mgb_pattern, single_gb_string, thead_part)
  142. # ordinary cases like branch 1
  143. thead_part = thead_part.replace("<td>", "<td><b>").replace("<b><b>", "<b>")
  144. # convert <tb><b></b></tb> back to <tb></tb>, empty cell has no <b></b>.
  145. # but space cell(<tb> </tb>) is suitable for <td><b> </b></td>
  146. thead_part = thead_part.replace("<td><b></b></td>", "<td></td>")
  147. # deal with duplicated <b></b>
  148. thead_part = deal_duplicate_bb(thead_part)
  149. # deal with isolate span tokens, which causes by wrong predict by structure prediction.
  150. # eg.PMC5994107_011_00.png
  151. thead_part = deal_isolate_span(thead_part)
  152. # replace original result with new thead part.
  153. result_token = result_token.replace(origin_thead_part, thead_part)
  154. return result_token
  155. def deal_eb_token(master_token):
  156. """
  157. post process with <eb></eb>, <eb1></eb1>, ...
  158. emptyBboxTokenDict = {
  159. "[]": '<eb></eb>',
  160. "[' ']": '<eb1></eb1>',
  161. "['<b>', ' ', '</b>']": '<eb2></eb2>',
  162. "['\\u2028', '\\u2028']": '<eb3></eb3>',
  163. "['<sup>', ' ', '</sup>']": '<eb4></eb4>',
  164. "['<b>', '</b>']": '<eb5></eb5>',
  165. "['<i>', ' ', '</i>']": '<eb6></eb6>',
  166. "['<b>', '<i>', '</i>', '</b>']": '<eb7></eb7>',
  167. "['<b>', '<i>', ' ', '</i>', '</b>']": '<eb8></eb8>',
  168. "['<i>', '</i>']": '<eb9></eb9>',
  169. "['<b>', ' ', '\\u2028', ' ', '\\u2028', ' ', '</b>']": '<eb10></eb10>',
  170. }
  171. :param master_token:
  172. :return:
  173. """
  174. master_token = master_token.replace("<eb></eb>", "<td></td>")
  175. master_token = master_token.replace("<eb1></eb1>", "<td> </td>")
  176. master_token = master_token.replace("<eb2></eb2>", "<td><b> </b></td>")
  177. master_token = master_token.replace("<eb3></eb3>", "<td>\u2028\u2028</td>")
  178. master_token = master_token.replace("<eb4></eb4>", "<td><sup> </sup></td>")
  179. master_token = master_token.replace("<eb5></eb5>", "<td><b></b></td>")
  180. master_token = master_token.replace("<eb6></eb6>", "<td><i> </i></td>")
  181. master_token = master_token.replace("<eb7></eb7>", "<td><b><i></i></b></td>")
  182. master_token = master_token.replace("<eb8></eb8>", "<td><b><i> </i></b></td>")
  183. master_token = master_token.replace("<eb9></eb9>", "<td><i></i></td>")
  184. master_token = master_token.replace(
  185. "<eb10></eb10>", "<td><b> \u2028 \u2028 </b></td>"
  186. )
  187. return master_token
  188. def distance(box_1, box_2):
  189. x1, y1, x2, y2 = box_1
  190. x3, y3, x4, y4 = box_2
  191. dis = abs(x3 - x1) + abs(y3 - y1) + abs(x4 - x2) + abs(y4 - y2)
  192. dis_2 = abs(x3 - x1) + abs(y3 - y1)
  193. dis_3 = abs(x4 - x2) + abs(y4 - y2)
  194. return dis + min(dis_2, dis_3)
  195. def compute_iou(rec1, rec2):
  196. """
  197. computing IoU
  198. :param rec1: (y0, x0, y1, x1), which reflects
  199. (top, left, bottom, right)
  200. :param rec2: (y0, x0, y1, x1)
  201. :return: scala value of IoU
  202. """
  203. # computing area of each rectangles
  204. S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1])
  205. S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1])
  206. # computing the sum_area
  207. sum_area = S_rec1 + S_rec2
  208. # find the each edge of intersect rectangle
  209. left_line = max(rec1[1], rec2[1])
  210. right_line = min(rec1[3], rec2[3])
  211. top_line = max(rec1[0], rec2[0])
  212. bottom_line = min(rec1[2], rec2[2])
  213. # judge if there is an intersect
  214. if left_line >= right_line or top_line >= bottom_line:
  215. return 0.0
  216. intersect = (right_line - left_line) * (bottom_line - top_line)
  217. return (intersect / (sum_area - intersect)) * 1.0