node-corenlp icon indicating copy to clipboard operation
node-corenlp copied to clipboard

Question about usage

Open owendall opened this issue 7 years ago • 1 comments

Hi,

So glad to see a node interface to CoreNLP.

As I am new to working with this API, I am wondering the best practices with starting from a document and working through sentences.

Suggestions for enhancing the following code and links to any examples would be much appreciated!


// 2017-11-25 learning corenlp v1.1.8
// The coreNLP server is a Docker image mapped to port 9000
require("babel-polyfill");

//  Need this polyfill even if you are not using async/await.
//  I received this error in both node.js 7.8.0 and node.js 8.5.0:
//------   /Users/owendall/Desktop/github-applications/ai/core-nlp-test/node_modules/corenlp/dist/pipeline.js:139
//-----    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(annotable) {
//-----    ReferenceError: regeneratorRuntime is not defined

const corenlp = require("corenlp");
const coreNLP = corenlp.default; // convenient when not using `import`
const connector = new corenlp.ConnectorServer({
    dsn: 'http://localhost:9000',
});

// initialize the pipeline and document to annotate
const props = new corenlp.Properties({
    annotators: 'ssplit,tokenize,pos,ner,parse'
});

const pipeline = new corenlp.Pipeline(props, 'English', connector);

console.log("Pipeline Details: ",JSON.stringify(pipeline, null,2));// Let's look at the internals

const doc = new coreNLP.simple.Document(
    'I live on the river, close to the river bank. I had an old boat on the river. I used the boat on the river every day.'
);

console.log(JSON.stringify(doc,null,2));

pipeline.annotate(doc)
  .then(function(doc) {
    var sentenceArray = doc.sentences();
    console.log("Found "+ sentenceArray.length + " sentences"); 
    console.log("sentenceArray: ",JSON.stringify(sentenceArray,null,2));
    sentenceArray.map(function(sentence){
      console.log("Sentence: ", sentence)
    });
  })
  .catch(function(err) {
    console.log('err', err);
  });

owendall avatar Nov 25 '17 18:11 owendall

@owendall Here's an example of how it could be done.

const doc = new CoreNLP.simple.Document(I live on the river, close to the river bank. I had an old boat on the river. I used the boat on the river every day.);

pipeline.annotate(doc) .then(doc => { const sent = doc.sentences(); sent.forEach(sentence => { const tree = CoreNLP.util.Tree.fromSentence(sentence); tree.visitLeaves(node => console.log(node.word(), '=', node.pos())); }) }) .catch(err => { console.log('err', err); });

kenpachiii avatar Jan 29 '18 01:01 kenpachiii