/** * Construct the default annotator pool from the passed in properties, and overwriting annotators which have changed * since the last call. * * @param inputProps Properties to determine behavior of annotators * @param annotatorImplementation Source of annotator implementations * @return A populated AnnotatorPool */ private static AnnotatorPool constructAnnotatorPool(final Properties inputProps, final AnnotatorImplementations annotatorImplementation) { AnnotatorPool pool = new AnnotatorPool(); for (Map.Entry<String, BiFunction<Properties, AnnotatorImplementations, Annotator>> entry : getNamedAnnotators().entrySet()) { AnnotatorSignature key = new AnnotatorSignature(entry.getKey(), PropertiesUtils.getSignature(entry.getKey(), inputProps)); pool.register(entry.getKey(), inputProps, GLOBAL_ANNOTATOR_CACHE.computeIfAbsent(key, (sig) -> Lazy.cache(() -> entry.getValue().apply(inputProps, annotatorImplementation)))); } registerCustomAnnotators(pool, annotatorImplementation, inputProps); return pool; }
/** * Either get a custom annotator which was recently defined, or create it if it has never been defined. * This method is synchronized to avoid race conditions when loading the annotators. * * @param name The name of the annotator. * @param props The properties used to create the annotator, if we need to create it. * @param annotator The actual function used to make the annotator, if needed. * * @return An annotator as specified by the given name and properties. */ private synchronized static Supplier<Annotator> getOrCreate(String name, Properties props, Supplier<Annotator> annotator) { StanfordCoreNLP.AnnotatorSignature key = new StanfordCoreNLP.AnnotatorSignature(name, PropertiesUtils.getSignature(name, props)); customAnnotators.register(name, props, StanfordCoreNLP.GLOBAL_ANNOTATOR_CACHE.computeIfAbsent(key, (sig) -> Lazy.cache(annotator))); return () -> customAnnotators.get(name); }
pool.clear();
logger.info("Adding annotator " + name); Annotator an = pool.get(name); this.addAnnotator(an);
/** * Construct the default annotator pool, and save it as the static annotator pool * for CoreNLP. * * @see StanfordCoreNLP#constructAnnotatorPool(Properties, AnnotatorImplementations) */ public static synchronized AnnotatorPool getDefaultAnnotatorPool(final Properties inputProps, final AnnotatorImplementations annotatorImplementation) { // if the pool already exists reuse! AnnotatorPool pool = AnnotatorPool.SINGLETON; for (Map.Entry<String, BiFunction<Properties, AnnotatorImplementations, Annotator>> entry : getNamedAnnotators().entrySet()) { AnnotatorSignature key = new AnnotatorSignature(entry.getKey(), PropertiesUtils.getSignature(entry.getKey(), inputProps)); pool.register(entry.getKey(), inputProps, GLOBAL_ANNOTATOR_CACHE.computeIfAbsent(key, (sig) -> Lazy.cache(() -> entry.getValue().apply(inputProps, annotatorImplementation)))); } registerCustomAnnotators(pool, annotatorImplementation, inputProps); return pool; }
public static synchronized Annotator getExistingAnnotator(String name) { if(pool == null){ System.err.println("ERROR: attempted to fetch annotator \"" + name + "\" before the annotator pool was created!"); return null; } try { Annotator a = pool.get(name); return a; } catch(IllegalArgumentException e) { System.err.println("ERROR: attempted to fetch annotator \"" + name + "\" but the annotator pool does not store any such type!"); return null; } }
/** * Register any custom annotators defined in the input properties, and add them to the pool. * * @param pool The annotator pool to add the new custom annotators to. * @param annotatorImplementation The implementation thunk to use to create any new annotators. * @param inputProps The properties to read new annotator definitions from. */ private static void registerCustomAnnotators(AnnotatorPool pool, AnnotatorImplementations annotatorImplementation, Properties inputProps) { // add annotators loaded via reflection from class names specified // in the properties for (String property : inputProps.stringPropertyNames()) { if (property.startsWith(CUSTOM_ANNOTATOR_PREFIX)) { final String customName = property.substring(CUSTOM_ANNOTATOR_PREFIX.length()); final String customClassName = inputProps.getProperty(property); logger.info("Registering annotator " + customName + " with class " + customClassName); AnnotatorSignature key = new AnnotatorSignature(customName, PropertiesUtils.getSignature(customName, inputProps)); pool.register(customName, inputProps, GLOBAL_ANNOTATOR_CACHE.computeIfAbsent(key, (sig) -> Lazy.cache(() -> annotatorImplementation.custom(inputProps, property)))); } } }
/** * Construct the default annotator pool from the passed in properties, and overwriting annotators which have changed * since the last call. * * @param inputProps Properties to determine behavior of annotators * @param annotatorImplementation Source of annotator implementations * @return A populated AnnotatorPool */ private static AnnotatorPool constructAnnotatorPool(final Properties inputProps, final AnnotatorImplementations annotatorImplementation) { AnnotatorPool pool = new AnnotatorPool(); for (Map.Entry<String, BiFunction<Properties, AnnotatorImplementations, Annotator>> entry : getNamedAnnotators().entrySet()) { AnnotatorSignature key = new AnnotatorSignature(entry.getKey(), PropertiesUtils.getSignature(entry.getKey(), inputProps)); pool.register(entry.getKey(), inputProps, GLOBAL_ANNOTATOR_CACHE.computeIfAbsent(key, (sig) -> Lazy.cache(() -> entry.getValue().apply(inputProps, annotatorImplementation)))); } registerCustomAnnotators(pool, annotatorImplementation, inputProps); return pool; }
/** * Either get a custom annotator which was recently defined, or create it if it has never been defined. * This method is synchronized to avoid race conditions when loading the annotators. * * @param name The name of the annotator. * @param props The properties used to create the annotator, if we need to create it. * @param annotator The actual function used to make the annotator, if needed. * * @return An annotator as specified by the given name and properties. */ private synchronized static Supplier<Annotator> getOrCreate(String name, Properties props, Supplier<Annotator> annotator) { StanfordCoreNLP.AnnotatorSignature key = new StanfordCoreNLP.AnnotatorSignature(name, PropertiesUtils.getSignature(name, props)); customAnnotators.register(name, props, StanfordCoreNLP.GLOBAL_ANNOTATOR_CACHE.computeIfAbsent(key, (sig) -> Lazy.cache(annotator))); return () -> customAnnotators.get(name); }
public static synchronized Annotator getExistingAnnotator(String name) { if(pool == null){ System.err.println("ERROR: attempted to fetch annotator \"" + name + "\" before the annotator pool was created!"); return null; } try { Annotator a = pool.get(name); return a; } catch(IllegalArgumentException e) { System.err.println("ERROR: attempted to fetch annotator \"" + name + "\" but the annotator pool does not store any such type!"); return null; } }
/** * Construct the default annotator pool, and save it as the static annotator pool * for CoreNLP. * * @see StanfordCoreNLP#constructAnnotatorPool(Properties, AnnotatorImplementations) */ public static synchronized AnnotatorPool getDefaultAnnotatorPool(final Properties inputProps, final AnnotatorImplementations annotatorImplementation) { // if the pool already exists reuse! AnnotatorPool pool = AnnotatorPool.SINGLETON; for (Map.Entry<String, BiFunction<Properties, AnnotatorImplementations, Annotator>> entry : getNamedAnnotators().entrySet()) { AnnotatorSignature key = new AnnotatorSignature(entry.getKey(), PropertiesUtils.getSignature(entry.getKey(), inputProps)); pool.register(entry.getKey(), inputProps, GLOBAL_ANNOTATOR_CACHE.computeIfAbsent(key, (sig) -> Lazy.cache(() -> entry.getValue().apply(inputProps, annotatorImplementation)))); } registerCustomAnnotators(pool, annotatorImplementation, inputProps); return pool; }
pool = new AnnotatorPool(); pool.register(STANFORD_TOKENIZE, new Factory<Annotator>() { private static final long serialVersionUID = 1L; public Annotator create() { pool.register(STANFORD_CLEAN_XML, new Factory<Annotator>() { private static final long serialVersionUID = 1L; public Annotator create() { pool.register(STANFORD_SSPLIT, new Factory<Annotator>() { private static final long serialVersionUID = 1L; public Annotator create() { pool.register(STANFORD_POS, new Factory<Annotator>() { private static final long serialVersionUID = 1L; public Annotator create() { pool.register(STANFORD_LEMMA, new Factory<Annotator>() { private static final long serialVersionUID = 1L; public Annotator create() { pool.register(STANFORD_NER, new Factory<Annotator>() { private static final long serialVersionUID = 1L; public Annotator create() { pool.register(STANFORD_REGEXNER, new Factory<Annotator>() { private static final long serialVersionUID = 1L; public Annotator create() {
System.err.println("Adding annotator " + name); Annotator an = pool.get(name); this.addAnnotator(an);
/** * Register any custom annotators defined in the input properties, and add them to the pool. * * @param pool The annotator pool to add the new custom annotators to. * @param annotatorImplementation The implementation thunk to use to create any new annotators. * @param inputProps The properties to read new annotator definitions from. */ private static void registerCustomAnnotators(AnnotatorPool pool, AnnotatorImplementations annotatorImplementation, Properties inputProps) { // add annotators loaded via reflection from class names specified // in the properties for (String property : inputProps.stringPropertyNames()) { if (property.startsWith(CUSTOM_ANNOTATOR_PREFIX)) { final String customName = property.substring(CUSTOM_ANNOTATOR_PREFIX.length()); final String customClassName = inputProps.getProperty(property); logger.info("Registering annotator " + customName + " with class " + customClassName); AnnotatorSignature key = new AnnotatorSignature(customName, PropertiesUtils.getSignature(customName, inputProps)); pool.register(customName, inputProps, GLOBAL_ANNOTATOR_CACHE.computeIfAbsent(key, (sig) -> Lazy.cache(() -> annotatorImplementation.custom(inputProps, property)))); } } }
pool = new AnnotatorPool(); pool.register(STANFORD_TOKENIZE, new AnnotatorFactory(inputProps) { private static final long serialVersionUID = 1L; @Override pool.register(STANFORD_CLEAN_XML, new AnnotatorFactory(inputProps) { private static final long serialVersionUID = 1L; @Override pool.register(STANFORD_SSPLIT, new AnnotatorFactory(inputProps) { private static final long serialVersionUID = 1L; @Override pool.register(STANFORD_POS, new AnnotatorFactory(inputProps) { private static final long serialVersionUID = 1L; @Override pool.register(STANFORD_LEMMA, new AnnotatorFactory(inputProps) { private static final long serialVersionUID = 1L; @Override pool.register(STANFORD_NER, new AnnotatorFactory(inputProps) { private static final long serialVersionUID = 1L; @Override pool.register(STANFORD_REGEXNER, new AnnotatorFactory(inputProps) { private static final long serialVersionUID = 1L; @Override
logger.info("Adding annotator " + name); Annotator an = pool.get(name); this.addAnnotator(an);