DurationAnnotator implements SugiliteTextAnnotator { @Override public List<AnnotatingResult> annotate(String text) { text = text.replaceAll("[\\u00A0\\u2007\\u202F]+", " "); if (cache.containsKey(text)){ return cache.get(text); } List<AnnotatingResult> results = new ArrayList<>(); String regex = "\\b\\d+?(.\\d+?)?(\\s)?(d|h|hr(s)?|hour(s)?|m|min(s)?|minute(s)?|s|sec)\\b"; Pattern pattern = Pattern.compile(regex); int curEnd = -3; Matcher matcher = pattern.matcher(text); while (matcher.find()) { try { String matchedString = text.substring(matcher.start(), matcher.end()); String[] parsed; if (matchedString.contains(" ")) parsed = matchedString.split(" "); else { parsed = new String[2]; parsed[0] = matchedString.split("[a-z]")[0]; parsed[1] = matchedString.substring(parsed[0].length()); } double num = Double.valueOf(parsed[0]); if (parsed[1].startsWith("h")) num *= HOUR; else if (parsed[1].startsWith("d")) num *= DAY; else if (parsed[1].startsWith("m")) num *= MINUTE; else if (parsed[1].startsWith("s")) num *= SECOND; if (matcher.start() - curEnd == 1 && text.charAt(curEnd) == ' ') { AnnotatingResult last = results.get(results.size() - 1); last.setNumericValue(last.getNumericValue() + num); } else { AnnotatingResult res = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), num); results.add(res); } curEnd = matcher.end(); } catch (Exception e){ e.printStackTrace(); } } cache.put(text, results); return results; } DurationAnnotator(); @Override List<AnnotatingResult> annotate(String text); static void main(String[] args );  }
@Test public void testBasic() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("5 hrs"); assertEquals(res.size(), 1); assertEquals(res.get(0).getRelation(), SugiliteRelation.CONTAINS_DURATION); assertEquals(res.get(0).getNumericValue().intValue(), 5*3600000); }
@Test public void testBasic1() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("4 d 33.5 minutes 5 s"); assertEquals(res.size(), 1); assertEquals(res.get(0).getRelation(), SugiliteRelation.CONTAINS_DURATION); assertEquals(res.get(0).getNumericValue().intValue(), (int)(4*86400000+33.5*60000+5*1000)); }
@Test public void testBasic2() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("flight time: 17hr 16.5mins, " + "flight departs at 19:05"); assertEquals(res.size(), 1); assertEquals(res.get(0).getRelation(), SugiliteRelation.CONTAINS_DURATION); assertEquals(res.get(0).getNumericValue().intValue(), (int)(17*3600000+16.5*60000)); }
@Test public void testMultiple() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("Flights from Pittsburgh to" + "Havana: Delta: 9 hr 36 min, American: 10 hours, United: 9 h 40sec"); assertEquals(res.size(), 3); res.sort(Comparator.comparingDouble(SugiliteTextParentAnnotator.AnnotatingResult::getNumericValue)); assertEquals(res.get(0).getNumericValue().intValue(), 9*3600000+40*1000); assertEquals(res.get(1).getNumericValue().intValue(), 9*3600000+36*60000); assertEquals(res.get(2).getNumericValue().intValue(), 10*3600000); }
@Test public void testMultipleWithBadInput() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("Durations: 5:30, 5 hr 29 min, " + "5:28:00"); assertEquals(res.size(), 1); assertEquals(res.get(0).getNumericValue().intValue(), 5*3600000+29*60000); }