| 
									
										
										
										
											2024-02-21 16:32:38 +08:00
										 |  |  | #  Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | #  you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | #  You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #      http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | #  distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | #  See the License for the specific language governing permissions and | 
					
						
							|  |  |  | #  limitations under the License. | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  | import copy | 
					
						
							| 
									
										
										
										
											2024-04-23 15:31:43 +08:00
										 |  |  | from tika import parser | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2024-03-22 19:21:09 +08:00
										 |  |  | from io import BytesIO | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-23 18:28:12 +08:00
										 |  |  | from rag.nlp import bullets_category, is_english, tokenize, remove_contents_table, \ | 
					
						
							| 
									
										
										
										
											2024-04-19 18:02:53 +08:00
										 |  |  |     hierarchical_merge, make_colon_as_title, naive_merge, random_choices, tokenize_table, add_positions, \ | 
					
						
							|  |  |  |     tokenize_chunks, find_codec | 
					
						
							| 
									
										
										
										
											2024-04-28 19:13:33 +08:00
										 |  |  | from rag.nlp import rag_tokenizer | 
					
						
							| 
									
										
										
										
											2024-05-30 09:12:55 +08:00
										 |  |  | from deepdoc.parser import PdfParser, DocxParser, PlainParser, HtmlParser | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-21 16:32:38 +08:00
										 |  |  | class Pdf(PdfParser): | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |     def __call__(self, filename, binary=None, from_page=0, | 
					
						
							|  |  |  |                  to_page=100000, zoomin=3, callback=None): | 
					
						
							| 
									
										
										
										
											2024-04-26 20:07:26 +08:00
										 |  |  |         callback(msg="OCR is running...") | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |         self.__images__( | 
					
						
							|  |  |  |             filename if not binary else binary, | 
					
						
							|  |  |  |             zoomin, | 
					
						
							|  |  |  |             from_page, | 
					
						
							| 
									
										
										
										
											2024-03-05 12:08:41 +08:00
										 |  |  |             to_page, | 
					
						
							|  |  |  |             callback) | 
					
						
							| 
									
										
										
										
											2024-03-05 16:33:47 +08:00
										 |  |  |         callback(msg="OCR finished") | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         from timeit import default_timer as timer | 
					
						
							|  |  |  |         start = timer() | 
					
						
							| 
									
										
										
										
											2024-02-21 16:32:38 +08:00
										 |  |  |         self._layouts_rec(zoomin) | 
					
						
							| 
									
										
										
										
											2024-03-05 12:08:41 +08:00
										 |  |  |         callback(0.67, "Layout analysis finished") | 
					
						
							| 
									
										
										
										
											2024-04-23 14:41:10 +08:00
										 |  |  |         print("layouts:", timer() - start) | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |         self._table_transformer_job(zoomin) | 
					
						
							|  |  |  |         callback(0.68, "Table analysis finished") | 
					
						
							|  |  |  |         self._text_merge() | 
					
						
							| 
									
										
										
										
											2024-03-05 12:08:41 +08:00
										 |  |  |         tbls = self._extract_table_figure(True, zoomin, True, True) | 
					
						
							|  |  |  |         self._naive_vertical_merge() | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |         self._filter_forpages() | 
					
						
							|  |  |  |         self._merge_with_same_bullet() | 
					
						
							|  |  |  |         callback(0.75, "Text merging finished.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         callback(0.8, "Text extraction finished") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |         return [(b["text"] + self._line_tag(b, zoomin), b.get("layoutno", "")) | 
					
						
							|  |  |  |                 for b in self.boxes], tbls | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  | def chunk(filename, binary=None, from_page=0, to_page=100000, | 
					
						
							|  |  |  |           lang="Chinese", callback=None, **kwargs): | 
					
						
							| 
									
										
										
										
											2024-02-19 19:22:17 +08:00
										 |  |  |     """
 | 
					
						
							|  |  |  |         Supported file formats are docx, pdf, txt. | 
					
						
							|  |  |  |         Since a book is long and not all the parts are useful, if it's a PDF, | 
					
						
							|  |  |  |         please setup the page ranges for every book in order eliminate negative effects and save elapsed computing time. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |     doc = { | 
					
						
							|  |  |  |         "docnm_kwd": filename, | 
					
						
							| 
									
										
										
										
											2024-04-28 19:13:33 +08:00
										 |  |  |         "title_tks": rag_tokenizer.tokenize(re.sub(r"\.[a-zA-Z]+$", "", filename)) | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-28 19:13:33 +08:00
										 |  |  |     doc["title_sm_tks"] = rag_tokenizer.fine_grained_tokenize(doc["title_tks"]) | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |     pdf_parser = None | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |     sections, tbls = [], [] | 
					
						
							| 
									
										
										
										
											2024-04-22 15:46:09 +08:00
										 |  |  |     if re.search(r"\.docx$", filename, re.IGNORECASE): | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |         callback(0.1, "Start to parse.") | 
					
						
							| 
									
										
										
										
											2024-02-21 16:32:38 +08:00
										 |  |  |         doc_parser = DocxParser() | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |         # TODO: table of contents need to be removed | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |         sections, tbls = doc_parser( | 
					
						
							|  |  |  |             binary if binary else filename, from_page=from_page, to_page=to_page) | 
					
						
							|  |  |  |         remove_contents_table(sections, eng=is_english( | 
					
						
							|  |  |  |             random_choices([t for t, _ in sections], k=200))) | 
					
						
							| 
									
										
										
										
											2024-04-23 19:10:33 +08:00
										 |  |  |         tbls = [((None, lns), None) for lns in tbls] | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |         callback(0.8, "Finish parsing.") | 
					
						
							| 
									
										
										
										
											2024-03-22 19:21:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |     elif re.search(r"\.pdf$", filename, re.IGNORECASE): | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |         pdf_parser = Pdf() if kwargs.get( | 
					
						
							|  |  |  |             "parser_config", {}).get( | 
					
						
							|  |  |  |             "layout_recognize", True) else PlainParser() | 
					
						
							| 
									
										
										
										
											2024-03-04 14:42:26 +08:00
										 |  |  |         sections, tbls = pdf_parser(filename if not binary else binary, | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |                                     from_page=from_page, to_page=to_page, callback=callback) | 
					
						
							| 
									
										
										
										
											2024-03-22 19:21:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |     elif re.search(r"\.txt$", filename, re.IGNORECASE): | 
					
						
							|  |  |  |         callback(0.1, "Start to parse.") | 
					
						
							|  |  |  |         txt = "" | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |         if binary: | 
					
						
							| 
									
										
										
										
											2024-04-19 18:02:53 +08:00
										 |  |  |             encoding = find_codec(binary) | 
					
						
							| 
									
										
										
										
											2024-05-07 12:25:47 +08:00
										 |  |  |             txt = binary.decode(encoding, errors="ignore") | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             with open(filename, "r") as f: | 
					
						
							|  |  |  |                 while True: | 
					
						
							|  |  |  |                     l = f.readline() | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |                     if not l: | 
					
						
							|  |  |  |                         break | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |                     txt += l | 
					
						
							| 
									
										
										
										
											2024-02-05 18:08:17 +08:00
										 |  |  |         sections = txt.split("\n") | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |         sections = [(l, "") for l in sections if l] | 
					
						
							|  |  |  |         remove_contents_table(sections, eng=is_english( | 
					
						
							|  |  |  |             random_choices([t for t, _ in sections], k=200))) | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  |         callback(0.8, "Finish parsing.") | 
					
						
							| 
									
										
										
										
											2024-03-22 19:21:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-30 09:12:55 +08:00
										 |  |  |     elif re.search(r"\.(htm|html)$", filename, re.IGNORECASE): | 
					
						
							|  |  |  |         callback(0.1, "Start to parse.") | 
					
						
							|  |  |  |         sections = HtmlParser()(filename, binary) | 
					
						
							|  |  |  |         sections = [(l, "") for l in sections if l] | 
					
						
							|  |  |  |         remove_contents_table(sections, eng=is_english( | 
					
						
							|  |  |  |             random_choices([t for t, _ in sections], k=200))) | 
					
						
							|  |  |  |         callback(0.8, "Finish parsing.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-23 15:31:43 +08:00
										 |  |  |     elif re.search(r"\.doc$", filename, re.IGNORECASE): | 
					
						
							|  |  |  |         callback(0.1, "Start to parse.") | 
					
						
							|  |  |  |         binary = BytesIO(binary) | 
					
						
							|  |  |  |         doc_parsed = parser.from_buffer(binary) | 
					
						
							|  |  |  |         sections = doc_parsed['content'].split('\n') | 
					
						
							|  |  |  |         sections = [(l, "") for l in sections if l] | 
					
						
							|  |  |  |         remove_contents_table(sections, eng=is_english( | 
					
						
							|  |  |  |             random_choices([t for t, _ in sections], k=200))) | 
					
						
							|  |  |  |         callback(0.8, "Finish parsing.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |     else: | 
					
						
							|  |  |  |         raise NotImplementedError( | 
					
						
							| 
									
										
										
										
											2024-04-23 15:31:43 +08:00
										 |  |  |             "file type not supported yet(doc, docx, pdf, txt supported)") | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 19:21:37 +08:00
										 |  |  |     make_colon_as_title(sections) | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |     bull = bullets_category( | 
					
						
							|  |  |  |         [t for t in random_choices([t for t, _ in sections], k=100)]) | 
					
						
							| 
									
										
										
										
											2024-03-22 19:21:09 +08:00
										 |  |  |     if bull >= 0: | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |         chunks = ["\n".join(ck) | 
					
						
							| 
									
										
										
										
											2024-04-11 10:13:43 +08:00
										 |  |  |                   for ck in hierarchical_merge(bull, sections, 5)] | 
					
						
							| 
									
										
										
										
											2024-03-01 19:48:01 +08:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |         sections = [s.split("@") for s, _ in sections] | 
					
						
							| 
									
										
										
										
											2024-05-30 09:25:05 +08:00
										 |  |  |         sections = [(pr[0], "@" + pr[1]) if len(pr) == 2 else (pr[0], '') for pr in sections ] | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |         chunks = naive_merge( | 
					
						
							|  |  |  |             sections, kwargs.get( | 
					
						
							|  |  |  |                 "chunk_token_num", 256), kwargs.get( | 
					
						
							|  |  |  |                 "delimer", "\n。;!?")) | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # is it English | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  |     # is_english(random_choices([t for t, _ in sections], k=218)) | 
					
						
							|  |  |  |     eng = lang.lower() == "english" | 
					
						
							| 
									
										
										
										
											2024-03-01 19:48:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res = tokenize_table(tbls, doc, eng) | 
					
						
							| 
									
										
										
										
											2024-03-22 19:21:09 +08:00
										 |  |  |     res.extend(tokenize_chunks(chunks, doc, eng, pdf_parser)) | 
					
						
							| 
									
										
										
										
											2024-02-01 18:53:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return res | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     import sys | 
					
						
							| 
									
										
										
										
											2024-03-27 11:33:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 12:08:41 +08:00
										 |  |  |     def dummy(prog=None, msg=""): | 
					
						
							| 
									
										
										
										
											2024-02-02 19:21:37 +08:00
										 |  |  |         pass | 
					
						
							|  |  |  |     chunk(sys.argv[1], from_page=1, to_page=10, callback=dummy) |