A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
move date-matching regex pattern to base parser module for use by all subclasses
Joshua Taillon72c828170e2d
diff --git a/src/documents/parsers.py b/src/documents/parsers.pyindex c44e4c5bf..c28b31a6b 100644--- a/src/documents/parsers.py+++ b/src/documents/parsers.py@@ -1,9 +1,24 @@import loggingimport shutilimport tempfile+import refrom django.conf import settings+# This regular expression will try to find dates in the document at+# hand and will match the following formats:+# - XX.YY.ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits+# - XX/YY/ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits+# - XX-YY-ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits+# - XX. MONTH ZZZZ with XX being 1 or 2 and ZZZZ being 2 or 4 digits+# - MONTH ZZZZ, with ZZZZ being 4 digits+# - MONTH XX, ZZZZ with XX being 1 or 2 and ZZZZ being 4 digits+pattern = re.compile(+ r'\b([0-9]{1,2})[\.\/-]([0-9]{1,2})[\.\/-]([0-9]{4}|[0-9]{2})\b|' ++ r'\b([0-9]{1,2}[\. ]+[^ ]{3,9} ([0-9]{4}|[0-9]{2}))\b|' ++ r'\b([^\W\d_]{3,9} [0-9]{1,2}, ([0-9]{4}))\b|' ++ r'\b([^\W\d_]{3,9} [0-9]{4})\b')+class ParseError(Exception):passdiff --git a/src/paperless_tesseract/parsers.py b/src/paperless_tesseract/parsers.pyindex add65985a..f0690e5bc 100644--- a/src/paperless_tesseract/parsers.py+++ b/src/paperless_tesseract/parsers.py@@ -14,7 +14,7 @@ from pyocr.libtesseract.tesseract_raw import \from pyocr.tesseract import TesseractErrorimport pdftotext-from documents.parsers import DocumentParser, ParseError+from documents.parsers import DocumentParser, ParseError, patternfrom .languages import ISO639@@ -210,20 +210,6 @@ class RasterisedDocumentParser(DocumentParser):except ParseError as e:return None- # This regular expression will try to find dates in the document at- # hand and will match the following formats:- # - XX.YY.ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits- # - XX/YY/ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits- # - XX-YY-ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits- # - XX. MONTH ZZZZ with XX being 1 or 2 and ZZZZ being 2 or 4 digits- # - MONTH ZZZZ, with ZZZZ being 4 digits- # - MONTH XX, ZZZZ with XX being 1 or 2 and ZZZZ being 4 digits- pattern = re.compile(- r'\b([0-9]{1,2})[\.\/-]([0-9]{1,2})[\.\/-]([0-9]{4}|[0-9]{2})\b|' +- r'\b([0-9]{1,2}[\. ]+[^ ]{3,9} ([0-9]{4}|[0-9]{2}))\b|' +- r'\b([^\W\d_]{3,9} [0-9]{1,2}, ([0-9]{4}))\b|' +- r'\b([^\W\d_]{3,9} [0-9]{4})\b')-# Iterate through all regex matches and try to parse the datefor m in re.finditer(pattern, text):datestring = m.group(0)diff --git a/src/paperless_text/parsers.py b/src/paperless_text/parsers.pyindex 77f7a4118..50c341769 100644--- a/src/paperless_text/parsers.py+++ b/src/paperless_text/parsers.py@@ -5,7 +5,7 @@ import subprocessimport dateparserfrom django.conf import settings-from documents.parsers import DocumentParser, ParseError+from documents.parsers import DocumentParser, ParseError, patternclass TextDocumentParser(DocumentParser):@@ -13,7 +13,6 @@ class TextDocumentParser(DocumentParser):This parser directly parses a text document (.txt, .md, or .csv)"""-CONVERT = settings.CONVERT_BINARYTHREADS = int(settings.OCR_THREADS) if settings.OCR_THREADS else NoneUNPAPER = settings.UNPAPER_BINARY@@ -94,20 +93,6 @@ class TextDocumentParser(DocumentParser):except ParseError as e:return None- # This regular expression will try to find dates in the document at- # hand and will match the following formats:- # - XX.YY.ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits- # - XX/YY/ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits- # - XX-YY-ZZZZ with XX + YY being 1 or 2 and ZZZZ being 2 or 4 digits- # - XX. MONTH ZZZZ with XX being 1 or 2 and ZZZZ being 2 or 4 digits- # - MONTH ZZZZ, with ZZZZ being 4 digits- # - MONTH XX, ZZZZ with XX being 1 or 2 and ZZZZ being 4 digits- pattern = re.compile(- r'\b([0-9]{1,2})[\.\/-]([0-9]{1,2})[\.\/-]([0-9]{4}|[0-9]{2})\b|' +- r'\b([0-9]{1,2}[\. ]+[^ ]{3,9} ([0-9]{4}|[0-9]{2}))\b|' +- r'\b([^\W\d_]{3,9} [0-9]{1,2}, ([0-9]{4}))\b|' +- r'\b([^\W\d_]{3,9} [0-9]{4})\b')-# Iterate through all regex matches and try to parse the datefor m in re.finditer(pattern, text):datestring = m.group(0)