您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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