You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import copy
  2. from nltk.stem import PorterStemmer
  3. stemmer = PorterStemmer()
  4. import re
  5. from nltk import word_tokenize
  6. from . import huqie
  7. from rag.utils import num_tokens_from_string
  8. import random
  9. BULLET_PATTERN = [[
  10. r"第[零一二三四五六七八九十百0-9]+(分?编|部分)",
  11. r"第[零一二三四五六七八九十百0-9]+章",
  12. r"第[零一二三四五六七八九十百0-9]+节",
  13. r"第[零一二三四五六七八九十百0-9]+条",
  14. r"[\((][零一二三四五六七八九十百]+[\))]",
  15. ], [
  16. r"第[0-9]+章",
  17. r"第[0-9]+节",
  18. r"[0-9]{,3}[\. 、]",
  19. r"[0-9]{,2}\.[0-9]{,2}",
  20. r"[0-9]{,2}\.[0-9]{,2}\.[0-9]{,2}",
  21. r"[0-9]{,2}\.[0-9]{,2}\.[0-9]{,2}\.[0-9]{,2}",
  22. ], [
  23. r"第[零一二三四五六七八九十百0-9]+章",
  24. r"第[零一二三四五六七八九十百0-9]+节",
  25. r"[零一二三四五六七八九十百]+[ 、]",
  26. r"[\((][零一二三四五六七八九十百]+[\))]",
  27. r"[\((][0-9]{,2}[\))]",
  28. ], [
  29. r"PART (ONE|TWO|THREE|FOUR|FIVE|SIX|SEVEN|EIGHT|NINE|TEN)",
  30. r"Chapter (I+V?|VI*|XI|IX|X)",
  31. r"Section [0-9]+",
  32. r"Article [0-9]+"
  33. ]
  34. ]
  35. def random_choices(arr, k):
  36. k = min(len(arr), k)
  37. return random.choices(arr, k=k)
  38. def bullets_category(sections):
  39. global BULLET_PATTERN
  40. hits = [0] * len(BULLET_PATTERN)
  41. for i, pro in enumerate(BULLET_PATTERN):
  42. for sec in sections:
  43. for p in pro:
  44. if re.match(p, sec):
  45. hits[i] += 1
  46. break
  47. maxium = 0
  48. res = -1
  49. for i, h in enumerate(hits):
  50. if h <= maxium: continue
  51. res = i
  52. maxium = h
  53. return res
  54. def is_english(texts):
  55. eng = 0
  56. for t in texts:
  57. if re.match(r"[a-zA-Z]{2,}", t.strip()):
  58. eng += 1
  59. if eng / len(texts) > 0.8:
  60. return True
  61. return False
  62. def tokenize(d, t, eng):
  63. d["content_with_weight"] = t
  64. if eng:
  65. t = re.sub(r"([a-z])-([a-z])", r"\1\2", t)
  66. d["content_ltks"] = " ".join([stemmer.stem(w) for w in word_tokenize(t)])
  67. else:
  68. d["content_ltks"] = huqie.qie(t)
  69. d["content_sm_ltks"] = huqie.qieqie(d["content_ltks"])
  70. def tokenize_table(tbls, doc, eng, batch_size=10):
  71. res = []
  72. # add tables
  73. for (img, rows), poss in tbls:
  74. if not rows:continue
  75. if isinstance(rows, str):
  76. d = copy.deepcopy(doc)
  77. r = re.sub(r"<[^<>]{,12}>", "", rows)
  78. tokenize(d, r, eng)
  79. d["content_with_weight"] = rows
  80. d["image"] = img
  81. add_positions(d, poss)
  82. res.append(d)
  83. continue
  84. de = "; " if eng else "; "
  85. for i in range(0, len(rows), batch_size):
  86. d = copy.deepcopy(doc)
  87. r = de.join(rows[i:i + batch_size])
  88. tokenize(d, r, eng)
  89. d["image"] = img
  90. add_positions(d, poss)
  91. res.append(d)
  92. return res
  93. def add_positions(d, poss):
  94. if not poss:return
  95. d["page_num_int"] = []
  96. d["position_int"] = []
  97. d["top_int"] = []
  98. for pn, left, right, top, bottom in poss:
  99. d["page_num_int"].append(pn+1)
  100. d["top_int"].append(top)
  101. d["position_int"].append((pn+1, left, right, top, bottom))
  102. def remove_contents_table(sections, eng=False):
  103. i = 0
  104. while i < len(sections):
  105. def get(i):
  106. nonlocal sections
  107. return (sections[i] if type(sections[i]) == type("") else sections[i][0]).strip()
  108. if not re.match(r"(contents|目录|目次|table of contents|致谢|acknowledge)$",
  109. re.sub(r"( | |\u3000)+", "", get(i).split("@@")[0], re.IGNORECASE)):
  110. i += 1
  111. continue
  112. sections.pop(i)
  113. if i >= len(sections): break
  114. prefix = get(i)[:3] if not eng else " ".join(get(i).split(" ")[:2])
  115. while not prefix:
  116. sections.pop(i)
  117. if i >= len(sections): break
  118. prefix = get(i)[:3] if not eng else " ".join(get(i).split(" ")[:2])
  119. sections.pop(i)
  120. if i >= len(sections) or not prefix: break
  121. for j in range(i, min(i + 128, len(sections))):
  122. if not re.match(prefix, get(j)):
  123. continue
  124. for _ in range(i, j): sections.pop(i)
  125. break
  126. def make_colon_as_title(sections):
  127. if not sections: return []
  128. if type(sections[0]) == type(""): return sections
  129. i = 0
  130. while i < len(sections):
  131. txt, layout = sections[i]
  132. i += 1
  133. txt = txt.split("@")[0].strip()
  134. if not txt:
  135. continue
  136. if txt[-1] not in "::":
  137. continue
  138. txt = txt[::-1]
  139. arr = re.split(r"([。?!!?;;]| .)", txt)
  140. if len(arr) < 2 or len(arr[1]) < 32:
  141. continue
  142. sections.insert(i - 1, (arr[0][::-1], "title"))
  143. i += 1
  144. def hierarchical_merge(bull, sections, depth):
  145. if not sections or bull < 0: return []
  146. if type(sections[0]) == type(""): sections = [(s, "") for s in sections]
  147. sections = [(t,o) for t, o in sections if t and len(t.split("@")[0].strip()) > 1 and not re.match(r"[0-9]+$", t.split("@")[0].strip())]
  148. bullets_size = len(BULLET_PATTERN[bull])
  149. levels = [[] for _ in range(bullets_size + 2)]
  150. def not_title(txt):
  151. if re.match(r"第[零一二三四五六七八九十百0-9]+条", txt): return False
  152. if len(txt) >= 128: return True
  153. return re.search(r"[,;,。;!!]", txt)
  154. for i, (txt, layout) in enumerate(sections):
  155. for j, p in enumerate(BULLET_PATTERN[bull]):
  156. if re.match(p, txt.strip()) and not not_title(txt):
  157. levels[j].append(i)
  158. break
  159. else:
  160. if re.search(r"(title|head)", layout):
  161. levels[bullets_size].append(i)
  162. else:
  163. levels[bullets_size + 1].append(i)
  164. sections = [t for t, _ in sections]
  165. for s in sections: print("--", s)
  166. def binary_search(arr, target):
  167. if not arr: return -1
  168. if target > arr[-1]: return len(arr) - 1
  169. if target < arr[0]: return -1
  170. s, e = 0, len(arr)
  171. while e - s > 1:
  172. i = (e + s) // 2
  173. if target > arr[i]:
  174. s = i
  175. continue
  176. elif target < arr[i]:
  177. e = i
  178. continue
  179. else:
  180. assert False
  181. return s
  182. cks = []
  183. readed = [False] * len(sections)
  184. levels = levels[::-1]
  185. for i, arr in enumerate(levels[:depth]):
  186. for j in arr:
  187. if readed[j]: continue
  188. readed[j] = True
  189. cks.append([j])
  190. if i + 1 == len(levels) - 1: continue
  191. for ii in range(i + 1, len(levels)):
  192. jj = binary_search(levels[ii], j)
  193. if jj < 0: continue
  194. if jj > cks[-1][-1]: cks[-1].pop(-1)
  195. cks[-1].append(levels[ii][jj])
  196. for ii in cks[-1]: readed[ii] = True
  197. for i in range(len(cks)):
  198. cks[i] = [sections[j] for j in cks[i][::-1]]
  199. print("--------------\n", "\n* ".join(cks[i]))
  200. return cks
  201. def naive_merge(sections, chunk_token_num=128, delimiter="\n。;!?"):
  202. if not sections: return []
  203. if type(sections[0]) == type(""): sections = [(s, "") for s in sections]
  204. cks = [""]
  205. tk_nums = [0]
  206. def add_chunk(t, pos):
  207. nonlocal cks, tk_nums, delimiter
  208. tnum = num_tokens_from_string(t)
  209. if tnum < 8: pos = ""
  210. if tk_nums[-1] > chunk_token_num:
  211. if t.find(pos) < 0: t += pos
  212. cks.append(t)
  213. tk_nums.append(tnum)
  214. else:
  215. if cks[-1].find(pos) < 0: t += pos
  216. cks[-1] += t
  217. tk_nums[-1] += tnum
  218. for sec, pos in sections:
  219. add_chunk(sec, pos)
  220. continue
  221. s, e = 0, 1
  222. while e < len(sec):
  223. if sec[e] in delimiter:
  224. add_chunk(sec[s: e+1], pos)
  225. s = e + 1
  226. e = s + 1
  227. else:
  228. e += 1
  229. if s < e: add_chunk(sec[s: e], pos)
  230. return cks