detect_para.py 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472
  1. import os
  2. import sys
  3. import json
  4. import re
  5. import math
  6. import unicodedata
  7. from collections import Counter
  8. import numpy as np
  9. from termcolor import cprint
  10. from magic_pdf.libs.commons import fitz
  11. from magic_pdf.libs.nlp_utils import NLPModels
  12. if sys.version_info[0] >= 3:
  13. sys.stdout.reconfigure(encoding="utf-8") # type: ignore
  14. def open_pdf(pdf_path):
  15. try:
  16. pdf_document = fitz.open(pdf_path) # type: ignore
  17. return pdf_document
  18. except Exception as e:
  19. print(f"无法打开PDF文件:{pdf_path}。原因是:{e}")
  20. raise e
  21. def print_green_on_red(text):
  22. cprint(text, "green", "on_red", attrs=["bold"], end="\n\n")
  23. def print_green(text):
  24. print()
  25. cprint(text, "green", attrs=["bold"], end="\n\n")
  26. def print_red(text):
  27. print()
  28. cprint(text, "red", attrs=["bold"], end="\n\n")
  29. def print_yellow(text):
  30. print()
  31. cprint(text, "yellow", attrs=["bold"], end="\n\n")
  32. def safe_get(dict_obj, key, default):
  33. val = dict_obj.get(key)
  34. if val is None:
  35. return default
  36. else:
  37. return val
  38. def is_bbox_overlap(bbox1, bbox2):
  39. """
  40. This function checks if bbox1 and bbox2 overlap or not
  41. Parameters
  42. ----------
  43. bbox1 : list
  44. bbox1
  45. bbox2 : list
  46. bbox2
  47. Returns
  48. -------
  49. bool
  50. True if bbox1 and bbox2 overlap, else False
  51. """
  52. x0_1, y0_1, x1_1, y1_1 = bbox1
  53. x0_2, y0_2, x1_2, y1_2 = bbox2
  54. if x0_1 > x1_2 or x0_2 > x1_1:
  55. return False
  56. if y0_1 > y1_2 or y0_2 > y1_1:
  57. return False
  58. return True
  59. def is_in_bbox(bbox1, bbox2):
  60. """
  61. This function checks if bbox1 is in bbox2
  62. Parameters
  63. ----------
  64. bbox1 : list
  65. bbox1
  66. bbox2 : list
  67. bbox2
  68. Returns
  69. -------
  70. bool
  71. True if bbox1 is in bbox2, else False
  72. """
  73. x0_1, y0_1, x1_1, y1_1 = bbox1
  74. x0_2, y0_2, x1_2, y1_2 = bbox2
  75. if x0_1 >= x0_2 and y0_1 >= y0_2 and x1_1 <= x1_2 and y1_1 <= y1_2:
  76. return True
  77. else:
  78. return False
  79. def calculate_para_bbox(lines):
  80. """
  81. This function calculates the minimum bbox of the paragraph
  82. Parameters
  83. ----------
  84. lines : list
  85. lines
  86. Returns
  87. -------
  88. para_bbox : list
  89. bbox of the paragraph
  90. """
  91. x0 = min(line["bbox"][0] for line in lines)
  92. y0 = min(line["bbox"][1] for line in lines)
  93. x1 = max(line["bbox"][2] for line in lines)
  94. y1 = max(line["bbox"][3] for line in lines)
  95. return [x0, y0, x1, y1]
  96. def is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction=2):
  97. """
  98. This function checks if the line is right aligned from its neighbors
  99. Parameters
  100. ----------
  101. curr_line_bbox : list
  102. bbox of the current line
  103. prev_line_bbox : list
  104. bbox of the previous line
  105. next_line_bbox : list
  106. bbox of the next line
  107. avg_char_width : float
  108. average of char widths
  109. direction : int
  110. 0 for prev, 1 for next, 2 for both
  111. Returns
  112. -------
  113. bool
  114. True if the line is right aligned from its neighbors, False otherwise.
  115. """
  116. horizontal_ratio = 0.5
  117. horizontal_thres = horizontal_ratio * avg_char_width
  118. _, _, x1, _ = curr_line_bbox
  119. _, _, prev_x1, _ = prev_line_bbox if prev_line_bbox else (0, 0, 0, 0)
  120. _, _, next_x1, _ = next_line_bbox if next_line_bbox else (0, 0, 0, 0)
  121. if direction == 0:
  122. return abs(x1 - prev_x1) < horizontal_thres
  123. elif direction == 1:
  124. return abs(x1 - next_x1) < horizontal_thres
  125. elif direction == 2:
  126. return abs(x1 - prev_x1) < horizontal_thres and abs(x1 - next_x1) < horizontal_thres
  127. else:
  128. return False
  129. def is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction=2):
  130. """
  131. This function checks if the line is left aligned from its neighbors
  132. Parameters
  133. ----------
  134. curr_line_bbox : list
  135. bbox of the current line
  136. prev_line_bbox : list
  137. bbox of the previous line
  138. next_line_bbox : list
  139. bbox of the next line
  140. avg_char_width : float
  141. average of char widths
  142. direction : int
  143. 0 for prev, 1 for next, 2 for both
  144. Returns
  145. -------
  146. bool
  147. True if the line is left aligned from its neighbors, False otherwise.
  148. """
  149. horizontal_ratio = 0.5
  150. horizontal_thres = horizontal_ratio * avg_char_width
  151. x0, _, _, _ = curr_line_bbox
  152. prev_x0, _, _, _ = prev_line_bbox if prev_line_bbox else (0, 0, 0, 0)
  153. next_x0, _, _, _ = next_line_bbox if next_line_bbox else (0, 0, 0, 0)
  154. if direction == 0:
  155. return abs(x0 - prev_x0) < horizontal_thres
  156. elif direction == 1:
  157. return abs(x0 - next_x0) < horizontal_thres
  158. elif direction == 2:
  159. return abs(x0 - prev_x0) < horizontal_thres and abs(x0 - next_x0) < horizontal_thres
  160. else:
  161. return False
  162. def end_with_punctuation(line_text):
  163. """
  164. This function checks if the line ends with punctuation marks
  165. """
  166. english_end_puncs = [".", "?", "!"]
  167. chinese_end_puncs = ["。", "?", "!"]
  168. end_puncs = english_end_puncs + chinese_end_puncs
  169. last_non_space_char = None
  170. for ch in line_text[::-1]:
  171. if not ch.isspace():
  172. last_non_space_char = ch
  173. break
  174. if last_non_space_char is None:
  175. return False
  176. return last_non_space_char in end_puncs
  177. def is_nested_list(lst):
  178. if isinstance(lst, list):
  179. return any(isinstance(sub, list) for sub in lst)
  180. return False
  181. class DenseSingleLineBlockException(Exception):
  182. """
  183. This class defines the exception type for dense single line-block.
  184. """
  185. def __init__(self, message="DenseSingleLineBlockException"):
  186. self.message = message
  187. super().__init__(self.message)
  188. def __str__(self):
  189. return f"{self.message}"
  190. def __repr__(self):
  191. return f"{self.message}"
  192. class TitleDetectionException(Exception):
  193. """
  194. This class defines the exception type for title detection.
  195. """
  196. def __init__(self, message="TitleDetectionException"):
  197. self.message = message
  198. super().__init__(self.message)
  199. def __str__(self):
  200. return f"{self.message}"
  201. def __repr__(self):
  202. return f"{self.message}"
  203. class TitleLevelException(Exception):
  204. """
  205. This class defines the exception type for title level.
  206. """
  207. def __init__(self, message="TitleLevelException"):
  208. self.message = message
  209. super().__init__(self.message)
  210. def __str__(self):
  211. return f"{self.message}"
  212. def __repr__(self):
  213. return f"{self.message}"
  214. class ParaSplitException(Exception):
  215. """
  216. This class defines the exception type for paragraph splitting.
  217. """
  218. def __init__(self, message="ParaSplitException"):
  219. self.message = message
  220. super().__init__(self.message)
  221. def __str__(self):
  222. return f"{self.message}"
  223. def __repr__(self):
  224. return f"{self.message}"
  225. class ParaMergeException(Exception):
  226. """
  227. This class defines the exception type for paragraph merging.
  228. """
  229. def __init__(self, message="ParaMergeException"):
  230. self.message = message
  231. super().__init__(self.message)
  232. def __str__(self):
  233. return f"{self.message}"
  234. def __repr__(self):
  235. return f"{self.message}"
  236. class DiscardByException:
  237. """
  238. This class discards pdf files by exception
  239. """
  240. def __init__(self) -> None:
  241. pass
  242. def discard_by_single_line_block(self, pdf_dic, exception: DenseSingleLineBlockException):
  243. """
  244. This function discards pdf files by single line block exception
  245. Parameters
  246. ----------
  247. pdf_dic : dict
  248. pdf dictionary
  249. exception : str
  250. exception message
  251. Returns
  252. -------
  253. error_message : str
  254. """
  255. exception_page_nums = 0
  256. page_num = 0
  257. for page_id, page in pdf_dic.items():
  258. if page_id.startswith("page_"):
  259. page_num += 1
  260. if "preproc_blocks" in page.keys():
  261. preproc_blocks = page["preproc_blocks"]
  262. all_single_line_blocks = []
  263. for block in preproc_blocks:
  264. if len(block["lines"]) == 1:
  265. all_single_line_blocks.append(block)
  266. if len(preproc_blocks) > 0 and len(all_single_line_blocks) / len(preproc_blocks) > 0.9:
  267. exception_page_nums += 1
  268. if page_num == 0:
  269. return None
  270. if exception_page_nums / page_num > 0.1: # Low ratio means basically, whenever this is the case, it is discarded
  271. return exception.message
  272. return None
  273. def discard_by_title_detection(self, pdf_dic, exception: TitleDetectionException):
  274. """
  275. This function discards pdf files by title detection exception
  276. Parameters
  277. ----------
  278. pdf_dic : dict
  279. pdf dictionary
  280. exception : str
  281. exception message
  282. Returns
  283. -------
  284. error_message : str
  285. """
  286. # return exception.message
  287. return None
  288. def discard_by_title_level(self, pdf_dic, exception: TitleLevelException):
  289. """
  290. This function discards pdf files by title level exception
  291. Parameters
  292. ----------
  293. pdf_dic : dict
  294. pdf dictionary
  295. exception : str
  296. exception message
  297. Returns
  298. -------
  299. error_message : str
  300. """
  301. # return exception.message
  302. return None
  303. def discard_by_split_para(self, pdf_dic, exception: ParaSplitException):
  304. """
  305. This function discards pdf files by split para exception
  306. Parameters
  307. ----------
  308. pdf_dic : dict
  309. pdf dictionary
  310. exception : str
  311. exception message
  312. Returns
  313. -------
  314. error_message : str
  315. """
  316. # return exception.message
  317. return None
  318. def discard_by_merge_para(self, pdf_dic, exception: ParaMergeException):
  319. """
  320. This function discards pdf files by merge para exception
  321. Parameters
  322. ----------
  323. pdf_dic : dict
  324. pdf dictionary
  325. exception : str
  326. exception message
  327. Returns
  328. -------
  329. error_message : str
  330. """
  331. # return exception.message
  332. return None
  333. class LayoutFilterProcessor:
  334. def __init__(self) -> None:
  335. pass
  336. def batch_process_blocks(self, pdf_dict):
  337. """
  338. This function processes the blocks in batch.
  339. Parameters
  340. ----------
  341. self : object
  342. The instance of the class.
  343. pdf_dict : dict
  344. pdf dictionary
  345. Returns
  346. -------
  347. pdf_dict : dict
  348. pdf dictionary
  349. """
  350. for page_id, blocks in pdf_dict.items():
  351. if page_id.startswith("page_"):
  352. if "layout_bboxes" in blocks.keys() and "para_blocks" in blocks.keys():
  353. layout_bbox_objs = blocks["layout_bboxes"]
  354. if layout_bbox_objs is None:
  355. continue
  356. layout_bboxes = [bbox_obj["layout_bbox"] for bbox_obj in layout_bbox_objs]
  357. # Enlarge each value of x0, y0, x1, y1 for each layout_bbox to prevent loss of text.
  358. layout_bboxes = [
  359. [math.ceil(x0), math.ceil(y0), math.ceil(x1), math.ceil(y1)] for x0, y0, x1, y1 in layout_bboxes
  360. ]
  361. para_blocks = blocks["para_blocks"]
  362. if para_blocks is None:
  363. continue
  364. for lb_bbox in layout_bboxes:
  365. for i, para_block in enumerate(para_blocks):
  366. para_bbox = para_block["bbox"]
  367. para_blocks[i]["in_layout"] = 0
  368. if is_in_bbox(para_bbox, lb_bbox):
  369. para_blocks[i]["in_layout"] = 1
  370. blocks["para_blocks"] = para_blocks
  371. return pdf_dict
  372. class RawBlockProcessor:
  373. def __init__(self) -> None:
  374. self.y_tolerance = 2
  375. self.pdf_dic = {}
  376. def __span_flags_decomposer(self, span_flags):
  377. """
  378. Make font flags human readable.
  379. Parameters
  380. ----------
  381. self : object
  382. The instance of the class.
  383. span_flags : int
  384. span flags
  385. Returns
  386. -------
  387. l : dict
  388. decomposed flags
  389. """
  390. l = {
  391. "is_superscript": False,
  392. "is_italic": False,
  393. "is_serifed": False,
  394. "is_sans_serifed": False,
  395. "is_monospaced": False,
  396. "is_proportional": False,
  397. "is_bold": False,
  398. }
  399. if span_flags & 2**0:
  400. l["is_superscript"] = True # 表示上标
  401. if span_flags & 2**1:
  402. l["is_italic"] = True # 表示斜体
  403. if span_flags & 2**2:
  404. l["is_serifed"] = True # 表示衬线字体
  405. else:
  406. l["is_sans_serifed"] = True # 表示非衬线字体
  407. if span_flags & 2**3:
  408. l["is_monospaced"] = True # 表示等宽字体
  409. else:
  410. l["is_proportional"] = True # 表示比例字体
  411. if span_flags & 2**4:
  412. l["is_bold"] = True # 表示粗体
  413. return l
  414. def __make_new_lines(self, raw_lines):
  415. """
  416. This function makes new lines.
  417. Parameters
  418. ----------
  419. self : object
  420. The instance of the class.
  421. raw_lines : list
  422. raw lines
  423. Returns
  424. -------
  425. new_lines : list
  426. new lines
  427. """
  428. new_lines = []
  429. new_line = None
  430. for raw_line in raw_lines:
  431. raw_line_bbox = raw_line["bbox"]
  432. raw_line_spans = raw_line["spans"]
  433. raw_line_text = "".join([span["text"] for span in raw_line_spans])
  434. raw_line_dir = raw_line.get("dir", None)
  435. decomposed_line_spans = []
  436. for span in raw_line_spans:
  437. raw_flags = span["flags"]
  438. decomposed_flags = self.__span_flags_decomposer(raw_flags)
  439. span["decomposed_flags"] = decomposed_flags
  440. decomposed_line_spans.append(span)
  441. if new_line is None: # Handle the first line
  442. new_line = {
  443. "bbox": raw_line_bbox,
  444. "text": raw_line_text,
  445. "dir": raw_line_dir if raw_line_dir else (0, 0),
  446. "spans": decomposed_line_spans,
  447. }
  448. else: # Handle the rest lines
  449. if (
  450. abs(raw_line_bbox[1] - new_line["bbox"][1]) <= self.y_tolerance
  451. and abs(raw_line_bbox[3] - new_line["bbox"][3]) <= self.y_tolerance
  452. ):
  453. new_line["bbox"] = (
  454. min(new_line["bbox"][0], raw_line_bbox[0]), # left
  455. new_line["bbox"][1], # top
  456. max(new_line["bbox"][2], raw_line_bbox[2]), # right
  457. raw_line_bbox[3], # bottom
  458. )
  459. new_line["text"] += raw_line_text
  460. new_line["spans"].extend(raw_line_spans)
  461. new_line["dir"] = (
  462. new_line["dir"][0] + raw_line_dir[0],
  463. new_line["dir"][1] + raw_line_dir[1],
  464. )
  465. else:
  466. new_lines.append(new_line)
  467. new_line = {
  468. "bbox": raw_line_bbox,
  469. "text": raw_line_text,
  470. "dir": raw_line_dir if raw_line_dir else (0, 0),
  471. "spans": raw_line_spans,
  472. }
  473. if new_line:
  474. new_lines.append(new_line)
  475. return new_lines
  476. def __make_new_block(self, raw_block):
  477. """
  478. This function makes a new block.
  479. Parameters
  480. ----------
  481. self : object
  482. The instance of the class.
  483. ----------
  484. raw_block : dict
  485. a raw block
  486. Returns
  487. -------
  488. new_block : dict
  489. """
  490. new_block = {}
  491. block_id = raw_block["number"]
  492. block_bbox = raw_block["bbox"]
  493. block_text = "".join(span["text"] for line in raw_block["lines"] for span in line["spans"])
  494. raw_lines = raw_block["lines"]
  495. block_lines = self.__make_new_lines(raw_lines)
  496. new_block["block_id"] = block_id
  497. new_block["bbox"] = block_bbox
  498. new_block["text"] = block_text
  499. new_block["lines"] = block_lines
  500. return new_block
  501. def batch_process_blocks(self, pdf_dic):
  502. """
  503. This function processes the blocks in batch.
  504. Parameters
  505. ----------
  506. self : object
  507. The instance of the class.
  508. ----------
  509. blocks : list
  510. Input block is a list of raw blocks.
  511. Returns
  512. -------
  513. result_dict : dict
  514. result dictionary
  515. """
  516. for page_id, blocks in pdf_dic.items():
  517. if page_id.startswith("page_"):
  518. para_blocks = []
  519. if "preproc_blocks" in blocks.keys():
  520. input_blocks = blocks["preproc_blocks"]
  521. for raw_block in input_blocks:
  522. new_block = self.__make_new_block(raw_block)
  523. para_blocks.append(new_block)
  524. blocks["para_blocks"] = para_blocks
  525. return pdf_dic
  526. class BlockStatisticsCalculator:
  527. """
  528. This class calculates the statistics of the block.
  529. """
  530. def __init__(self) -> None:
  531. pass
  532. def __calc_stats_of_new_lines(self, new_lines):
  533. """
  534. This function calculates the paragraph metrics
  535. Parameters
  536. ----------
  537. combined_lines : list
  538. combined lines
  539. Returns
  540. -------
  541. X0 : float
  542. Median of x0 values, which represents the left average boundary of the block
  543. X1 : float
  544. Median of x1 values, which represents the right average boundary of the block
  545. avg_char_width : float
  546. Average of char widths, which represents the average char width of the block
  547. avg_char_height : float
  548. Average of line heights, which represents the average line height of the block
  549. """
  550. x0_values = []
  551. x1_values = []
  552. char_widths = []
  553. char_heights = []
  554. block_font_types = []
  555. block_font_sizes = []
  556. block_directions = []
  557. if len(new_lines) > 0:
  558. for i, line in enumerate(new_lines):
  559. line_bbox = line["bbox"]
  560. line_text = line["text"]
  561. line_spans = line["spans"]
  562. num_chars = len([ch for ch in line_text if not ch.isspace()])
  563. x0_values.append(line_bbox[0])
  564. x1_values.append(line_bbox[2])
  565. if num_chars > 0:
  566. char_width = (line_bbox[2] - line_bbox[0]) / num_chars
  567. char_widths.append(char_width)
  568. for span in line_spans:
  569. block_font_types.append(span["font"])
  570. block_font_sizes.append(span["size"])
  571. if "dir" in line:
  572. block_directions.append(line["dir"])
  573. # line_font_types = [span["font"] for span in line_spans]
  574. char_heights = [span["size"] for span in line_spans]
  575. X0 = np.median(x0_values) if x0_values else 0
  576. X1 = np.median(x1_values) if x1_values else 0
  577. avg_char_width = sum(char_widths) / len(char_widths) if char_widths else 0
  578. avg_char_height = sum(char_heights) / len(char_heights) if char_heights else 0
  579. # max_freq_font_type = max(set(block_font_types), key=block_font_types.count) if block_font_types else None
  580. max_span_length = 0
  581. max_span_font_type = None
  582. for line in new_lines:
  583. line_spans = line["spans"]
  584. for span in line_spans:
  585. span_length = span["bbox"][2] - span["bbox"][0]
  586. if span_length > max_span_length:
  587. max_span_length = span_length
  588. max_span_font_type = span["font"]
  589. max_freq_font_type = max_span_font_type
  590. avg_font_size = sum(block_font_sizes) / len(block_font_sizes) if block_font_sizes else None
  591. avg_dir_horizontal = sum([dir[0] for dir in block_directions]) / len(block_directions) if block_directions else 0
  592. avg_dir_vertical = sum([dir[1] for dir in block_directions]) / len(block_directions) if block_directions else 0
  593. median_font_size = float(np.median(block_font_sizes)) if block_font_sizes else None
  594. return (
  595. X0,
  596. X1,
  597. avg_char_width,
  598. avg_char_height,
  599. max_freq_font_type,
  600. avg_font_size,
  601. (avg_dir_horizontal, avg_dir_vertical),
  602. median_font_size,
  603. )
  604. def __make_new_block(self, input_block):
  605. new_block = {}
  606. raw_lines = input_block["lines"]
  607. stats = self.__calc_stats_of_new_lines(raw_lines)
  608. block_id = input_block["block_id"]
  609. block_bbox = input_block["bbox"]
  610. block_text = input_block["text"]
  611. block_lines = raw_lines
  612. block_avg_left_boundary = stats[0]
  613. block_avg_right_boundary = stats[1]
  614. block_avg_char_width = stats[2]
  615. block_avg_char_height = stats[3]
  616. block_font_type = stats[4]
  617. block_font_size = stats[5]
  618. block_direction = stats[6]
  619. block_median_font_size = stats[7]
  620. new_block["block_id"] = block_id
  621. new_block["bbox"] = block_bbox
  622. new_block["text"] = block_text
  623. new_block["dir"] = block_direction
  624. new_block["X0"] = block_avg_left_boundary
  625. new_block["X1"] = block_avg_right_boundary
  626. new_block["avg_char_width"] = block_avg_char_width
  627. new_block["avg_char_height"] = block_avg_char_height
  628. new_block["block_font_type"] = block_font_type
  629. new_block["block_font_size"] = block_font_size
  630. new_block["lines"] = block_lines
  631. new_block["median_font_size"] = block_median_font_size
  632. return new_block
  633. def batch_process_blocks(self, pdf_dic):
  634. """
  635. This function processes the blocks in batch.
  636. Parameters
  637. ----------
  638. self : object
  639. The instance of the class.
  640. ----------
  641. blocks : list
  642. Input block is a list of raw blocks.
  643. Schema can refer to the value of key ""preproc_blocks".
  644. Returns
  645. -------
  646. result_dict : dict
  647. result dictionary
  648. """
  649. for page_id, blocks in pdf_dic.items():
  650. if page_id.startswith("page_"):
  651. para_blocks = []
  652. if "para_blocks" in blocks.keys():
  653. input_blocks = blocks["para_blocks"]
  654. for input_block in input_blocks:
  655. new_block = self.__make_new_block(input_block)
  656. para_blocks.append(new_block)
  657. blocks["para_blocks"] = para_blocks
  658. return pdf_dic
  659. class DocStatisticsCalculator:
  660. """
  661. This class calculates the statistics of the document.
  662. """
  663. def __init__(self) -> None:
  664. pass
  665. def calc_stats_of_doc(self, pdf_dict):
  666. """
  667. This function computes the statistics of the document
  668. Parameters
  669. ----------
  670. result_dict : dict
  671. result dictionary
  672. Returns
  673. -------
  674. statistics : dict
  675. statistics of the document
  676. """
  677. total_text_length = 0
  678. total_num_blocks = 0
  679. for page_id, blocks in pdf_dict.items():
  680. if page_id.startswith("page_"):
  681. if "para_blocks" in blocks.keys():
  682. para_blocks = blocks["para_blocks"]
  683. for para_block in para_blocks:
  684. total_text_length += len(para_block["text"])
  685. total_num_blocks += 1
  686. avg_text_length = total_text_length / total_num_blocks if total_num_blocks else 0
  687. font_list = []
  688. for page_id, blocks in pdf_dict.items():
  689. if page_id.startswith("page_"):
  690. if "para_blocks" in blocks.keys():
  691. input_blocks = blocks["para_blocks"]
  692. for input_block in input_blocks:
  693. block_text_length = len(input_block.get("text", ""))
  694. if block_text_length < avg_text_length * 0.5:
  695. continue
  696. block_font_type = safe_get(input_block, "block_font_type", "")
  697. block_font_size = safe_get(input_block, "block_font_size", 0)
  698. font_list.append((block_font_type, block_font_size))
  699. font_counter = Counter(font_list)
  700. most_common_font = font_counter.most_common(1)[0] if font_list else (("", 0), 0)
  701. second_most_common_font = font_counter.most_common(2)[1] if len(font_counter) > 1 else (("", 0), 0)
  702. statistics = {
  703. "num_pages": 0,
  704. "num_blocks": 0,
  705. "num_paras": 0,
  706. "num_titles": 0,
  707. "num_header_blocks": 0,
  708. "num_footer_blocks": 0,
  709. "num_watermark_blocks": 0,
  710. "num_vertical_margin_note_blocks": 0,
  711. "most_common_font_type": most_common_font[0][0],
  712. "most_common_font_size": most_common_font[0][1],
  713. "number_of_most_common_font": most_common_font[1],
  714. "second_most_common_font_type": second_most_common_font[0][0],
  715. "second_most_common_font_size": second_most_common_font[0][1],
  716. "number_of_second_most_common_font": second_most_common_font[1],
  717. "avg_text_length": avg_text_length,
  718. }
  719. for page_id, blocks in pdf_dict.items():
  720. if page_id.startswith("page_"):
  721. blocks = pdf_dict[page_id]["para_blocks"]
  722. statistics["num_pages"] += 1
  723. for block_id, block_data in enumerate(blocks):
  724. statistics["num_blocks"] += 1
  725. if "paras" in block_data.keys():
  726. statistics["num_paras"] += len(block_data["paras"])
  727. for line in block_data["lines"]:
  728. if line.get("is_title", 0):
  729. statistics["num_titles"] += 1
  730. if block_data.get("is_header", 0):
  731. statistics["num_header_blocks"] += 1
  732. if block_data.get("is_footer", 0):
  733. statistics["num_footer_blocks"] += 1
  734. if block_data.get("is_watermark", 0):
  735. statistics["num_watermark_blocks"] += 1
  736. if block_data.get("is_vertical_margin_note", 0):
  737. statistics["num_vertical_margin_note_blocks"] += 1
  738. pdf_dict["statistics"] = statistics
  739. return pdf_dict
  740. class TitleProcessor:
  741. """
  742. This class processes the title.
  743. """
  744. def __init__(self, *doc_statistics) -> None:
  745. if len(doc_statistics) > 0:
  746. self.doc_statistics = doc_statistics[0]
  747. self.nlp_model = NLPModels()
  748. self.MAX_TITLE_LEVEL = 3
  749. self.numbered_title_pattern = r"""
  750. ^ # 行首
  751. ( # 开始捕获组
  752. [\(\(]\d+[\)\)] # 括号内数字,支持中文和英文括号,例如:(1) 或 (1)
  753. |\d+[\)\)]\s # 数字后跟右括号和空格,支持中文和英文括号,例如:2) 或 2)
  754. |[\(\(][A-Z][\)\)] # 括号内大写字母,支持中文和英文括号,例如:(A) 或 (A)
  755. |[A-Z][\)\)]\s # 大写字母后跟右括号和空格,例如:A) 或 A)
  756. |[\(\(][IVXLCDM]+[\)\)] # 括号内罗马数字,支持中文和英文括号,例如:(I) 或 (I)
  757. |[IVXLCDM]+[\)\)]\s # 罗马数字后跟右括号和空格,例如:I) 或 I)
  758. |\d+(\.\d+)*\s # 数字或复合数字编号后跟空格,例如:1. 或 3.2.1
  759. |[一二三四五六七八九十百千]+[、\s] # 中文序号后跟顿号和空格,例如:一、
  760. |[\(|\(][一二三四五六七八九十百千]+[\)|\)]\s* # 中文括号内中文序号后跟空格,例如:(一)
  761. |[A-Z]\.\d+(\.\d+)?\s # 大写字母后跟点和数字,例如:A.1 或 A.1.1
  762. |[\(\(][a-z][\)\)] # 括号内小写字母,支持中文和英文括号,例如:(a) 或 (a)
  763. |[a-z]\)\s # 小写字母后跟右括号和空格,例如:a)
  764. |[A-Z]-\s # 大写字母后跟短横线和空格,例如:A-
  765. |\w+:\s # 英文序号词后跟冒号和空格,例如:First:
  766. |第[一二三四五六七八九十百千]+[章节部分条款]\s # 以“第”开头的中文标题后跟空格
  767. |[IVXLCDM]+\. # 罗马数字后跟点,例如:I.
  768. |\d+\.\s # 单个数字后跟点和空格,例如:1.
  769. ) # 结束捕获组
  770. .+ # 标题的其余部分
  771. """
  772. def _is_potential_title(
  773. self,
  774. curr_line,
  775. prev_line,
  776. prev_line_is_title,
  777. next_line,
  778. avg_char_width,
  779. avg_char_height,
  780. median_font_size,
  781. ):
  782. """
  783. This function checks if the line is a potential title.
  784. Parameters
  785. ----------
  786. curr_line : dict
  787. current line
  788. prev_line : dict
  789. previous line
  790. next_line : dict
  791. next line
  792. avg_char_width : float
  793. average of char widths
  794. avg_char_height : float
  795. average of line heights
  796. Returns
  797. -------
  798. bool
  799. True if the line is a potential title, False otherwise.
  800. """
  801. def __is_line_centered(line_bbox, page_bbox, avg_char_width):
  802. """
  803. This function checks if the line is centered on the page
  804. Parameters
  805. ----------
  806. line_bbox : list
  807. bbox of the line
  808. page_bbox : list
  809. bbox of the page
  810. avg_char_width : float
  811. average of char widths
  812. Returns
  813. -------
  814. bool
  815. True if the line is centered on the page, False otherwise.
  816. """
  817. horizontal_ratio = 0.5
  818. horizontal_thres = horizontal_ratio * avg_char_width
  819. x0, _, x1, _ = line_bbox
  820. _, _, page_x1, _ = page_bbox
  821. return abs((x0 + x1) / 2 - page_x1 / 2) < horizontal_thres
  822. def __is_bold_font_line(line):
  823. """
  824. Check if a line contains any bold font style.
  825. """
  826. def _is_bold_span(span):
  827. # if span text is empty or only contains space, return False
  828. if not span["text"].strip():
  829. return False
  830. return bool(span["flags"] & 2**4) # Check if the font is bold
  831. for span in line["spans"]:
  832. if not _is_bold_span(span):
  833. return False
  834. return True
  835. def __is_italic_font_line(line):
  836. """
  837. Check if a line contains any italic font style.
  838. """
  839. def __is_italic_span(span):
  840. return bool(span["flags"] & 2**1) # Check if the font is italic
  841. for span in line["spans"]:
  842. if not __is_italic_span(span):
  843. return False
  844. return True
  845. def __is_punctuation_heavy(line_text):
  846. """
  847. Check if the line contains a high ratio of punctuation marks, which may indicate
  848. that the line is not a title.
  849. Parameters:
  850. line_text (str): Text of the line.
  851. Returns:
  852. bool: True if the line is heavy with punctuation, False otherwise.
  853. """
  854. # Pattern for common title format like "X.Y. Title"
  855. pattern = r"\b\d+\.\d+\..*\b"
  856. # If the line matches the title format, return False
  857. if re.match(pattern, line_text.strip()):
  858. return False
  859. # Find all punctuation marks in the line
  860. punctuation_marks = re.findall(r"[^\w\s]", line_text)
  861. number_of_punctuation_marks = len(punctuation_marks)
  862. text_length = len(line_text)
  863. if text_length == 0:
  864. return False
  865. punctuation_ratio = number_of_punctuation_marks / text_length
  866. if punctuation_ratio >= 0.1:
  867. return True
  868. return False
  869. def __has_mixed_font_styles(spans, strict_mode=False):
  870. """
  871. This function checks if the line has mixed font styles, the strict mode will compare the font types
  872. Parameters
  873. ----------
  874. spans : list
  875. spans of the line
  876. strict_mode : bool
  877. True for strict mode, the font types will be fully compared
  878. False for non-strict mode, the font types will be compared by the most longest common prefix
  879. Returns
  880. -------
  881. bool
  882. True if the line has mixed font styles, False otherwise.
  883. """
  884. if strict_mode:
  885. font_styles = set()
  886. for span in spans:
  887. font_style = span["font"].lower()
  888. font_styles.add(font_style)
  889. return len(font_styles) > 1
  890. else: # non-strict mode
  891. font_styles = []
  892. for span in spans:
  893. font_style = span["font"].lower()
  894. font_styles.append(font_style)
  895. if len(font_styles) > 1:
  896. longest_common_prefix = os.path.commonprefix(font_styles)
  897. if len(longest_common_prefix) > 0:
  898. return False
  899. else:
  900. return True
  901. else:
  902. return False
  903. def __is_different_font_type_from_neighbors(curr_line_font_type, prev_line_font_type, next_line_font_type):
  904. """
  905. This function checks if the current line has a different font type from the previous and next lines
  906. Parameters
  907. ----------
  908. curr_line_font_type : str
  909. font type of the current line
  910. prev_line_font_type : str
  911. font type of the previous line
  912. next_line_font_type : str
  913. font type of the next line
  914. Returns
  915. -------
  916. bool
  917. True if the current line has a different font type from the previous and next lines, False otherwise.
  918. """
  919. return all(
  920. curr_line_font_type != other_font_type.lower()
  921. for other_font_type in [prev_line_font_type, next_line_font_type]
  922. if other_font_type is not None
  923. )
  924. def __is_larger_font_size_from_neighbors(curr_line_font_size, prev_line_font_size, next_line_font_size):
  925. """
  926. This function checks if the current line has a larger font size than the previous and next lines
  927. Parameters
  928. ----------
  929. curr_line_font_size : float
  930. font size of the current line
  931. prev_line_font_size : float
  932. font size of the previous line
  933. next_line_font_size : float
  934. font size of the next line
  935. Returns
  936. -------
  937. bool
  938. True if the current line has a larger font size than the previous and next lines, False otherwise.
  939. """
  940. return all(
  941. curr_line_font_size > other_font_size * 1.2
  942. for other_font_size in [prev_line_font_size, next_line_font_size]
  943. if other_font_size is not None
  944. )
  945. def __is_similar_to_pre_line(curr_line_font_type, prev_line_font_type, curr_line_font_size, prev_line_font_size):
  946. """
  947. This function checks if the current line is similar to the previous line
  948. Parameters
  949. ----------
  950. curr_line : dict
  951. current line
  952. prev_line : dict
  953. previous line
  954. Returns
  955. -------
  956. bool
  957. True if the current line is similar to the previous line, False otherwise.
  958. """
  959. if curr_line_font_type == prev_line_font_type and curr_line_font_size == prev_line_font_size:
  960. return True
  961. else:
  962. return False
  963. def __is_same_font_type_of_docAvg(curr_line_font_type):
  964. """
  965. This function checks if the current line has the same font type as the document average font type
  966. Parameters
  967. ----------
  968. curr_line_font_type : str
  969. font type of the current line
  970. Returns
  971. -------
  972. bool
  973. True if the current line has the same font type as the document average font type, False otherwise.
  974. """
  975. doc_most_common_font_type = safe_get(self.doc_statistics, "most_common_font_type", "").lower()
  976. doc_second_most_common_font_type = safe_get(self.doc_statistics, "second_most_common_font_type", "").lower()
  977. return curr_line_font_type.lower() in [doc_most_common_font_type, doc_second_most_common_font_type]
  978. def __is_font_size_not_less_than_docAvg(curr_line_font_size, ratio: float = 1):
  979. """
  980. This function checks if the current line has a large enough font size
  981. Parameters
  982. ----------
  983. curr_line_font_size : float
  984. font size of the current line
  985. ratio : float
  986. ratio of the current line font size to the document average font size
  987. Returns
  988. -------
  989. bool
  990. True if the current line has a large enough font size, False otherwise.
  991. """
  992. doc_most_common_font_size = safe_get(self.doc_statistics, "most_common_font_size", 0)
  993. doc_second_most_common_font_size = safe_get(self.doc_statistics, "second_most_common_font_size", 0)
  994. doc_avg_font_size = min(doc_most_common_font_size, doc_second_most_common_font_size)
  995. return curr_line_font_size >= doc_avg_font_size * ratio
  996. def __is_sufficient_spacing_above_and_below(
  997. curr_line_bbox,
  998. prev_line_bbox,
  999. next_line_bbox,
  1000. avg_char_height,
  1001. median_font_size,
  1002. ):
  1003. """
  1004. This function checks if the current line has sufficient spacing above and below
  1005. Parameters
  1006. ----------
  1007. curr_line_bbox : list
  1008. bbox of the current line
  1009. prev_line_bbox : list
  1010. bbox of the previous line
  1011. next_line_bbox : list
  1012. bbox of the next line
  1013. avg_char_width : float
  1014. average of char widths
  1015. avg_char_height : float
  1016. average of line heights
  1017. Returns
  1018. -------
  1019. bool
  1020. True if the current line has sufficient spacing above and below, False otherwise.
  1021. """
  1022. vertical_ratio = 1.25
  1023. vertical_thres = vertical_ratio * median_font_size
  1024. _, y0, _, y1 = curr_line_bbox
  1025. sufficient_spacing_above = False
  1026. if prev_line_bbox:
  1027. vertical_spacing_above = min(y0 - prev_line_bbox[1], y1 - prev_line_bbox[3])
  1028. sufficient_spacing_above = vertical_spacing_above > vertical_thres
  1029. else:
  1030. sufficient_spacing_above = True
  1031. sufficient_spacing_below = False
  1032. if next_line_bbox:
  1033. vertical_spacing_below = min(next_line_bbox[1] - y0, next_line_bbox[3] - y1)
  1034. sufficient_spacing_below = vertical_spacing_below > vertical_thres
  1035. else:
  1036. sufficient_spacing_below = True
  1037. return (sufficient_spacing_above, sufficient_spacing_below)
  1038. def __is_word_list_line_by_rules(curr_line_text):
  1039. """
  1040. This function checks if the current line is a word list
  1041. Parameters
  1042. ----------
  1043. curr_line_text : str
  1044. text of the current line
  1045. Returns
  1046. -------
  1047. bool
  1048. True if the current line is a name list, False otherwise.
  1049. """
  1050. # name_list_pattern = r"([a-zA-Z][a-zA-Z\s]{0,20}[a-zA-Z]|[\u4e00-\u9fa5·]{2,16})(?=[,,;;\s]|$)"
  1051. name_list_pattern = r"(?<![\u4e00-\u9fa5])([A-Z][a-z]{0,19}\s[A-Z][a-z]{0,19}|[\u4e00-\u9fa5]{2,6})(?=[,,;;\s]|$)"
  1052. compiled_pattern = re.compile(name_list_pattern)
  1053. if compiled_pattern.search(curr_line_text):
  1054. return True
  1055. else:
  1056. return False
  1057. def __get_text_catgr_by_nlp(curr_line_text):
  1058. """
  1059. This function checks if the current line is a name list using nlp model, such as spacy
  1060. Parameters
  1061. ----------
  1062. curr_line_text : str
  1063. text of the current line
  1064. Returns
  1065. -------
  1066. bool
  1067. True if the current line is a name list, False otherwise.
  1068. """
  1069. result = self.nlp_model.detect_entity_catgr_using_nlp(curr_line_text)
  1070. return result
  1071. def __is_numbered_title(curr_line_text):
  1072. """
  1073. This function checks if the current line is a numbered list
  1074. Parameters
  1075. ----------
  1076. curr_line_text : str
  1077. text of the current line
  1078. Returns
  1079. -------
  1080. bool
  1081. True if the current line is a numbered list, False otherwise.
  1082. """
  1083. compiled_pattern = re.compile(self.numbered_title_pattern, re.VERBOSE)
  1084. if compiled_pattern.search(curr_line_text):
  1085. return True
  1086. else:
  1087. return False
  1088. def __is_end_with_ending_puncs(line_text):
  1089. """
  1090. This function checks if the current line ends with a ending punctuation mark
  1091. Parameters
  1092. ----------
  1093. line_text : str
  1094. text of the current line
  1095. Returns
  1096. -------
  1097. bool
  1098. True if the current line ends with a punctuation mark, False otherwise.
  1099. """
  1100. end_puncs = [".", "?", "!", "。", "?", "!", "…"]
  1101. line_text = line_text.rstrip()
  1102. if line_text[-1] in end_puncs:
  1103. return True
  1104. return False
  1105. def __contains_only_no_meaning_symbols(line_text):
  1106. """
  1107. This function checks if the current line contains only symbols that have no meaning, if so, it is not a title.
  1108. Situation contains:
  1109. 1. Only have punctuation marks
  1110. 2. Only have other non-meaning symbols
  1111. Parameters
  1112. ----------
  1113. line_text : str
  1114. text of the current line
  1115. Returns
  1116. -------
  1117. bool
  1118. True if the current line contains only symbols that have no meaning, False otherwise.
  1119. """
  1120. punctuation_marks = re.findall(r"[^\w\s]", line_text) # find all punctuation marks
  1121. number_of_punctuation_marks = len(punctuation_marks)
  1122. text_length = len(line_text)
  1123. if text_length == 0:
  1124. return False
  1125. punctuation_ratio = number_of_punctuation_marks / text_length
  1126. if punctuation_ratio >= 0.9:
  1127. return True
  1128. return False
  1129. def __is_equation(line_text):
  1130. """
  1131. This function checks if the current line is an equation.
  1132. Parameters
  1133. ----------
  1134. line_text : str
  1135. Returns
  1136. -------
  1137. bool
  1138. True if the current line is an equation, False otherwise.
  1139. """
  1140. equation_reg = r"\$.*?\\overline.*?\$" # to match interline equations
  1141. if re.search(equation_reg, line_text):
  1142. return True
  1143. else:
  1144. return False
  1145. def __is_title_by_len(text, max_length=200):
  1146. """
  1147. This function checks if the current line is a title by length.
  1148. Parameters
  1149. ----------
  1150. text : str
  1151. text of the current line
  1152. max_length : int
  1153. max length of the title
  1154. Returns
  1155. -------
  1156. bool
  1157. True if the current line is a title, False otherwise.
  1158. """
  1159. text = text.strip()
  1160. return len(text) <= max_length
  1161. def __compute_line_font_type_and_size(curr_line):
  1162. """
  1163. This function computes the font type and font size of the line.
  1164. Parameters
  1165. ----------
  1166. line : dict
  1167. line
  1168. Returns
  1169. -------
  1170. font_type : str
  1171. font type of the line
  1172. font_size : float
  1173. font size of the line
  1174. """
  1175. spans = curr_line["spans"]
  1176. max_accumulated_length = 0
  1177. max_span_font_size = curr_line["spans"][0]["size"] # default value, float type
  1178. max_span_font_type = curr_line["spans"][0]["font"].lower() # default value, string type
  1179. for span in spans:
  1180. if span["text"].isspace():
  1181. continue
  1182. span_length = span["bbox"][2] - span["bbox"][0]
  1183. if span_length > max_accumulated_length:
  1184. max_accumulated_length = span_length
  1185. max_span_font_size = span["size"]
  1186. max_span_font_type = span["font"].lower()
  1187. return max_span_font_type, max_span_font_size
  1188. def __is_a_consistent_sub_title(pre_line, curr_line):
  1189. """
  1190. This function checks if the current line is a consistent sub title.
  1191. Parameters
  1192. ----------
  1193. pre_line : dict
  1194. previous line
  1195. curr_line : dict
  1196. current line
  1197. Returns
  1198. -------
  1199. bool
  1200. True if the current line is a consistent sub title, False otherwise.
  1201. """
  1202. if pre_line is None:
  1203. return False
  1204. start_letter_of_pre_line = pre_line["text"][0]
  1205. start_letter_of_curr_line = curr_line["text"][0]
  1206. has_same_prefix_digit = (
  1207. start_letter_of_pre_line.isdigit()
  1208. and start_letter_of_curr_line.isdigit()
  1209. and start_letter_of_pre_line == start_letter_of_curr_line
  1210. )
  1211. # prefix text of curr_line satisfies the following title format: x.x
  1212. prefix_text_pattern = r"^\d+\.\d+"
  1213. has_subtitle_format = re.match(prefix_text_pattern, curr_line["text"])
  1214. if has_same_prefix_digit or has_subtitle_format:
  1215. return True
  1216. """
  1217. Title detecting main Process.
  1218. """
  1219. """
  1220. Basic features about the current line.
  1221. """
  1222. curr_line_bbox = curr_line["bbox"]
  1223. curr_line_text = curr_line["text"]
  1224. curr_line_font_type, curr_line_font_size = __compute_line_font_type_and_size(curr_line)
  1225. if len(curr_line_text.strip()) == 0: # skip empty lines
  1226. return False, False
  1227. prev_line_bbox = prev_line["bbox"] if prev_line else None
  1228. if prev_line:
  1229. prev_line_font_type, prev_line_font_size = __compute_line_font_type_and_size(prev_line)
  1230. else:
  1231. prev_line_font_type, prev_line_font_size = None, None
  1232. next_line_bbox = next_line["bbox"] if next_line else None
  1233. if next_line:
  1234. next_line_font_type, next_line_font_size = __compute_line_font_type_and_size(next_line)
  1235. else:
  1236. next_line_font_type, next_line_font_size = None, None
  1237. """
  1238. Aggregated features about the current line.
  1239. """
  1240. is_italc_font = __is_italic_font_line(curr_line)
  1241. is_bold_font = __is_bold_font_line(curr_line)
  1242. is_font_size_little_less_than_doc_avg = __is_font_size_not_less_than_docAvg(curr_line_font_size, ratio=0.8)
  1243. is_font_size_not_less_than_doc_avg = __is_font_size_not_less_than_docAvg(curr_line_font_size, ratio=1)
  1244. is_much_larger_font_than_doc_avg = __is_font_size_not_less_than_docAvg(curr_line_font_size, ratio=1.6)
  1245. is_not_same_font_type_of_docAvg = not __is_same_font_type_of_docAvg(curr_line_font_type)
  1246. is_potential_title_font = is_bold_font or is_font_size_not_less_than_doc_avg or is_not_same_font_type_of_docAvg
  1247. is_mix_font_styles_strict = __has_mixed_font_styles(curr_line["spans"], strict_mode=True)
  1248. is_mix_font_styles_loose = __has_mixed_font_styles(curr_line["spans"], strict_mode=False)
  1249. is_punctuation_heavy = __is_punctuation_heavy(curr_line_text)
  1250. is_word_list_line_by_rules = __is_word_list_line_by_rules(curr_line_text)
  1251. is_person_or_org_list_line_by_nlp = __get_text_catgr_by_nlp(curr_line_text) in ["PERSON", "GPE", "ORG"]
  1252. is_font_size_larger_than_neighbors = __is_larger_font_size_from_neighbors(
  1253. curr_line_font_size, prev_line_font_size, next_line_font_size
  1254. )
  1255. is_font_type_diff_from_neighbors = __is_different_font_type_from_neighbors(
  1256. curr_line_font_type, prev_line_font_type, next_line_font_type
  1257. )
  1258. has_sufficient_spaces_above, has_sufficient_spaces_below = __is_sufficient_spacing_above_and_below(
  1259. curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_height, median_font_size
  1260. )
  1261. is_similar_to_pre_line = __is_similar_to_pre_line(
  1262. curr_line_font_type, prev_line_font_type, curr_line_font_size, prev_line_font_size
  1263. )
  1264. is_consis_sub_title = __is_a_consistent_sub_title(prev_line, curr_line)
  1265. """
  1266. Further aggregated features about the current line.
  1267. Attention:
  1268. Features that start with __ are for internal use.
  1269. """
  1270. __is_line_left_aligned_from_neighbors = is_line_left_aligned_from_neighbors(
  1271. curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width
  1272. )
  1273. __is_font_diff_from_neighbors = is_font_size_larger_than_neighbors or is_font_type_diff_from_neighbors
  1274. is_a_left_inline_title = (
  1275. is_mix_font_styles_strict and __is_line_left_aligned_from_neighbors and __is_font_diff_from_neighbors
  1276. )
  1277. is_title_by_check_prev_line = prev_line is None and has_sufficient_spaces_above and is_potential_title_font
  1278. is_title_by_check_next_line = next_line is None and has_sufficient_spaces_below and is_potential_title_font
  1279. is_title_by_check_pre_and_next_line = (
  1280. (prev_line is not None or next_line is not None)
  1281. and has_sufficient_spaces_above
  1282. and has_sufficient_spaces_below
  1283. and is_potential_title_font
  1284. )
  1285. is_numbered_title = __is_numbered_title(curr_line_text) and (
  1286. (has_sufficient_spaces_above or prev_line is None) and (has_sufficient_spaces_below or next_line is None)
  1287. )
  1288. is_not_end_with_ending_puncs = not __is_end_with_ending_puncs(curr_line_text)
  1289. is_not_only_no_meaning_symbols = not __contains_only_no_meaning_symbols(curr_line_text)
  1290. is_equation = __is_equation(curr_line_text)
  1291. is_title_by_len = __is_title_by_len(curr_line_text)
  1292. """
  1293. Decide if the line is a title.
  1294. """
  1295. is_title = (
  1296. is_not_end_with_ending_puncs # not end with ending punctuation marks
  1297. and is_not_only_no_meaning_symbols # not only have no meaning symbols
  1298. and is_title_by_len # is a title by length, default max length is 200
  1299. and not is_equation # an interline equation should never be a title
  1300. and is_potential_title_font # is a potential title font, which is bold or larger than the document average font size or not the same font type as the document average font type
  1301. and (
  1302. (is_not_same_font_type_of_docAvg and is_font_size_not_less_than_doc_avg)
  1303. or (is_bold_font and is_much_larger_font_than_doc_avg and is_not_same_font_type_of_docAvg)
  1304. or (
  1305. is_much_larger_font_than_doc_avg
  1306. and (is_title_by_check_prev_line or is_title_by_check_next_line or is_title_by_check_pre_and_next_line)
  1307. )
  1308. or (
  1309. is_font_size_little_less_than_doc_avg
  1310. and is_bold_font
  1311. and (is_title_by_check_prev_line or is_title_by_check_next_line or is_title_by_check_pre_and_next_line)
  1312. )
  1313. ) # Consider the following situations: bold font, much larger font than doc avg, not same font type as doc avg, sufficient spacing above and below
  1314. and (
  1315. (
  1316. not is_person_or_org_list_line_by_nlp
  1317. and (
  1318. is_much_larger_font_than_doc_avg
  1319. or (is_not_same_font_type_of_docAvg and is_font_size_not_less_than_doc_avg)
  1320. )
  1321. )
  1322. or (
  1323. not (is_word_list_line_by_rules and is_person_or_org_list_line_by_nlp)
  1324. and not is_a_left_inline_title
  1325. and not is_punctuation_heavy
  1326. and (is_title_by_check_prev_line or is_title_by_check_next_line or is_title_by_check_pre_and_next_line)
  1327. )
  1328. or (
  1329. is_person_or_org_list_line_by_nlp
  1330. and (is_bold_font and is_much_larger_font_than_doc_avg and is_not_same_font_type_of_docAvg)
  1331. and (is_bold_font and is_much_larger_font_than_doc_avg and is_not_same_font_type_of_docAvg)
  1332. )
  1333. or (is_numbered_title and not is_a_left_inline_title)
  1334. ) # Exclude the following situations: person/org list
  1335. )
  1336. # ) or (prev_line_is_title and is_consis_sub_title)
  1337. is_name_or_org_list_to_be_removed = (
  1338. (is_person_or_org_list_line_by_nlp)
  1339. and is_punctuation_heavy
  1340. and (is_title_by_check_prev_line or is_title_by_check_next_line or is_title_by_check_pre_and_next_line)
  1341. ) and not is_title
  1342. if is_name_or_org_list_to_be_removed:
  1343. is_author_or_org_list = True
  1344. else:
  1345. is_author_or_org_list = False
  1346. # return is_title, is_author_or_org_list
  1347. """
  1348. # print reason why the line is a title
  1349. if is_title:
  1350. print_green("This line is a title.")
  1351. print_green("↓" * 10)
  1352. print()
  1353. print("curr_line_text: ", curr_line_text)
  1354. print()
  1355. # print reason why the line is not a title
  1356. line_text = curr_line_text.strip()
  1357. test_text = "Career/Personal Life"
  1358. text_content_condition = line_text == test_text
  1359. if not is_title and text_content_condition: # Print specific line
  1360. # if not is_title: # Print each line
  1361. print_red("This line is not a title.")
  1362. print_red("↓" * 10)
  1363. print()
  1364. print("curr_line_text: ", curr_line_text)
  1365. print()
  1366. if is_not_end_with_ending_puncs:
  1367. print_green(f"is_not_end_with_ending_puncs")
  1368. else:
  1369. print_red(f"is_end_with_ending_puncs")
  1370. if is_not_only_no_meaning_symbols:
  1371. print_green(f"is_not_only_no_meaning_symbols")
  1372. else:
  1373. print_red(f"is_only_no_meaning_symbols")
  1374. if is_title_by_len:
  1375. print_green(f"is_title_by_len: {is_title_by_len}")
  1376. else:
  1377. print_red(f"is_not_title_by_len: {is_title_by_len}")
  1378. if is_equation:
  1379. print_red(f"is_equation")
  1380. else:
  1381. print_green(f"is_not_equation")
  1382. if is_potential_title_font:
  1383. print_green(f"is_potential_title_font")
  1384. else:
  1385. print_red(f"is_not_potential_title_font")
  1386. if is_punctuation_heavy:
  1387. print_red("is_punctuation_heavy")
  1388. else:
  1389. print_green("is_not_punctuation_heavy")
  1390. if is_bold_font:
  1391. print_green(f"is_bold_font")
  1392. else:
  1393. print_red(f"is_not_bold_font")
  1394. if is_font_size_not_less_than_doc_avg:
  1395. print_green(f"is_larger_font_than_doc_avg")
  1396. else:
  1397. print_red(f"is_not_larger_font_than_doc_avg")
  1398. if is_much_larger_font_than_doc_avg:
  1399. print_green(f"is_much_larger_font_than_doc_avg")
  1400. else:
  1401. print_red(f"is_not_much_larger_font_than_doc_avg")
  1402. if is_not_same_font_type_of_docAvg:
  1403. print_green(f"is_not_same_font_type_of_docAvg")
  1404. else:
  1405. print_red(f"is_same_font_type_of_docAvg")
  1406. if is_word_list_line_by_rules:
  1407. print_red("is_word_list_line_by_rules")
  1408. else:
  1409. print_green("is_not_name_list_by_rules")
  1410. if is_person_or_org_list_line_by_nlp:
  1411. print_red("is_person_or_org_list_line_by_nlp")
  1412. else:
  1413. print_green("is_not_person_or_org_list_line_by_nlp")
  1414. if not is_numbered_title:
  1415. print_red("is_not_numbered_title")
  1416. else:
  1417. print_green("is_numbered_title")
  1418. if is_a_left_inline_title:
  1419. print_red("is_a_left_inline_title")
  1420. else:
  1421. print_green("is_not_a_left_inline_title")
  1422. if not is_title_by_check_prev_line:
  1423. print_red("is_not_title_by_check_prev_line")
  1424. else:
  1425. print_green("is_title_by_check_prev_line")
  1426. if not is_title_by_check_next_line:
  1427. print_red("is_not_title_by_check_next_line")
  1428. else:
  1429. print_green("is_title_by_check_next_line")
  1430. if not is_title_by_check_pre_and_next_line:
  1431. print_red("is_not_title_by_check_pre_and_next_line")
  1432. else:
  1433. print_green("is_title_by_check_pre_and_next_line")
  1434. # print_green("Common features:")
  1435. # print_green("↓" * 10)
  1436. # print(f" curr_line_font_type: {curr_line_font_type}")
  1437. # print(f" curr_line_font_size: {curr_line_font_size}")
  1438. # print()
  1439. """
  1440. return is_title, is_author_or_org_list
  1441. def _detect_title(self, input_block):
  1442. """
  1443. Use the functions 'is_potential_title' to detect titles of each paragraph block.
  1444. If a line is a title, then the value of key 'is_title' of the line will be set to True.
  1445. """
  1446. raw_lines = input_block["lines"]
  1447. prev_line_is_title_flag = False
  1448. for i, curr_line in enumerate(raw_lines):
  1449. prev_line = raw_lines[i - 1] if i > 0 else None
  1450. next_line = raw_lines[i + 1] if i < len(raw_lines) - 1 else None
  1451. blk_avg_char_width = input_block["avg_char_width"]
  1452. blk_avg_char_height = input_block["avg_char_height"]
  1453. blk_media_font_size = input_block["median_font_size"]
  1454. is_title, is_author_or_org_list = self._is_potential_title(
  1455. curr_line,
  1456. prev_line,
  1457. prev_line_is_title_flag,
  1458. next_line,
  1459. blk_avg_char_width,
  1460. blk_avg_char_height,
  1461. blk_media_font_size,
  1462. )
  1463. if is_title:
  1464. curr_line["is_title"] = is_title
  1465. prev_line_is_title_flag = True
  1466. else:
  1467. curr_line["is_title"] = False
  1468. prev_line_is_title_flag = False
  1469. # print(f"curr_line['text']: {curr_line['text']}")
  1470. # print(f"curr_line['is_title']: {curr_line['is_title']}")
  1471. # print(f"prev_line['text']: {prev_line['text'] if prev_line else None}")
  1472. # print(f"prev_line_is_title_flag: {prev_line_is_title_flag}")
  1473. # print()
  1474. if is_author_or_org_list:
  1475. curr_line["is_author_or_org_list"] = is_author_or_org_list
  1476. else:
  1477. curr_line["is_author_or_org_list"] = False
  1478. return input_block
  1479. def batch_detect_titles(self, pdf_dic):
  1480. """
  1481. This function batch process the blocks to detect titles.
  1482. Parameters
  1483. ----------
  1484. pdf_dict : dict
  1485. result dictionary
  1486. Returns
  1487. -------
  1488. pdf_dict : dict
  1489. result dictionary
  1490. """
  1491. num_titles = 0
  1492. for page_id, blocks in pdf_dic.items():
  1493. if page_id.startswith("page_"):
  1494. para_blocks = []
  1495. if "para_blocks" in blocks.keys():
  1496. para_blocks = blocks["para_blocks"]
  1497. all_single_line_blocks = []
  1498. for block in para_blocks:
  1499. if len(block["lines"]) == 1:
  1500. all_single_line_blocks.append(block)
  1501. new_para_blocks = []
  1502. if not len(all_single_line_blocks) == len(para_blocks): # Not all blocks are single line blocks.
  1503. for para_block in para_blocks:
  1504. new_block = self._detect_title(para_block)
  1505. new_para_blocks.append(new_block)
  1506. num_titles += sum([line.get("is_title", 0) for line in new_block["lines"]])
  1507. else: # All blocks are single line blocks.
  1508. for para_block in para_blocks:
  1509. new_para_blocks.append(para_block)
  1510. num_titles += sum([line.get("is_title", 0) for line in para_block["lines"]])
  1511. para_blocks = new_para_blocks
  1512. blocks["para_blocks"] = para_blocks
  1513. for para_block in para_blocks:
  1514. all_titles = all(safe_get(line, "is_title", False) for line in para_block["lines"])
  1515. para_text_len = sum([len(line["text"]) for line in para_block["lines"]])
  1516. if (
  1517. all_titles and para_text_len < 200
  1518. ): # total length of the paragraph is less than 200, more than this should not be a title
  1519. para_block["is_block_title"] = 1
  1520. else:
  1521. para_block["is_block_title"] = 0
  1522. all_name_or_org_list_to_be_removed = all(
  1523. safe_get(line, "is_author_or_org_list", False) for line in para_block["lines"]
  1524. )
  1525. if all_name_or_org_list_to_be_removed and page_id == "page_0":
  1526. para_block["is_block_an_author_or_org_list"] = 1
  1527. else:
  1528. para_block["is_block_an_author_or_org_list"] = 0
  1529. pdf_dic["statistics"]["num_titles"] = num_titles
  1530. return pdf_dic
  1531. def _recog_title_level(self, title_blocks):
  1532. """
  1533. This function determines the title level based on the font size of the title.
  1534. Parameters
  1535. ----------
  1536. title_blocks : list
  1537. Returns
  1538. -------
  1539. title_blocks : list
  1540. """
  1541. font_sizes = np.array([safe_get(tb["block"], "block_font_size", 0) for tb in title_blocks])
  1542. # Use the mean and std of font sizes to remove extreme values
  1543. mean_font_size = np.mean(font_sizes)
  1544. std_font_size = np.std(font_sizes)
  1545. min_extreme_font_size = mean_font_size - std_font_size # type: ignore
  1546. max_extreme_font_size = mean_font_size + std_font_size # type: ignore
  1547. # Compute the threshold for title level
  1548. middle_font_sizes = font_sizes[(font_sizes > min_extreme_font_size) & (font_sizes < max_extreme_font_size)]
  1549. if middle_font_sizes.size > 0:
  1550. middle_mean_font_size = np.mean(middle_font_sizes)
  1551. level_threshold = middle_mean_font_size
  1552. else:
  1553. level_threshold = mean_font_size
  1554. for tb in title_blocks:
  1555. title_block = tb["block"]
  1556. title_font_size = safe_get(title_block, "block_font_size", 0)
  1557. current_level = 1 # Initialize title level, the biggest level is 1
  1558. # print(f"Before adjustment by font size, {current_level}")
  1559. if title_font_size >= max_extreme_font_size:
  1560. current_level = 1
  1561. elif title_font_size <= min_extreme_font_size:
  1562. current_level = 3
  1563. elif float(title_font_size) >= float(level_threshold):
  1564. current_level = 2
  1565. else:
  1566. current_level = 3
  1567. # print(f"After adjustment by font size, {current_level}")
  1568. title_block["block_title_level"] = current_level
  1569. return title_blocks
  1570. def batch_recog_title_level(self, pdf_dic):
  1571. """
  1572. This function batch process the blocks to recognize title level.
  1573. Parameters
  1574. ----------
  1575. pdf_dict : dict
  1576. result dictionary
  1577. Returns
  1578. -------
  1579. pdf_dict : dict
  1580. result dictionary
  1581. """
  1582. title_blocks = []
  1583. # Collect all titles
  1584. for page_id, blocks in pdf_dic.items():
  1585. if page_id.startswith("page_"):
  1586. para_blocks = blocks.get("para_blocks", [])
  1587. for block in para_blocks:
  1588. if block.get("is_block_title"):
  1589. title_obj = {"page_id": page_id, "block": block}
  1590. title_blocks.append(title_obj)
  1591. # Determine title level
  1592. if title_blocks:
  1593. # Determine title level based on font size
  1594. title_blocks = self._recog_title_level(title_blocks)
  1595. return pdf_dic
  1596. class BlockTerminationProcessor:
  1597. """
  1598. This class is used to process the block termination.
  1599. """
  1600. def __init__(self) -> None:
  1601. pass
  1602. def _is_consistent_lines(
  1603. self,
  1604. curr_line,
  1605. prev_line,
  1606. next_line,
  1607. consistent_direction, # 0 for prev, 1 for next, 2 for both
  1608. ):
  1609. """
  1610. This function checks if the line is consistent with its neighbors
  1611. Parameters
  1612. ----------
  1613. curr_line : dict
  1614. current line
  1615. prev_line : dict
  1616. previous line
  1617. next_line : dict
  1618. next line
  1619. consistent_direction : int
  1620. 0 for prev, 1 for next, 2 for both
  1621. Returns
  1622. -------
  1623. bool
  1624. True if the line is consistent with its neighbors, False otherwise.
  1625. """
  1626. curr_line_font_size = curr_line["spans"][0]["size"]
  1627. curr_line_font_type = curr_line["spans"][0]["font"].lower()
  1628. if consistent_direction == 0:
  1629. if prev_line:
  1630. prev_line_font_size = prev_line["spans"][0]["size"]
  1631. prev_line_font_type = prev_line["spans"][0]["font"].lower()
  1632. return curr_line_font_size == prev_line_font_size and curr_line_font_type == prev_line_font_type
  1633. else:
  1634. return False
  1635. elif consistent_direction == 1:
  1636. if next_line:
  1637. next_line_font_size = next_line["spans"][0]["size"]
  1638. next_line_font_type = next_line["spans"][0]["font"].lower()
  1639. return curr_line_font_size == next_line_font_size and curr_line_font_type == next_line_font_type
  1640. else:
  1641. return False
  1642. elif consistent_direction == 2:
  1643. if prev_line and next_line:
  1644. prev_line_font_size = prev_line["spans"][0]["size"]
  1645. prev_line_font_type = prev_line["spans"][0]["font"].lower()
  1646. next_line_font_size = next_line["spans"][0]["size"]
  1647. next_line_font_type = next_line["spans"][0]["font"].lower()
  1648. return (curr_line_font_size == prev_line_font_size and curr_line_font_type == prev_line_font_type) and (
  1649. curr_line_font_size == next_line_font_size and curr_line_font_type == next_line_font_type
  1650. )
  1651. else:
  1652. return False
  1653. else:
  1654. return False
  1655. def _is_regular_line(self, curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, X0, X1, avg_line_height):
  1656. """
  1657. This function checks if the line is a regular line
  1658. Parameters
  1659. ----------
  1660. curr_line_bbox : list
  1661. bbox of the current line
  1662. prev_line_bbox : list
  1663. bbox of the previous line
  1664. next_line_bbox : list
  1665. bbox of the next line
  1666. avg_char_width : float
  1667. average of char widths
  1668. X0 : float
  1669. median of x0 values, which represents the left average boundary of the page
  1670. X1 : float
  1671. median of x1 values, which represents the right average boundary of the page
  1672. avg_line_height : float
  1673. average of line heights
  1674. Returns
  1675. -------
  1676. bool
  1677. True if the line is a regular line, False otherwise.
  1678. """
  1679. horizontal_ratio = 0.5
  1680. vertical_ratio = 0.5
  1681. horizontal_thres = horizontal_ratio * avg_char_width
  1682. vertical_thres = vertical_ratio * avg_line_height
  1683. x0, y0, x1, y1 = curr_line_bbox
  1684. x0_near_X0 = abs(x0 - X0) < horizontal_thres
  1685. x1_near_X1 = abs(x1 - X1) < horizontal_thres
  1686. prev_line_is_end_of_para = prev_line_bbox and (abs(prev_line_bbox[2] - X1) > avg_char_width)
  1687. sufficient_spacing_above = False
  1688. if prev_line_bbox:
  1689. vertical_spacing_above = y1 - prev_line_bbox[3]
  1690. sufficient_spacing_above = vertical_spacing_above > vertical_thres
  1691. sufficient_spacing_below = False
  1692. if next_line_bbox:
  1693. vertical_spacing_below = next_line_bbox[1] - y0
  1694. sufficient_spacing_below = vertical_spacing_below > vertical_thres
  1695. return (
  1696. (sufficient_spacing_above or sufficient_spacing_below)
  1697. or (not x0_near_X0 and not x1_near_X1)
  1698. or prev_line_is_end_of_para
  1699. )
  1700. def _is_possible_start_of_para(self, curr_line, prev_line, next_line, X0, X1, avg_char_width, avg_font_size):
  1701. """
  1702. This function checks if the line is a possible start of a paragraph
  1703. Parameters
  1704. ----------
  1705. curr_line : dict
  1706. current line
  1707. prev_line : dict
  1708. previous line
  1709. next_line : dict
  1710. next line
  1711. X0 : float
  1712. median of x0 values, which represents the left average boundary of the page
  1713. X1 : float
  1714. median of x1 values, which represents the right average boundary of the page
  1715. avg_char_width : float
  1716. average of char widths
  1717. avg_line_height : float
  1718. average of line heights
  1719. Returns
  1720. -------
  1721. bool
  1722. True if the line is a possible start of a paragraph, False otherwise.
  1723. """
  1724. start_confidence = 0.5 # Initial confidence of the line being a start of a paragraph
  1725. decision_path = [] # Record the decision path
  1726. curr_line_bbox = curr_line["bbox"]
  1727. prev_line_bbox = prev_line["bbox"] if prev_line else None
  1728. next_line_bbox = next_line["bbox"] if next_line else None
  1729. indent_ratio = 1
  1730. vertical_ratio = 1.5
  1731. vertical_thres = vertical_ratio * avg_font_size
  1732. left_horizontal_ratio = 0.5
  1733. left_horizontal_thres = left_horizontal_ratio * avg_char_width
  1734. right_horizontal_ratio = 2.5
  1735. right_horizontal_thres = right_horizontal_ratio * avg_char_width
  1736. x0, y0, x1, y1 = curr_line_bbox
  1737. indent_condition = x0 > X0 + indent_ratio * avg_char_width
  1738. if indent_condition:
  1739. start_confidence += 0.2
  1740. decision_path.append("indent_condition_met")
  1741. x0_near_X0 = abs(x0 - X0) < left_horizontal_thres
  1742. if x0_near_X0:
  1743. start_confidence += 0.1
  1744. decision_path.append("x0_near_X0")
  1745. x1_near_X1 = abs(x1 - X1) < right_horizontal_thres
  1746. if x1_near_X1:
  1747. start_confidence += 0.1
  1748. decision_path.append("x1_near_X1")
  1749. if prev_line is None:
  1750. prev_line_is_end_of_para = True
  1751. start_confidence += 0.2
  1752. decision_path.append("no_prev_line")
  1753. else:
  1754. prev_line_is_end_of_para, _, _ = self._is_possible_end_of_para(prev_line, next_line, X0, X1, avg_char_width)
  1755. if prev_line_is_end_of_para:
  1756. start_confidence += 0.1
  1757. decision_path.append("prev_line_is_end_of_para")
  1758. sufficient_spacing_above = False
  1759. if prev_line_bbox:
  1760. vertical_spacing_above = y1 - prev_line_bbox[3]
  1761. sufficient_spacing_above = vertical_spacing_above > vertical_thres
  1762. if sufficient_spacing_above:
  1763. start_confidence += 0.2
  1764. decision_path.append("sufficient_spacing_above")
  1765. sufficient_spacing_below = False
  1766. if next_line_bbox:
  1767. vertical_spacing_below = next_line_bbox[1] - y0
  1768. sufficient_spacing_below = vertical_spacing_below > vertical_thres
  1769. if sufficient_spacing_below:
  1770. start_confidence += 0.2
  1771. decision_path.append("sufficient_spacing_below")
  1772. is_regular_line = self._is_regular_line(
  1773. curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, X0, X1, avg_font_size
  1774. )
  1775. if is_regular_line:
  1776. start_confidence += 0.1
  1777. decision_path.append("is_regular_line")
  1778. is_start_of_para = (
  1779. (sufficient_spacing_above or sufficient_spacing_below)
  1780. or (indent_condition)
  1781. or (not indent_condition and x0_near_X0 and x1_near_X1 and not is_regular_line)
  1782. or prev_line_is_end_of_para
  1783. )
  1784. return (is_start_of_para, start_confidence, decision_path)
  1785. def _is_possible_end_of_para(self, curr_line, next_line, X0, X1, avg_char_width):
  1786. """
  1787. This function checks if the line is a possible end of a paragraph
  1788. Parameters
  1789. ----------
  1790. curr_line : dict
  1791. current line
  1792. next_line : dict
  1793. next line
  1794. X0 : float
  1795. median of x0 values, which represents the left average boundary of the page
  1796. X1 : float
  1797. median of x1 values, which represents the right average boundary of the page
  1798. avg_char_width : float
  1799. average of char widths
  1800. Returns
  1801. -------
  1802. bool
  1803. True if the line is a possible end of a paragraph, False otherwise.
  1804. """
  1805. end_confidence = 0.5 # Initial confidence of the line being a end of a paragraph
  1806. decision_path = [] # Record the decision path
  1807. curr_line_bbox = curr_line["bbox"]
  1808. next_line_bbox = next_line["bbox"] if next_line else None
  1809. left_horizontal_ratio = 0.5
  1810. right_horizontal_ratio = 0.5
  1811. x0, _, x1, y1 = curr_line_bbox
  1812. next_x0, next_y0, _, _ = next_line_bbox if next_line_bbox else (0, 0, 0, 0)
  1813. x0_near_X0 = abs(x0 - X0) < left_horizontal_ratio * avg_char_width
  1814. if x0_near_X0:
  1815. end_confidence += 0.1
  1816. decision_path.append("x0_near_X0")
  1817. x1_smaller_than_X1 = x1 < X1 - right_horizontal_ratio * avg_char_width
  1818. if x1_smaller_than_X1:
  1819. end_confidence += 0.1
  1820. decision_path.append("x1_smaller_than_X1")
  1821. next_line_is_start_of_para = (
  1822. next_line_bbox
  1823. and (next_x0 > X0 + left_horizontal_ratio * avg_char_width)
  1824. and (not is_line_left_aligned_from_neighbors(curr_line_bbox, None, next_line_bbox, avg_char_width, direction=1))
  1825. )
  1826. if next_line_is_start_of_para:
  1827. end_confidence += 0.2
  1828. decision_path.append("next_line_is_start_of_para")
  1829. is_line_left_aligned_from_neighbors_bool = is_line_left_aligned_from_neighbors(
  1830. curr_line_bbox, None, next_line_bbox, avg_char_width
  1831. )
  1832. if is_line_left_aligned_from_neighbors_bool:
  1833. end_confidence += 0.1
  1834. decision_path.append("line_is_left_aligned_from_neighbors")
  1835. is_line_right_aligned_from_neighbors_bool = is_line_right_aligned_from_neighbors(
  1836. curr_line_bbox, None, next_line_bbox, avg_char_width
  1837. )
  1838. if not is_line_right_aligned_from_neighbors_bool:
  1839. end_confidence += 0.1
  1840. decision_path.append("line_is_not_right_aligned_from_neighbors")
  1841. is_end_of_para = end_with_punctuation(curr_line["text"]) and (
  1842. (x0_near_X0 and x1_smaller_than_X1)
  1843. or (is_line_left_aligned_from_neighbors_bool and not is_line_right_aligned_from_neighbors_bool)
  1844. )
  1845. return (is_end_of_para, end_confidence, decision_path)
  1846. def _cut_paras_per_block(
  1847. self,
  1848. block,
  1849. ):
  1850. """
  1851. Processes a raw block from PyMuPDF and returns the processed block.
  1852. Parameters
  1853. ----------
  1854. raw_block : dict
  1855. A raw block from pymupdf.
  1856. Returns
  1857. -------
  1858. processed_block : dict
  1859. """
  1860. def _construct_para(lines, is_block_title, para_title_level):
  1861. """
  1862. Construct a paragraph from given lines.
  1863. """
  1864. font_sizes = [span["size"] for line in lines for span in line["spans"]]
  1865. avg_font_size = sum(font_sizes) / len(font_sizes) if font_sizes else 0
  1866. font_colors = [span["color"] for line in lines for span in line["spans"]]
  1867. most_common_font_color = max(set(font_colors), key=font_colors.count) if font_colors else None
  1868. font_type_lengths = {}
  1869. for line in lines:
  1870. for span in line["spans"]:
  1871. font_type = span["font"]
  1872. bbox_width = span["bbox"][2] - span["bbox"][0]
  1873. if font_type in font_type_lengths:
  1874. font_type_lengths[font_type] += bbox_width
  1875. else:
  1876. font_type_lengths[font_type] = bbox_width
  1877. # get the font type with the longest bbox width
  1878. most_common_font_type = max(font_type_lengths, key=font_type_lengths.get) if font_type_lengths else None # type: ignore
  1879. para_bbox = calculate_para_bbox(lines)
  1880. para_text = " ".join(line["text"] for line in lines)
  1881. return {
  1882. "para_bbox": para_bbox,
  1883. "para_text": para_text,
  1884. "para_font_type": most_common_font_type,
  1885. "para_font_size": avg_font_size,
  1886. "para_font_color": most_common_font_color,
  1887. "is_para_title": is_block_title,
  1888. "para_title_level": para_title_level,
  1889. }
  1890. block_bbox = block["bbox"]
  1891. block_text = block["text"]
  1892. block_lines = block["lines"]
  1893. X0 = safe_get(block, "X0", 0)
  1894. X1 = safe_get(block, "X1", 0)
  1895. avg_char_width = safe_get(block, "avg_char_width", 0)
  1896. avg_char_height = safe_get(block, "avg_char_height", 0)
  1897. avg_font_size = safe_get(block, "avg_font_size", 0)
  1898. is_block_title = safe_get(block, "is_block_title", False)
  1899. para_title_level = safe_get(block, "block_title_level", 0)
  1900. # Segment into paragraphs
  1901. para_ranges = []
  1902. in_paragraph = False
  1903. start_idx_of_para = None
  1904. # Create the processed paragraphs
  1905. processed_paras = {}
  1906. para_bboxes = []
  1907. end_idx_of_para = 0
  1908. for line_index, line in enumerate(block_lines):
  1909. curr_line = line
  1910. prev_line = block_lines[line_index - 1] if line_index > 0 else None
  1911. next_line = block_lines[line_index + 1] if line_index < len(block_lines) - 1 else None
  1912. """
  1913. Start processing paragraphs.
  1914. """
  1915. # Check if the line is the start of a paragraph
  1916. is_start_of_para, start_confidence, decision_path = self._is_possible_start_of_para(
  1917. curr_line, prev_line, next_line, X0, X1, avg_char_width, avg_font_size
  1918. )
  1919. if not in_paragraph and is_start_of_para:
  1920. in_paragraph = True
  1921. start_idx_of_para = line_index
  1922. # print_green(">>> Start of a paragraph")
  1923. # print(" curr_line_text: ", curr_line["text"])
  1924. # print(" start_confidence: ", start_confidence)
  1925. # print(" decision_path: ", decision_path)
  1926. # Check if the line is the end of a paragraph
  1927. is_end_of_para, end_confidence, decision_path = self._is_possible_end_of_para(
  1928. curr_line, next_line, X0, X1, avg_char_width
  1929. )
  1930. if in_paragraph and (is_end_of_para or not next_line):
  1931. para_ranges.append((start_idx_of_para, line_index))
  1932. start_idx_of_para = None
  1933. in_paragraph = False
  1934. # print_red(">>> End of a paragraph")
  1935. # print(" curr_line_text: ", curr_line["text"])
  1936. # print(" end_confidence: ", end_confidence)
  1937. # print(" decision_path: ", decision_path)
  1938. # Add the last paragraph if it is not added
  1939. if in_paragraph and start_idx_of_para is not None:
  1940. para_ranges.append((start_idx_of_para, len(block_lines) - 1))
  1941. # Process the matched paragraphs
  1942. for para_index, (start_idx, end_idx) in enumerate(para_ranges):
  1943. matched_lines = block_lines[start_idx : end_idx + 1]
  1944. para_properties = _construct_para(matched_lines, is_block_title, para_title_level)
  1945. para_key = f"para_{len(processed_paras)}"
  1946. processed_paras[para_key] = para_properties
  1947. para_bboxes.append(para_properties["para_bbox"])
  1948. end_idx_of_para = end_idx + 1
  1949. # Deal with the remaining lines
  1950. if end_idx_of_para < len(block_lines):
  1951. unmatched_lines = block_lines[end_idx_of_para:]
  1952. unmatched_properties = _construct_para(unmatched_lines, is_block_title, para_title_level)
  1953. unmatched_key = f"para_{len(processed_paras)}"
  1954. processed_paras[unmatched_key] = unmatched_properties
  1955. para_bboxes.append(unmatched_properties["para_bbox"])
  1956. block["paras"] = processed_paras
  1957. return block
  1958. def batch_process_blocks(self, pdf_dict):
  1959. """
  1960. Parses the blocks of all pages.
  1961. Parameters
  1962. ----------
  1963. pdf_dict : dict
  1964. PDF dictionary.
  1965. filter_blocks : list
  1966. List of bounding boxes to filter.
  1967. Returns
  1968. -------
  1969. result_dict : dict
  1970. Result dictionary.
  1971. """
  1972. num_paras = 0
  1973. for page_id, page in pdf_dict.items():
  1974. if page_id.startswith("page_"):
  1975. para_blocks = []
  1976. if "para_blocks" in page.keys():
  1977. input_blocks = page["para_blocks"]
  1978. for input_block in input_blocks:
  1979. new_block = self._cut_paras_per_block(input_block)
  1980. para_blocks.append(new_block)
  1981. num_paras += len(new_block["paras"])
  1982. page["para_blocks"] = para_blocks
  1983. pdf_dict["statistics"]["num_paras"] = num_paras
  1984. return pdf_dict
  1985. class BlockContinuationProcessor:
  1986. """
  1987. This class is used to process the blocks to detect block continuations.
  1988. """
  1989. def __init__(self) -> None:
  1990. pass
  1991. def __is_similar_font_type(self, font_type_1, font_type_2, prefix_length_ratio=0.3):
  1992. """
  1993. This function checks if the two font types are similar.
  1994. Definition of similar font types: the two font types have a common prefix,
  1995. and the length of the common prefix is at least a certain ratio of the length of the shorter font type.
  1996. Parameters
  1997. ----------
  1998. font_type1 : str
  1999. font type 1
  2000. font_type2 : str
  2001. font type 2
  2002. prefix_length_ratio : float
  2003. minimum ratio of the common prefix length to the length of the shorter font type
  2004. Returns
  2005. -------
  2006. bool
  2007. True if the two font types are similar, False otherwise.
  2008. """
  2009. if isinstance(font_type_1, list):
  2010. font_type_1 = font_type_1[0] if font_type_1 else ""
  2011. if isinstance(font_type_2, list):
  2012. font_type_2 = font_type_2[0] if font_type_2 else ""
  2013. if font_type_1 == font_type_2:
  2014. return True
  2015. # Find the length of the common prefix
  2016. common_prefix_length = len(os.path.commonprefix([font_type_1, font_type_2]))
  2017. # Calculate the minimum prefix length based on the ratio
  2018. min_prefix_length = int(min(len(font_type_1), len(font_type_2)) * prefix_length_ratio)
  2019. return common_prefix_length >= min_prefix_length
  2020. def __is_same_block_font(self, block_1, block_2):
  2021. """
  2022. This function compares the font of block1 and block2
  2023. Parameters
  2024. ----------
  2025. block1 : dict
  2026. block1
  2027. block2 : dict
  2028. block2
  2029. Returns
  2030. -------
  2031. is_same : bool
  2032. True if block1 and block2 have the same font, else False
  2033. """
  2034. block_1_font_type = safe_get(block_1, "block_font_type", "")
  2035. block_1_font_size = safe_get(block_1, "block_font_size", 0)
  2036. block_1_avg_char_width = safe_get(block_1, "avg_char_width", 0)
  2037. block_2_font_type = safe_get(block_2, "block_font_type", "")
  2038. block_2_font_size = safe_get(block_2, "block_font_size", 0)
  2039. block_2_avg_char_width = safe_get(block_2, "avg_char_width", 0)
  2040. if isinstance(block_1_font_size, list):
  2041. block_1_font_size = block_1_font_size[0] if block_1_font_size else 0
  2042. if isinstance(block_2_font_size, list):
  2043. block_2_font_size = block_2_font_size[0] if block_2_font_size else 0
  2044. block_1_text = safe_get(block_1, "text", "")
  2045. block_2_text = safe_get(block_2, "text", "")
  2046. if block_1_avg_char_width == 0 or block_2_avg_char_width == 0:
  2047. return False
  2048. if not block_1_text or not block_2_text:
  2049. return False
  2050. else:
  2051. text_len_ratio = len(block_2_text) / len(block_1_text)
  2052. if text_len_ratio < 0.2:
  2053. avg_char_width_condition = (
  2054. abs(block_1_avg_char_width - block_2_avg_char_width) / min(block_1_avg_char_width, block_2_avg_char_width)
  2055. < 0.5
  2056. )
  2057. else:
  2058. avg_char_width_condition = (
  2059. abs(block_1_avg_char_width - block_2_avg_char_width) / min(block_1_avg_char_width, block_2_avg_char_width)
  2060. < 0.2
  2061. )
  2062. block_font_size_condition = abs(block_1_font_size - block_2_font_size) < 1
  2063. return (
  2064. self.__is_similar_font_type(block_1_font_type, block_2_font_type)
  2065. and avg_char_width_condition
  2066. and block_font_size_condition
  2067. )
  2068. def _is_alphabet_char(self, char):
  2069. if (char >= "\u0041" and char <= "\u005a") or (char >= "\u0061" and char <= "\u007a"):
  2070. return True
  2071. else:
  2072. return False
  2073. def _is_chinese_char(self, char):
  2074. if char >= "\u4e00" and char <= "\u9fa5":
  2075. return True
  2076. else:
  2077. return False
  2078. def _is_other_letter_char(self, char):
  2079. try:
  2080. cat = unicodedata.category(char)
  2081. if cat == "Lu" or cat == "Ll":
  2082. return not self._is_alphabet_char(char) and not self._is_chinese_char(char)
  2083. except TypeError:
  2084. print("The input to the function must be a single character.")
  2085. return False
  2086. def _is_year(self, s: str):
  2087. try:
  2088. number = int(s)
  2089. return 1900 <= number <= 2099
  2090. except ValueError:
  2091. return False
  2092. def _match_brackets(self, text):
  2093. # pattern = r"^[\(\)\[\]()【】{}{}<><>〔〕〘〙\"\'“”‘’]"
  2094. pattern = r"^[\(\)\]()】{}{}>>〕〙\"\'“”‘’]"
  2095. return bool(re.match(pattern, text))
  2096. def _is_para_font_consistent(self, para_1, para_2):
  2097. """
  2098. This function compares the font of para1 and para2
  2099. Parameters
  2100. ----------
  2101. para1 : dict
  2102. para1
  2103. para2 : dict
  2104. para2
  2105. Returns
  2106. -------
  2107. is_same : bool
  2108. True if para1 and para2 have the same font, else False
  2109. """
  2110. if para_1 is None or para_2 is None:
  2111. return False
  2112. para_1_font_type = safe_get(para_1, "para_font_type", "")
  2113. para_1_font_size = safe_get(para_1, "para_font_size", 0)
  2114. para_1_font_color = safe_get(para_1, "para_font_color", "")
  2115. para_2_font_type = safe_get(para_2, "para_font_type", "")
  2116. para_2_font_size = safe_get(para_2, "para_font_size", 0)
  2117. para_2_font_color = safe_get(para_2, "para_font_color", "")
  2118. if isinstance(para_1_font_type, list): # get the most common font type
  2119. para_1_font_type = max(set(para_1_font_type), key=para_1_font_type.count)
  2120. if isinstance(para_2_font_type, list):
  2121. para_2_font_type = max(set(para_2_font_type), key=para_2_font_type.count)
  2122. if isinstance(para_1_font_size, list): # compute average font type
  2123. para_1_font_size = sum(para_1_font_size) / len(para_1_font_size)
  2124. if isinstance(para_2_font_size, list): # compute average font type
  2125. para_2_font_size = sum(para_2_font_size) / len(para_2_font_size)
  2126. return (
  2127. self.__is_similar_font_type(para_1_font_type, para_2_font_type)
  2128. and abs(para_1_font_size - para_2_font_size) < 1.5
  2129. # and para_font_color1 == para_font_color2
  2130. )
  2131. def _is_para_puncs_consistent(self, para_1, para_2):
  2132. """
  2133. This function determines whether para1 and para2 are originally from the same paragraph by checking the puncs of para1(former) and para2(latter)
  2134. Parameters
  2135. ----------
  2136. para1 : dict
  2137. para1
  2138. para2 : dict
  2139. para2
  2140. Returns
  2141. -------
  2142. is_same : bool
  2143. True if para1 and para2 are from the same paragraph by using the puncs, else False
  2144. """
  2145. para_1_text = safe_get(para_1, "para_text", "").strip()
  2146. para_2_text = safe_get(para_2, "para_text", "").strip()
  2147. para_1_bboxes = safe_get(para_1, "para_bbox", [])
  2148. para_1_font_sizes = safe_get(para_1, "para_font_size", 0)
  2149. para_2_bboxes = safe_get(para_2, "para_bbox", [])
  2150. para_2_font_sizes = safe_get(para_2, "para_font_size", 0)
  2151. # print_yellow(" Features of determine puncs_consistent:")
  2152. # print(f" para_1_text: {para_1_text}")
  2153. # print(f" para_2_text: {para_2_text}")
  2154. # print(f" para_1_bboxes: {para_1_bboxes}")
  2155. # print(f" para_2_bboxes: {para_2_bboxes}")
  2156. # print(f" para_1_font_sizes: {para_1_font_sizes}")
  2157. # print(f" para_2_font_sizes: {para_2_font_sizes}")
  2158. if is_nested_list(para_1_bboxes):
  2159. x0_1, y0_1, x1_1, y1_1 = para_1_bboxes[-1]
  2160. else:
  2161. x0_1, y0_1, x1_1, y1_1 = para_1_bboxes
  2162. if is_nested_list(para_2_bboxes):
  2163. x0_2, y0_2, x1_2, y1_2 = para_2_bboxes[0]
  2164. para_2_font_sizes = para_2_font_sizes[0] # type: ignore
  2165. else:
  2166. x0_2, y0_2, x1_2, y1_2 = para_2_bboxes
  2167. right_align_threshold = 0.5 * (para_1_font_sizes + para_2_font_sizes) * 0.8
  2168. are_two_paras_right_aligned = abs(x1_1 - x1_2) < right_align_threshold
  2169. left_indent_threshold = 0.5 * (para_1_font_sizes + para_2_font_sizes) * 0.8
  2170. is_para1_left_indent_than_papa2 = x0_1 - x0_2 > left_indent_threshold
  2171. is_para2_left_indent_than_papa1 = x0_2 - x0_1 > left_indent_threshold
  2172. # Check if either para_text1 or para_text2 is empty
  2173. if not para_1_text or not para_2_text:
  2174. return False
  2175. # Define the end puncs for a sentence to end and hyphen
  2176. end_puncs = [".", "?", "!", "。", "?", "!", "…"]
  2177. hyphen = ["-", "—"]
  2178. # Check if para_text1 ends with either hyphen or non-end punctuation or spaces
  2179. para_1_end_with_hyphen = para_1_text and para_1_text[-1] in hyphen
  2180. para_1_end_with_end_punc = para_1_text and para_1_text[-1] in end_puncs
  2181. para_1_end_with_space = para_1_text and para_1_text[-1] == " "
  2182. para_1_not_end_with_end_punc = para_1_text and para_1_text[-1] not in end_puncs
  2183. # print_yellow(f" para_1_end_with_hyphen: {para_1_end_with_hyphen}")
  2184. # print_yellow(f" para_1_end_with_end_punc: {para_1_end_with_end_punc}")
  2185. # print_yellow(f" para_1_not_end_with_end_punc: {para_1_not_end_with_end_punc}")
  2186. # print_yellow(f" para_1_end_with_space: {para_1_end_with_space}")
  2187. if para_1_end_with_hyphen: # If para_text1 ends with hyphen
  2188. # print_red(f"para_1 is end with hyphen.")
  2189. para_2_is_consistent = para_2_text and (
  2190. para_2_text[0] in hyphen
  2191. or (self._is_alphabet_char(para_2_text[0]) and para_2_text[0].islower())
  2192. or (self._is_chinese_char(para_2_text[0]))
  2193. or (self._is_other_letter_char(para_2_text[0]))
  2194. )
  2195. if para_2_is_consistent:
  2196. # print(f"para_2 is consistent.\n")
  2197. return True
  2198. else:
  2199. # print(f"para_2 is not consistent.\n")
  2200. pass
  2201. elif para_1_end_with_end_punc: # If para_text1 ends with ending punctuations
  2202. # print_red(f"para_1 is end with end_punc.")
  2203. para_2_is_consistent = (
  2204. para_2_text
  2205. and (
  2206. para_2_text[0]
  2207. == " "
  2208. # or (self._is_alphabet_char(para_2_text[0]) and para_2_text[0].isupper())
  2209. # or (self._is_chinese_char(para_2_text[0]))
  2210. # or (self._is_other_letter_char(para_2_text[0]))
  2211. )
  2212. and not is_para2_left_indent_than_papa1
  2213. )
  2214. if para_2_is_consistent:
  2215. # print(f"para_2 is consistent.\n")
  2216. return True
  2217. else:
  2218. # print(f"para_2 is not consistent.\n")
  2219. pass
  2220. elif para_1_not_end_with_end_punc: # If para_text1 is not end with ending punctuations
  2221. # print_red(f"para_1 is NOT end with end_punc.")
  2222. para_2_is_consistent = para_2_text and (
  2223. para_2_text[0] == " "
  2224. or (self._is_alphabet_char(para_2_text[0]) and para_2_text[0].islower())
  2225. or (self._is_alphabet_char(para_2_text[0]))
  2226. or (self._is_year(para_2_text[0:4]))
  2227. or (are_two_paras_right_aligned or is_para1_left_indent_than_papa2)
  2228. or (self._is_chinese_char(para_2_text[0]))
  2229. or (self._is_other_letter_char(para_2_text[0]))
  2230. or (self._match_brackets(para_2_text[0]))
  2231. )
  2232. if para_2_is_consistent:
  2233. # print(f"para_2 is consistent.\n")
  2234. return True
  2235. else:
  2236. # print(f"para_2 is not consistent.\n")
  2237. pass
  2238. elif para_1_end_with_space: # If para_text1 ends with space
  2239. # print_red(f"para_1 is end with space.")
  2240. para_2_is_consistent = para_2_text and (
  2241. para_2_text[0] == " "
  2242. or (self._is_alphabet_char(para_2_text[0]) and para_2_text[0].islower())
  2243. or (self._is_chinese_char(para_2_text[0]))
  2244. or (self._is_other_letter_char(para_2_text[0]))
  2245. )
  2246. if para_2_is_consistent:
  2247. # print(f"para_2 is consistent.\n")
  2248. return True
  2249. else:
  2250. pass
  2251. # print(f"para_2 is not consistent.\n")
  2252. return False
  2253. def _is_block_consistent(self, block_1, block_2):
  2254. """
  2255. This function determines whether block1 and block2 are originally from the same block
  2256. Parameters
  2257. ----------
  2258. block1 : dict
  2259. block1s
  2260. block2 : dict
  2261. block2
  2262. Returns
  2263. -------
  2264. is_same : bool
  2265. True if block1 and block2 are from the same block, else False
  2266. """
  2267. return self.__is_same_block_font(block_1, block_2)
  2268. def _is_para_continued(self, para_1, para_2):
  2269. """
  2270. This function determines whether para1 and para2 are originally from the same paragraph
  2271. Parameters
  2272. ----------
  2273. para1 : dict
  2274. para1
  2275. para2 : dict
  2276. para2
  2277. Returns
  2278. -------
  2279. is_same : bool
  2280. True if para1 and para2 are from the same paragraph, else False
  2281. """
  2282. is_para_font_consistent = self._is_para_font_consistent(para_1, para_2)
  2283. is_para_puncs_consistent = self._is_para_puncs_consistent(para_1, para_2)
  2284. return is_para_font_consistent and is_para_puncs_consistent
  2285. def _are_boundaries_of_block_consistent(self, block_1, block_2):
  2286. """
  2287. This function checks if the boundaries of block1 and block2 are consistent
  2288. Parameters
  2289. ----------
  2290. block1 : dict
  2291. block1
  2292. block2 : dict
  2293. block2
  2294. Returns
  2295. -------
  2296. is_consistent : bool
  2297. True if the boundaries of block1 and block2 are consistent, else False
  2298. """
  2299. last_line_of_block_1 = block_1["lines"][-1]
  2300. first_line_of_block_2 = block_2["lines"][0]
  2301. spans_of_last_line_of_block_1 = last_line_of_block_1["spans"]
  2302. spans_of_first_line_of_block_2 = first_line_of_block_2["spans"]
  2303. font_type_of_last_line_of_block_1 = spans_of_last_line_of_block_1[0]["font"].lower()
  2304. font_size_of_last_line_of_block_1 = spans_of_last_line_of_block_1[0]["size"]
  2305. font_color_of_last_line_of_block_1 = spans_of_last_line_of_block_1[0]["color"]
  2306. font_flags_of_last_line_of_block_1 = spans_of_last_line_of_block_1[0]["flags"]
  2307. font_type_of_first_line_of_block_2 = spans_of_first_line_of_block_2[0]["font"].lower()
  2308. font_size_of_first_line_of_block_2 = spans_of_first_line_of_block_2[0]["size"]
  2309. font_color_of_first_line_of_block_2 = spans_of_first_line_of_block_2[0]["color"]
  2310. font_flags_of_first_line_of_block_2 = spans_of_first_line_of_block_2[0]["flags"]
  2311. return (
  2312. self.__is_similar_font_type(font_type_of_last_line_of_block_1, font_type_of_first_line_of_block_2)
  2313. and abs(font_size_of_last_line_of_block_1 - font_size_of_first_line_of_block_2) < 1
  2314. # and font_color_of_last_line_of_block1 == font_color_of_first_line_of_block2
  2315. and font_flags_of_last_line_of_block_1 == font_flags_of_first_line_of_block_2
  2316. )
  2317. def should_merge_next_para(self, curr_para, next_para):
  2318. """
  2319. This function checks if the next_para should be merged into the curr_para.
  2320. Parameters
  2321. ----------
  2322. curr_para : dict
  2323. The current paragraph.
  2324. next_para : dict
  2325. The next paragraph.
  2326. Returns
  2327. -------
  2328. bool
  2329. True if the next_para should be merged into the curr_para, False otherwise.
  2330. """
  2331. if self._is_para_continued(curr_para, next_para):
  2332. return True
  2333. else:
  2334. return False
  2335. def batch_tag_paras(self, pdf_dict):
  2336. """
  2337. This function tags the paragraphs in the pdf_dict.
  2338. Parameters
  2339. ----------
  2340. pdf_dict : dict
  2341. PDF dictionary.
  2342. Returns
  2343. -------
  2344. pdf_dict : dict
  2345. PDF dictionary with tagged paragraphs.
  2346. """
  2347. the_last_page_id = len(pdf_dict) - 1
  2348. for curr_page_idx, (curr_page_id, curr_page_content) in enumerate(pdf_dict.items()):
  2349. if curr_page_id.startswith("page_") and curr_page_content.get("para_blocks", []):
  2350. para_blocks_of_curr_page = curr_page_content["para_blocks"]
  2351. next_page_idx = curr_page_idx + 1
  2352. next_page_id = f"page_{next_page_idx}"
  2353. next_page_content = pdf_dict.get(next_page_id, {})
  2354. for i, current_block in enumerate(para_blocks_of_curr_page):
  2355. for para_id, curr_para in current_block["paras"].items():
  2356. curr_para["curr_para_location"] = [
  2357. curr_page_idx,
  2358. current_block["block_id"],
  2359. int(para_id.split("_")[-1]),
  2360. ]
  2361. curr_para["next_para_location"] = None # 默认设置为None
  2362. curr_para["merge_next_para"] = False # 默认设置为False
  2363. next_block = para_blocks_of_curr_page[i + 1] if i < len(para_blocks_of_curr_page) - 1 else None
  2364. if next_block:
  2365. curr_block_last_para_key = list(current_block["paras"].keys())[-1]
  2366. curr_blk_last_para = current_block["paras"][curr_block_last_para_key]
  2367. next_block_first_para_key = list(next_block["paras"].keys())[0]
  2368. next_blk_first_para = next_block["paras"][next_block_first_para_key]
  2369. if self.should_merge_next_para(curr_blk_last_para, next_blk_first_para):
  2370. curr_blk_last_para["next_para_location"] = [
  2371. curr_page_idx,
  2372. next_block["block_id"],
  2373. int(next_block_first_para_key.split("_")[-1]),
  2374. ]
  2375. curr_blk_last_para["merge_next_para"] = True
  2376. else:
  2377. # Handle the case where the next block is in a different page
  2378. curr_block_last_para_key = list(current_block["paras"].keys())[-1]
  2379. curr_blk_last_para = current_block["paras"][curr_block_last_para_key]
  2380. while not next_page_content.get("para_blocks", []) and next_page_idx <= the_last_page_id:
  2381. next_page_idx += 1
  2382. next_page_id = f"page_{next_page_idx}"
  2383. next_page_content = pdf_dict.get(next_page_id, {})
  2384. if next_page_content.get("para_blocks", []):
  2385. next_blk_first_para_key = list(next_page_content["para_blocks"][0]["paras"].keys())[0]
  2386. next_blk_first_para = next_page_content["para_blocks"][0]["paras"][next_blk_first_para_key]
  2387. if self.should_merge_next_para(curr_blk_last_para, next_blk_first_para):
  2388. curr_blk_last_para["next_para_location"] = [
  2389. next_page_idx,
  2390. next_page_content["para_blocks"][0]["block_id"],
  2391. int(next_blk_first_para_key.split("_")[-1]),
  2392. ]
  2393. curr_blk_last_para["merge_next_para"] = True
  2394. return pdf_dict
  2395. def find_block_by_id(self, para_blocks, block_id):
  2396. """
  2397. This function finds a block by its id.
  2398. Parameters
  2399. ----------
  2400. para_blocks : list
  2401. List of blocks.
  2402. block_id : int
  2403. Id of the block to find.
  2404. Returns
  2405. -------
  2406. block : dict
  2407. The block with the given id.
  2408. """
  2409. for blk_idx, block in enumerate(para_blocks):
  2410. if block.get("block_id") == block_id:
  2411. return block
  2412. return None
  2413. def batch_merge_paras(self, pdf_dict):
  2414. """
  2415. This function merges the paragraphs in the pdf_dict.
  2416. Parameters
  2417. ----------
  2418. pdf_dict : dict
  2419. PDF dictionary.
  2420. Returns
  2421. -------
  2422. pdf_dict : dict
  2423. PDF dictionary with merged paragraphs.
  2424. """
  2425. for page_id, page_content in pdf_dict.items():
  2426. if page_id.startswith("page_") and page_content.get("para_blocks", []):
  2427. para_blocks_of_page = page_content["para_blocks"]
  2428. for i in range(len(para_blocks_of_page)):
  2429. current_block = para_blocks_of_page[i]
  2430. paras = current_block["paras"]
  2431. for para_id, curr_para in list(paras.items()):
  2432. # print(f"current para_id: {para_id}")
  2433. # 跳过标题段落
  2434. if curr_para.get("is_para_title"):
  2435. continue
  2436. while curr_para.get("merge_next_para"):
  2437. curr_para_location = curr_para.get("curr_para_location")
  2438. next_para_location = curr_para.get("next_para_location")
  2439. # print(f"curr_para_location: {curr_para_location}, next_para_location: {next_para_location}")
  2440. if not next_para_location:
  2441. break
  2442. if curr_para_location == next_para_location:
  2443. # print_red("The next para is in the same block as the current para.")
  2444. curr_para["merge_next_para"] = False
  2445. break
  2446. next_page_idx, next_block_id, next_para_id = next_para_location
  2447. next_page_id = f"page_{next_page_idx}"
  2448. next_page_content = pdf_dict.get(next_page_id)
  2449. if not next_page_content:
  2450. break
  2451. next_block = self.find_block_by_id(next_page_content.get("para_blocks", []), next_block_id)
  2452. if not next_block:
  2453. break
  2454. next_para = next_block["paras"].get(f"para_{next_para_id}")
  2455. if not next_para or next_para.get("is_para_title"):
  2456. break
  2457. # 合并段落文本
  2458. curr_para_text = curr_para.get("para_text", "")
  2459. next_para_text = next_para.get("para_text", "")
  2460. curr_para["para_text"] = curr_para_text + " " + next_para_text
  2461. # 更新 next_para_location
  2462. curr_para["next_para_location"] = next_para.get("next_para_location")
  2463. # 将下一个段落文本置为空,表示已被合并
  2464. next_para["para_text"] = ""
  2465. # 更新 merge_next_para 标记
  2466. curr_para["merge_next_para"] = next_para.get("merge_next_para", False)
  2467. return pdf_dict
  2468. class DrawAnnos:
  2469. """
  2470. This class draws annotations on the pdf file
  2471. ----------------------------------------
  2472. Color Code
  2473. ----------------------------------------
  2474. Red: (1, 0, 0)
  2475. Green: (0, 1, 0)
  2476. Blue: (0, 0, 1)
  2477. Yellow: (1, 1, 0) - mix of red and green
  2478. Cyan: (0, 1, 1) - mix of green and blue
  2479. Magenta: (1, 0, 1) - mix of red and blue
  2480. White: (1, 1, 1) - red, green and blue full intensity
  2481. Black: (0, 0, 0) - no color component whatsoever
  2482. Gray: (0.5, 0.5, 0.5) - equal and medium intensity of red, green and blue color components
  2483. Orange: (1, 0.65, 0) - maximum intensity of red, medium intensity of green, no blue component
  2484. """
  2485. def __init__(self) -> None:
  2486. pass
  2487. def __is_nested_list(self, lst):
  2488. """
  2489. This function returns True if the given list is a nested list of any degree.
  2490. """
  2491. if isinstance(lst, list):
  2492. return any(self.__is_nested_list(i) for i in lst) or any(isinstance(i, list) for i in lst)
  2493. return False
  2494. def __valid_rect(self, bbox):
  2495. # Ensure that the rectangle is not empty or invalid
  2496. if isinstance(bbox[0], list):
  2497. return False # It's a nested list, hence it can't be valid rect
  2498. else:
  2499. return bbox[0] < bbox[2] and bbox[1] < bbox[3]
  2500. def __draw_nested_boxes(self, page, nested_bbox, color=(0, 1, 1)):
  2501. """
  2502. This function draws the nested boxes
  2503. Parameters
  2504. ----------
  2505. page : fitz.Page
  2506. page
  2507. nested_bbox : list
  2508. nested bbox
  2509. color : tuple
  2510. color, by default (0, 1, 1) # draw with cyan color for combined paragraph
  2511. """
  2512. if self.__is_nested_list(nested_bbox): # If it's a nested list
  2513. for bbox in nested_bbox:
  2514. self.__draw_nested_boxes(page, bbox, color) # Recursively call the function
  2515. elif self.__valid_rect(nested_bbox): # If valid rectangle
  2516. para_rect = fitz.Rect(nested_bbox)
  2517. para_anno = page.add_rect_annot(para_rect)
  2518. para_anno.set_colors(stroke=color) # draw with cyan color for combined paragraph
  2519. para_anno.set_border(width=1)
  2520. para_anno.update()
  2521. def draw_annos(self, input_pdf_path, pdf_dic, output_pdf_path):
  2522. """
  2523. This function draws annotations on the pdf file.
  2524. Parameters
  2525. ----------
  2526. input_pdf_path : str
  2527. path to the input pdf file
  2528. pdf_dic : dict
  2529. pdf dictionary
  2530. output_pdf_path : str
  2531. path to the output pdf file
  2532. pdf_dic : dict
  2533. pdf dictionary
  2534. """
  2535. pdf_doc = open_pdf(input_pdf_path)
  2536. if pdf_dic is None:
  2537. pdf_dic = {}
  2538. if output_pdf_path is None:
  2539. output_pdf_path = input_pdf_path.replace(".pdf", "_anno.pdf")
  2540. for page_id, page in enumerate(pdf_doc): # type: ignore
  2541. page_key = f"page_{page_id}"
  2542. for ele_key, ele_data in pdf_dic[page_key].items():
  2543. if ele_key == "para_blocks":
  2544. para_blocks = ele_data
  2545. for para_block in para_blocks:
  2546. if "paras" in para_block.keys():
  2547. paras = para_block["paras"]
  2548. for para_key, para_content in paras.items():
  2549. para_bbox = para_content["para_bbox"]
  2550. # print(f"para_bbox: {para_bbox}")
  2551. # print(f"is a nested list: {self.__is_nested_list(para_bbox)}")
  2552. if self.__is_nested_list(para_bbox) and len(para_bbox) > 1:
  2553. color = (0, 1, 1)
  2554. self.__draw_nested_boxes(
  2555. page, para_bbox, color
  2556. ) # draw with cyan color for combined paragraph
  2557. else:
  2558. if self.__valid_rect(para_bbox):
  2559. para_rect = fitz.Rect(para_bbox)
  2560. para_anno = page.add_rect_annot(para_rect)
  2561. para_anno.set_colors(stroke=(0, 1, 0)) # draw with green color for normal paragraph
  2562. para_anno.set_border(width=0.5)
  2563. para_anno.update()
  2564. is_para_title = para_content["is_para_title"]
  2565. if is_para_title:
  2566. if self.__is_nested_list(para_content["para_bbox"]) and len(para_content["para_bbox"]) > 1:
  2567. color = (0, 0, 1)
  2568. self.__draw_nested_boxes(
  2569. page, para_content["para_bbox"], color
  2570. ) # draw with cyan color for combined title
  2571. else:
  2572. if self.__valid_rect(para_content["para_bbox"]):
  2573. para_rect = fitz.Rect(para_content["para_bbox"])
  2574. if self.__valid_rect(para_content["para_bbox"]):
  2575. para_anno = page.add_rect_annot(para_rect)
  2576. para_anno.set_colors(stroke=(0, 0, 1)) # draw with blue color for normal title
  2577. para_anno.set_border(width=0.5)
  2578. para_anno.update()
  2579. pdf_doc.save(output_pdf_path)
  2580. pdf_doc.close()
  2581. class ParaProcessPipeline:
  2582. def __init__(self) -> None:
  2583. pass
  2584. def para_process_pipeline(self, pdf_info_dict, para_debug_mode=None, input_pdf_path=None, output_pdf_path=None):
  2585. """
  2586. This function processes the paragraphs, including:
  2587. 1. Read raw input json file into pdf_dic
  2588. 2. Detect and replace equations
  2589. 3. Combine spans into a natural line
  2590. 4. Check if the paragraphs are inside bboxes passed from "layout_bboxes" key
  2591. 5. Compute statistics for each block
  2592. 6. Detect titles in the document
  2593. 7. Detect paragraphs inside each block
  2594. 8. Divide the level of the titles
  2595. 9. Detect and combine paragraphs from different blocks into one paragraph
  2596. 10. Check whether the final results after checking headings, dividing paragraphs within blocks, and merging paragraphs between blocks are plausible and reasonable.
  2597. 11. Draw annotations on the pdf file
  2598. Parameters
  2599. ----------
  2600. pdf_dic_json_fpath : str
  2601. path to the pdf dictionary json file.
  2602. Notice: data noises, including overlap blocks, header, footer, watermark, vertical margin note have been removed already.
  2603. input_pdf_doc : str
  2604. path to the input pdf file
  2605. output_pdf_path : str
  2606. path to the output pdf file
  2607. Returns
  2608. -------
  2609. pdf_dict : dict
  2610. result dictionary
  2611. """
  2612. error_info = None
  2613. output_json_file = ""
  2614. output_dir = ""
  2615. if input_pdf_path is not None:
  2616. input_pdf_path = os.path.abspath(input_pdf_path)
  2617. # print_green_on_red(f">>>>>>>>>>>>>>>>>>> Process the paragraphs of {input_pdf_path}")
  2618. if output_pdf_path is not None:
  2619. output_dir = os.path.dirname(output_pdf_path)
  2620. output_json_file = f"{output_dir}/pdf_dic.json"
  2621. def __save_pdf_dic(pdf_dic, output_pdf_path, stage="0", para_debug_mode=para_debug_mode):
  2622. """
  2623. Save the pdf_dic to a json file
  2624. """
  2625. output_pdf_file_name = os.path.basename(output_pdf_path)
  2626. # output_dir = os.path.dirname(output_pdf_path)
  2627. output_dir = "\\tmp\\pdf_parse"
  2628. output_pdf_file_name = output_pdf_file_name.replace(".pdf", f"_stage_{stage}.json")
  2629. pdf_dic_json_fpath = os.path.join(output_dir, output_pdf_file_name)
  2630. if not os.path.exists(output_dir):
  2631. os.makedirs(output_dir)
  2632. if para_debug_mode == "full":
  2633. with open(pdf_dic_json_fpath, "w", encoding="utf-8") as f:
  2634. json.dump(pdf_dic, f, indent=2, ensure_ascii=False)
  2635. # Validate the output already exists
  2636. if not os.path.exists(pdf_dic_json_fpath):
  2637. print_red(f"Failed to save the pdf_dic to {pdf_dic_json_fpath}")
  2638. return None
  2639. else:
  2640. print_green(f"Succeed to save the pdf_dic to {pdf_dic_json_fpath}")
  2641. return pdf_dic_json_fpath
  2642. """
  2643. Preprocess the lines of block
  2644. """
  2645. # Combine spans into a natural line
  2646. rawBlockProcessor = RawBlockProcessor()
  2647. pdf_dic = rawBlockProcessor.batch_process_blocks(pdf_info_dict)
  2648. # print(f"pdf_dic['page_0']['para_blocks'][0]: {pdf_dic['page_0']['para_blocks'][0]}", end="\n\n")
  2649. # Check if the paragraphs are inside bboxes passed from "layout_bboxes" key
  2650. layoutFilter = LayoutFilterProcessor()
  2651. pdf_dic = layoutFilter.batch_process_blocks(pdf_dic)
  2652. # Compute statistics for each block
  2653. blockStatisticsCalculator = BlockStatisticsCalculator()
  2654. pdf_dic = blockStatisticsCalculator.batch_process_blocks(pdf_dic)
  2655. # print(f"pdf_dic['page_0']['para_blocks'][0]: {pdf_dic['page_0']['para_blocks'][0]}", end="\n\n")
  2656. # Compute statistics for all blocks(namely this pdf document)
  2657. docStatisticsCalculator = DocStatisticsCalculator()
  2658. pdf_dic = docStatisticsCalculator.calc_stats_of_doc(pdf_dic)
  2659. # print(f"pdf_dic['statistics']: {pdf_dic['statistics']}", end="\n\n")
  2660. # Dump the first three stages of pdf_dic to a json file
  2661. if para_debug_mode == "full":
  2662. pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="0", para_debug_mode=para_debug_mode)
  2663. """
  2664. Detect titles in the document
  2665. """
  2666. doc_statistics = pdf_dic["statistics"]
  2667. titleProcessor = TitleProcessor(doc_statistics)
  2668. pdf_dic = titleProcessor.batch_detect_titles(pdf_dic)
  2669. if para_debug_mode == "full":
  2670. pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="1", para_debug_mode=para_debug_mode)
  2671. """
  2672. Detect and divide the level of the titles
  2673. """
  2674. titleProcessor = TitleProcessor()
  2675. pdf_dic = titleProcessor.batch_recog_title_level(pdf_dic)
  2676. if para_debug_mode == "full":
  2677. pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="2", para_debug_mode=para_debug_mode)
  2678. """
  2679. Detect and split paragraphs inside each block
  2680. """
  2681. blockInnerParasProcessor = BlockTerminationProcessor()
  2682. pdf_dic = blockInnerParasProcessor.batch_process_blocks(pdf_dic)
  2683. if para_debug_mode == "full":
  2684. pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="3", para_debug_mode=para_debug_mode)
  2685. # pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="3", para_debug_mode="full")
  2686. # print_green(f"pdf_dic_json_fpath: {pdf_dic_json_fpath}")
  2687. """
  2688. Detect and combine paragraphs from different blocks into one paragraph
  2689. """
  2690. blockContinuationProcessor = BlockContinuationProcessor()
  2691. pdf_dic = blockContinuationProcessor.batch_tag_paras(pdf_dic)
  2692. pdf_dic = blockContinuationProcessor.batch_merge_paras(pdf_dic)
  2693. if para_debug_mode == "full":
  2694. pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="4", para_debug_mode=para_debug_mode)
  2695. # pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="4", para_debug_mode="full")
  2696. # print_green(f"pdf_dic_json_fpath: {pdf_dic_json_fpath}")
  2697. """
  2698. Discard pdf files by checking exceptions and return the error info to the caller
  2699. """
  2700. discardByException = DiscardByException()
  2701. is_discard_by_single_line_block = discardByException.discard_by_single_line_block(
  2702. pdf_dic, exception=DenseSingleLineBlockException()
  2703. )
  2704. is_discard_by_title_detection = discardByException.discard_by_title_detection(
  2705. pdf_dic, exception=TitleDetectionException()
  2706. )
  2707. is_discard_by_title_level = discardByException.discard_by_title_level(pdf_dic, exception=TitleLevelException())
  2708. is_discard_by_split_para = discardByException.discard_by_split_para(pdf_dic, exception=ParaSplitException())
  2709. is_discard_by_merge_para = discardByException.discard_by_merge_para(pdf_dic, exception=ParaMergeException())
  2710. if is_discard_by_single_line_block is not None:
  2711. error_info = is_discard_by_single_line_block
  2712. elif is_discard_by_title_detection is not None:
  2713. error_info = is_discard_by_title_detection
  2714. elif is_discard_by_title_level is not None:
  2715. error_info = is_discard_by_title_level
  2716. elif is_discard_by_split_para is not None:
  2717. error_info = is_discard_by_split_para
  2718. elif is_discard_by_merge_para is not None:
  2719. error_info = is_discard_by_merge_para
  2720. if error_info is not None:
  2721. return pdf_dic, error_info
  2722. """
  2723. Dump the final pdf_dic to a json file
  2724. """
  2725. if para_debug_mode is not None:
  2726. with open(output_json_file, "w", encoding="utf-8") as f:
  2727. json.dump(pdf_info_dict, f, ensure_ascii=False, indent=4)
  2728. """
  2729. Draw the annotations
  2730. """
  2731. if para_debug_mode is not None:
  2732. drawAnnos = DrawAnnos()
  2733. drawAnnos.draw_annos(input_pdf_path, pdf_dic, output_pdf_path)
  2734. """
  2735. Remove the intermediate files which are generated in the process of paragraph processing if debug_mode is simple
  2736. """
  2737. if para_debug_mode is not None:
  2738. for fpath in os.listdir(output_dir):
  2739. if fpath.endswith(".json") and "stage" in fpath:
  2740. os.remove(os.path.join(output_dir, fpath))
  2741. return pdf_dic, error_info
  2742. """
  2743. Run this script to test the function with Command:
  2744. python detect_para.py [pdf_path] [output_pdf_path]
  2745. Params:
  2746. - pdf_path: the path of the pdf file
  2747. - output_pdf_path: the path of the output pdf file
  2748. """
  2749. if __name__ == "__main__":
  2750. DEFAULT_PDF_PATH = (
  2751. "app/pdf_toolbox/tests/assets/paper/paper.pdf" if os.name != "nt" else "app\\pdf_toolbox\\tests\\assets\\paper\\paper.pdf"
  2752. )
  2753. input_pdf_path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_PDF_PATH
  2754. output_pdf_path = sys.argv[2] if len(sys.argv) > 2 else input_pdf_path.split(".")[0] + "_recogPara.pdf"
  2755. output_json_path = sys.argv[3] if len(sys.argv) > 3 else input_pdf_path.split(".")[0] + "_recogPara.json"
  2756. import stat
  2757. # Remove existing output file if it exists
  2758. if os.path.exists(output_pdf_path):
  2759. os.chmod(output_pdf_path, stat.S_IWRITE)
  2760. os.remove(output_pdf_path)
  2761. input_pdf_doc = open_pdf(input_pdf_path)
  2762. # postprocess the paragraphs
  2763. paraProcessPipeline = ParaProcessPipeline()
  2764. # parse paragraph and save to json file
  2765. pdf_dic = {}
  2766. blockInnerParasProcessor = BlockTerminationProcessor()
  2767. """
  2768. Construct the pdf dictionary.
  2769. """
  2770. for page_id, page in enumerate(input_pdf_doc): # type: ignore
  2771. # print(f"Processing page {page_id}")
  2772. # print(f"page: {page}")
  2773. raw_blocks = page.get_text("dict")["blocks"]
  2774. # Save text blocks to "preproc_blocks"
  2775. preproc_blocks = []
  2776. for block in raw_blocks:
  2777. if block["type"] == 0:
  2778. preproc_blocks.append(block)
  2779. layout_bboxes = []
  2780. # Construct the pdf dictionary as schema above
  2781. page_dict = {
  2782. "para_blocks": None,
  2783. "preproc_blocks": preproc_blocks,
  2784. "images": None,
  2785. "tables": None,
  2786. "interline_equations": None,
  2787. "inline_equations": None,
  2788. "layout_bboxes": None,
  2789. "pymu_raw_blocks": None,
  2790. "global_statistic": None,
  2791. "droped_text_block": None,
  2792. "droped_image_block": None,
  2793. "droped_table_block": None,
  2794. "image_backup": None,
  2795. "table_backup": None,
  2796. }
  2797. pdf_dic[f"page_{page_id}"] = page_dict
  2798. # print(f"pdf_dic: {pdf_dic}")
  2799. with open(output_json_path, "w", encoding="utf-8") as f:
  2800. json.dump(pdf_dic, f, ensure_ascii=False, indent=4)
  2801. pdf_dic = paraProcessPipeline.para_process_pipeline(output_json_path, input_pdf_doc, output_pdf_path)