CoreNLP may be "your one stop shop for natural language processing in Java", but you can easily use it from Javascript and Typescript by running it in server mode. This library provides a thin, type-safe wrapper around cross-fetch for accessing the CoreNLP HTTP API from Node.js or a browser.
npm install corenlp-ts
import { CoreNLP, DependencyGraph } from 'corenlp-ts';
const { CORENLP_URL = 'http://localhost:9000' } = process.env;
function printDepParse(g: DependencyGraph, relation = 'root', depth = 0, visited = new Set<DependencyGraph>()): void {
let word = g.token.word;
if (g.token.lemma && g.token.lemma !== word) {
word += ` [${g.token.lemma}]`;
}
console.log(`${' '.repeat(depth)}${relation}/${g.token.pos}: ${word} (${g.token.index})`);
if (!visited.has(g)) {
visited.add(g);
for (const dep of g.dependents) {
printDepParse(dep.dependent, dep.relation, depth + 1, visited);
}
}
}
(async function name() {
const nlp = new CoreNLP(CORENLP_URL, { annotators: ['lemma', 'depparse'] });
const res = await nlp.annotate('She flew to Bali or to Turkey');
res.sentences.forEach((s) => printDepParse(s.toDependencyGraph()));
})();
root/VBD: flew [fly] (2)
nsubj/PRP: She [she] (1)
nsubj/PRP: She [she] (1)
conj:or/VBD: flew [fly] (2)
obl:to/NNP: Bali (4)
case/IN: to (3)
cc/CC: or (5)
obl:to/NNP: Turkey (7)
case/IN: to (6)
The easiest way to run the CoreNLP server is using Docker. I recommend my own minimalist image based on OpenJDK 8 and Alpine Linux.
docker run -m 4g --publish 9000:9000 --detach --name corenlp scurrilous/corenlp-jre8-alpine:latest
corenlp-ts
is available under the ISC license.