RegexNameFinder implements TokenNameFinder { public Span[] find(String tokens[]) { Map<Integer, Integer> sentencePosTokenMap = new HashMap<Integer, Integer>(); StringBuffer sentenceString = new StringBuffer(tokens.length * 10); for (int i = 0; i < tokens.length; i++) { int startIndex = sentenceString.length(); sentencePosTokenMap.put(startIndex, i); sentenceString.append(tokens[i]); int endIndex = sentenceString.length(); sentencePosTokenMap.put(endIndex, i + 1); if (i < tokens.length - 1) { sentenceString.append(' '); } } Collection<Span> annotations = new LinkedList<Span>(); for (Pattern mPattern : mPatterns) { Matcher matcher = mPattern.matcher(sentenceString); while (matcher.find()) { Integer tokenStartIndex = sentencePosTokenMap.get(matcher.start()); Integer tokenEndIndex = sentencePosTokenMap.get(matcher.end()); if (tokenStartIndex != null && tokenEndIndex != null) { Span annotation = new Span(tokenStartIndex, tokenEndIndex, sType); annotations.add(annotation); } } } return annotations.toArray( new Span[annotations.size()]); } RegexNameFinder(Pattern patterns[], String type);  RegexNameFinder(Pattern patterns[]); Span[] find(String tokens[]); void clearAdaptiveData();  }
@Test public void testFindSingleTokenPattern() { Pattern testPattern = Pattern.compile("test"); String sentence[] = new String[]{"a", "test", "b", "c"}; RegexNameFinder finder = new RegexNameFinder(new Pattern[]{testPattern}); Span[] result = finder.find(sentence); assertTrue(result.length == 1); assertTrue(result[0].getStart() == 1); assertTrue(result[0].getEnd() == 2); }
@Test public void testFindTokenizdPattern() { Pattern testPattern = Pattern.compile("[0-9]+ year"); String sentence[] = new String[]{"a", "80", "year", "b", "c"}; RegexNameFinder finder = new RegexNameFinder(new Pattern[]{testPattern}, "match"); Span[] result = finder.find(sentence); assertTrue(result.length == 1); assertTrue(result[0].getStart() == 1); assertTrue(result[0].getEnd() == 3); assertTrue(result[0].getType().equals("match")); }
@Test public void testFindMatchingPatternWithoutMatchingTokenBounds() { Pattern testPattern = Pattern.compile("[0-8] year"); String sentence[] = new String[]{"a", "80", "year", "c"}; RegexNameFinder finder = new RegexNameFinder(new Pattern[]{testPattern}); Span[] result = finder.find(sentence); assertTrue(result.length == 0); }