NumberAnnotator 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]{3})*(\\.[0-9]+)?\\b"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String matchedString = text.substring(matcher.start(), matcher.end()).replaceAll(",", ""); double value = Double.valueOf(matchedString); results.add(new AnnotatingResult(RELATION, text.substring(matcher.start(), matcher.end()), matcher.start(), matcher.end(), value)); } cache.put(text, results); return results; } NumberAnnotator(); @Override List<AnnotatingResult> annotate(String text);  }
@Test public void testBasic() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("34.56"); assertEquals(res.size(), 1); assertEquals(res.get(0).getRelation(), SugiliteRelation.CONTAINS_NUMBER); assertEquals(Double.compare(res.get(0).getNumericValue(), 34.56), 0); }
@Test public void testBasic1() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("12,345.67"); assertEquals(res.size(), 1); assertEquals(res.get(0).getRelation(), SugiliteRelation.CONTAINS_NUMBER); assertEquals(Double.compare(res.get(0).getNumericValue(), 12345.67), 0); }
@Test public void testMultiple() { List<SugiliteTextParentAnnotator.AnnotatingResult> res = annotator.annotate("2, 3.05, 23,333,444.08"); assertEquals(res.size(), 3); assertEquals(Double.compare(res.get(0).getNumericValue(), 2), 0); assertEquals(Double.compare(res.get(1).getNumericValue(), 3.05), 0); assertEquals(Double.compare(res.get(2).getNumericValue(), 23333444.08), 0); }