/** * Returns a RegexNamefinder that will utilize specified default regexes. * * @param defaults the OpenNLP default regexes * @return {@link RegexNameFinder} */ public static synchronized RegexNameFinder getDefaultRegexNameFinders( DEFAULT_REGEX_NAME_FINDER... defaults) { Objects.requireNonNull(defaults, "defaults must not be null"); return new RegexNameFinder(defaultsToMap(defaults)); }
/** * Allows for use of selected Defaults as well as regexes from external * configuration * * @param config a map where the key is a type, and the value is a * Pattern[]. If the keys clash with default keys, the config * map will win * @param defaults the OpenNLP default regexes * @return {@link RegexNameFinder} */ public static synchronized RegexNameFinder getDefaultRegexNameFinders( Map<String, Pattern[]> config, DEFAULT_REGEX_NAME_FINDER... defaults) { Objects.requireNonNull(config, "config must not be null"); Map<String, Pattern[]> defaultsToMap = new HashMap<>(); if (defaults != null) { defaultsToMap = defaultsToMap(defaults); } defaultsToMap.putAll(config); return new RegexNameFinder(defaultsToMap); }
@Test public void testFindMatchingPatternWithoutMatchingTokenBounds() { Pattern testPattern = Pattern.compile("[0-8] year"); // does match "0 year" String[] sentence = new String[]{"a", "80", "year", "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 == 0); } }
@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); }
@Test public void testFindTokenizdPattern() { Pattern testPattern = Pattern.compile("[0-9]+ year"); String[] sentence = new String[]{"a", "80", "year", "b", "c"}; Pattern[] patterns = new Pattern[]{testPattern}; Map<String, Pattern[]> regexMap = new HashMap<>(); String type = "match"; 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() == 3); Assert.assertTrue(result[0].getType().equals("match")); }
/** * Returns a RegexNamefinder that will utilize specified default regexes. * * @param defaults the OpenNLP default regexes * @return {@link RegexNameFinder} */ public static synchronized RegexNameFinder getDefaultRegexNameFinders( DEFAULT_REGEX_NAME_FINDER... defaults) { Objects.requireNonNull(defaults, "defaults must not be null"); return new RegexNameFinder(defaultsToMap(defaults)); }
/** * Returns a RegexNamefinder that will utilize specified default regexes. * * @param defaults the OpenNLP default regexes * @return {@link RegexNameFinder} */ public static synchronized RegexNameFinder getDefaultRegexNameFinders( DEFAULT_REGEX_NAME_FINDER... defaults) { Objects.requireNonNull(defaults, "defaults must not be null"); return new RegexNameFinder(defaultsToMap(defaults)); }
/** * Allows for use of selected Defaults as well as regexes from external * configuration * * @param config a map where the key is a type, and the value is a * Pattern[]. If the keys clash with default keys, the config * map will win * @param defaults the OpenNLP default regexes * @return {@link RegexNameFinder} */ public static synchronized RegexNameFinder getDefaultRegexNameFinders( Map<String, Pattern[]> config, DEFAULT_REGEX_NAME_FINDER... defaults) { Objects.requireNonNull(config, "config must not be null"); Map<String, Pattern[]> defaultsToMap = new HashMap<>(); if (defaults != null) { defaultsToMap = defaultsToMap(defaults); } defaultsToMap.putAll(config); return new RegexNameFinder(defaultsToMap); }
/** * Allows for use of selected Defaults as well as regexes from external * configuration * * @param config a map where the key is a type, and the value is a * Pattern[]. If the keys clash with default keys, the config * map will win * @param defaults the OpenNLP default regexes * @return {@link RegexNameFinder} */ public static synchronized RegexNameFinder getDefaultRegexNameFinders( Map<String, Pattern[]> config, DEFAULT_REGEX_NAME_FINDER... defaults) { Objects.requireNonNull(config, "config must not be null"); Map<String, Pattern[]> defaultsToMap = new HashMap<>(); if (defaults != null) { defaultsToMap = defaultsToMap(defaults); } defaultsToMap.putAll(config); return new RegexNameFinder(defaultsToMap); }