stanfordnlp / CoreNLP

CoreNLP: A Java suite of core NLP tools for tokenization, sentence segmentation, NER, parsing, coreference, sentiment analysis, etc.
http://stanfordnlp.github.io/CoreNLP/
GNU General Public License v3.0
9.71k stars 2.7k forks source link

Cannot instantiate a StanfordCoreNLP pipeline in a Springboot Project using Maven #1448

Open clxxiii opened 6 months ago

clxxiii commented 6 months ago

I made a super simple example maven project, that has two dependencies: stanford-corenlp and spring-boot-starter-web, and got the pom.xml trimmed down as much as I know how.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.5</version>
    <relativePath/>
  </parent>

  <groupId>com.test</groupId>
  <artifactId>spring-nlp-test</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
    <maven.compiler.release>21</maven.compiler.release>
  </properties>

  <dependencies>
    <!-- Stanford Core NLP import -->
    <dependency>
      <groupId>edu.stanford.nlp</groupId>
      <artifactId>stanford-corenlp</artifactId>
      <version>4.5.5</version>
    </dependency>
    <!-- Import Models -->
    <dependency>
      <groupId>edu.stanford.nlp</groupId>
      <artifactId>stanford-corenlp</artifactId>
      <version>4.5.5</version>
      <classifier>models</classifier>
    </dependency>
    <!-- Springboot Dependency -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

My example project only has one file, an App.java with a main class which only makes a StanfordCoreNLP pipeline.

package com.test;

import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import java.util.Properties;

public class App {
    public static void main( String[] args ) {
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize,pos,lemma,ner,depparse");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    }
}

Running this main function results in the following error:

May 24, 2024 11:28:29 AM de.jollyday.util.XMLUtil unmarshallConfiguration
WARNING: Could not create JAXB context using the current threads context classloader. Defaulting to ObjectFactory classloader.
Exception in thread "main" edu.stanford.nlp.util.ReflectionLoading$ReflectionLoadingException: Error creating edu.stanford.nlp.time.TimeExpressionExtractorImpl
    at edu.stanford.nlp.util.ReflectionLoading.loadByReflection(ReflectionLoading.java:38)
    at edu.stanford.nlp.time.TimeExpressionExtractorFactory.create(TimeExpressionExtractorFactory.java:60)
    at edu.stanford.nlp.time.TimeExpressionExtractorFactory.createExtractor(TimeExpressionExtractorFactory.java:43)
    at edu.stanford.nlp.ie.regexp.NumberSequenceClassifier.<init>(NumberSequenceClassifier.java:86)
    at edu.stanford.nlp.ie.NERClassifierCombiner.<init>(NERClassifierCombiner.java:110)
    at edu.stanford.nlp.pipeline.NERCombinerAnnotator.<init>(NERCombinerAnnotator.java:147)
    at edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(AnnotatorImplementations.java:90)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$8(StanfordCoreNLP.java:632)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$null$33(StanfordCoreNLP.java:711)
    at edu.stanford.nlp.util.Lazy$3.compute(Lazy.java:126)
    at edu.stanford.nlp.util.Lazy.get(Lazy.java:31)
    at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:149)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:280)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:194)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:190)
    at com.test.App.main(App.java:12)
Caused by: edu.stanford.nlp.util.MetaClass$ClassCreationException: MetaClass couldn't create public edu.stanford.nlp.time.TimeExpressionExtractorImpl(java.lang.String,java.util.Properties) with args [sutime, {}]
    at edu.stanford.nlp.util.MetaClass$ClassFactory.createInstance(MetaClass.java:237)
    at edu.stanford.nlp.util.MetaClass.createInstance(MetaClass.java:372)
    at edu.stanford.nlp.util.ReflectionLoading.loadByReflection(ReflectionLoading.java:36)
    ... 15 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:74)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at edu.stanford.nlp.util.MetaClass$ClassFactory.createInstance(MetaClass.java:233)
    ... 17 more
Caused by: java.lang.RuntimeException: Error initializing binder 1
    at edu.stanford.nlp.time.Options.<init>(Options.java:114)
    at edu.stanford.nlp.time.TimeExpressionExtractorImpl.init(TimeExpressionExtractorImpl.java:45)
    at edu.stanford.nlp.time.TimeExpressionExtractorImpl.<init>(TimeExpressionExtractorImpl.java:40)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    ... 20 more
Caused by: java.lang.IllegalStateException: Cannot instantiate configuration.
    at de.jollyday.datasource.impl.XmlFileDataSource.getConfiguration(XmlFileDataSource.java:39)
    at de.jollyday.impl.DefaultHolidayManager.doInit(DefaultHolidayManager.java:239)
    at de.jollyday.HolidayManager.init(HolidayManager.java:319)
    at de.jollyday.caching.HolidayManagerValueHandler.createValue(HolidayManagerValueHandler.java:44)
    at de.jollyday.caching.HolidayManagerValueHandler.createValue(HolidayManagerValueHandler.java:13)
    at de.jollyday.util.Cache.get(Cache.java:51)
    at de.jollyday.HolidayManager.createManager(HolidayManager.java:168)
    at de.jollyday.HolidayManager.getInstance(HolidayManager.java:148)
    at edu.stanford.nlp.time.JollyDayHolidays.init(JollyDayHolidays.java:57)
    at edu.stanford.nlp.time.Options.<init>(Options.java:112)
    ... 23 more
Caused by: java.lang.IllegalStateException: Cannot parse holidays XML file.
    at de.jollyday.util.XMLUtil.unmarshallConfiguration(XMLUtil.java:78)
    at de.jollyday.datasource.impl.XmlFileDataSource.getConfiguration(XmlFileDataSource.java:37)
    ... 32 more
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:168)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:355)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:508)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:465)
    at de.jollyday.util.XMLUtil$JAXBContextCreator.create(XMLUtil.java:170)
    at de.jollyday.util.XMLUtil.unmarshallConfiguration(XMLUtil.java:71)
    ... 33 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
    at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
    at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:165)
    ... 38 more

These two libraries share zero of the same dependencies (which you can verify if you recreate my setup you can use mvn depenency:tree). Both libraries have similar slf4j dependencies, but excluding these dependencies from both sides still causes the same issue.

clxxiii commented 6 months ago

As a temporary fix, I'm able to pass props.setProperty("ner.useSUTime", "0"); to the NLP constructor to disable the problematic import.

AngledLuffa commented 6 months ago

What version Java? There are Java versions where the current release of jollyday isn't compatible. There are a few library upgrades we could make which would better support all versions of Java 17, for example.

clxxiii commented 6 months ago

This is Java 21

j3ernhard commented 5 months ago

JAX-B Java XML binding is since Java 11 no longer part of Java.

Libraries for JAX-B (API, implementation and runtime) have to be included as dependencies:

         <dependency> 
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>4.0.5</version>
        </dependency>

In my project it works with Java 21 and Spring Boot 3, but Spring Boot 3 is the release with jakarta migration.