Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

__init__.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import copy
  2. import random
  3. from .pdf_parser import HuParser as PdfParser
  4. from .docx_parser import HuDocxParser as DocxParser
  5. from .excel_parser import HuExcelParser as ExcelParser
  6. import re
  7. from nltk import word_tokenize
  8. from rag.nlp import stemmer, huqie
  9. from ..utils import num_tokens_from_string
  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: continue
  52. res = i
  53. maxium = h
  54. return res
  55. def is_english(texts):
  56. eng = 0
  57. for t in texts:
  58. if re.match(r"[a-zA-Z]{2,}", t.strip()):
  59. eng += 1
  60. if eng / len(texts) > 0.8:
  61. return True
  62. return False
  63. def tokenize(d, t, eng):
  64. d["content_with_weight"] = t
  65. if eng:
  66. t = re.sub(r"([a-z])-([a-z])", r"\1\2", t)
  67. d["content_ltks"] = " ".join([stemmer.stem(w) for w in word_tokenize(t)])
  68. else:
  69. d["content_ltks"] = huqie.qie(t)
  70. d["content_sm_ltks"] = huqie.qieqie(d["content_ltks"])
  71. def remove_contents_table(sections, eng=False):
  72. i = 0
  73. while i < len(sections):
  74. def get(i):
  75. nonlocal sections
  76. return (sections[i] if type(sections[i]) == type("") else sections[i][0]).strip()
  77. if not re.match(r"(contents|目录|目次|table of contents|致谢|acknowledge)$",
  78. re.sub(r"( | |\u3000)+", "", get(i).split("@@")[0], re.IGNORECASE)):
  79. i += 1
  80. continue
  81. sections.pop(i)
  82. if i >= len(sections): break
  83. prefix = get(i)[:3] if not eng else " ".join(get(i).split(" ")[:2])
  84. while not prefix:
  85. sections.pop(i)
  86. if i >= len(sections): break
  87. prefix = get(i)[:3] if not eng else " ".join(get(i).split(" ")[:2])
  88. sections.pop(i)
  89. if i >= len(sections) or not prefix: break
  90. for j in range(i, min(i + 128, len(sections))):
  91. if not re.match(prefix, get(j)):
  92. continue
  93. for _ in range(i, j): sections.pop(i)
  94. break
  95. def make_colon_as_title(sections):
  96. if not sections: return []
  97. if type(sections[0]) == type(""): return sections
  98. i = 0
  99. while i < len(sections):
  100. txt, layout = sections[i]
  101. i += 1
  102. txt = txt.split("@")[0].strip()
  103. if not txt:
  104. continue
  105. if txt[-1] not in "::":
  106. continue
  107. txt = txt[::-1]
  108. arr = re.split(r"([。?!!?;;]| .)", txt)
  109. if len(arr) < 2 or len(arr[1]) < 32:
  110. continue
  111. sections.insert(i - 1, (arr[0][::-1], "title"))
  112. i += 1
  113. def hierarchical_merge(bull, sections, depth):
  114. if not sections or bull < 0: return []
  115. if type(sections[0]) == type(""): sections = [(s, "") for s in sections]
  116. 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())]
  117. bullets_size = len(BULLET_PATTERN[bull])
  118. levels = [[] for _ in range(bullets_size + 2)]
  119. def not_title(txt):
  120. if re.match(r"第[零一二三四五六七八九十百0-9]+条", txt): return False
  121. if len(txt) >= 128: return True
  122. return re.search(r"[,;,。;!!]", txt)
  123. for i, (txt, layout) in enumerate(sections):
  124. for j, p in enumerate(BULLET_PATTERN[bull]):
  125. if re.match(p, txt.strip()) and not not_title(txt):
  126. levels[j].append(i)
  127. break
  128. else:
  129. if re.search(r"(title|head)", layout):
  130. levels[bullets_size].append(i)
  131. else:
  132. levels[bullets_size + 1].append(i)
  133. sections = [t for t, _ in sections]
  134. for s in sections: print("--", s)
  135. def binary_search(arr, target):
  136. if not arr: return -1
  137. if target > arr[-1]: return len(arr) - 1
  138. if target < arr[0]: return -1
  139. s, e = 0, len(arr)
  140. while e - s > 1:
  141. i = (e + s) // 2
  142. if target > arr[i]:
  143. s = i
  144. continue
  145. elif target < arr[i]:
  146. e = i
  147. continue
  148. else:
  149. assert False
  150. return s
  151. cks = []
  152. readed = [False] * len(sections)
  153. levels = levels[::-1]
  154. for i, arr in enumerate(levels[:depth]):
  155. for j in arr:
  156. if readed[j]: continue
  157. readed[j] = True
  158. cks.append([j])
  159. if i + 1 == len(levels) - 1: continue
  160. for ii in range(i + 1, len(levels)):
  161. jj = binary_search(levels[ii], j)
  162. if jj < 0: continue
  163. if jj > cks[-1][-1]: cks[-1].pop(-1)
  164. cks[-1].append(levels[ii][jj])
  165. for ii in cks[-1]: readed[ii] = True
  166. for i in range(len(cks)):
  167. cks[i] = [sections[j] for j in cks[i][::-1]]
  168. print("--------------\n", "\n* ".join(cks[i]))
  169. return cks
  170. def naive_merge(sections, chunk_token_num=128, delimiter="\n。;!?"):
  171. if not sections: return []
  172. if type(sections[0]) == type(""): sections = [(s, "") for s in sections]
  173. cks = [""]
  174. tk_nums = [0]
  175. def add_chunk(t, pos):
  176. nonlocal cks, tk_nums, delimiter
  177. tnum = num_tokens_from_string(t)
  178. if tnum < 8: pos = ""
  179. if tk_nums[-1] > chunk_token_num:
  180. cks.append(t + pos)
  181. tk_nums.append(tnum)
  182. else:
  183. cks[-1] += t + pos
  184. tk_nums[-1] += tnum
  185. for sec, pos in sections:
  186. s, e = 0, 1
  187. while e < len(sec):
  188. if sec[e] in delimiter:
  189. add_chunk(sec[s: e+1], pos)
  190. s = e + 1
  191. e = s + 1
  192. else:
  193. e += 1
  194. if s < e: add_chunk(sec[s: e], pos)
  195. return cks