Java 执行和测试stanford core nlp示例

Java 执行和测试stanford core nlp示例,java,nlp,stanford-nlp,Java,Nlp,Stanford Nlp,我下载了StanfordCore nlp软件包,并尝试在我的机器上进行测试 使用命令:java-cp“*”-mx1g-edu.stanford.nlp.touction.mountainpipeline-file-input.txt 我得到的情绪结果是正面或负面input.txt包含要测试的句子 关于更多命令:java-cp-stanford-corenlp-3.3.0.jar;斯坦福-corenlp-3.3.0-models.jar;xom.jar;joda-time.jar-Xmx600m

我下载了StanfordCore nlp软件包,并尝试在我的机器上进行测试

使用命令:
java-cp“*”-mx1g-edu.stanford.nlp.touction.mountainpipeline-file-input.txt

我得到的情绪结果是
正面
负面
input.txt
包含要测试的句子

关于更多命令:
java-cp-stanford-corenlp-3.3.0.jar;斯坦福-corenlp-3.3.0-models.jar;xom.jar;joda-time.jar-Xmx600m edu.stanford.nlp.pipeline.StanfordCoreNLP-注释器标记化、ssplit、pos、引理、parse-file input.txt执行时给出以下行:

H:\Drive E\Stanford\stanfor-corenlp-full-2013~>java -cp stanford-corenlp-3.3.0.j
ar;stanford-corenlp-3.3.0-models.jar;xom.jar;joda-time.jar -Xmx600m edu.stanford
.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,parse -file
input.txt
Adding annotator tokenize
Adding annotator ssplit
Adding annotator pos
Reading POS tagger model from edu/stanford/nlp/models/pos-tagger/english-left3wo
rds/english-left3words-distsim.tagger ... done [36.6 sec].
Adding annotator lemma
Adding annotator parse
Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCF
G.ser.gz ... done [13.7 sec].

Ready to process: 1 files, skipped 0, total 1
Processing file H:\Drive E\Stanford\stanfor-corenlp-full-2013~\input.txt ... wri
ting to H:\Drive E\Stanford\stanfor-corenlp-full-2013~\input.txt.xml {
  Annotating file H:\Drive E\Stanford\stanfor-corenlp-full-2013~\input.txt [13.6
81 seconds]
} [20.280 seconds]
Processed 1 documents
Skipped 0 documents, error annotating 0 documents
Annotation pipeline timing information:
PTBTokenizerAnnotator: 0.4 sec.
WordsToSentencesAnnotator: 0.0 sec.
POSTaggerAnnotator: 1.8 sec.
MorphaAnnotator: 2.2 sec.
ParserAnnotator: 9.1 sec.
TOTAL: 13.6 sec. for 10 tokens at 0.7 tokens/sec.
Pipeline setup: 58.2 sec.
Total time for StanfordCoreNLP pipeline: 79.6 sec.

H:\Drive E\Stanford\stanfor-corenlp-full-2013~>
我能理解。没有提供信息的结果

我有一个例子:

