2017-03-04 11 views
-1

私はStanford CoreNLPを使用して、1つのディレクトリ内に含まれる25,000件のテキストムービーレビューに対する感情分析を行っています。これを行うためには、単一のテキストファイル内の個々のセンテンスのみを分析するため、スタンフォードコードを少し変更する必要があります。ディレクトリ内の各ファイルの内容をJavaで処理する

次のようにこれを実行するに私の試みは、次のとおりです。私は次のエラーが表示されているの

import java.io.File; 
import java.io.IOException; 
import java.nio.charset.Charset; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import java.util.Properties; 

import org.apache.commons.io.FileUtils; 

import com.google.common.io.Files; 

import edu.stanford.nlp.dcoref.CorefChain; 
import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation; 
import edu.stanford.nlp.ling.CoreLabel; 
import edu.stanford.nlp.pipeline.Annotation; 
import edu.stanford.nlp.pipeline.StanfordCoreNLP; 
import edu.stanford.nlp.semgraph.SemanticGraph; 
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation; 
import edu.stanford.nlp.trees.Tree; 
import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation; 
import edu.stanford.nlp.util.CoreMap; 
import java.io.File; 
import java.util.Iterator; 
import org.apache.commons.io.*; 

/** A simple corenlp example ripped directly from the Stanford CoreNLP website using text from wikinews. */ 
public class sentimentMain { 

    public static void main(String[] args) throws IOException { 
    // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
    Properties props = new Properties(); 
    props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); 
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 

    // read some text from the file.. 
    Iterator it = FileUtils.iterateFiles(new File("C:\\stanford-corenlp-full-2016-10-31\\train\\neg"), null, false); 
    Iterator it1 = FileUtils.iterateFiles(new File("C:\\stanford-corenlp-full-2016-10-31\\train\\pos"), null, false); 
    Iterator it2 = FileUtils.iterateFiles(new File("C:\\stanford-corenlp-full-2016-10-31\\train\\unsup"), null, false); 

    File inputFile = new File ((String) (it.next())); 
    String text = Files.toString(inputFile, Charset.forName("UTF-8")); 
    System.out.println(text); 

    //File inputFile = new File("C:/stanford-corenlp-full-2016-10-31/input.txt"); 
    //String text = Files.toString(inputFile, Charset.forName("UTF-8")); 

    // create an empty Annotation just with the given text 
    Annotation document = new Annotation(text); 

    // run all Annotators on this text 
    pipeline.annotate(document); 

    // these are all the sentences in this document 
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types 
    List<CoreMap> sentences = document.get(SentencesAnnotation.class); 

    for(CoreMap sentence: sentences) { 
     // traversing the words in the current sentence 
     // a CoreLabel is a CoreMap with additional token-specific methods 
     for (CoreLabel token: sentence.get(TokensAnnotation.class)) { 
     // this is the text of the token 
     String word = token.get(TextAnnotation.class); 
     // this is the POS tag of the token 
     String pos = token.get(PartOfSpeechAnnotation.class); 
     // this is the NER label of the token 
     String ne = token.get(NamedEntityTagAnnotation.class); 

     System.out.println("word: " + word + " pos: " + pos + " ne:" + ne); 
     } 

     // this is the parse tree of the current sentence 
     Tree tree = sentence.get(TreeAnnotation.class); 
     System.out.println("parse tree:\n" + tree); 

     // this is the Stanford dependency graph of the current sentence 
     SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class); 
     System.out.println("dependency graph:\n" + dependencies); 
    } 

    // This is the coreference link graph 
    // Each chain stores a set of mentions that link to each other, 
    // along with a method for getting the most representative mention 
    // Both sentence and token offsets start at 1! 
    Map<Integer, CorefChain> graph = 
     document.get(CorefChainAnnotation.class); 

    } 

} 

Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String 
    at sentimentMain.main(sentimentMain.java:46) 

私は「it.next()は、」変換できないことを理解文字列に変換しますが、他の方法を知っている人は、ファイルの内容が処理のための文字列として入力されていることを確認できますか?事前に

感謝:)

+0

