Eclipse Maven:“无法解析为类型”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32113346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:48:20  来源:igfitidea点击:

Eclipse Maven: "Cannot be resolved to a type" error

javaeclipsemavennlpopennlp

提问by Dakshila Kamalsooriya

I am trying to build a simple sentence detector using OpenNLP 1.5.0. I am using Maven Eclipse for that. I downloaded "en-sent.bin" model file from http://opennlp.sourceforge.net/models-1.5/and placed it inside src/main/resources folder. My pom.xml file is as follows.

我正在尝试使用 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> 

Inside 'src/main/java' folder I have created "dk.research.new1" package and its App.java file contains the following code.

在“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]);
        }
    }
}

I get the following error messages in App.java file.

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

  • SentenceDetector cannot be resolved to a type
  • InputStream cannot be resolved to a type
  • SentenceModel cannot be resolved to a type
  • SentenceDetectorME cannot be resolved to a type
  • IOException cannot be resolved to a type
  • SentenceDetector 无法解析为类型
  • InputStream 无法解析为类型
  • SentenceModel 无法解析为类型
  • SentenceDetectorME 无法解析为类型
  • IOException 无法解析为类型

Why am I getting these errors? I tried Project->Clean but I can't get rid of these errors. What do I have to do to get this solved?

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

回答by PatrykG

In your POM in <dependencies>section add:

在您的 POM<dependencies>部分添加:

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

And then, you have to import classes in Java file before line public class Appadd:

然后,您必须在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;