2010-09-17 16 views
9

Sto utilizzando Stanford Parser per analizzare le relazioni di dipendenza tra un paio di parole, ma ho anche bisogno del tagging delle parole. Tuttavia, in ParseDemo.java, il programma emette solo l'albero di tagging. Ho bisogno di tagging di ogni parola in questo modo:Come ottenere tagging POS utilizzando Stanford Parser

My/PRP$ dog/NN also/RB likes/VBZ eating/VBG bananas/NNS ./. 

non in questo modo:

(ROOT 
    (S 
    (NP (PRP$ My) (NN dog)) 
    (ADVP (RB also)) 
    (VP (VBZ likes) 
     (S 
     (VP (VBG eating) 
      (S 
      (ADJP (NNS bananas)))))) 
    (. .))) 

Chi mi può aiutare? molte grazie.

risposta

12

Se siete interessati soprattutto a manipolare i tag in un programma, e non è necessario la funzionalità TreePrint, si può solo ottenere le parole nella categoria un elenco:

LexicalizedParser lp = 
    LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"); 
Tree parse = lp.apply(Arrays.asList(sent)); 
List taggedWords = parse.taggedYield();  
+0

Che cos'è lp? .... –

+1

Un LexicalizedParser ... modificato in precedenza. –

+0

@ChristopherManning Come si può ottenere questo in python –

3

Quando si esegue edu.stanford.nlp.parser.lexparser.LexicalizedParser sulla riga di comando, che si desidera utilizzare:

-outputFormat "wordsAndTags" 

Programatically, utilizzare la classe TreePrint costruito con FormatString = "wordsAndTags" e chiamare StampaAlberoPre , in questo modo:

TreePrint posPrinter = new TreePrint("wordsAndTags", yourPrintWriter); 
posPrinter.printTree(yourLexParser.getBestParse()); 
2
String[] sent = { "This", "is", "an", "easy", "sentence", "." }; 
List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent); 
Tree parse = lp.apply(rawWords); 
ArrayList ar=parse.taggedYield(); 
System.out.println(ar.toString()); 
+0

cosa è 'lp' qui? ti sei perso per spiegarlo. – talha06

+1

@ talha06: Suppongo che sia un LexicalizedParser – citronas

+0

Sì, è un LexicalizedParser. –

0

Questa risposta è un po 'obsolete così ho deciso di aggiungere la mia. Quindi, con la versione Stanford Parser 3.6.0 (dipendenze Maven):

<dependency> 
     <groupId>edu.stanford.nlp</groupId> 
     <artifactId>stanford-parser</artifactId> 
     <version>3.6.0</version> 
    </dependency> 
    <dependency> 
     <groupId>edu.stanford.nlp</groupId> 
     <artifactId>stanford-corenlp</artifactId> 
     <version>3.6.0</version> 
    </dependency> 
    <dependency> 
     <groupId>edu.stanford.nlp</groupId> 
     <artifactId>stanford-corenlp</artifactId> 
     <version>3.6.0</version> 
     <classifier>models</classifier> 
    </dependency> 

 private static MaxentTagger tagger = new MaxentTagger(MaxentTagger.DEFAULT_JAR_PATH); 
     public String getTaggedString(String someString) { 

      String taggedString = tagger.tagString(someString); 
      return taggedString; 
     } 

Ciò restituirà I_PRP claim_VBP the_DT rights_NNS per 'I claim the rights'

Quindi, se si desidera rilevare i verbi in una frase utilizzando Java e stanford parser puoi fare questo:

public boolean containsVerb(String someString) { 
     String taggedString = tagger.tagString(someString); 
     String[] tokens = taggedString.split(" "); 
     for (String tok : tokens){ 
      String[] taggedTokens = tok.split("_"); 
      if (taggedTokens[1].startsWith("VB")){ 
       return true; 
      } 

     } 
     return false; 
} 
Problemi correlati