@Test public void testFindSingleTokenPattern() { Pattern testPattern = Pattern.compile("test"); String[] sentence = new String[]{"a", "test", "b", "c"}; Pattern[] patterns = new Pattern[]{testPattern}; Map<String, Pattern[]> regexMap = new HashMap<>(); String type = "testtype"; regexMap.put(type, patterns); RegexNameFinder finder = new RegexNameFinder(regexMap); Span[] result = finder.find(sentence); Assert.assertTrue(result.length == 1); Assert.assertTrue(result[0].getStart() == 1); Assert.assertTrue(result[0].getEnd() == 2); }
@Override public Span[] find(String[] tokens) { Map<Integer, Integer> sentencePosTokenMap = new HashMap<>(); StringBuilder sentenceString = new StringBuilder(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<>(); if (regexMap != null) { for (Map.Entry<String, Pattern[]> entry : regexMap.entrySet()) { for (Pattern mPattern : entry.getValue()) { 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, entry.getKey()); annotations.add(annotation); } } } } } else { 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 implements TokenNameFinder { @Override public Span[] find(String[] tokens) { Map<Integer, Integer> sentencePosTokenMap = new HashMap<>(); StringBuilder sentenceString = new StringBuilder(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<>(); if (regexMap != null) { for (Map.Entry<String, Pattern[]> entry : regexMap.entrySet()) { for (Pattern mPattern : entry.getValue()) { 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, entry.getKey()); annotations.add(annotation); } } } } } else { 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 implements TokenNameFinder { @Override public Span[] find(String[] tokens) { Map<Integer, Integer> sentencePosTokenMap = new HashMap<>(); StringBuilder sentenceString = new StringBuilder(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<>(); if (regexMap != null) { for (Map.Entry<String, Pattern[]> entry : regexMap.entrySet()) { for (Pattern mPattern : entry.getValue()) { 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, entry.getKey()); annotations.add(annotation); } } } } } else { 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(Map<String, Pattern[]> regexMap);  RegexNameFinder(Pattern[] patterns, String type); @Deprecated  RegexNameFinder(Pattern[] patterns); }
RegexNameFinder implements TokenNameFinder { @Override public Span[] find(String[] tokens) { Map<Integer, Integer> sentencePosTokenMap = new HashMap<>(); StringBuilder sentenceString = new StringBuilder(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<>(); if (regexMap != null) { for (Map.Entry<String, Pattern[]> entry : regexMap.entrySet()) { for (Pattern mPattern : entry.getValue()) { 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, entry.getKey()); annotations.add(annotation); } } } } } else { 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(Map<String, Pattern[]> regexMap);  RegexNameFinder(Pattern[] patterns, String type); @Deprecated  RegexNameFinder(Pattern[] patterns); @Override Span[] find(String[] tokens); Span[] find(String text); @Override void clearAdaptiveData(); Pattern[] getmPatterns(); void setmPatterns(Pattern[] mPatterns); String getsType(); void setsType(String sType); }
RegexNameFinder implements TokenNameFinder { @Override public Span[] find(String[] tokens) { Map<Integer, Integer> sentencePosTokenMap = new HashMap<>(); StringBuilder sentenceString = new StringBuilder(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<>(); if (regexMap != null) { for (Map.Entry<String, Pattern[]> entry : regexMap.entrySet()) { for (Pattern mPattern : entry.getValue()) { 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, entry.getKey()); annotations.add(annotation); } } } } } else { 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(Map<String, Pattern[]> regexMap);  RegexNameFinder(Pattern[] patterns, String type); @Deprecated  RegexNameFinder(Pattern[] patterns); @Override Span[] find(String[] tokens); Span[] find(String text); @Override void clearAdaptiveData(); Pattern[] getmPatterns(); void setmPatterns(Pattern[] mPatterns); String getsType(); void setsType(String sType);  }