【问题标题】:Stanford CoreNLP properties File not found斯坦福 CoreNLP 属性文件未找到
【发布时间】:2016-02-24 10:52:42
【问题描述】:

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

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

但在初始化函数中仍然出现异常:

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);
    }
}

【问题讨论】:

  • 您应该将您的properties 文件放在您的src/main/resources 文件夹中。
  • 我试过了,但还是不行。
  • 请检查我的回答。

标签: java sentiment-analysis


【解决方案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>

【讨论】:

  • 它给出了异常:无法打开“edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz”作为类路径
  • 您的代码运行正常,但在下面显示的调试器显示异常.. "pipeline = new StanfordCoreNLP(prop);"
  • 抛出的异常是什么?你介意用你的编辑更新你的问题吗?
  • @ariberio 我编辑了问题并更新了异常。
  • @MuhammadUmar,您的classpath 中缺少stanford-corenlp-X.X.X-models.jar。您可以在here 上找到当前版本,也可以在Maven's Central Repository 上按版本直接浏览。
【解决方案2】:

使用

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

而不是

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

解决问题

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多