我已经安装了 Maven,将 POM 配置为:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.simplestcodings</groupId>
<artifactId>nlp</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>4.5.6</version>
<classifier>models</classifier>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
我的 Java 类如下所示:
package com.simplestcodings;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.util.CoreMap;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SentimentAnalysis {
public static void main(String[] args) {
// Initialize the Stanford NLP pipeline
StanfordCoreNLP pipeline = new StanfordCoreNLP("application.properties");
// Sample text for sentiment analysis
String text = "Simplest codings is the best place to learn and grow. I am glad to be here.";
// Perform sentiment analysis
String sentiment = getSentiment(text, pipeline);
// Display the result
log.info("Text: {}, Sentiment: {}",text, sentiment);
// Another Sample text for sentiment analysis
text = "I hate this place. I am not coming back here again. I am very disappointed.";
// Perform sentiment analysis
sentiment = getSentiment(text, pipeline);
// Display the result
log.info("Text: {}, Sentiment: {}",text, sentiment);
}
private static String getSentiment(String text, StanfordCoreNLP pipeline) {
// Create an Annotation object with the input text
Annotation annotation = new Annotation(text);
// Run all the NLP annotators on the text
pipeline.annotate(annotation);
// Extract the sentiment from the annotation
CoreMap sentence = annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0);
String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
return sentiment;
}
}
但这些是我收到的错误:
我尝试过使缓存无效,重建项目并检查文件版本:
JAVA:
java version "22.0.1" 2024-04-16 Java(TM) SE Runtime Environment (build 22.0.1+8-16) Java HotSpot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)
马文:Apache Maven 3.9.6
我也尝试在 Amazon Coretto JDK 17 中设置项目,就像教程一样,但仍然没有成功
请帮忙。
假设导入和类/方法名称有效(在本例中它们似乎有效),这种行为通常表明 Maven 项目的配置被 IDEA 误解或 IDEA 的缓存已损坏/损坏。 尝试以下操作:
关闭IDEA的所有实例
删除或重命名在
系统目录
中找到的
projects
和 workspace
文件夹(如果存在)
重新导入项目:打开 -> 导航到项目目录 -> 双击根目录
pom.xml
-> 作为项目打开
如果问题仍然存在,您可以尝试重命名/删除整个系统目录
如果问题仍然存在,请提交 YouTrack Issue 或 支持票。