Search code examples
javacallbacklistenerstanford-nlpexist-db

Is there a listener in the Stanford CoreNLP pipeline checking for abort?


I am working on an XQuery implementation of the Stanford CoreNLP pipeline for eXist-db. eXist-db is an open source XML database.

I have written a function module for eXist that is written in Java that wraps around the CoreNLP pipeline.

Here is an example of a call:

xquery version "3.1";

import module namespace nlp="http://exist-db.org/xquery/stanford-nlp";

let $text := "The fate of Lehman Brothers, the beleaguered investment bank, " ||
             "hung in the balance on Sunday as Federal Reserve officials and " ||
             "the leaders of major financial institutions continued to gather in " ||
             "emergency meetings trying to complete a plan to rescue the stricken " ||
             "bank.  Several possible plans emerged from the talks, held at the " ||
             "Federal Reserve Bank of New York and led by Timothy R. Geithner, " ||
             "the president of the New York Fed, and Treasury Secretary Henry M. Paulson Jr."

let $properties := map { 
                     "annotators" : "tokenize, ssplit, pos, lemma, ner, depparse, coref",
                     "tokenize.language" : "en" 
                   }

return nlp:parse($text, $properties)

The function needs to be able to respond to a call to kill the running query. The call is system:kill-running-xquery()

Is there a listener or a callback feature in StanfordCoreNLP that will allow for the pipeline to terminate cleanly?


Solution

  • If you use the Stanford CoreNLP Server then you can send a shutdown command. In Java code you can make a pipeline that is actually just backed by a server.

    // creates a StanfordCoreNLP object with POS tagging, lemmatization, NER, parsing, and coreference resolution
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    StanfordCoreNLPClient pipeline = new StanfordCoreNLPClient(props, "http://localhost", 9000, 2);
    // read some text in the text variable
    String text = ... // 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);
    pipeline.shutdown();
    

    More info here: https://stanfordnlp.github.io/CoreNLP/corenlp-server.html

    The pipeline class StanfordCoreNLP (as opposed to StanfordCoreNLPClient) doesn't have any ability to be shutdown in this manner.