Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

pdf_parser.py 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import random
  4. import xgboost as xgb
  5. from io import BytesIO
  6. import torch
  7. import re
  8. import pdfplumber
  9. import logging
  10. from PIL import Image, ImageDraw
  11. import numpy as np
  12. from timeit import default_timer as timer
  13. from PyPDF2 import PdfReader as pdf2_read
  14. from api.utils.file_utils import get_project_base_directory
  15. from deepdoc.vision import OCR, Recognizer, LayoutRecognizer, TableStructureRecognizer
  16. from rag.nlp import rag_tokenizer
  17. from copy import deepcopy
  18. from huggingface_hub import snapshot_download
  19. logging.getLogger("pdfminer").setLevel(logging.WARNING)
  20. class RAGFlowPdfParser:
  21. def __init__(self):
  22. self.ocr = OCR()
  23. if hasattr(self, "model_speciess"):
  24. self.layouter = LayoutRecognizer("layout." + self.model_speciess)
  25. else:
  26. self.layouter = LayoutRecognizer("layout")
  27. self.tbl_det = TableStructureRecognizer()
  28. self.updown_cnt_mdl = xgb.Booster()
  29. if torch.cuda.is_available():
  30. self.updown_cnt_mdl.set_param({"device": "cuda"})
  31. try:
  32. model_dir = os.path.join(
  33. get_project_base_directory(),
  34. "rag/res/deepdoc")
  35. self.updown_cnt_mdl.load_model(os.path.join(
  36. model_dir, "updown_concat_xgb.model"))
  37. except Exception as e:
  38. model_dir = snapshot_download(
  39. repo_id="InfiniFlow/text_concat_xgb_v1.0",
  40. local_dir=os.path.join(get_project_base_directory(), "rag/res/deepdoc"),
  41. local_dir_use_symlinks=False)
  42. self.updown_cnt_mdl.load_model(os.path.join(
  43. model_dir, "updown_concat_xgb.model"))
  44. self.page_from = 0
  45. """
  46. If you have trouble downloading HuggingFace models, -_^ this might help!!
  47. For Linux:
  48. export HF_ENDPOINT=https://hf-mirror.com
  49. For Windows:
  50. Good luck
  51. ^_-
  52. """
  53. def __char_width(self, c):
  54. return (c["x1"] - c["x0"]) // max(len(c["text"]), 1)
  55. def __height(self, c):
  56. return c["bottom"] - c["top"]
  57. def _x_dis(self, a, b):
  58. return min(abs(a["x1"] - b["x0"]), abs(a["x0"] - b["x1"]),
  59. abs(a["x0"] + a["x1"] - b["x0"] - b["x1"]) / 2)
  60. def _y_dis(
  61. self, a, b):
  62. return (
  63. b["top"] + b["bottom"] - a["top"] - a["bottom"]) / 2
  64. def _match_proj(self, b):
  65. proj_patt = [
  66. r"第[零一二三四五六七八九十百]+章",
  67. r"第[零一二三四五六七八九十百]+[条节]",
  68. r"[零一二三四五六七八九十百]+[、是  ]",
  69. r"[\((][零一二三四五六七八九十百]+[)\)]",
  70. r"[\((][0-9]+[)\)]",
  71. r"[0-9]+(、|\.[  ]|)|\.[^0-9./a-zA-Z_%><-]{4,})",
  72. r"[0-9]+\.[0-9.]+(、|\.[  ])",
  73. r"[⚫•➢①② ]",
  74. ]
  75. return any([re.match(p, b["text"]) for p in proj_patt])
  76. def _updown_concat_features(self, up, down):
  77. w = max(self.__char_width(up), self.__char_width(down))
  78. h = max(self.__height(up), self.__height(down))
  79. y_dis = self._y_dis(up, down)
  80. LEN = 6
  81. tks_down = rag_tokenizer.tokenize(down["text"][:LEN]).split(" ")
  82. tks_up = rag_tokenizer.tokenize(up["text"][-LEN:]).split(" ")
  83. tks_all = up["text"][-LEN:].strip() \
  84. + (" " if re.match(r"[a-zA-Z0-9]+",
  85. up["text"][-1] + down["text"][0]) else "") \
  86. + down["text"][:LEN].strip()
  87. tks_all = rag_tokenizer.tokenize(tks_all).split(" ")
  88. fea = [
  89. up.get("R", -1) == down.get("R", -1),
  90. y_dis / h,
  91. down["page_number"] - up["page_number"],
  92. up["layout_type"] == down["layout_type"],
  93. up["layout_type"] == "text",
  94. down["layout_type"] == "text",
  95. up["layout_type"] == "table",
  96. down["layout_type"] == "table",
  97. True if re.search(
  98. r"([。?!;!?;+))]|[a-z]\.)$",
  99. up["text"]) else False,
  100. True if re.search(r"[,:‘“、0-9(+-]$", up["text"]) else False,
  101. True if re.search(
  102. r"(^.?[/,?;:\],。;:’”?!》】)-])",
  103. down["text"]) else False,
  104. True if re.match(r"[\((][^\(\)()]+[)\)]$", up["text"]) else False,
  105. True if re.search(r"[,,][^。.]+$", up["text"]) else False,
  106. True if re.search(r"[,,][^。.]+$", up["text"]) else False,
  107. True if re.search(r"[\((][^\))]+$", up["text"])
  108. and re.search(r"[\))]", down["text"]) else False,
  109. self._match_proj(down),
  110. True if re.match(r"[A-Z]", down["text"]) else False,
  111. True if re.match(r"[A-Z]", up["text"][-1]) else False,
  112. True if re.match(r"[a-z0-9]", up["text"][-1]) else False,
  113. True if re.match(r"[0-9.%,-]+$", down["text"]) else False,
  114. up["text"].strip()[-2:] == down["text"].strip()[-2:] if len(up["text"].strip()
  115. ) > 1 and len(
  116. down["text"].strip()) > 1 else False,
  117. up["x0"] > down["x1"],
  118. abs(self.__height(up) - self.__height(down)) / min(self.__height(up),
  119. self.__height(down)),
  120. self._x_dis(up, down) / max(w, 0.000001),
  121. (len(up["text"]) - len(down["text"])) /
  122. max(len(up["text"]), len(down["text"])),
  123. len(tks_all) - len(tks_up) - len(tks_down),
  124. len(tks_down) - len(tks_up),
  125. tks_down[-1] == tks_up[-1],
  126. max(down["in_row"], up["in_row"]),
  127. abs(down["in_row"] - up["in_row"]),
  128. len(tks_down) == 1 and rag_tokenizer.tag(tks_down[0]).find("n") >= 0,
  129. len(tks_up) == 1 and rag_tokenizer.tag(tks_up[0]).find("n") >= 0
  130. ]
  131. return fea
  132. @staticmethod
  133. def sort_X_by_page(arr, threashold):
  134. # sort using y1 first and then x1
  135. arr = sorted(arr, key=lambda r: (r["page_number"], r["x0"], r["top"]))
  136. for i in range(len(arr) - 1):
  137. for j in range(i, -1, -1):
  138. # restore the order using th
  139. if abs(arr[j + 1]["x0"] - arr[j]["x0"]) < threashold \
  140. and arr[j + 1]["top"] < arr[j]["top"] \
  141. and arr[j + 1]["page_number"] == arr[j]["page_number"]:
  142. tmp = arr[j]
  143. arr[j] = arr[j + 1]
  144. arr[j + 1] = tmp
  145. return arr
  146. def _has_color(self, o):
  147. if o.get("ncs", "") == "DeviceGray":
  148. if o["stroking_color"] and o["stroking_color"][0] == 1 and o["non_stroking_color"] and \
  149. o["non_stroking_color"][0] == 1:
  150. if re.match(r"[a-zT_\[\]\(\)-]+", o.get("text", "")):
  151. return False
  152. return True
  153. def _table_transformer_job(self, ZM):
  154. logging.info("Table processing...")
  155. imgs, pos = [], []
  156. tbcnt = [0]
  157. MARGIN = 10
  158. self.tb_cpns = []
  159. assert len(self.page_layout) == len(self.page_images)
  160. for p, tbls in enumerate(self.page_layout): # for page
  161. tbls = [f for f in tbls if f["type"] == "table"]
  162. tbcnt.append(len(tbls))
  163. if not tbls:
  164. continue
  165. for tb in tbls: # for table
  166. left, top, right, bott = tb["x0"] - MARGIN, tb["top"] - MARGIN, \
  167. tb["x1"] + MARGIN, tb["bottom"] + MARGIN
  168. left *= ZM
  169. top *= ZM
  170. right *= ZM
  171. bott *= ZM
  172. pos.append((left, top))
  173. imgs.append(self.page_images[p].crop((left, top, right, bott)))
  174. assert len(self.page_images) == len(tbcnt) - 1
  175. if not imgs:
  176. return
  177. recos = self.tbl_det(imgs)
  178. tbcnt = np.cumsum(tbcnt)
  179. for i in range(len(tbcnt) - 1): # for page
  180. pg = []
  181. for j, tb_items in enumerate(
  182. recos[tbcnt[i]: tbcnt[i + 1]]): # for table
  183. poss = pos[tbcnt[i]: tbcnt[i + 1]]
  184. for it in tb_items: # for table components
  185. it["x0"] = (it["x0"] + poss[j][0])
  186. it["x1"] = (it["x1"] + poss[j][0])
  187. it["top"] = (it["top"] + poss[j][1])
  188. it["bottom"] = (it["bottom"] + poss[j][1])
  189. for n in ["x0", "x1", "top", "bottom"]:
  190. it[n] /= ZM
  191. it["top"] += self.page_cum_height[i]
  192. it["bottom"] += self.page_cum_height[i]
  193. it["pn"] = i
  194. it["layoutno"] = j
  195. pg.append(it)
  196. self.tb_cpns.extend(pg)
  197. def gather(kwd, fzy=10, ption=0.6):
  198. eles = Recognizer.sort_Y_firstly(
  199. [r for r in self.tb_cpns if re.match(kwd, r["label"])], fzy)
  200. eles = Recognizer.layouts_cleanup(self.boxes, eles, 5, ption)
  201. return Recognizer.sort_Y_firstly(eles, 0)
  202. # add R,H,C,SP tag to boxes within table layout
  203. headers = gather(r".*header$")
  204. rows = gather(r".* (row|header)")
  205. spans = gather(r".*spanning")
  206. clmns = sorted([r for r in self.tb_cpns if re.match(
  207. r"table column$", r["label"])], key=lambda x: (x["pn"], x["layoutno"], x["x0"]))
  208. clmns = Recognizer.layouts_cleanup(self.boxes, clmns, 5, 0.5)
  209. for b in self.boxes:
  210. if b.get("layout_type", "") != "table":
  211. continue
  212. ii = Recognizer.find_overlapped_with_threashold(b, rows, thr=0.3)
  213. if ii is not None:
  214. b["R"] = ii
  215. b["R_top"] = rows[ii]["top"]
  216. b["R_bott"] = rows[ii]["bottom"]
  217. ii = Recognizer.find_overlapped_with_threashold(
  218. b, headers, thr=0.3)
  219. if ii is not None:
  220. b["H_top"] = headers[ii]["top"]
  221. b["H_bott"] = headers[ii]["bottom"]
  222. b["H_left"] = headers[ii]["x0"]
  223. b["H_right"] = headers[ii]["x1"]
  224. b["H"] = ii
  225. ii = Recognizer.find_horizontally_tightest_fit(b, clmns)
  226. if ii is not None:
  227. b["C"] = ii
  228. b["C_left"] = clmns[ii]["x0"]
  229. b["C_right"] = clmns[ii]["x1"]
  230. ii = Recognizer.find_overlapped_with_threashold(b, spans, thr=0.3)
  231. if ii is not None:
  232. b["H_top"] = spans[ii]["top"]
  233. b["H_bott"] = spans[ii]["bottom"]
  234. b["H_left"] = spans[ii]["x0"]
  235. b["H_right"] = spans[ii]["x1"]
  236. b["SP"] = ii
  237. def __ocr(self, pagenum, img, chars, ZM=3):
  238. bxs = self.ocr.detect(np.array(img))
  239. if not bxs:
  240. self.boxes.append([])
  241. return
  242. bxs = [(line[0], line[1][0]) for line in bxs]
  243. bxs = Recognizer.sort_Y_firstly(
  244. [{"x0": b[0][0] / ZM, "x1": b[1][0] / ZM,
  245. "top": b[0][1] / ZM, "text": "", "txt": t,
  246. "bottom": b[-1][1] / ZM,
  247. "page_number": pagenum} for b, t in bxs if b[0][0] <= b[1][0] and b[0][1] <= b[-1][1]],
  248. self.mean_height[-1] / 3
  249. )
  250. # merge chars in the same rect
  251. for c in Recognizer.sort_X_firstly(
  252. chars, self.mean_width[pagenum - 1] // 4):
  253. ii = Recognizer.find_overlapped(c, bxs)
  254. if ii is None:
  255. self.lefted_chars.append(c)
  256. continue
  257. ch = c["bottom"] - c["top"]
  258. bh = bxs[ii]["bottom"] - bxs[ii]["top"]
  259. if abs(ch - bh) / max(ch, bh) >= 0.7 and c["text"] != ' ':
  260. self.lefted_chars.append(c)
  261. continue
  262. if c["text"] == " " and bxs[ii]["text"]:
  263. if re.match(r"[0-9a-zA-Z,.?;:!%%]", bxs[ii]["text"][-1]):
  264. bxs[ii]["text"] += " "
  265. else:
  266. bxs[ii]["text"] += c["text"]
  267. for b in bxs:
  268. if not b["text"]:
  269. left, right, top, bott = b["x0"] * ZM, b["x1"] * \
  270. ZM, b["top"] * ZM, b["bottom"] * ZM
  271. b["text"] = self.ocr.recognize(np.array(img),
  272. np.array([[left, top], [right, top], [right, bott], [left, bott]],
  273. dtype=np.float32))
  274. del b["txt"]
  275. bxs = [b for b in bxs if b["text"]]
  276. if self.mean_height[-1] == 0:
  277. self.mean_height[-1] = np.median([b["bottom"] - b["top"]
  278. for b in bxs])
  279. self.boxes.append(bxs)
  280. def _layouts_rec(self, ZM, drop=True):
  281. assert len(self.page_images) == len(self.boxes)
  282. self.boxes, self.page_layout = self.layouter(
  283. self.page_images, self.boxes, ZM, drop=drop)
  284. # cumlative Y
  285. for i in range(len(self.boxes)):
  286. self.boxes[i]["top"] += \
  287. self.page_cum_height[self.boxes[i]["page_number"] - 1]
  288. self.boxes[i]["bottom"] += \
  289. self.page_cum_height[self.boxes[i]["page_number"] - 1]
  290. def _text_merge(self):
  291. # merge adjusted boxes
  292. bxs = self.boxes
  293. def end_with(b, txt):
  294. txt = txt.strip()
  295. tt = b.get("text", "").strip()
  296. return tt and tt.find(txt) == len(tt) - len(txt)
  297. def start_with(b, txts):
  298. tt = b.get("text", "").strip()
  299. return tt and any([tt.find(t.strip()) == 0 for t in txts])
  300. # horizontally merge adjacent box with the same layout
  301. i = 0
  302. while i < len(bxs) - 1:
  303. b = bxs[i]
  304. b_ = bxs[i + 1]
  305. if b.get("layoutno", "0") != b_.get("layoutno", "1") or b.get("layout_type", "") in ["table", "figure",
  306. "equation"]:
  307. i += 1
  308. continue
  309. if abs(self._y_dis(b, b_)
  310. ) < self.mean_height[bxs[i]["page_number"] - 1] / 3:
  311. # merge
  312. bxs[i]["x1"] = b_["x1"]
  313. bxs[i]["top"] = (b["top"] + b_["top"]) / 2
  314. bxs[i]["bottom"] = (b["bottom"] + b_["bottom"]) / 2
  315. bxs[i]["text"] += b_["text"]
  316. bxs.pop(i + 1)
  317. continue
  318. i += 1
  319. continue
  320. dis_thr = 1
  321. dis = b["x1"] - b_["x0"]
  322. if b.get("layout_type", "") != "text" or b_.get(
  323. "layout_type", "") != "text":
  324. if end_with(b, ",") or start_with(b_, "(,"):
  325. dis_thr = -8
  326. else:
  327. i += 1
  328. continue
  329. if abs(self._y_dis(b, b_)) < self.mean_height[bxs[i]["page_number"] - 1] / 5 \
  330. and dis >= dis_thr and b["x1"] < b_["x1"]:
  331. # merge
  332. bxs[i]["x1"] = b_["x1"]
  333. bxs[i]["top"] = (b["top"] + b_["top"]) / 2
  334. bxs[i]["bottom"] = (b["bottom"] + b_["bottom"]) / 2
  335. bxs[i]["text"] += b_["text"]
  336. bxs.pop(i + 1)
  337. continue
  338. i += 1
  339. self.boxes = bxs
  340. def _naive_vertical_merge(self):
  341. bxs = Recognizer.sort_Y_firstly(
  342. self.boxes, np.median(
  343. self.mean_height) / 3)
  344. i = 0
  345. while i + 1 < len(bxs):
  346. b = bxs[i]
  347. b_ = bxs[i + 1]
  348. if b["page_number"] < b_["page_number"] and re.match(
  349. r"[0-9 •一—-]+$", b["text"]):
  350. bxs.pop(i)
  351. continue
  352. if not b["text"].strip():
  353. bxs.pop(i)
  354. continue
  355. concatting_feats = [
  356. b["text"].strip()[-1] in ",;:'\",、‘“;:-",
  357. len(b["text"].strip()) > 1 and b["text"].strip(
  358. )[-2] in ",;:'\",‘“、;:",
  359. b["text"].strip()[0] in "。;?!?”)),,、:",
  360. ]
  361. # features for not concating
  362. feats = [
  363. b.get("layoutno", 0) != b_.get("layoutno", 0),
  364. b["text"].strip()[-1] in "。?!?",
  365. self.is_english and b["text"].strip()[-1] in ".!?",
  366. b["page_number"] == b_["page_number"] and b_["top"] -
  367. b["bottom"] > self.mean_height[b["page_number"] - 1] * 1.5,
  368. b["page_number"] < b_["page_number"] and abs(
  369. b["x0"] - b_["x0"]) > self.mean_width[b["page_number"] - 1] * 4,
  370. ]
  371. # split features
  372. detach_feats = [b["x1"] < b_["x0"],
  373. b["x0"] > b_["x1"]]
  374. if (any(feats) and not any(concatting_feats)) or any(detach_feats):
  375. print(
  376. b["text"],
  377. b_["text"],
  378. any(feats),
  379. any(concatting_feats),
  380. any(detach_feats))
  381. i += 1
  382. continue
  383. # merge up and down
  384. b["bottom"] = b_["bottom"]
  385. b["text"] += b_["text"]
  386. b["x0"] = min(b["x0"], b_["x0"])
  387. b["x1"] = max(b["x1"], b_["x1"])
  388. bxs.pop(i + 1)
  389. self.boxes = bxs
  390. def _concat_downward(self, concat_between_pages=True):
  391. # count boxes in the same row as a feature
  392. for i in range(len(self.boxes)):
  393. mh = self.mean_height[self.boxes[i]["page_number"] - 1]
  394. self.boxes[i]["in_row"] = 0
  395. j = max(0, i - 12)
  396. while j < min(i + 12, len(self.boxes)):
  397. if j == i:
  398. j += 1
  399. continue
  400. ydis = self._y_dis(self.boxes[i], self.boxes[j]) / mh
  401. if abs(ydis) < 1:
  402. self.boxes[i]["in_row"] += 1
  403. elif ydis > 0:
  404. break
  405. j += 1
  406. # concat between rows
  407. boxes = deepcopy(self.boxes)
  408. blocks = []
  409. while boxes:
  410. chunks = []
  411. def dfs(up, dp):
  412. chunks.append(up)
  413. i = dp
  414. while i < min(dp + 12, len(boxes)):
  415. ydis = self._y_dis(up, boxes[i])
  416. smpg = up["page_number"] == boxes[i]["page_number"]
  417. mh = self.mean_height[up["page_number"] - 1]
  418. mw = self.mean_width[up["page_number"] - 1]
  419. if smpg and ydis > mh * 4:
  420. break
  421. if not smpg and ydis > mh * 16:
  422. break
  423. down = boxes[i]
  424. if not concat_between_pages and down["page_number"] > up["page_number"]:
  425. break
  426. if up.get("R", "") != down.get(
  427. "R", "") and up["text"][-1] != ",":
  428. i += 1
  429. continue
  430. if re.match(r"[0-9]{2,3}/[0-9]{3}$", up["text"]) \
  431. or re.match(r"[0-9]{2,3}/[0-9]{3}$", down["text"]) \
  432. or not down["text"].strip():
  433. i += 1
  434. continue
  435. if not down["text"].strip():
  436. i += 1
  437. continue
  438. if up["x1"] < down["x0"] - 10 * \
  439. mw or up["x0"] > down["x1"] + 10 * mw:
  440. i += 1
  441. continue
  442. if i - dp < 5 and up.get("layout_type") == "text":
  443. if up.get("layoutno", "1") == down.get(
  444. "layoutno", "2"):
  445. dfs(down, i + 1)
  446. boxes.pop(i)
  447. return
  448. i += 1
  449. continue
  450. fea = self._updown_concat_features(up, down)
  451. if self.updown_cnt_mdl.predict(
  452. xgb.DMatrix([fea]))[0] <= 0.5:
  453. i += 1
  454. continue
  455. dfs(down, i + 1)
  456. boxes.pop(i)
  457. return
  458. dfs(boxes[0], 1)
  459. boxes.pop(0)
  460. if chunks:
  461. blocks.append(chunks)
  462. # concat within each block
  463. boxes = []
  464. for b in blocks:
  465. if len(b) == 1:
  466. boxes.append(b[0])
  467. continue
  468. t = b[0]
  469. for c in b[1:]:
  470. t["text"] = t["text"].strip()
  471. c["text"] = c["text"].strip()
  472. if not c["text"]:
  473. continue
  474. if t["text"] and re.match(
  475. r"[0-9\.a-zA-Z]+$", t["text"][-1] + c["text"][-1]):
  476. t["text"] += " "
  477. t["text"] += c["text"]
  478. t["x0"] = min(t["x0"], c["x0"])
  479. t["x1"] = max(t["x1"], c["x1"])
  480. t["page_number"] = min(t["page_number"], c["page_number"])
  481. t["bottom"] = c["bottom"]
  482. if not t["layout_type"] \
  483. and c["layout_type"]:
  484. t["layout_type"] = c["layout_type"]
  485. boxes.append(t)
  486. self.boxes = Recognizer.sort_Y_firstly(boxes, 0)
  487. def _filter_forpages(self):
  488. if not self.boxes:
  489. return
  490. findit = False
  491. i = 0
  492. while i < len(self.boxes):
  493. if not re.match(r"(contents|目录|目次|table of contents|致谢|acknowledge)$",
  494. re.sub(r"( | |\u3000)+", "", self.boxes[i]["text"].lower())):
  495. i += 1
  496. continue
  497. findit = True
  498. eng = re.match(
  499. r"[0-9a-zA-Z :'.-]{5,}",
  500. self.boxes[i]["text"].strip())
  501. self.boxes.pop(i)
  502. if i >= len(self.boxes):
  503. break
  504. prefix = self.boxes[i]["text"].strip()[:3] if not eng else " ".join(
  505. self.boxes[i]["text"].strip().split(" ")[:2])
  506. while not prefix:
  507. self.boxes.pop(i)
  508. if i >= len(self.boxes):
  509. break
  510. prefix = self.boxes[i]["text"].strip()[:3] if not eng else " ".join(
  511. self.boxes[i]["text"].strip().split(" ")[:2])
  512. self.boxes.pop(i)
  513. if i >= len(self.boxes) or not prefix:
  514. break
  515. for j in range(i, min(i + 128, len(self.boxes))):
  516. if not re.match(prefix, self.boxes[j]["text"]):
  517. continue
  518. for k in range(i, j):
  519. self.boxes.pop(i)
  520. break
  521. if findit:
  522. return
  523. page_dirty = [0] * len(self.page_images)
  524. for b in self.boxes:
  525. if re.search(r"(··|··|··)", b["text"]):
  526. page_dirty[b["page_number"] - 1] += 1
  527. page_dirty = set([i + 1 for i, t in enumerate(page_dirty) if t > 3])
  528. if not page_dirty:
  529. return
  530. i = 0
  531. while i < len(self.boxes):
  532. if self.boxes[i]["page_number"] in page_dirty:
  533. self.boxes.pop(i)
  534. continue
  535. i += 1
  536. def _merge_with_same_bullet(self):
  537. i = 0
  538. while i + 1 < len(self.boxes):
  539. b = self.boxes[i]
  540. b_ = self.boxes[i + 1]
  541. if not b["text"].strip():
  542. self.boxes.pop(i)
  543. continue
  544. if not b_["text"].strip():
  545. self.boxes.pop(i + 1)
  546. continue
  547. if b["text"].strip()[0] != b_["text"].strip()[0] \
  548. or b["text"].strip()[0].lower() in set("qwertyuopasdfghjklzxcvbnm") \
  549. or rag_tokenizer.is_chinese(b["text"].strip()[0]) \
  550. or b["top"] > b_["bottom"]:
  551. i += 1
  552. continue
  553. b_["text"] = b["text"] + "\n" + b_["text"]
  554. b_["x0"] = min(b["x0"], b_["x0"])
  555. b_["x1"] = max(b["x1"], b_["x1"])
  556. b_["top"] = b["top"]
  557. self.boxes.pop(i)
  558. def _extract_table_figure(self, need_image, ZM,
  559. return_html, need_position):
  560. tables = {}
  561. figures = {}
  562. # extract figure and table boxes
  563. i = 0
  564. lst_lout_no = ""
  565. nomerge_lout_no = []
  566. while i < len(self.boxes):
  567. if "layoutno" not in self.boxes[i]:
  568. i += 1
  569. continue
  570. lout_no = str(self.boxes[i]["page_number"]) + \
  571. "-" + str(self.boxes[i]["layoutno"])
  572. if TableStructureRecognizer.is_caption(self.boxes[i]) or self.boxes[i]["layout_type"] in ["table caption",
  573. "title",
  574. "figure caption",
  575. "reference"]:
  576. nomerge_lout_no.append(lst_lout_no)
  577. if self.boxes[i]["layout_type"] == "table":
  578. if re.match(r"(数据|资料|图表)*来源[:: ]", self.boxes[i]["text"]):
  579. self.boxes.pop(i)
  580. continue
  581. if lout_no not in tables:
  582. tables[lout_no] = []
  583. tables[lout_no].append(self.boxes[i])
  584. self.boxes.pop(i)
  585. lst_lout_no = lout_no
  586. continue
  587. if need_image and self.boxes[i]["layout_type"] == "figure":
  588. if re.match(r"(数据|资料|图表)*来源[:: ]", self.boxes[i]["text"]):
  589. self.boxes.pop(i)
  590. continue
  591. if lout_no not in figures:
  592. figures[lout_no] = []
  593. figures[lout_no].append(self.boxes[i])
  594. self.boxes.pop(i)
  595. lst_lout_no = lout_no
  596. continue
  597. i += 1
  598. # merge table on different pages
  599. nomerge_lout_no = set(nomerge_lout_no)
  600. tbls = sorted([(k, bxs) for k, bxs in tables.items()],
  601. key=lambda x: (x[1][0]["top"], x[1][0]["x0"]))
  602. i = len(tbls) - 1
  603. while i - 1 >= 0:
  604. k0, bxs0 = tbls[i - 1]
  605. k, bxs = tbls[i]
  606. i -= 1
  607. if k0 in nomerge_lout_no:
  608. continue
  609. if bxs[0]["page_number"] == bxs0[0]["page_number"]:
  610. continue
  611. if bxs[0]["page_number"] - bxs0[0]["page_number"] > 1:
  612. continue
  613. mh = self.mean_height[bxs[0]["page_number"] - 1]
  614. if self._y_dis(bxs0[-1], bxs[0]) > mh * 23:
  615. continue
  616. tables[k0].extend(tables[k])
  617. del tables[k]
  618. def x_overlapped(a, b):
  619. return not any([a["x1"] < b["x0"], a["x0"] > b["x1"]])
  620. # find captions and pop out
  621. i = 0
  622. while i < len(self.boxes):
  623. c = self.boxes[i]
  624. # mh = self.mean_height[c["page_number"]-1]
  625. if not TableStructureRecognizer.is_caption(c):
  626. i += 1
  627. continue
  628. # find the nearest layouts
  629. def nearest(tbls):
  630. nonlocal c
  631. mink = ""
  632. minv = 1000000000
  633. for k, bxs in tbls.items():
  634. for b in bxs:
  635. if b.get("layout_type", "").find("caption") >= 0:
  636. continue
  637. y_dis = self._y_dis(c, b)
  638. x_dis = self._x_dis(
  639. c, b) if not x_overlapped(
  640. c, b) else 0
  641. dis = y_dis * y_dis + x_dis * x_dis
  642. if dis < minv:
  643. mink = k
  644. minv = dis
  645. return mink, minv
  646. tk, tv = nearest(tables)
  647. fk, fv = nearest(figures)
  648. # if min(tv, fv) > 2000:
  649. # i += 1
  650. # continue
  651. if tv < fv and tk:
  652. tables[tk].insert(0, c)
  653. logging.debug(
  654. "TABLE:" +
  655. self.boxes[i]["text"] +
  656. "; Cap: " +
  657. tk)
  658. elif fk:
  659. figures[fk].insert(0, c)
  660. logging.debug(
  661. "FIGURE:" +
  662. self.boxes[i]["text"] +
  663. "; Cap: " +
  664. tk)
  665. self.boxes.pop(i)
  666. res = []
  667. positions = []
  668. def cropout(bxs, ltype, poss):
  669. nonlocal ZM
  670. pn = set([b["page_number"] - 1 for b in bxs])
  671. if len(pn) < 2:
  672. pn = list(pn)[0]
  673. ht = self.page_cum_height[pn]
  674. b = {
  675. "x0": np.min([b["x0"] for b in bxs]),
  676. "top": np.min([b["top"] for b in bxs]) - ht,
  677. "x1": np.max([b["x1"] for b in bxs]),
  678. "bottom": np.max([b["bottom"] for b in bxs]) - ht
  679. }
  680. louts = [l for l in self.page_layout[pn] if l["type"] == ltype]
  681. ii = Recognizer.find_overlapped(b, louts, naive=True)
  682. if ii is not None:
  683. b = louts[ii]
  684. else:
  685. logging.warn(
  686. f"Missing layout match: {pn + 1},%s" %
  687. (bxs[0].get(
  688. "layoutno", "")))
  689. left, top, right, bott = b["x0"], b["top"], b["x1"], b["bottom"]
  690. if right < left: right = left + 1
  691. poss.append((pn + self.page_from, left, right, top, bott))
  692. return self.page_images[pn] \
  693. .crop((left * ZM, top * ZM,
  694. right * ZM, bott * ZM))
  695. pn = {}
  696. for b in bxs:
  697. p = b["page_number"] - 1
  698. if p not in pn:
  699. pn[p] = []
  700. pn[p].append(b)
  701. pn = sorted(pn.items(), key=lambda x: x[0])
  702. imgs = [cropout(arr, ltype, poss) for p, arr in pn]
  703. pic = Image.new("RGB",
  704. (int(np.max([i.size[0] for i in imgs])),
  705. int(np.sum([m.size[1] for m in imgs]))),
  706. (245, 245, 245))
  707. height = 0
  708. for img in imgs:
  709. pic.paste(img, (0, int(height)))
  710. height += img.size[1]
  711. return pic
  712. # crop figure out and add caption
  713. for k, bxs in figures.items():
  714. txt = "\n".join([b["text"] for b in bxs])
  715. if not txt:
  716. continue
  717. poss = []
  718. res.append(
  719. (cropout(
  720. bxs,
  721. "figure", poss),
  722. [txt]))
  723. positions.append(poss)
  724. for k, bxs in tables.items():
  725. if not bxs:
  726. continue
  727. bxs = Recognizer.sort_Y_firstly(bxs, np.mean(
  728. [(b["bottom"] - b["top"]) / 2 for b in bxs]))
  729. poss = []
  730. res.append((cropout(bxs, "table", poss),
  731. self.tbl_det.construct_table(bxs, html=return_html, is_english=self.is_english)))
  732. positions.append(poss)
  733. assert len(positions) == len(res)
  734. if need_position:
  735. return list(zip(res, positions))
  736. return res
  737. def proj_match(self, line):
  738. if len(line) <= 2:
  739. return
  740. if re.match(r"[0-9 ().,%%+/-]+$", line):
  741. return False
  742. for p, j in [
  743. (r"第[零一二三四五六七八九十百]+章", 1),
  744. (r"第[零一二三四五六七八九十百]+[条节]", 2),
  745. (r"[零一二三四五六七八九十百]+[、  ]", 3),
  746. (r"[\((][零一二三四五六七八九十百]+[)\)]", 4),
  747. (r"[0-9]+(、|\.[  ]|\.[^0-9])", 5),
  748. (r"[0-9]+\.[0-9]+(、|[.  ]|[^0-9])", 6),
  749. (r"[0-9]+\.[0-9]+\.[0-9]+(、|[  ]|[^0-9])", 7),
  750. (r"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(、|[  ]|[^0-9])", 8),
  751. (r".{,48}[::??]$", 9),
  752. (r"[0-9]+)", 10),
  753. (r"[\((][0-9]+[)\)]", 11),
  754. (r"[零一二三四五六七八九十百]+是", 12),
  755. (r"[⚫•➢✓]", 12)
  756. ]:
  757. if re.match(p, line):
  758. return j
  759. return
  760. def _line_tag(self, bx, ZM):
  761. pn = [bx["page_number"]]
  762. top = bx["top"] - self.page_cum_height[pn[0] - 1]
  763. bott = bx["bottom"] - self.page_cum_height[pn[0] - 1]
  764. page_images_cnt = len(self.page_images)
  765. if pn[-1] - 1 >= page_images_cnt: return ""
  766. while bott * ZM > self.page_images[pn[-1] - 1].size[1]:
  767. bott -= self.page_images[pn[-1] - 1].size[1] / ZM
  768. pn.append(pn[-1] + 1)
  769. if pn[-1] - 1 >= page_images_cnt:
  770. return ""
  771. return "@@{}\t{:.1f}\t{:.1f}\t{:.1f}\t{:.1f}##" \
  772. .format("-".join([str(p) for p in pn]),
  773. bx["x0"], bx["x1"], top, bott)
  774. def __filterout_scraps(self, boxes, ZM):
  775. def width(b):
  776. return b["x1"] - b["x0"]
  777. def height(b):
  778. return b["bottom"] - b["top"]
  779. def usefull(b):
  780. if b.get("layout_type"):
  781. return True
  782. if width(
  783. b) > self.page_images[b["page_number"] - 1].size[0] / ZM / 3:
  784. return True
  785. if b["bottom"] - b["top"] > self.mean_height[b["page_number"] - 1]:
  786. return True
  787. return False
  788. res = []
  789. while boxes:
  790. lines = []
  791. widths = []
  792. pw = self.page_images[boxes[0]["page_number"] - 1].size[0] / ZM
  793. mh = self.mean_height[boxes[0]["page_number"] - 1]
  794. mj = self.proj_match(
  795. boxes[0]["text"]) or boxes[0].get(
  796. "layout_type",
  797. "") == "title"
  798. def dfs(line, st):
  799. nonlocal mh, pw, lines, widths
  800. lines.append(line)
  801. widths.append(width(line))
  802. width_mean = np.mean(widths)
  803. mmj = self.proj_match(
  804. line["text"]) or line.get(
  805. "layout_type",
  806. "") == "title"
  807. for i in range(st + 1, min(st + 20, len(boxes))):
  808. if (boxes[i]["page_number"] - line["page_number"]) > 0:
  809. break
  810. if not mmj and self._y_dis(
  811. line, boxes[i]) >= 3 * mh and height(line) < 1.5 * mh:
  812. break
  813. if not usefull(boxes[i]):
  814. continue
  815. if mmj or \
  816. (self._x_dis(boxes[i], line) < pw / 10): \
  817. # and abs(width(boxes[i])-width_mean)/max(width(boxes[i]),width_mean)<0.5):
  818. # concat following
  819. dfs(boxes[i], i)
  820. boxes.pop(i)
  821. break
  822. try:
  823. if usefull(boxes[0]):
  824. dfs(boxes[0], 0)
  825. else:
  826. logging.debug("WASTE: " + boxes[0]["text"])
  827. except Exception as e:
  828. pass
  829. boxes.pop(0)
  830. mw = np.mean(widths)
  831. if mj or mw / pw >= 0.35 or mw > 200:
  832. res.append(
  833. "\n".join([c["text"] + self._line_tag(c, ZM) for c in lines]))
  834. else:
  835. logging.debug("REMOVED: " +
  836. "<<".join([c["text"] for c in lines]))
  837. return "\n\n".join(res)
  838. @staticmethod
  839. def total_page_number(fnm, binary=None):
  840. try:
  841. pdf = pdfplumber.open(
  842. fnm) if not binary else pdfplumber.open(BytesIO(binary))
  843. return len(pdf.pages)
  844. except Exception as e:
  845. logging.error(str(e))
  846. def __images__(self, fnm, zoomin=3, page_from=0,
  847. page_to=299, callback=None):
  848. self.lefted_chars = []
  849. self.mean_height = []
  850. self.mean_width = []
  851. self.boxes = []
  852. self.garbages = {}
  853. self.page_cum_height = [0]
  854. self.page_layout = []
  855. self.page_from = page_from
  856. st = timer()
  857. try:
  858. self.pdf = pdfplumber.open(fnm) if isinstance(
  859. fnm, str) else pdfplumber.open(BytesIO(fnm))
  860. self.page_images = [p.to_image(resolution=72 * zoomin).annotated for i, p in
  861. enumerate(self.pdf.pages[page_from:page_to])]
  862. self.page_chars = [[c for c in page.chars if self._has_color(c)] for page in
  863. self.pdf.pages[page_from:page_to]]
  864. self.total_page = len(self.pdf.pages)
  865. except Exception as e:
  866. logging.error(str(e))
  867. self.outlines = []
  868. try:
  869. self.pdf = pdf2_read(fnm if isinstance(fnm, str) else BytesIO(fnm))
  870. outlines = self.pdf.outline
  871. def dfs(arr, depth):
  872. for a in arr:
  873. if isinstance(a, dict):
  874. self.outlines.append((a["/Title"], depth))
  875. continue
  876. dfs(a, depth + 1)
  877. dfs(outlines, 0)
  878. except Exception as e:
  879. logging.warning(f"Outlines exception: {e}")
  880. if not self.outlines:
  881. logging.warning(f"Miss outlines")
  882. logging.info("Images converted.")
  883. self.is_english = [re.search(r"[a-zA-Z0-9,/¸;:'\[\]\(\)!@#$%^&*\"?<>._-]{30,}", "".join(
  884. random.choices([c["text"] for c in self.page_chars[i]], k=min(100, len(self.page_chars[i]))))) for i in
  885. range(len(self.page_chars))]
  886. if sum([1 if e else 0 for e in self.is_english]) > len(
  887. self.page_images) / 2:
  888. self.is_english = True
  889. else:
  890. self.is_english = False
  891. self.is_english = False
  892. st = timer()
  893. for i, img in enumerate(self.page_images):
  894. chars = self.page_chars[i] if not self.is_english else []
  895. self.mean_height.append(
  896. np.median(sorted([c["height"] for c in chars])) if chars else 0
  897. )
  898. self.mean_width.append(
  899. np.median(sorted([c["width"] for c in chars])) if chars else 8
  900. )
  901. self.page_cum_height.append(img.size[1] / zoomin)
  902. j = 0
  903. while j + 1 < len(chars):
  904. if chars[j]["text"] and chars[j + 1]["text"] \
  905. and re.match(r"[0-9a-zA-Z,.:;!%]+", chars[j]["text"] + chars[j + 1]["text"]) \
  906. and chars[j + 1]["x0"] - chars[j]["x1"] >= min(chars[j + 1]["width"],
  907. chars[j]["width"]) / 2:
  908. chars[j]["text"] += " "
  909. j += 1
  910. self.__ocr(i + 1, img, chars, zoomin)
  911. if callback and i % 6 == 5:
  912. callback(prog=(i + 1) * 0.6 / len(self.page_images), msg="")
  913. # print("OCR:", timer()-st)
  914. if not self.is_english and not any(
  915. [c for c in self.page_chars]) and self.boxes:
  916. bxes = [b for bxs in self.boxes for b in bxs]
  917. self.is_english = re.search(r"[\na-zA-Z0-9,/¸;:'\[\]\(\)!@#$%^&*\"?<>._-]{30,}",
  918. "".join([b["text"] for b in random.choices(bxes, k=min(30, len(bxes)))]))
  919. logging.info("Is it English:", self.is_english)
  920. self.page_cum_height = np.cumsum(self.page_cum_height)
  921. assert len(self.page_cum_height) == len(self.page_images) + 1
  922. def __call__(self, fnm, need_image=True, zoomin=3, return_html=False):
  923. self.__images__(fnm, zoomin)
  924. self._layouts_rec(zoomin)
  925. self._table_transformer_job(zoomin)
  926. self._text_merge()
  927. self._concat_downward()
  928. self._filter_forpages()
  929. tbls = self._extract_table_figure(
  930. need_image, zoomin, return_html, False)
  931. return self.__filterout_scraps(deepcopy(self.boxes), zoomin), tbls
  932. def remove_tag(self, txt):
  933. return re.sub(r"@@[\t0-9.-]+?##", "", txt)
  934. def crop(self, text, ZM=3, need_position=False):
  935. imgs = []
  936. poss = []
  937. for tag in re.findall(r"@@[0-9-]+\t[0-9.\t]+##", text):
  938. pn, left, right, top, bottom = tag.strip(
  939. "#").strip("@").split("\t")
  940. left, right, top, bottom = float(left), float(
  941. right), float(top), float(bottom)
  942. poss.append(([int(p) - 1 for p in pn.split("-")],
  943. left, right, top, bottom))
  944. if not poss:
  945. if need_position:
  946. return None, None
  947. return
  948. max_width = max(
  949. np.max([right - left for (_, left, right, _, _) in poss]), 6)
  950. GAP = 6
  951. pos = poss[0]
  952. poss.insert(0, ([pos[0][0]], pos[1], pos[2], max(
  953. 0, pos[3] - 120), max(pos[3] - GAP, 0)))
  954. pos = poss[-1]
  955. poss.append(([pos[0][-1]], pos[1], pos[2], min(self.page_images[pos[0][-1]].size[1] / ZM, pos[4] + GAP),
  956. min(self.page_images[pos[0][-1]].size[1] / ZM, pos[4] + 120)))
  957. positions = []
  958. for ii, (pns, left, right, top, bottom) in enumerate(poss):
  959. right = left + max_width
  960. bottom *= ZM
  961. for pn in pns[1:]:
  962. bottom += self.page_images[pn - 1].size[1]
  963. imgs.append(
  964. self.page_images[pns[0]].crop((left * ZM, top * ZM,
  965. right *
  966. ZM, min(
  967. bottom, self.page_images[pns[0]].size[1])
  968. ))
  969. )
  970. if 0 < ii < len(poss) - 1:
  971. positions.append((pns[0] + self.page_from, left, right, top, min(
  972. bottom, self.page_images[pns[0]].size[1]) / ZM))
  973. bottom -= self.page_images[pns[0]].size[1]
  974. for pn in pns[1:]:
  975. imgs.append(
  976. self.page_images[pn].crop((left * ZM, 0,
  977. right * ZM,
  978. min(bottom,
  979. self.page_images[pn].size[1])
  980. ))
  981. )
  982. if 0 < ii < len(poss) - 1:
  983. positions.append((pn + self.page_from, left, right, 0, min(
  984. bottom, self.page_images[pn].size[1]) / ZM))
  985. bottom -= self.page_images[pn].size[1]
  986. if not imgs:
  987. if need_position:
  988. return None, None
  989. return
  990. height = 0
  991. for img in imgs:
  992. height += img.size[1] + GAP
  993. height = int(height)
  994. width = int(np.max([i.size[0] for i in imgs]))
  995. pic = Image.new("RGB",
  996. (width, height),
  997. (245, 245, 245))
  998. height = 0
  999. for ii, img in enumerate(imgs):
  1000. if ii == 0 or ii + 1 == len(imgs):
  1001. img = img.convert('RGBA')
  1002. overlay = Image.new('RGBA', img.size, (0, 0, 0, 0))
  1003. overlay.putalpha(128)
  1004. img = Image.alpha_composite(img, overlay).convert("RGB")
  1005. pic.paste(img, (0, int(height)))
  1006. height += img.size[1] + GAP
  1007. if need_position:
  1008. return pic, positions
  1009. return pic
  1010. def get_position(self, bx, ZM):
  1011. poss = []
  1012. pn = bx["page_number"]
  1013. top = bx["top"] - self.page_cum_height[pn - 1]
  1014. bott = bx["bottom"] - self.page_cum_height[pn - 1]
  1015. poss.append((pn, bx["x0"], bx["x1"], top, min(
  1016. bott, self.page_images[pn - 1].size[1] / ZM)))
  1017. while bott * ZM > self.page_images[pn - 1].size[1]:
  1018. bott -= self.page_images[pn - 1].size[1] / ZM
  1019. top = 0
  1020. pn += 1
  1021. poss.append((pn, bx["x0"], bx["x1"], top, min(
  1022. bott, self.page_images[pn - 1].size[1] / ZM)))
  1023. return poss
  1024. class PlainParser(object):
  1025. def __call__(self, filename, from_page=0, to_page=100000, **kwargs):
  1026. self.outlines = []
  1027. lines = []
  1028. try:
  1029. self.pdf = pdf2_read(
  1030. filename if isinstance(
  1031. filename, str) else BytesIO(filename))
  1032. for page in self.pdf.pages[from_page:to_page]:
  1033. lines.extend([t for t in page.extract_text().split("\n")])
  1034. outlines = self.pdf.outline
  1035. def dfs(arr, depth):
  1036. for a in arr:
  1037. if isinstance(a, dict):
  1038. self.outlines.append((a["/Title"], depth))
  1039. continue
  1040. dfs(a, depth + 1)
  1041. dfs(outlines, 0)
  1042. except Exception as e:
  1043. logging.warning(f"Outlines exception: {e}")
  1044. if not self.outlines:
  1045. logging.warning(f"Miss outlines")
  1046. return [(l, "") for l in lines], []
  1047. def crop(self, ck, need_position):
  1048. raise NotImplementedError
  1049. @staticmethod
  1050. def remove_tag(txt):
  1051. raise NotImplementedError
  1052. if __name__ == "__main__":
  1053. pass