TimeAnnotator 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 regex12h = "\\b(0?[1-9]|1[0-2]):([0-5][0-9])(\\s)?([apAP][mM]?)\\b"; String regex24h = "\\b([0-1]?[0-9]|2[0-3]):([0-5][0-9])\\b"; String regexH = "\\b((0?[1-9])|(1[0-2]))(\\s)?([apAP][mM]?)\\b"; Pattern pattern12h = Pattern.compile(regex12h); Pattern pattern24h = Pattern.compile(regex24h); Pattern patternH = Pattern.compile(regexH); boolean[] checkList = new boolean[text.length()]; for (boolean b : checkList) b = false; Matcher matcher = pattern12h.matcher(text); while (matcher.find()) { String matchedString = text.substring(matcher.start(), matcher.end()); boolean foundBefore = false; for (int i = matcher.start(); i < matcher.end(); i++) { if (checkList[i]) { foundBefore = true; break; } checkList[i] = true; } if (foundBefore) continue; String hourString = matchedString.split(":")[0]; String minuteString = matchedString.split(":")[1].substring(0, 2); Double value = null; try { int hour = Integer.valueOf(hourString) % 12; if (matchedString.contains("p") || matchedString.contains("P")) hour += 12; int minute = Integer.valueOf(minuteString); value = (double) (hour * 60 + minute) * 60; } catch (Exception e) { e.printStackTrace(); } AnnotatingResult result = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value); results.add(result); } matcher = pattern24h.matcher(text); while (matcher.find()) { String matchedString = text.substring(matcher.start(), matcher.end()); boolean foundBefore = false; for (int i = matcher.start(); i < matcher.end(); i++) { if (checkList[i]) { foundBefore = true; break; } checkList[i] = true; } if (foundBefore) continue; String hourString = matchedString.split(":")[0]; String minuteString = matchedString.split(":")[1]; Double value = null; try { int hour = Integer.valueOf(hourString); int minute = Integer.valueOf(minuteString); value = (double) (hour * 60 + minute) * 60; } catch (Exception e) { e.printStackTrace(); } AnnotatingResult result = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value); results.add(result); } matcher = patternH.matcher(text); while (matcher.find()) { String matchedString = text.substring(matcher.start(), matcher.end()); boolean foundBefore = false; for (int i = matcher.start(); i < matcher.end(); i++) { if (checkList[i]) { foundBefore = true; break; } checkList[i] = true; } if (foundBefore) continue; String hourString = matchedString.split("(\\s)?[aApP]")[0]; Double value = null; try { int hour = Integer.valueOf(hourString) % 12; if (matchedString.contains("p") || matchedString.contains("P")) hour += 12; value = (double) (hour * 60) * 60; } catch (Exception e) { e.printStackTrace(); } AnnotatingResult result = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value); results.add(result); } cache.put(text, results); return results; } TimeAnnotator(); @Override List<AnnotatingResult> annotate(String text);  }
@Test public void testBasicInput1() { List<SugiliteTextParentAnnotator.AnnotatingResult> results = annotator.annotate("Current time: 11:59 pm"); assertTrue(results.size() == 1); assertEquals(results.get(0).getNumericValue().intValue(), (23*3600)+(59*60)); assertTrue(results.get(0).getRelation().equals(SugiliteRelation.CONTAINS_TIME)); }
@Test public void testBasicInput2() { List<SugiliteTextParentAnnotator.AnnotatingResult> results = annotator.annotate("Current time: 13:59"); assertTrue(results.size() == 1); assertEquals(results.get(0).getNumericValue().intValue(), (13*3600)+(59*60)); assertTrue(results.get(0).getRelation().equals(SugiliteRelation.CONTAINS_TIME)); }
@Test public void testBasicInput3() { List<SugiliteTextParentAnnotator.AnnotatingResult> results = annotator.annotate("Current time: 3 PM"); assertTrue(results.size() == 1); assertEquals(results.get(0).getNumericValue().intValue(), 15*3600); assertTrue(results.get(0).getRelation().equals(SugiliteRelation.CONTAINS_TIME)); }
@Test public void testBadInput1() { List<SugiliteTextParentAnnotator.AnnotatingResult> results = annotator.annotate("Bad time: 0:60 pm"); assertTrue(results.size() == 0); }
@Test public void testBadInput2() { List<SugiliteTextParentAnnotator.AnnotatingResult> results = annotator.annotate("Bad time: 24:00"); assertTrue(results.size() == 0); }
@Test public void testMultipleInputs1() { List<SugiliteTextParentAnnotator.AnnotatingResult> results = annotator.annotate("Current bus: 12:00 pm, next bus: 13:59, next next bus: 4PM"); assertTrue(results.size() == 3); results.sort((a, b) -> Double.compare(a.getNumericValue(), b.getNumericValue())); assertEquals(results.get(0).getNumericValue().intValue(), (12*3600)); assertEquals(results.get(1).getNumericValue().intValue(), (13*3600)+(59*60)); assertEquals(results.get(2).getNumericValue().intValue(), (16*3600)); }
@Test public void testMultipleInputs2() { List<SugiliteTextParentAnnotator.AnnotatingResult> results = annotator.annotate("Flight takes off at 4PM and boarding stops at 3:45pm." + "The flight takes 32:15 and arrives at 00:00"); assertTrue(results.size() == 3); results.sort((a, b) -> Double.compare(a.getNumericValue(), b.getNumericValue())); assertEquals(results.get(0).getNumericValue().intValue(), (0)); assertEquals(results.get(1).getNumericValue().intValue(), (15*3600)+(45*60)); assertEquals(results.get(2).getNumericValue().intValue(), (16*3600)); }