Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def given_parser(self, *args, **kwds):
def collecting_get_date_data(parse):
@wraps(parse)
def wrapped(*args, **kwargs):
self.date_result = parse(*args, **kwargs)
return self.date_result
return wrapped
self.add_patch(patch.object(date_parser,
'parse',
collecting_get_date_data(date_parser.parse)))
self.date_parser = Mock(wraps=date_parser)
self.add_patch(patch('dateparser.date.date_parser', new=self.date_parser))
self.parser = DateDataParser(*args, **kwds)
def given_parser(self, restrict_to_languages=None, **params):
self.parser = date.DateDataParser(languages=restrict_to_languages, **params)
def annotate(self, doc):
# If no date is associated with the document, the document's date will
# be treated as the most recent date explicitly mentioned in the
# the document.
detect_date = doc.date is None
doc_date = doc.date or datetime.datetime.now()
strict_parser = DateDataParser(['en'], settings={
'STRICT_PARSING': True})
def date_to_datetime_range(text,
relative_base=None,
prefer_dates_from='past'):
if relative_base is None:
relative_base = doc_date
# Handle relative date ranges like "the past ___ days"
relative_num_days = re.sub(relative_duration_range_re, "", text)
if len(relative_num_days) < len(text):
num_days_datetime_range = date_to_datetime_range(relative_num_days)
if not num_days_datetime_range:
return None
return [num_days_datetime_range[0], relative_base]
text = clean_date_str(text)
if len(text) < 3:
def adapt(self, text, htmlpage=None):
try:
return DateDataParser().get_date_data(text)['date_obj']
except ValueError:
return
def _parse_date(self, string):
parser = DateDataParser()
date = parser.get_date_data(string)['date_obj']
if date is None:
raise RuntimeError('Unable to parse date: {!r}'.format(string))
return date.date()
def search_parse(self, shortname, text, settings):
translated, original = self.search(shortname, text, settings)
bad_translate_with_search = ['vi', 'hu'] # splitting done by spaces and some dictionary items contain spaces
if shortname not in bad_translate_with_search:
parser = DateDataParser(languages=['en'], settings=settings)
parsed, substrings = self.parse_found_objects(parser=parser, to_parse=translated,
original=original, translated=translated, settings=settings)
else:
parser = DateDataParser(languages=[shortname], settings=settings)
parsed, substrings = self.parse_found_objects(parser=parser, to_parse=original,
original=original, translated=translated, settings=settings)
parser._settings = Settings()
return list(zip(substrings, [i['date_obj'] for i in parsed]))