Java-该类型的方法未定义

Java-该类型的方法未定义,java,Java,我正在使用斯坦福CoreNLP对电影评论进行情感分析。因为它只适用于句子而不是整个文档,所以我正在编写自己的代码,以建立整个评论的平均情绪。我把这篇评论的每一句话都记下来,确定它的观点,并把它放在一个数组中。我正在使用情绪评分系统,这样我就可以平均得分,从而平均情绪。但是,当尝试填充情绪数组时,我收到以下错误: 类型字符串的get方法未定义 我的代码如下: import java.io.*; import java.util.*; import edu.stanford.nlp.coref.C

我正在使用斯坦福CoreNLP对电影评论进行情感分析。因为它只适用于句子而不是整个文档,所以我正在编写自己的代码,以建立整个评论的平均情绪。我把这篇评论的每一句话都记下来,确定它的观点,并把它放在一个数组中。我正在使用情绪评分系统,这样我就可以平均得分,从而平均情绪。但是,当尝试填充情绪数组时,我收到以下错误:

类型字符串的get方法未定义

我的代码如下:

import java.io.*;
import java.util.*;

import edu.stanford.nlp.coref.CorefCoreAnnotations;

import edu.stanford.nlp.coref.data.CorefChain;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.io.EncodingPrintWriter.out;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.pipeline.CoreNLPProtos.Sentiment;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.util.*;
import edu.stanford.nlp.sentiment.Evaluate;
import org.apache.commons.io.FileUtils;

/** This class demonstrates building and using a Stanford CoreNLP pipeline. */
public class sentimentMain {

