code: def deal_duplicate_bb(thead_part):
      """
      Deal duplicate <b> or </b> after replace.
      Keep one <b></b> in a <td></td> token.
      :param thead_part:
      :return:
      """
      # 1. find out <td></td> in <thead></thead>.
      td_pattern = (
          '<td rowspan="(\d)+" colspan="(\d)+">(.+?)</td>|'
          '<td colspan="(\d)+" rowspan="(\d)+">(.+?)</td>|'
          '<td rowspan="(\d)+">(.+?)</td>|'
          '<td colspan="(\d)+">(.+?)</td>|'
          "<td>(.*?)</td>"
      )
      td_iter = re.finditer(td_pattern, thead_part)
      td_list = [t.group() for t in td_iter]
  
      # 2. is multiply <b></b> in <td></td> or not?
      new_td_list = []
      for td_item in td_list:
          if td_item.count("<b>") > 1 or td_item.count("</b>") > 1:
              # multiply <b></b> in <td></td> case.
              # 1. remove all <b></b>
              td_item = td_item.replace("<b>", "").replace("</b>", "")
              # 2. replace <tb> -> <tb><b>, </tb> -> </b></tb>.
              td_item = td_item.replace("<td>", "<td><b>").replace(
                  "</td>", "</b></td>"
              )
              new_td_list.append(td_item)
          else:
              new_td_list.append(td_item)
  
      # 3. replace original thead part.
      for td_item, new_td_item in zip(td_list, new_td_list):
          thead_part = thead_part.replace(td_item, new_td_item)
      return thead_part
  
imports: import os
  import re
  import cv2
  import glob
  import copy
  import math
  import pickle
  import numpy
  from shapely.geometry import Polygon, MultiPoint
label_desc: Use image node_id 27 for calling a global function with example usage: deal_duplicate_bb(thead_part) and returns: thead_part