import java.io.*;
导入java.util.*;
导入edu.stanford.nlp.io.*;
导入edu.stanford.nlp.ling.*;
导入edu.stanford.nlp.pipeline.*;
导入edu.stanford.nlp.trees.*;
导入edu.stanford.nlp.util.*;
公共类StanfordCoreNlpDemo{
公共静态void main(字符串[]args)引发IOException{
打印输出;
如果(参数长度>1){
out=新的PrintWriter(args[1]);
}否则{
out=新的PrintWriter(System.out);
}
PrintWriter xmlOut=null;
如果(参数长度>2){
xmlOut=新的PrintWriter(args[2]);
}
StanfordCoreNLP管道=新的StanfordCoreNLP();
注释;
如果(args.length>0){
annotation=新注释(IOUtils.slurpFileNoExceptions(args[0]);
}否则{
注释=新注释(“Kosgi Santosh给斯坦福大学发了一封电子邮件,但没有得到回复。”);
}
管道注释(注释);
管道预打印(注释,输出);
如果(xmlOut!=null){
管道.xmlPrint(注释,xmlOut);
}
//注释是一张地图,您可以单独获取并使用各种分析。
//例如,它获取文本中第一个句子的解析树。
列出句子=annotation.get(coreanotations.SentencesAnnotation.class);
if(句子!=null&&句子.size()>0){
CoreMap语句=语句。获取(0);
Tree-Tree=句子.get(TreeCoreAnnotations.TreeAnnotation.class);
out.println();
println(“解析的第一句是:”);
树。打印(输出);
}
}
}
试图在netbeans中执行它,并包含必要的库。但它总是夹在中间,或者在线程“main”java.lang.OutOfMemoryError:java堆空间中给出异常
exception

属性/run/VM框中设置要分配的内存

知道如何使用命令行运行上面的java示例吗

我想获得示例的情绪分数

更新

输出:
java-cp“*”-mx1g edu.stanford.nlp.touction.mountainpipeline-file input.txt

输出:
java-cp stanford-corenlp-3.3.0.j
应收账;斯坦福-corenlp-3.3.0-models.jar;xom.jar;joda-time.jar-Xmx600m edu.stanford
.nlp.pipeline.StanfordCoreNLP-注释器标记化、ssplit、pos、引理、解析-文件
input.txt

根据示例,您需要运行情绪分析

java -cp "*" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -file input.txt
显然,这是一个内存昂贵的操作,它可能不会只用1GB就完成。 然后你可以使用“评估工具”

您需要将“情绪”注释器添加到注释器列表中:

-annotators tokenize,ssplit,pos,lemma,parse,sentiment

这将为XML中的每个句子节点添加一个“情感”属性。

您可以在代码中执行以下操作:

String text = "I am feeling very sad and frustrated.";
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
<...>
Annotation annotation = pipeline.process(text);
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
  String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
  System.out.println(sentiment + "\t" + sentence);
}

这对我来说很好-

Maven依赖项:

        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.5.2</version>
            <classifier>models</classifier>
        </dependency>
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-parser</artifactId>
            <version>3.5.2</version>
        </dependency>

edu.stanford.nlp
斯坦福大学科伦普
3.5.2
模型
edu.stanford.nlp
斯坦福大学科伦普
3.5.2
edu.stanford.nlp
斯坦福语法分析器
3.5.2
Java代码:

public static void main(String[] args) throws IOException {
        String text = "This World is an amazing place";
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        Annotation annotation = pipeline.process(text);
        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
            System.out.println(sentiment + "\t" + sentence);
        }
    }
publicstaticvoidmain(字符串[]args)引发IOException{
String text=“这个世界是一个神奇的地方”;
Properties props=新属性();
props.setProperty(“注释器”、“标记化、ssplit、pos、引理、解析、情感”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
注释=pipeline.process(文本);
列出句子=annotation.get(coreanotations.SentencesAnnotation.class);
for(CoreMap句子:句子){
字符串情绪=句子.get(感情用事注释.感情用事类.类);
System.out.println(情绪+“\t”+句子);
}
}
结果:


非常积极这个世界是一个令人惊奇的地方

你的一个例子产生了什么效果?(即“H:\Drive E\Stanford\stanfor-corenlp-full-2013~\input.txt.xml”)@home:
OutOfMemoryError
。还是同样的错误persist@ElliottFrisch:请参阅我更新了问题,因为内存不足,请尝试在示例命令中使用
-mx?g
配置<代码>?
表示分配用于运行代码的GB RAM。增加,直到成功。Elliott:你说得对,但我按照我的系统配置使用了
-mx1g
,在命令行中它也起了作用。你有测试斯坦福coreNLP情绪部分的经验吗?没有,我有商业情绪引擎的经验,你在那张图片中有情绪分数。我道歉!我是NLP的新手。我在图像中找不到情感分数,可能是我缺少了传统。如果你能把它照亮,我将不胜感激。在“net.Where you's passing the input station in the program”(你在程序中传递输入语句吗?)中找到的随机代码的效果似乎和我预期的一样好,添加到示例Annotation=pipeline.process(text);我找到了一个示例,其中不是所有6个注释器都用于执行情绪分析,而是只有4个注释器。不包括POS和引理,它如何影响结果?埃克萨
Negative    I am feeling very sad and frustrated.
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.5.2</version>
            <classifier>models</classifier>
        </dependency>
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-parser</artifactId>
            <version>3.5.2</version>
        </dependency>
public static void main(String[] args) throws IOException {
        String text = "This World is an amazing place";
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        Annotation annotation = pipeline.process(text);
        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
            System.out.println(sentiment + "\t" + sentence);
        }
    }