  /** Usage: java -cp "*" StanfordCoreNlpDemo [inputFile [outputTextFile [outputXmlFile]]] */
  public static void main(String[] args) throws IOException {

      //ArrayList<String> Sentences = new ArrayList<String>();
      //ArrayList<String> sentence_sentiment = new ArrayList<String>();

    // set up optional output files
    PrintWriter out;
    if (args.length > 1) {
      out = new PrintWriter(args[1]);
    } else {
      out = new PrintWriter(System.out);
    }
    PrintWriter xmlOut = null;
    if (args.length > 2) {
      xmlOut = new PrintWriter(args[2]);
    }
    // Add in sentiment
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, sentiment");

    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    File[] files = new File("C:/stanford-corenlp-full-2016-10-31/dataset").listFiles();

    String line = null;

    try{
        for (File file : files) {
            if (file.exists()) {
                BufferedReader in = new BufferedReader(new FileReader(file));
                while((line = in.readLine()) != null)
                {
                    Annotation document = new Annotation(line);

                    // run all the selected Annotators on this text
                    pipeline.annotate(document);
                    String str = FileUtils.readFileToString(file);

                    // this prints out the results of sentence analysis to file(s) in good formats
                    pipeline.prettyPrint(document, out);
                    if (xmlOut != null) {
                      pipeline.xmlPrint(document, xmlOut);
                    }

                    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
                    if (sentences != null && ! sentences.isEmpty()) {
                      CoreMap sentence = sentences.get(0);
                      for (CoreMap token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
                      }
                      Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
                      tree.pennPrint(out);
                      SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);   
                      Map<Integer, CorefChain> corefChains =
                          document.get(CorefCoreAnnotations.CorefChainAnnotation.class);
                      if (corefChains == null) { return; }
                      for (Map.Entry<Integer,CorefChain> entry: corefChains.entrySet()) {
                        for (CorefChain.CorefMention m : entry.getValue().getMentionsInTextualOrder()) {
                          // We need to subtract one since the indices count from 1 but the Lists start from 0
                          List<CoreLabel> tokens = sentences.get(m.sentNum - 1).get(CoreAnnotations.TokensAnnotation.class);
                        }
                      }
                      out.println("The first sentence overall sentiment rating is " + sentence.get(SentimentCoreAnnotations.SentimentClass.class));
                      Sentences.add(line);
                      sentence_sentiment.add(sentence.get(SentimentCoreAnnotations.SentimentClass.class));

                      //Sentences.forEach(s -> System.out.println(s));
                      //sentence_sentiment.forEach(s -> System.out.println(s));

                      averageSent(str);   
                    }
                }

                in.close();
            } else {
                System.out.println("File: " + file.getName() + file.toString());
            }
        }
    }catch(NullPointerException e){
        e.printStackTrace();
    }
    IOUtils.closeIgnoringExceptions(out);
    IOUtils.closeIgnoringExceptions(xmlOut);
  }

  public static void averageSent(String review) 
  { 
        //populate sentence array with each sentence of the review
        String [] sentences = review.split("[!?.:]+");

        //number of sentences in each review
        int review_sentences = review.split("[!?.:]+").length;

        //array of sentiments for each review
        String sentiments[] = new String[review_sentences];

        int total = 0;

        //populate sentiments array
        for (int i=0; i<= review_sentences; i++)
            {
                sentiments[i].add(sentences[i].get(SentimentCoreAnnotations.SentimentClass.class));
            }

        for (String sent: sentiments)
        {
            if (sent == "Very positive")
            {
                int veryPos = 4;
                total += veryPos;
            }
            else if (sent == "Positive")
            {
                int pos = 3;
                total += pos;
            }
            else if (sent == "Negative")
            {
                int neg = 2;
                total += neg;
            }
            else if (sent == "Very negative")
            {
                int veryNeg = 1;
                total += veryNeg;
            }
            else if (sent == "Neutral")
            {
                int neu = 0;
                total += neu;
            }

            //System.out.println("Total " +total);

            //int average = total/review_sent;
            //System.out.println("Average" + average);
    }

  }
}
import java.io.*;
导入java.util.*;
导入edu.stanford.nlp.coref.corefcore注释;
导入edu.stanford.nlp.coref.data.CorefChain;
导入edu.stanford.nlp.io.*;
导入edu.stanford.nlp.io.EncodingPrintWriter.out;
导入edu.stanford.nlp.ling.*;
导入edu.stanford.nlp.pipeline.*;
导入edu.stanford.nlp.pipeline.corenlprotos.touction;
导入edu.stanford.nlp.semgraph.SemanticGraph;
导入edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
导入edu.stanford.nlp.touction.mountain注释;
导入edu.stanford.nlp.trees.*;
导入edu.stanford.nlp.util.*;
导入edu.stanford.nlp.touction.Evaluate;
导入org.apache.commons.io.FileUtils;
/**本课程演示如何构建和使用斯坦福CoreNLP管道*/
公众阶级{
/**用法:java-cp“*”StanfordCoreNlpDemo[inputFile[OutputExtFile[outputXmlFile]]*/
公共静态void main(字符串[]args)引发IOException{
//ArrayList语句=新的ArrayList();
//ArrayList语句=新ArrayList();
//设置可选的输出文件
打印输出;
如果(参数长度>1){
out=新的PrintWriter(args[1]);
}否则{
out=新的PrintWriter(System.out);
}
PrintWriter xmlOut=null;
如果(参数长度>2){
xmlOut=新的PrintWriter(args[2]);
}
//加上感情
Properties props=新属性();
props.setProperty(“注释器”、“标记化、ssplit、pos、引理、ner、解析、dcoref、情感”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
File[]files=新文件(“C:/stanford-corenlp-full-2016-10-31/dataset”).listFiles();
字符串行=null;
试一试{
用于(文件:文件){
if(file.exists()){
BufferedReader in=新的BufferedReader(新文件读取器(文件));
而((line=in.readLine())!=null)
{
注释文档=新注释(行);
//在此文本上运行所有选定的注释器
管道注释(文件);
String str=FileUtils.readFileToString(文件);
//这会将句子分析的结果以良好的格式打印到文件中
管道。预打印(文件,输出);
如果(xmlOut!=null){
pipeline.xmlPrint(文档,xmlOut);
}
列出句子=document.get(coreanotations.SentencesAnnotation.class);
if(句!=null&!句.isEmpty()){
CoreMap语句=语句。获取(0);
for(CoreMap标记:句子.get(coreanotations.TokensAnnotation.class)){
}
Tree-Tree=句子.get(TreeCoreAnnotations.TreeAnnotation.class);
树。打印(输出);
SemanticGraph graph=句子.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenceAnnotation.class);
映射核心链=
get(corefcoreeannotations.CorefChainAnnotation.class);
如果(corefChains==null){return;}
对于(Map.Entry:corefChains.entrySet()){
for(CorefChain.corefformation m:entry.getValue().getReferencesIntextualorder()){
//我们需要减去1,因为索引从1开始计数,但列表从0开始
列表标记=句子.get(m.sentNum-1).get(coreanotations.TokensAnnotation.class);
}
}
out.println(“第一句整体情绪评级为”+句子.get(感伤CoreAnnotations.感伤类.class));
句子。加(行);
句子\情绪.添加(句子.获取(情绪注释.情绪类.类));
//forEach(s->System.out.println(s));
//句子\ u.forEach(s->System.out.println(s));
平均发送量(str);
}
}
in.close();
}否则{
System.out.println(“文件:”+File.getName()+File.toString());
}
}
}捕获(NullPointerException e){
e、 printStackTrace();
}
IOUtils.closeIgnoringExceptions(out);
IOUtils.closeIgnoringExceptions(xmlOut);
}
已发送公共静态无效平均值(字符串审阅)
{ 
//用复习的每个句子填充句子数组
String[]句=review.split(“[!?:]+”);
//每次复习的句子数
int review_句子=review.split(“[!?:]+”)。长度;
//每一篇评论都有一系列的感想
字符串情感[]=新字符串[复习句子];
int-total=0;
//填充情感数组
对于(int i=0;i这不起作用:

sentences[i].get(SentimentCoreAnnotations.SentimentClass.class)
就在它前面:

String [] sentences = review.split("[!?.:]+");
该行生成一个
String
对象数组,而
String
类没有
get
方法。

这不起作用:

sentences[i].get(SentimentCoreAnnotations.SentimentClass.class)
就在它前面:

String [] sentences = review.split("[!?.:]+");
该行生成一个
String
对象数组,而
String
类没有
ge