|
| 1 | +// requires ployfill for ES7 async/await |
1 | 2 | require("babel-polyfill"); |
2 | | -const corenlp = require("corenlp"), CoreNLP = corenlp.default; |
3 | | -const Properties = corenlp.Properties; |
4 | | -const Pipeline = corenlp.Pipeline; |
5 | | -const ConnectorServer = corenlp.ConnectorServer; |
| 3 | + |
| 4 | +const corenlp = require("corenlp"); |
| 5 | +const CoreNLP = corenlp.default; // convenient when not using `import` |
| 6 | + |
6 | 7 | /** |
7 | | - * NOTE: The server http://corenlp.run is used here just for demo purposes. |
| 8 | + * IMPORTANT |
| 9 | + * The server http://corenlp.run is used here just for demo purposes. |
8 | 10 | * It is not set up to handle a large volume of requests. Instructions for |
9 | 11 | * setting up your own server can be found in the Dedicated Server section (link below). |
10 | 12 | * @see {@lik https://stanfordnlp.github.io/CoreNLP/corenlp-server.html} |
11 | 13 | * @see {@link http://corenlp.run} |
12 | 14 | */ |
13 | | -const connector = new ConnectorServer({ dsn: 'http://corenlp.run' }); |
14 | | -const props = new Properties({ annotators: 'tokenize,ssplit,pos,parse' }); |
15 | | -const pipeline = new Pipeline(props, 'Spanish', connector); |
| 15 | +const connector = new corenlp.ConnectorServer({ |
| 16 | + dsn: 'http://corenlp.run', |
| 17 | +}); |
| 18 | + |
| 19 | +// initialize the pipeline and document to annotate |
| 20 | +const props = new corenlp.Properties({ |
| 21 | + annotators: 'tokenize,ssplit,pos,ner,parse', |
| 22 | +}); |
| 23 | +const pipeline = new corenlp.Pipeline(props, 'Spanish', connector); |
| 24 | +const sent = new CoreNLP.simple.Sentence( |
| 25 | + 'Jorge quiere cinco empanadas de queso y carne.' |
| 26 | +); |
| 27 | + |
| 28 | +// performs the call to corenlp (in this case via http) |
| 29 | +await pipeline.annotate(sent); |
| 30 | + |
| 31 | +// constituency parse string representation |
| 32 | +console.log('parse', sent.parse()); |
16 | 33 |
|
17 | | -const sent = new CoreNLP.simple.Sentence('...contemplando la membrana azul de tus ojos fulminando el tiempo, y el espacio.'); |
| 34 | +// constituency parse tree representation |
| 35 | +const tree = CoreNLP.util.Tree.fromSentence(sent); |
18 | 36 |
|
19 | | -// The response might take few seconds... |
20 | | -pipeline.annotate(sent) |
21 | | - .then(sent => { |
22 | | - console.log(sent.tokens().map(token => `${token}: ${token.pos()}`)); |
23 | | - console.log(sent.tokens().map(token => `${token}: ${token.posInfo().tag}`)); |
24 | | - }) |
25 | | - .catch(err => { |
26 | | - console.log('err', err); |
27 | | - }); |
| 37 | +// traverse the tree leaves and print some props |
| 38 | +tree.visitLeaves(node => |
| 39 | + console.log(node.word(), node.pos(), node.token().ner())); |
| 40 | + |
| 41 | +// dump the tree for debugging |
| 42 | +console.log(JSON.stringify(tree.dump(), null, 2)); |
0 commit comments