def parse(self, log: ProcessLogger, text, text_unit_id, _text_unit_lang, **kwargs) -> ParseResults: strict = kwargs.get(Locator.STRICT_DATES_PTR, STRICT_PARSE_DATES.val) found = list(dates.get_date_annotations( text, strict=strict)) if found: _all = [i.date.date() if isinstance(i.date, datetime.datetime) else i.date for i in found] return ParseResults({DateUsage: [DateUsage(text_unit_id=text_unit_id, date=item, count=_all.count(item)) for item in set(_all)]})
def test_en_dates(self): text = "Some date like February 26, 2018 and this one 10-11-2017" extracted_dates = list(get_date_annotations(text=text, locale='en')) for ant in extracted_dates: ant.text = text[ant.coords[0]:ant.coords[1]] extracted_dates.sort(key=lambda k: k.coords[0]) self.assertEqual((14, 33), extracted_dates[0].coords) self.assertEqual(datetime.date(2018, 2, 26), extracted_dates[0].date) self.assertEqual('February 26, 2018', extracted_dates[0].text.strip()) self.assertEqual((45, 56), extracted_dates[1].coords) self.assertEqual(datetime.date(2017, 10, 11), extracted_dates[1].date) self.assertEqual('10-11-2017', extracted_dates[1].text.strip())