Stanford CoreNLP属性文件未找到

时间:2016-02-24 10:52:43

标签: java sentiment-analysis

我正在尝试对推文进行情绪分析,但会遇到奇怪的异常。

我正在使用属性文件初始化管道,并将属性文件放在资源目录中的src->主文件夹中。 enter image description here

但仍然在init函数中获得Exception:

Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException:    java.io.IOException: Unable to open "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz" as class path, filename or URL
at edu.stanford.nlp.parser.common.ParserGrammar.loadModel(ParserGrammar.java:188)
at edu.stanford.nlp.pipeline.ParserAnnotator.loadModel(ParserAnnotator.java:212)
at edu.stanford.nlp.pipeline.ParserAnnotator.<init>(ParserAnnotator.java:115)
at edu.stanford.nlp.pipeline.AnnotatorImplementations.parse(AnnotatorImplementations.java:150)
at edu.stanford.nlp.pipeline.AnnotatorFactories$11.create(AnnotatorFactories.java:463)
at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:85)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:375)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:139)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:135)
at com.mycompany.sentmentanalysisontweets.NLP.init(NLP.java:27)
at com.mycompany.sentmentanalysisontweets.WhatToThink.main(WhatToThink.java:15)

在主要方法中,我调用init()方法。

public class Main {

    public static void main(String[] args) {
        NLP.init();
    }
}

class NLP {
    static StanfordCoreNLP pipeline;

    public static void init() {
        InputStream input = NLP.class.getClass().getResourceAsStream("/example.properties");
        Properties prop = new Properties();
        try {
          prop.load(input);
        } catch (IOException e) {
          e.printStackTrace();
        }

        pipeline = new StanfordCoreNLP(prop);
    }
}

2 个答案:

答案 0 :(得分:1)

具有以下结构:

src/main/resources
             |
              - example.properties

执行以下代码可以解决您的问题:

public class Main {

    public static void main(String[] args) {
        NLP.init();
    }
}

class NLP {
   static StanfordCoreNLP pipeline;

   public static void init() {
        InputStream input = NLP.class.getClass().getResourceAsStream("/example.properties");
        Properties prop = new Properties();
        try {
            prop.load(input);
        } catch (IOException e) {
            e.printStackTrace();
        }

        pipeline = new StanfordCoreNLP(prop);
    }
}

修改

您的pom.xml必须包含以下依赖项:

<dependencies>
    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.6.0</version>
    </dependency>
    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.6.0</version>
        <classifier>models</classifier>
    </dependency>
</dependencies>

答案 1 :(得分:0)

使用

            InputStream input = new FileInputStream(new File("./", "example.properties"));

而不是

            InputStream input = StanfordCoreNLP.class.getClass().getResourceAsStream("/example.properties");

将解决问题