Eclipse Maven:"Cannot be resolved to a type" 错误
Eclipse Maven: "Cannot be resolved to a type" error
我正在尝试使用 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 但我无法摆脱这些错误。我需要做什么才能解决这个问题?
在您的 POM 的 <dependencies>
部分添加:
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.6.0</version>
</dependency>
然后,您必须在 Java 文件中的 public class App
行之前导入 类 添加:
import java.io.IOException;
import java.io.InputStream;
import opennlp.tools.sentdetect.SentenceDetector;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
我正在尝试使用 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 但我无法摆脱这些错误。我需要做什么才能解决这个问题?
在您的 POM 的 <dependencies>
部分添加:
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.6.0</version>
</dependency>
然后,您必须在 Java 文件中的 public class App
行之前导入 类 添加:
import java.io.IOException;
import java.io.InputStream;
import opennlp.tools.sentdetect.SentenceDetector;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;