LengthAnnotator 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[0-9]+?(.)?[0-9]*?(\\s)?(km|m|ft|feet|mile(s)?|mi|yd|inch|cm)\\b"; Pattern pattern = Pattern.compile(regex); int curEnd = -3; Matcher matcher = pattern.matcher(text); while (matcher.find()) { 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()); } try { double value = -1; double num = Double.valueOf(parsed[0]); if (parsed[1].contains("mi")) value = (num * MILE); else if (parsed[1].contains("k")) value = (num * KILOMETER); else if (parsed[1].startsWith("m")) value = (num * METER); else if (parsed[1].contains("y")) value = (num * YARD); else if (parsed[1].contains("f")) value = (num * FEET); else if (parsed[1].startsWith("i")) value = (num * INCH); else if (parsed[1].startsWith("c")) value = (num * CENTIMETER); if (value >= 0) { if (matcher.start() - curEnd == 1 && text.charAt(curEnd) == ' ') { AnnotatingResult last = results.get(results.size() - 1); last.setNumericValue(last.getNumericValue() + value); } else { AnnotatingResult res = new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value); results.add(res); } curEnd = matcher.end(); } } catch (Exception e) { e.printStackTrace(); } } cache.put(text, results); return results; } LengthAnnotator(); @Override List<AnnotatingResult> annotate(String text); static void main(String[] args);  }
@Test public void testBasic() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("Distance to school: 5 miles"); assertEquals(res.size(), 1); assertEquals(res.get(0).getRelation(), SugiliteRelation.CONTAINS_LENGTH); assertEquals(res.get(0).getNumericValue().intValue(), 5 * MILE); }
@Test public void testBasic1() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("My height is 5 ft 11.5 inch"); assertEquals(res.size(), 1); assertEquals(res.get(0).getNumericValue().intValue(), (int)(5 * FEET + 11.5 * INCH)); }
@Test public void testBasic2() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("Today I ran 5 km 432.10 m"); assertEquals(res.size(), 1); assertEquals(res.get(0).getNumericValue().intValue(), (int)(5.4321 * KILOMETER)); }
@Test public void testBasic3() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("I want a 12 inch pizza"); assertEquals(res.size(), 1); assertEquals(res.get(0).getNumericValue().intValue(), (int)(12 * INCH)); }
@Test public void testBadInput() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("6.a km, 3m, 1.2 in"); }
@Test public void testGroup() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("To downtown: 5.5 mi, " + "To CMU: 6 km 666 m, To hell: 1 ft 2.34 inch"); assertEquals(res.size(), 3); res.sort((a, b) -> Double.compare(a.getNumericValue(), b.getNumericValue())); assertEquals(res.get(0).getNumericValue().intValue(), (int)(FEET + 2.34 * INCH)); assertEquals(res.get(1).getNumericValue().intValue(), 6 * KILOMETER + 666 * METER); assertEquals(res.get(2).getNumericValue().intValue(), (int)(5.5 * MILE)); }