2015-12-22 71 views
0

我有一个依赖树作为字符串。然而,我转换为树,并进行情绪分析。它只给我-1(不工作)。 我能够使用parse.pennPrint()成功地打印树,它对我来说看起来很好。依赖关系树情绪stanford nlp

String sentence="(TOP (S (S (NP (NNP china)) (VP (VBD experimented) (PP (IN in) (NP (DT the) (NN past))) (PP (IN with) (NP (NP (JJ various) (JJ political) (NNS systems,)) (VP (VBG including) (NP (JJ multi-party) (NN democracy,))))))) (CC but) (S (NP (PRP it)) (VP (VBD did) (RB not) (VP (VB work,) (S (NP (NN president) (NN xi) (NN jinping)) (VP (VBD said) (PP (IN during) (NP (NP (DT a) (NN visit)) (S (VP (TO to) (VP (VB europe,) (VP (VBG warning) (SBAR (IN that) (S (S (VP (VBG copying) (NP (JJ foreign) (JJ political) (CC or) (NN development) (NNS models)))) (VP (MD could) (VP (VB be) (ADJP (JJ catastrophic)))))))))))))))))))"; 
    int sentiment_score =0; 
    try{ 

     Tree parse = Tree.valueOf(sentence); 
     parse.pennPrint(); 


     sentiment_score = RNNCoreAnnotations.getPredictedClass(parse); 
     System.out.println("input tree, score: "+sentiment_score); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 

印刷树使用parse.pennPrint():

(TOP (S (S (NP(NNP中国)) (VP(VBD试验) (PP(IN中) (NP(DT的)(NN过去))) (PP(与) (NP (NP(JJ各种)(JJ政治)(NNS系统,)) (VP(VBG包括) (NP (JJ多党)(NN民主))))))) (CC但) (S (NP(PRP吧)) (VP(VBD做)(RB不) (VP(VB作品), (S (NP(NN总裁)(NN XI)(NN金平)) (VP(VBD所述) (PP(IN期间) (NP (NP(DT一)(NN访问)) (S (VP(TO到) (VP(VB欧洲,) (VP(VBG警告) (SBAR(在) (S (S (VP(VBG复制) (NP(JJ国外)(JJ政治)(VP(VB)) (ADJP(JJ catastrophic)))))))() ))))))

+0

树可以很好地形成,因此分析,但没有任何意义的注释由于一些其他的结构性问题。你从哪里得到字符串表示? – 9000

+0

'RNNCoreAnnotations'从哪里来? – alvas

+0

(1)我使用OpenNLP chunker来获取树(并随后转换为字符串)。但是我希望使用stanford nlp库的语义预测来获得语义分类。由于我能够将字符串转换为树并打印出来,因此我不认为它是我理解中的结构性问题。 (2)来自edu.stanford.nlp.neural.rnn.RNNCoreAnnotations.getPredictedClass(树状树)的RNNCoreAnnotations。谢谢! –

回答

0

您需要运行情感分类器 - 您的代码尝试从树中获取[不存在]情感标签。最简单的将是运行解析器以及从原始文本开始:

Annotation document = new Annotation("your text"); StanfordCoreNLP pipeline = new StanfordCoreNLP(new Properties(){{ setProperty("annotators", "tokenize,ssplit,parse,sentiment") }}); pipeline.annotate(document);