【问题标题】:Eclipse Maven: "Cannot be resolved to a type" errorEclipse Maven:“无法解析为类型”错误
【发布时间】:2015-11-13 19:21:12
【问题描述】:

我正在尝试使用 OpenNLP 1.5.0 构建一个简单的句子检测器。为此,我正在使用 Maven Eclipse。我从http://opennlp.sourceforge.net/models-1.5/ 下载了“en-sent.bin”模型文件并将其放在 src/main/resources 文件夹中。我的 pom.xml 文件如下。

<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>

    <groupId>dakshila.research</groupId>
    <artifactId>new1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>new1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project> 

在 'src/main/java' 文件夹中,我创建了“dk.research.new1”包,它的 App.java 文件包含以下代码。

package dk.research.new1;

public class App{
    public void SentenceSplitter() {
        SentenceDetector sentenceDetector = null;
        InputStream modelIn = null;

        try {
           modelIn = getClass().getResourceAsStream("en-sent.bin");
           final SentenceModel sentenceModel = new SentenceModel(modelIn);
           modelIn.close();
           sentenceDetector = new SentenceDetectorME(sentenceModel);
        } catch (final IOException ioe) {
               ioe.printStackTrace();
        } finally {
            if (modelIn != null) {
                try {
                    modelIn.close();
                } catch (final IOException e) {}
            }
        }
        String sentences[]=(sentenceDetector.sentDetect("I am a student. I am learning programming. I like java language."));
        for(int i=0; i<sentences.length;i++) {
            System.out.println(sentences[i]);
        }
    }
}

我在 App.java 文件中收到以下错误消息。

  • SentenceDetector 无法解析为类型
  • InputStream 无法解析为类型
  • SentenceModel 无法解析为类型
  • SentenceDetectorME 无法解析为类型
  • IOException 无法解析为类型

为什么会出现这些错误?我试过 Project->Clean 但我无法摆脱这些错误。我该怎么做才能解决这个问题?

【问题讨论】:

    标签: java eclipse maven nlp opennlp


    【解决方案1】:

    &lt;dependencies&gt; 部分的 POM 中添加:

    <dependency>
      <groupId>org.apache.opennlp</groupId>
      <artifactId>opennlp-tools</artifactId>
      <version>1.6.0</version>
    </dependency>
    

    然后,您必须在 public class App 行之前在 Java 文件中导入类添加:

    import java.io.IOException;
    import java.io.InputStream;
    
    import opennlp.tools.sentdetect.SentenceDetector;
    import opennlp.tools.sentdetect.SentenceDetectorME;
    import opennlp.tools.sentdetect.SentenceModel;
    

    【讨论】:

    • 我这样做了。 (版本应该是 1.5.0 对吗?)但我仍然收到这些错误。 The import opennlp cannot be resolved, SentenceModel cannot be resolved to a type, SentenceDetectorME cannot be resolved to a type。如何修复这些错误?
    • 1.5.0 不正确,因为在 Maven 存储库中不存在:mvnrepository.com/artifact/org.apache.opennlp/opennlp-tools 尝试 1.5.3
    • 好的。版本应该是 1.5.0。错误消息现在消失了。 :) 谢谢!但是当 App.java 运行时,会打印以下错误消息。 Error: Main method not found in class dk.research.new1.App, please define the main method as: public static void main(String[] args)我该如何解决?
    猜你喜欢
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多