Как сохранить CoreDocument в Stanford NLP на диск - PullRequest
0 голосов
/ 21 декабря 2018

После создания аннотированного CoreDocument необходимо сохранить его на диск, а затем извлечь его.

Вычисление аннотированного CoreDocument происходит медленно.После того, как он был создан, вы захотите использовать его позже, т.е. получите его с диска.

props.setProperty("annotators", 
"tokenize,ssplit,pos,lemma,ner,parse,depparse,coref,kbp,quote");
    // set a property for an annotator, in this case the coref annotator is being set to use the neural algorithm
    props.setProperty("coref.algorithm", "neural");
    // build pipeline
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    // create a document object
    CoreDocument document = new CoreDocument(content);
    // annnotate the document
    pipeline.annotate(document);

Ответы [ 2 ]

0 голосов
/ 03 января 2019
Thanks for the help, as I'm new to the stanford npl. The AnnotationSerialize class 
moved me forward in saving the document to disk. I had a further misunderstanding 
about interpreting the result.  I didn't realize that the result (pair.first) 
contained the full result.  The pertinent code is:

public void writeDoc(CoreDocument document, String filename ) {
    AnnotationSerializer serializer = new ProtobufAnnotationSerializer();
    FileOutputStream fos = null;
    try {
        OutputStream ks = new FileOutputStream(filename);
        ks = serializer.writeCoreDocument(document, ks);
        ks.flush();
        ks.close();
    }catch(IOException ioex) {
        logger.error("IOException "+ioex);
    }
  }

public void ReadSavedDoc(String filename) {
    try {
        File initialFile = new File(filename);
        InputStream ks = new FileInputStream(initialFile);

     // Read
        AnnotationSerializer serializer = new ProtobufAnnotationSerializer();
        InputStream kis = new ByteArrayInputStream(ks.readAllBytes());
        Pair<Annotation, InputStream> pair = serializer.read(kis);
        pair.second.close();
        Annotation readAnnotation = pair.first;
        kis.close();
     //Output
        List<CoreLabel> newTokens = 
readAnnotation.get(CoreAnnotations.TokensAnnotation.class);
        for(CoreLabel atoken: newTokens)
            System.out.println("atoken "+atoken);
        List<CoreMap> newSentences = 
readAnnotation.get(CoreAnnotations.SentencesAnnotation.class);
        logger.info("Sentences "+newSentences);
        String newEntity = 
readAnnotation.get(CoreAnnotations.NamedEntityTagAnnotation.class);
        System.out.println("named entity "+newEntity);
        String newPOS = 
readAnnotation.get(CoreAnnotations.PartOfSpeechAnnotation.class);
        logger.info("pos "+newPOS);
        for(CoreMap sentence : newSentences){
            System.out.println(sentence);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }  catch (ClassCastException e) {
        e.printStackTrace();
    } catch(Exception ex) {
        logger.error("Exception: "+ex);
        ex.printStackTrace();
    }

}
Hope this helps someone else.  Don
0 голосов
/ 22 декабря 2018

Вы должны взглянуть на класс AnnotationSerializer:

https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/pipeline/AnnotationSerializer.html

В частности, хотя существует несколько экземпляров этого класса, мы в основном использовали ProtobufAnnotationSerializer.

Вы можете увидеть примеры использования в некоторых интеграционных тестах.ProtobufSerializationSanityITest - простой пример того, как его использовать.ProtobufAnnotationSerializerSlowITest гораздо более тщательный, но сложный пример.Вы можете найти их в Github хранилище .

...