This is an implementation of a Wordnet API in pure JavaScript. It was initially written as a replacement of the WordNet code in NaturalNode/natural, which was hard to use for higher-level tasks. It provides a simple set of query functions that allow lookups against a WordNet database of your choice.
You'll need a copy of WordNet. There are several on Github, but for full functionality,
at least for now, I'd suggest using the npm package: wndb-with-exceptions
, available at:
https://github.com/morungos/WNdb-with-exceptions,
as it includes the morphological exception lists needed by validForms
. If you don't care about that,
you can get by with https://github.com/moos/WNdb, or even
download WordNet directly.
This module doesn't download and install the WordNet files, because there are several versions and it feels impolite to download and install one for you.
For easy use, therefore, it might be best to add both this module and a WordNet data module to your project, e.g.:
npm install node-wordnet --save
npm install wndb-with-exceptions --save
The constructor returns a new object to access a WordNet database. The passed options configure the interface. The following options are available:
-
dataDir -- specifies the location of the Wordnet directory.
If this option isn't passed, the module uses
require
to locatewndb-with-exceptions
, so if you don't want to deploy your own WordNet, all you need to do is addwndb-with-exceptions
as an application dependency and not pass a directory to the constructor. The original WordNet data files can always be manually downloaded and installed anywhere from http://wordnet.princeton.edu/wordnet/download.As a shortcut, if you pass a string directly to the constructor, it's interpreted as a Wordnet directory, and all other options default in sensible ways.
-
cache -- adds an LRU cache to the Wordnet access.
If the option is false, no cache is set; and if it is true, then a cache (using
lru-cache
with a default size of 2000 items) is set. In addition, the cache can be an object. If that object has aget
method then it's used as a cache directly, and if it doesn't, it's assumed to be a configuration object which will be used to configure a newlru-cache
. Benchmarks suggest that even this small cache improves performance by a factor of 100 or so.
Opens all access to the WordNet database. Queries will fail until this has resolved. It returns a promise that resolves when the database is ready for use.
Here's an example of looking up definitions for the word, "node". Returns a Promise resolving to the results.
const wordnet = new WordNet()
wordnet.open()
.then(() => wordnet.lookup('node'))
.then((results) => {
results.forEach((result) => {
console.log('------------------------------------');
console.log(result.synsetOffset);
console.log(result.pos);
console.log(result.lemma);
console.log(result.synonyms);
console.log(result.pos);
console.log(result.gloss);
});
})
.then(() => wordnet.close());
Given a synset offset and a part of speech, a definition can be looked up directly.
wordnet.get(4424418, 'n')
.then((result) => {
console.log('------------------------------------');
console.log(result.lemma);
console.log(result.pos);
console.log(result.gloss);
console.log(result.synonyms);
});
Returns a promise that resolves to valid morphological exceptions.
wordnet.validForms('axes#n')
.then(console.log);
Queries WordNet to find all the senses of a given word, optionally with a part-of-speech.
wordnet.querySense('axes#n').then(console.log);
Queries WordNet to find full information on a single sense of a term.
wordnet.findSense('lie#v#1').then(console.log);
Closes all the file handles being used by this instance. If new queries are done, the files may be silently re-opened, but that probably isn't a very good plan. Re-use of an instance after close is deprecated.