2016-11-15 3 views
0

Je trouve que les données pour former le modèle de sentiment dans train.txt est le format PTB ressemble à ceci.Créer un autre train.txt pour former le modèle de sentiment pour d'autres domaines

(3 (2 Yet) (3 (2 (2 the) (2 act)) (3 (4 (3 (2 is) (3 (2 still) (4 charming))) (2 here)) (2 .)))) 

que la vraie phrase devrait être

Yet the act is still charming here. 

Mais après Parse je suis arrivé la structure différente

(ROOT (S (CC Yet) (NP (DT the) (NN act)) (VP (VBZ is) (ADJP (RB still) (JJ charming)) (ADVP (RB here))) (. .))) 

suit mon code:

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

    // read some text in the text variable 
    String text = "Yet the act is still charming here .";// Add your text here! 

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

    // run all Annotators on this text 

    pipeline.annotate(annotation); 

    // 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 = annotation.get(CoreAnnotations.SentencesAnnotation.class); 

    // int sentiment = 0; 
    for(CoreMap sentence: sentences) { 
     // traversing the words in the current sentence 
     Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); 
     System.out.println(tree); 
     // System.out.println(tree.yield()); 
     tree.pennPrint(System.out); 
     // Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); 
     // sentiment = RNNCoreAnnotations.getPredictedClass(tree); 
    } 

    // System.out.print(sentiment); 
} 

Ensuite, deux questions apparaissent quand j'utilise m y possède des phrases pour créer le fichier train.txt.

1.Mon arbre est différent de celui dans train.txt, je sais que le numéro dans le dernier est la polarité du sentiment.Mais il semble que la structure de l'arbre est différent, je veux obtenir un arbre d'analyse binarized qui pourrait ressembler comme celui-ci

((Yet) (((the) (act)) ((((is) ((still) (charming))) (here)) (.)))) 

Une fois que je reçois le numéro de sentiment que je peux le remplir pour obtenir mon propre train.txt

2.How pour obtenir toutes les phrases à chaque nœud de l'arbre d'analyse syntaxique binarisée, dans cet exemple , Je devrais obtenir

Yet 
the 
act 
the act 
is 
still 
charming 
still charming 
is still charming 
here 
is still charming here 
. 
is still charming here . 
the act is still charming here . 
Yet the act is still charming here. 

Une fois que je les ai, je peux dépenser de l'argent en les annotant par des annotateurs humains.

En fait, je les ai beaucoup googlé, mais je n'ai pas pu les résoudre, alors je poste ici.Toutes les réponses utiles seraient les bienvenues!

Répondre

2

Ajouter ce aux propriétés pour obtenir des arbres binaires:

props.setProperty("parse.binaryTrees", "true"); 

arbre binaire sera accessible ainsi de la phrase:

Tree tree = sentence.set(TreeCoreAnnotations.BinarizedTreeAnnotation.class); 

Voici quelques exemples de code j'ai écrit:

import edu.stanford.nlp.ling.CoreAnnotations; 
import edu.stanford.nlp.ling.Word; 
import edu.stanford.nlp.pipeline.Annotation; 
import edu.stanford.nlp.pipeline.StanfordCoreNLP; 
import edu.stanford.nlp.trees.*; 

import java.util.ArrayList; 
import java.util.Properties; 

public class SubTreesExample { 

    public static void printSubTrees(Tree inputTree, String spacing) { 
     if (inputTree.isLeaf()) { 
      return; 
     } 
     ArrayList<Word> words = new ArrayList<Word>(); 
     for (Tree leaf : inputTree.getLeaves()) { 
      words.addAll(leaf.yieldWords()); 
     } 
     System.out.print(spacing+inputTree.label()+"\t"); 
     for (Word w : words) { 
      System.out.print(w.word()+ " "); 
     } 
     System.out.println(); 
     for (Tree subTree : inputTree.children()) { 
      printSubTrees(subTree, spacing + " "); 
     } 
    } 

    public static void main(String[] args) { 
     Properties props = new Properties(); 
     props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse"); 
     props.setProperty("parse.binaryTrees", "true"); 
     StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
     String text = "Yet the act is still charming here."; 
     Annotation annotation = new Annotation(text); 
     pipeline.annotate(annotation); 
     Tree sentenceTree = annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(
       TreeCoreAnnotations.BinarizedTreeAnnotation.class); 
     System.out.println("Penn tree:"); 
     sentenceTree.pennPrint(System.out); 
     System.out.println(); 
     System.out.println("Phrases:"); 
     printSubTrees(sentenceTree, ""); 

    } 
} 
+0

Génial! Si je veux former un modèle de sentiment chinois, la phrase dans train.txt doit toujours être binaire analysée? @StanfordNLPHelp – ryh