2012-05-21 4 views
9

Voglio fare un lavoro molto semplice: dato una stringa contenente pronomi, voglio risolverli.Risolvi la coreografia usando Stanford CoreNLP - impossibile caricare il parser model

per esempio, voglio girare la frase "Maria ha un agnellino, è carina". in "Maria ha un piccolo agnellino, Mary è carina".

Ho provato a utilizzare Stanford CoreNLP. Tuttavia, non riesco a far avviare il parser. Ho importato tutti i jar inclusi nel mio progetto usando Eclipse e ho assegnato 3 GB alla JVM (-Xmx3g).

l'errore è molto imbarazzante:

Exception in thread "main" java.lang.NoSuchMethodError: edu.stanford.nlp.parser.lexparser.LexicalizedParser.loadModel(Ljava/lang/String;[Ljava/lang/String;)Ledu/stanford/nlp/parser/lexparser/LexicalizedParser;

non capisco dove che viene da L, penso che è la radice del mio problema ... Questo è piuttosto strano. Ho cercato di entrare nei file sorgente, ma non vi è alcun riferimento sbagliato lì.

Codice:

import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation; 
import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation; 
import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefGraphAnnotation; 
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.trees.TreeCoreAnnotations.TreeAnnotation; 
import edu.stanford.nlp.ling.CoreLabel; 
import edu.stanford.nlp.dcoref.CorefChain; 
import edu.stanford.nlp.pipeline.*; 
import edu.stanford.nlp.trees.Tree; 
import edu.stanford.nlp.semgraph.SemanticGraph; 
import edu.stanford.nlp.util.CoreMap; 
import edu.stanford.nlp.util.IntTuple; 
import edu.stanford.nlp.util.Pair; 
import edu.stanford.nlp.util.Timing; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 

import java.util.Properties; 

public class Coref { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws IOException, ClassNotFoundException { 
    // 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 in the text variable 
    String text = "Mary has a little lamb. She is very cute."; // Add your text here! 

    // 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);  
     } 

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

     // this is the Stanford dependency graph of the current sentence 
     SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class); 
    } 

    // 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); 
    System.out.println(graph); 
    } 
} 

completa analisi dello stack:

Adding annotator tokenize Adding annotator ssplit Adding annotator pos Loading POS Model [edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger] ... Loading default properties from trained tagger edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger Reading POS tagger model from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [2.1 sec]. done [2.2 sec]. Adding annotator lemma Adding annotator ner Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz ... done [4.0 sec]. Loading classifier from edu/stanford/nlp/models/ner/english.muc.distsim.crf.ser.gz ... done [3.0 sec]. Loading classifier from edu/stanford/nlp/models/ner/english.conll.distsim.crf.ser.gz ... done [3.3 sec]. Adding annotator parse Exception in thread "main" java.lang.NoSuchMethodError: edu.stanford.nlp.parser.lexparser.LexicalizedParser.loadModel(Ljava/lang/String;[Ljava/lang/String;)Ledu/stanford/nlp/parser/lexparser/LexicalizedParser; at edu.stanford.nlp.pipeline.ParserAnnotator.loadModel(ParserAnnotator.java:115) at edu.stanford.nlp.pipeline.ParserAnnotator.(ParserAnnotator.java:64) at edu.stanford.nlp.pipeline.StanfordCoreNLP$12.create(StanfordCoreNLP.java:603) at edu.stanford.nlp.pipeline.StanfordCoreNLP$12.create(StanfordCoreNLP.java:585) at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:62) at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:329) at edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:196) at edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:186) at edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:178) at Coref.main(Coref.java:41)

+0

Ok, risulta che la lettera L deriva dalla lingua stessa. Quindi, non capisco cosa sta succedendo ... – Tex

risposta

9

Sì, la L è solo una cosa bizzarra Sun da sin da Java 1.0.

LexicalizedParser.loadModel(String, String ...) è un nuovo metodo aggiunto al parser, che non è stato trovato. Sospetto che ciò significhi avere un'altra versione del parser nel classpath che viene invece utilizzata.

Prova questo: al guscio esterno di qualsiasi IDE, dare a questi comandi (dando il percorso di Stanford-corenlp modo appropriato, e cambiando: a, se su Windows:

javac -cp ".:stanford-corenlp-2012-04-09/*" Coref.java 
java -mx3g -cp ".:stanford-corenlp-2012-04-09/*" Coref 

I carichi parser e il codice funziona correttamente per me - ho solo bisogno di aggiungere alcune dichiarazioni di stampa in modo da poter vedere cosa ha fatto :-).

+0

Sì, entrambi funzionano: rimuovendo il parser dal mio classpath o provando il buon funzionamento della shell. Grazie! – Tex

+0

Qualcuno può dirmi quale versione di StanfordCoreNLP è compatibile con questo codice. Ho provato a utilizzare la versione 3.2.0 e 1.2.0 ma sta dimostrando che CorefCoreAnnotations non è disponibile. – user1580096

+0

Ho aggiornato il codice per lavorare con la corrente v3.2.0 del parser e mi sono permesso di aggiungere un paio di istruzioni di stampa allo stesso tempo. Il cambiamento necessario era che quel semigrafo era stato spostato fuori dal pacchetto di alberi per essere un diretto discendente di e.s.n. –