Skip to content

Commit e6bbd7f

Browse files
committed
docs(README.md): updating documentation
1 parent 6c2bcbc commit e6bbd7f

File tree

4 files changed

+131
-54
lines changed

4 files changed

+131
-54
lines changed

README.md

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ You may want to download, apart of the full package, other language models (see
3434

3535
### 3. Configure Stanford CoreNLP
3636

37-
#### 3.1. Use CoreNLP as HTTP Server
37+
#### 3.1. Using StanfordCoreNLPServer
3838

3939
```bash
4040
# Run the server using all jars in the current directory (e.g., the CoreNLP home directory)
@@ -44,35 +44,45 @@ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -t
4444
CoreNLP connects by default via StanfordCoreNLPServer, using port 9000. You can also opt to setup the connection differently:
4545

4646
```javascript
47-
import CoreNLP from 'corenlp';
47+
import CoreNLP, { Properties, Pipeline, ConnectorServer } from 'corenlp';
4848

49-
CoreNLP.setup('English', new CoreNLP.connector.ConnectorServer({ dsn: 'http://localhost:9000' }));
49+
const connector = new ConnectorServer({ dsn: 'http://localhost:9000' });
50+
const props = new Properties({
51+
annotators: 'tokenize,ssplit,pos,lemma,ner,parse',
52+
});
53+
const pipeline = new Pipeline(props, 'English', connector);
5054
```
5155

5256
#### 3.2. Use CoreNLP via CLI
5357

5458
CoreNLP expects by default the StanfordCoreNLP package to be placed (unzipped) inside the path `${YOUR_NPM_PROJECT_ROOT}/corenlp/`. You can also opt to setup the CLI interface differently:
5559

5660
```javascript
57-
import CoreNLP from 'corenlp';
58-
59-
CoreNLP.setup('Spanish', new CoreNLP.connector.ConnectorCli({
60-
// specify the paths relative to your project root
61-
classPath: 'corenlp/stanford-corenlp-full-2017-06-09/*',
62-
mainClass: 'edu.stanford.nlp.pipeline.StanfordCoreNLP',
63-
props: 'StanfordCoreNLP-spanish.properties',
64-
}));
61+
import CoreNLP, { Properties, Pipeline, ConnectorCli } from 'corenlp';
62+
63+
const connector = new ConnectorCli({
64+
classPath: 'corenlp/stanford-corenlp-full-2017-06-09/*', // specify the paths relative to your npm project root
65+
mainClass: 'edu.stanford.nlp.pipeline.StanfordCoreNLP', // optional
66+
props: 'StanfordCoreNLP-spanish.properties', // optional
67+
});
68+
const props = new Properties({
69+
annotators: 'tokenize,ssplit,pos,lemma,ner,parse',
70+
});
71+
const pipeline = new Pipeline(props, 'English', connector);
6572
```
6673

6774
### 4. Use it
6875

6976
```javascript
70-
// ...
77+
// ... initialize pipeline first (see above)
7178

7279
const sent = new CoreNLP.simple.Sentence('Hello world');
73-
sent.applyAnnotator(CoreNLP.simple.annotator.TokenizerAnnotator)
74-
.then(() => {
80+
pipeline.annotate(sent)
81+
.then(sent => {
7582
console.log(sent.words());
83+
})
84+
.catch(err => {
85+
console.log('err', err);
7686
});
7787
```
7888

@@ -84,29 +94,16 @@ NOTE2: The examples below assumes `es6` syntax, if you use require, use as follo
8494
### English
8595

8696
```javascript
87-
import CoreNLP from 'corenlp';
97+
import CoreNLP, { Properties, Pipeline } from 'corenlp';
8898

89-
CoreNLP.setup('English'); // check method docs for more setup options
90-
const sent = new CoreNLP.simple.Sentence('The little dog runs so fast.');
91-
sent.applyAnnotator(CoreNLP.simple.annotator.ParserAnnotator)
92-
.then(() => {
93-
console.log('parse', sent.parse());
94-
console.log(CoreNLP.util.Tree.fromSentence(sent).dump());
95-
})
96-
.catch(err => {
97-
console.log('err', err);
98-
});
99-
```
100-
101-
### Spanish
99+
const props = new Properties({
100+
annotators: 'tokenize,ssplit,pos,lemma,ner,parse',
101+
});
102+
const pipeline = new Pipeline(props, 'English'); // uses ConnectorServer by default
102103

103-
```javascript
104-
import CoreNLP from 'corenlp';
105-
106-
CoreNLP.setup('Spanish'); // check method docs for more setup options
107-
const sent = new CoreNLP.simple.Sentence('El pájaro veloz come kiwi.');
108-
sent.applyAnnotator(CoreNLP.simple.annotator.ParserAnnotator)
109-
.then(() => {
104+
const sent = new CoreNLP.simple.Sentence('The little dog runs so fast.');
105+
pipeline.annotate(sent)
106+
.then(sent => {
110107
console.log('parse', sent.parse());
111108
console.log(CoreNLP.util.Tree.fromSentence(sent).dump());
112109
})
@@ -121,6 +118,11 @@ We will update this section soon. In the meantime, you can browse the project c
121118
In summary, this NodeJS library aims to replicate the CoreNLP Simple Java interface but in Javascript. There are some minor differences however, for example the need to call `applyAnnotator` asynchronously.
122119

123120
```bash
121+
Properties
122+
Pipeline
123+
Service
124+
ConnectorCli
125+
ConnectorServer
124126
CoreNLP
125127
connector
126128
ConnectorServer # https://stanfordnlp.github.io/CoreNLP/corenlp-server.html

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"devDependencies": {
5050
"babel-core": "^6.25.0",
51+
"babel-plugin-transform-export-extensions": "^6.22.0",
5152
"babel-plugin-transform-object-rest-spread": "^6.26.0",
5253
"babel-polyfill": "^6.23.0",
5354
"babel-preset-env": "^1.6.0",

src/index.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Properties from './properties';
2-
import Pipeline from './pipeline';
31
import Document from './simple/document';
42
import Sentence from './simple/sentence';
53
import Token from './simple/token';
@@ -14,24 +12,23 @@ import ParserAnnotator from './simple/annotator/parse';
1412
import DependencyParseAnnotator from './simple/annotator/depparse';
1513
import RelationExtractorAnnotator from './simple/annotator/relation';
1614
import RegexNERAnnotator from './simple/annotator/regexner';
17-
import Service from './service';
18-
import ConnectorServer from './connector/connector-server';
19-
import ConnectorCli from './connector/connector-cli';
2015
import Tree from './util/tree';
16+
import _Properties from './properties';
17+
import _Pipeline from './pipeline';
18+
import _Service from './service';
19+
import _ConnectorCli from './connector/connector-cli';
20+
import _ConnectorServer from './connector/connector-server';
21+
22+
export const Properties = _Properties;
23+
export const Pipeline = _Pipeline;
24+
export const Service = _Service;
25+
export const ConnectorCli = _ConnectorCli;
26+
export const ConnectorServer = _ConnectorServer;
2127

2228
/**
2329
* CoreNLP NodeJS Interface
2430
*/
2531
export default {
26-
connector: {
27-
ConnectorServer,
28-
ConnectorCli,
29-
},
30-
31-
Properties,
32-
Pipeline,
33-
Service,
34-
3532
/**
3633
* https://stanfordnlp.github.io/CoreNLP/simple.html
3734
*/

src/index.spec.js

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import CoreNLP from '.';
1+
import CoreNLP, {
2+
Properties,
3+
Pipeline,
4+
ConnectorServer,
5+
ConnectorCli,
6+
} from '.';
27
import Document from './simple/document';
38
import Sentence from './simple/sentence';
49
import Token from './simple/token';
@@ -13,8 +18,6 @@ import ParserAnnotator from './simple/annotator/parse';
1318
import DependencyParseAnnotator from './simple/annotator/depparse';
1419
import RelationExtractorAnnotator from './simple/annotator/relation';
1520
import RegexNERAnnotator from './simple/annotator/regexner';
16-
import ConnectorServer from './connector/connector-server';
17-
import ConnectorCli from './connector/connector-cli';
1821
import Tree from './util/tree';
1922

2023
describe('CoreNLP Library entry point', () => {
@@ -27,11 +30,11 @@ describe('CoreNLP Library entry point', () => {
2730

2831
describe('connector', () => {
2932
it('should have ConnectorServer', async () => {
30-
expect(CoreNLP.connector).to.have.property('ConnectorServer').that.equals(ConnectorServer);
33+
expect(ConnectorServer).to.be.a('function');
3134
});
3235

3336
it('should have ConnectorCli', async () => {
34-
expect(CoreNLP.connector).to.have.property('ConnectorCli').that.equals(ConnectorCli);
37+
expect(ConnectorCli).to.be.a('function');
3538
});
3639
});
3740

@@ -78,5 +81,79 @@ describe('CoreNLP Library entry point', () => {
7881
expect(CoreNLP.util).to.have.property('Tree').that.equals(Tree);
7982
});
8083
});
84+
85+
describe('Integration Test', () => {
86+
context('Using ConnectorServer', async () => {
87+
it('should allow to initialize a pipeline and run annotations', async () => {
88+
const connector = new ConnectorServer({ dsn: 'http://localhost:9000' });
89+
sinon.stub(connector, 'get').returns(Promise.resolve({
90+
sentences: [{
91+
tokens: [
92+
{
93+
word: 'Hello',
94+
pos: 'UH',
95+
ner: 'O',
96+
},
97+
{
98+
word: 'world',
99+
pos: 'NN',
100+
ner: 'O',
101+
},
102+
],
103+
}],
104+
}));
105+
const props = new Properties({
106+
annotators: 'tokenize,ssplit',
107+
});
108+
const pipeline = new Pipeline(props, 'English', connector);
109+
const sent = new CoreNLP.simple.Sentence('Hello world');
110+
await pipeline.annotate(sent);
111+
expect(sent.word(0)).to.equal('Hello');
112+
expect(sent.word(1)).to.equal('world');
113+
expect(sent.token(0).pos()).to.equal('UH');
114+
expect(sent.token(1).pos()).to.equal('NN');
115+
expect(sent.token(0).ner()).to.equal('O');
116+
expect(sent.token(1).ner()).to.equal('O');
117+
});
118+
});
119+
120+
context('Using ConnectorCli', async () => {
121+
it('should allow to initialize a pipeline and run annotations', async () => {
122+
const connector = new ConnectorCli({
123+
classPath: 'corenlp/stanford-corenlp-full-2017-06-09/*',
124+
mainClass: 'edu.stanford.nlp.pipeline.StanfordCoreNLP',
125+
props: 'StanfordCoreNLP-spanish.properties',
126+
});
127+
sinon.stub(connector, 'get').returns(Promise.resolve({
128+
sentences: [{
129+
tokens: [
130+
{
131+
word: 'Hello',
132+
pos: 'UH',
133+
ner: 'O',
134+
},
135+
{
136+
word: 'world',
137+
pos: 'NN',
138+
ner: 'O',
139+
},
140+
],
141+
}],
142+
}));
143+
const props = new Properties({
144+
annotators: 'tokenize,ssplit',
145+
});
146+
const pipeline = new Pipeline(props, 'English', connector);
147+
const sent = new CoreNLP.simple.Sentence('Hello world');
148+
await pipeline.annotate(sent);
149+
expect(sent.word(0)).to.equal('Hello');
150+
expect(sent.word(1)).to.equal('world');
151+
expect(sent.token(0).pos()).to.equal('UH');
152+
expect(sent.token(1).pos()).to.equal('NN');
153+
expect(sent.token(0).ner()).to.equal('O');
154+
expect(sent.token(1).ner()).to.equal('O');
155+
});
156+
});
157+
});
81158
});
82159
});

0 commit comments

Comments
 (0)