Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about usage #29

Open
owendall opened this issue Nov 25, 2017 · 1 comment
Open

Question about usage #29

owendall opened this issue Nov 25, 2017 · 1 comment
Labels

Comments

@owendall
Copy link

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);
  });
@kenpachiii
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants