diff options
| author | Scott Gasch <[email protected]> | 2022-06-08 17:23:33 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2022-06-08 17:23:33 -0700 |
| commit | 82e1225c6f8a6ee59f373c07a7873b373acb19dc (patch) | |
| tree | f4e92e96094916ba241cf511a6f19d190716fdc8 /string_utils.py | |
| parent | d787af63a44ba2caa4937e1da9e87255d67b210a (diff) | |
Improve dateparse and make string_utils.extract_date.
Diffstat (limited to 'string_utils.py')
| -rw-r--r-- | string_utils.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/string_utils.py b/string_utils.py index 4127079..7c40dc9 100644 --- a/string_utils.py +++ b/string_utils.py @@ -1388,6 +1388,29 @@ def to_date(in_str: str) -> Optional[datetime.date]: return None +def extract_date(in_str: Any) -> Optional[str]: + import itertools + + import dateparse.dateparse_utils as du + + d = du.DateParser() # type: ignore + chunks = in_str.split() + for ngram in itertools.chain( + list_utils.ngrams(chunks, 5), + list_utils.ngrams(chunks, 4), + list_utils.ngrams(chunks, 3), + list_utils.ngrams(chunks, 2), + ): + try: + expr = " ".join(ngram) + logger.debug(f"Trying {expr}") + if d.parse(expr): + return d.get_date() + except du.ParseException: # type: ignore + pass + return None + + def is_valid_date(in_str: str) -> bool: """ Args: |