'Annotation document = new Annotation(text);'スコープ内にない 'text'変数にアクセスしようとしています。あなたは 'while(it.hasNext()){'ループ内でそれを定義しています。 –

答えて

0

そのまともなIDEがあなたを示しているだろうまっすぐコンパイルエラー、。 変数 - "text"はwhileループの外側では使用できません。whileループの開始前に宣言するか、ループ中に文書宣言を入れる必要があります。

下記の編集コードをご覧ください。

import java.io.File; 
import java.io.IOException; 
import java.nio.charset.Charset; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import java.util.Properties; 

import org.apache.commons.io.FileUtils; 

import com.google.common.io.Files; 

import edu.stanford.nlp.dcoref.CorefChain; 
import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation; 
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation; 
import edu.stanford.nlp.ling.CoreLabel; 
import edu.stanford.nlp.pipeline.Annotation; 
import edu.stanford.nlp.pipeline.StanfordCoreNLP; 
import edu.stanford.nlp.semgraph.SemanticGraph; 
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation; 
import edu.stanford.nlp.trees.Tree; 
import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation; 
import edu.stanford.nlp.util.CoreMap; 
import java.io.File; 
import java.util.Iterator; 
import org.apache.commons.io.*; 

/** A simple corenlp example ripped directly from the Stanford CoreNLP website using text from wikinews. */ 
public class sentimentMain { 

    public static void main(String[] args) throws IOException { 
    // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
    Properties props = new Properties(); 
    props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); 
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 

    // read some text from the file.. 
    Iterator it = FileUtils.iterateFiles(new File("C:\\stanford-corenlp-full-2016-10-31\\train\\neg"), null, false); 
    Iterator it1 = FileUtils.iterateFiles(new File("C:\\stanford-corenlp-full-2016-10-31\\train\\pos"), null, false); 
    Iterator it2 = FileUtils.iterateFiles(new File("C:\\stanford-corenlp-full-2016-10-31\\train\\unsup"), null, false); 

    while(it.hasNext()){ 

     File inputFile = new File ((String) (it.next())); 
     String text = Files.toString(inputFile, Charset.forName("UTF-8")); 
     System.out.println(text); 
    //File inputFile = new File("C:/stanford-corenlp-full-2016-10-31/input.txt"); 
    //String text = Files.toString(inputFile, Charset.forName("UTF-8")); 

    // create an empty Annotation just with the given text 
    Annotation document = new Annotation(text); 

    // run all Annotators on this text 
    pipeline.annotate(document); 

    // these are all the sentences in this document 
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types 
    List<CoreMap> sentences = document.get(SentencesAnnotation.class); 

    for(CoreMap sentence: sentences) { 
     // traversing the words in the current sentence 
     // a CoreLabel is a CoreMap with additional token-specific methods 
     for (CoreLabel token: sentence.get(TokensAnnotation.class)) { 
     // this is the text of the token 
     String word = token.get(TextAnnotation.class); 
     // this is the POS tag of the token 
     String pos = token.get(PartOfSpeechAnnotation.class); 
     // this is the NER label of the token 
     String ne = token.get(NamedEntityTagAnnotation.class); 

     System.out.println("word: " + word + " pos: " + pos + " ne:" + ne); 
     } 

     // this is the parse tree of the current sentence 
     Tree tree = sentence.get(TreeAnnotation.class); 
     System.out.println("parse tree:\n" + tree); 

     // this is the Stanford dependency graph of the current sentence 
     SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class); 
     System.out.println("dependency graph:\n" + dependencies); 
    } 

    // This is the coreference link graph 
    // Each chain stores a set of mentions that link to each other, 
    // along with a method for getting the most representative mention 
    // Both sentence and token offsets start at 1! 
    Map<Integer, CorefChain> graph = 
     document.get(CorefChainAnnotation.class); 

    } 
    } 

} 
+0

このコードは、私が持っていた問題を修正しました。今、それは私にit.next()をキャストできません: – user7575479

+0

スレッド "main"の例外java.lang.ClassCastException:java.io.Fileはjava.lang.Stringにキャストできません \t at sentimentMain.main(sentimentMain.java:50) – user7575479

関連する問題