2017-09-14 4 views
0

A titre d'exemple, j'ai l'arbre d'analyse suivant de l'analyseur de Stanford. Comment puis-je extraire les étiquettes comme S et SBAR pour finalement extraire des clauses. J'ai essayé un petit morceau de code (ce qui est évidemment incorrect) comme point de départ, en utilisant différentes méthodes de Tree, mais rien ne m'a donné les résultats souhaités.Parsing des étiquettes de nœuds pour extraire de manière prossable les clauses d'un arbre syntaxique

code:

for (Tree subtree: parseTree.getLeaves()){ 
      if (subtree.label().equals("S")||subtree.label().equals("SBAR")) 
       System.out.println("SUBTREE:::"+"\t"+ subtree.getLeaves()); 
     } 

Parse Arbre:

(ROOT 
     (S 
     (NP 
      (NP (DT A) (NNP Bristol) (NN hospital)) 
      (SBAR 
      (WHNP (WDT that)) 
      (S 
       (VP (VBD retained) 
       (NP 
        (NP (DT the) (NNS hearts)) 
        (PP (IN of) 
        (NP 
         (NP (CD 300) (NNS children)) 
         (SBAR 
         (WHNP (WP who)) 
         (S 
          (VP (VBD died) 
          (PP (IN in) 
           (NP (JJ complex) (NNS operations))))))))))))) 
     (VP (VBD behaved) 
      (ADVP (IN in) (DT a)) 
      ('' '') 
      (S 
      (VP (VBG cavalier) ('' '') 
       (NP (NN fashion)))) 
      (PP (IN towards) 
      (NP (DT the) (NNS parents)))) 
     (. .))) 

Répondre

0

Voici quelques exemples de code pour passer par un arbre et de trouver le S et SBAR:

package edu.stanford.nlp.examples; 

import edu.stanford.nlp.ling.*; 
import edu.stanford.nlp.pipeline.*; 
import edu.stanford.nlp.trees.*; 

import java.util.*; 

public class FindSAndSBARInTreeExample { 

    public static void findSAndSBAR(Tree tree) { 
    for (Tree subtree : tree.getChildrenAsList()) { 
     if (subtree.label().value().equals("S") || subtree.label().value().equals("SBAR")) { 
     System.out.println("---"); 
     System.out.println(subtree.yieldWords()); 
     } 
     findSAndSBAR(subtree); 
    } 
    } 

    public static void main(String[] args) { 
    // set up pipeline properties 
    Properties props = new Properties(); 
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse"); 
    // use faster shift reduce parser 
    props.setProperty("parse.model", "edu/stanford/nlp/models/srparser/englishSR.ser.gz"); 
    props.setProperty("parse.maxlen", "100"); 
    // set up Stanford CoreNLP pipeline 
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
    // build annotation for a review 
    Annotation annotation = 
     new Annotation(
      "A Bristol hospital that retained the hearts of 300 children who died in " + 
       "complex operations behaved in a \"cavalier fashion\" towards the parents"); 
    // annotate 
    pipeline.annotate(annotation); 
    // get tree 
    Tree tree = 
     annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(TreeCoreAnnotations.TreeAnnotation.class); 
    System.out.println(tree); 
    // find S and SBAR 
    findSAndSBAR(tree); 
    } 
} 
0

Une autre façon de Pour ce faire, utilisez Tregex. Voici quelques exemples de code:

package edu.stanford.nlp.examples; 

import edu.stanford.nlp.ling.*; 
import edu.stanford.nlp.pipeline.*; 
import edu.stanford.nlp.trees.*; 
import edu.stanford.nlp.trees.tregex.*; 
import edu.stanford.nlp.util.*; 

import java.util.*; 

public class TregexUsageExample { 

    public static void main(String[] args) { 
    // set up pipeline 
    Properties props = new Properties(); 
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse"); 
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
    // Spanish example 
    Annotation annotation = 
     new Annotation(
      "A Bristol hospital that retained the hearts of 300 children who died in " + 
       "complex operations behaved in a \"cavalier fashion\" towards the parents"); 
    pipeline.annotate(annotation); 
    // get first sentence 
    CoreMap firstSentence = annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0); 
    Tree firstSentenceTree = firstSentence.get(TreeCoreAnnotations.TreeAnnotation.class); 
    // use Tregex to match 
    String SorSBARPattern = "/SBAR|^S$/"; 
    TregexPattern SorSBARTregexPattern = TregexPattern.compile(SorSBARPattern); 
    TregexMatcher SorSBARTregexMatcher = SorSBARTregexPattern.matcher(firstSentenceTree); 
    while (SorSBARTregexMatcher.find()) { 
     SorSBARTregexMatcher.getMatch().pennPrint(); 
    } 
    } 
} 
+0

En fait, je recommanderais d'utiliser Tregex plutôt que ma réponse originale. – StanfordNLPHelp