Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extractors: better discard regexes #87

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions htmldate/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,22 @@
}

TEXT_DATE_PATTERN = re.compile(r"[.:,_/ -]|^\d+$")
NO_TEXT_DATE_PATTERN = re.compile(
r"\d{3,}\D+\d{3,}|\d{2}:\d{2}(:| )|\+\d{2}\D+|\D*\d{4}\D*$"
)
# leads to errors: \D+\d{3,}\D+


DISCARD_PATTERNS = re.compile(
r"[$€¥Ұ£¢₽₱฿#]|" # currency symbols
r"CNY|EUR|GBP|JPY|USD|" # currency codes
r"http|" # protocols
r"\.(com|net|org)|" # TLDs
r"IBAN|" # bank accountrs
r"\+\d{2}\b" # amounts/telephone numbers
r"^\d{2}:\d{2}(?: |:|$)|"
r"^\D*\d{4}\D*$|"
r"[$€¥Ұ£¢₽₱฿#₹]|" # currency symbols and special characters
r"[A-Z]{3}[^A-Z]|" # currency codes
r"(?:^|\D)(?:\+\d{2}|\d{3}|\d{5})\D|" # tel./IPs/postal codes
r"ftps?|https?|sftp|" # protocols
r"\.(com|net|org|info|gov|edu|de|fr|io)\b|" # TLDs
r"IBAN|[A-Z]{2}[0-9]{2}|" # bank accounts
r"®" # ©
Dismissed Show dismissed Hide dismissed
Dismissed Show dismissed Hide dismissed
)
# further testing required:
# \d[,.]\d+ # currency amounts
# \b\d{5}\s # postal codes
# leads to errors: ^\D+\d{3,}\D+

# use of regex module for speed?
TEXT_PATTERNS = re.compile(
Expand Down Expand Up @@ -461,7 +461,7 @@
return None

# check if string only contains time/single year or digits and not a date
if NO_TEXT_DATE_PATTERN.match(string):
if DISCARD_PATTERNS.search(string):
return None

# try to parse using the faster method
Expand All @@ -472,7 +472,7 @@
# use slow but extensive search
if extensive_search:
# additional filters to prevent computational cost
if not TEXT_DATE_PATTERN.search(string) or DISCARD_PATTERNS.search(string):
if not TEXT_DATE_PATTERN.search(string):
return None
# send to date parser
dateparser_result = external_date_parser(string, outputformat)
Expand Down
Loading