Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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