DateAnnotator implements SugiliteTextAnnotator { @Override public List<SugiliteTextParentAnnotator.AnnotatingResult> annotate(String text) { text = text.replaceAll("[\\u00A0\\u2007\\u202F]+", " "); if (cache.containsKey(text)){ return cache.get(text); } List<SugiliteTextParentAnnotator.AnnotatingResult> results = new ArrayList<>(); String mmddyyyy = "\\b(0?[1-9]|1[0-2])[/-](0?[1-9]|[1-2][0-9]|3[0-1])[/-][0-9]{4}\\b"; String complex = "\\b(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?" + "|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)[. ](0?[1-9]|[1-2][0-9]|3[0-1])((,)? [0-9]{4})?\\b"; String yyyymmdd = "\\b[0-9]{4}[-./](0?[1-9]|1[0-2])[-./](0?[1-9]|[1-2][0-9]|3[0-1])\\b"; String mmdd = "\\b(Mon(day)?|Tue(s(day)?)?|Wed(nesday)?|Thu(r(sday)?)?|Fri(day)?|Sat(urday)?|Sun(day)?) " + "(0?[1-9]|1[0-2])/(0?[1-9]|[1-2][0-9]|3[0-1])\\b"; Pattern pattern1 = Pattern.compile(mmddyyyy); Pattern pattern2 = Pattern.compile(complex); Pattern pattern3 = Pattern.compile(yyyymmdd); Pattern pattern4 = Pattern.compile(mmdd); Set<Integer> start = new HashSet<>(); Matcher matcher = pattern1.matcher(text); while (matcher.find()) { String matchedString = text.substring(matcher.start(), matcher.end()); String[] parsed = matchedString.split("[/-]"); int month = Integer.valueOf(parsed[0]); int date = Integer.valueOf(parsed[1]); int year = Integer.valueOf(parsed[2]); if (year < 1970) continue; Calendar cal = new GregorianCalendar(); cal.set(year, month - 1, date, 0, 0, 0); double value = (double) (cal.getTime().getTime()); AnnotatingResult res = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value); results.add(res); start.add(matcher.start()); } matcher = pattern2.matcher(text); while (matcher.find()) { String matchedString = text.substring(matcher.start(), matcher.end()); String[] parsed = matchedString.split("[, .]"); String m = parsed[0].substring(0, 3); String d = parsed[1]; if (d.length() < 2) d = "0" + d; String y; if (parsed.length == 2) y = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); else y = parsed[parsed.length - 1]; String comb = m + " " + d + " " + y; try { Date date = new SimpleDateFormat("MMM dd yyyy").parse(comb); double value = (double) (date.getTime()); AnnotatingResult res = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value); results.add(res); } catch (java.text.ParseException e) { } } matcher = pattern3.matcher(text); while (matcher.find()) { String matchedString = text.substring(matcher.start(), matcher.end()); String[] parsed = matchedString.split("[-./]"); int year = Integer.valueOf(parsed[0]); int month = Integer.valueOf(parsed[1]); int date = Integer.valueOf(parsed[2]); if (year < 1970) continue; Calendar cal = new GregorianCalendar(); cal.set(year, month - 1, date, 0, 0, 0); double value = (double) (cal.getTime().getTime()); AnnotatingResult res = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value); results.add(res); } matcher = pattern4.matcher(text); while (matcher.find()) { String matchedString = text.substring(matcher.start(), matcher.end()); String[] parsed = matchedString.split("[ /]"); int monthStart = matcher.start() + parsed[0].length() + 1; if (start.contains(monthStart)) continue; Calendar cal = new GregorianCalendar(); int year = Calendar.getInstance().get(Calendar.YEAR); int month = Integer.valueOf(parsed[1]); int date = Integer.valueOf(parsed[2]); cal.set(year, month - 1, date, 0, 0, 0); double value = (double) (cal.getTime().getTime()); AnnotatingResult res = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value); results.add(res); } cache.put(text, results); return results; } DateAnnotator(); @Override List<SugiliteTextParentAnnotator.AnnotatingResult> annotate(String text);  }
@Test public void testBasic1() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("2/8/2018"); assertEquals(res.size(), 1); Calendar cal = new GregorianCalendar(); cal.set(2018, 1, 8, 0, 0, 0); assertEquals(res.get(0).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); }
@Test public void testBasic2() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("Feb 8, 2018"); assertEquals(res.size(), 1); Calendar cal = new GregorianCalendar(); cal.set(2018, 1, 8, 0, 0, 0); assertEquals(res.get(0).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); }
@Test public void testBasic3() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("2018.2.08"); assertEquals(res.size(), 1); Calendar cal = new GregorianCalendar(); cal.set(2018, 1, 8, 0, 0, 0); assertEquals(res.get(0).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); }
@Test public void testBadInput() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("3/32/2018"); assertEquals(res.size(), 0); }
@Test public void testBadInput1() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("8 Feb 2018"); assertEquals(res.size(), 0); }
@Test public void testGroup() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate ("Start date: Feb 8, 2018 End date: 03/31/2018 Result will be announced on 2018.5.1"); assertEquals(res.size(), 3); res.sort((a, b) -> Double.compare(a.getNumericValue(), b.getNumericValue())); Calendar cal = new GregorianCalendar(); cal.set(2018, 1, 8, 0, 0, 0); assertEquals(res.get(0).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); cal.set(2018, 2, 31, 0, 0, 0); assertEquals(res.get(1).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); cal.set(2018, 4, 1, 0, 0, 0); assertEquals(res.get(2).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); }
@Test public void testGroupWithBadInput() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate ("Today is February 15 2018 Tomorrow is 2/16/2018. I'll blow up the world on Feb 32, 2018 " + "and fly back to mars on 13/31/2018"); assertEquals(res.size(), 2); res.sort((a, b) -> Double.compare(a.getNumericValue(), b.getNumericValue())); Calendar cal = new GregorianCalendar(); cal.set(2018, 1, 15, 0, 0, 0); assertEquals(res.get(0).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); cal.set(2018, 1, 16, 0, 0, 0); assertEquals(res.get(1).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); }
@Test public void testBasic() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("02-08-2018"); assertEquals(res.size(), 1); assertEquals(res.get(0).getRelation(), SugiliteRelation.CONTAINS_DATE); Calendar cal = new GregorianCalendar(); cal.set(2018, 1, 8, 0, 0, 0); assertEquals(res.get(0).getNumericValue().longValue()/1000, cal.getTime().getTime()/1000); }