Skip to content

Commit

Permalink
Merge branch 'release/0.1.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
morungos committed Apr 9, 2015
2 parents 24592ac + 0cda745 commit 6b7529f
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 55 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Expand Up @@ -2,6 +2,12 @@ Release history
===============


Version 0.1.8
-------------

* Added an optional LRU cache


Version 0.1.7
-------------

Expand Down
31 changes: 23 additions & 8 deletions README.md
Expand Up @@ -40,16 +40,31 @@ npm install wndb-with-exceptions --save
API
---

### new WordNet([directory])
### new WordNet([options | string])

The constructor returns a new object to access a WordNet database at the specified
directory. If no directory is passed, the module uses `require` to locate
`wndb-with-exceptions`, so if you don't want to deploy your own WordNet, all you
need to do is add `wndb-with-exceptions` as an application dependency and not
pass a directory to the constructor.
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 locate
`wndb-with-exceptions`, so if you don't want to deploy your own WordNet, all you
need to do is add `wndb-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 a `get` 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 new `lru-cache`.

The original WordNet data files can always be manually downloaded and installed
anywhere from http://wordnet.princeton.edu/wordnet/download.

### lookup(word, callback)

Expand Down
125 changes: 103 additions & 22 deletions lib/wordnet.js
@@ -1,4 +1,4 @@
var DataFile, IndexFile, Promise, WordNet, async, fs, path,
var DataFile, IndexFile, LRU, Promise, WordNet, async, fs, path,
__slice = [].slice;

IndexFile = require('./index_file');
Expand All @@ -13,32 +13,57 @@ path = require('path');

fs = require('fs');

LRU = require('lru-cache');

require('es6-shim');

WordNet = (function() {
var exceptions, forms, tokenDetach, unique, _forms, _loadExceptions, _validForms, _validFormsWithExceptions;

function WordNet(dataDir) {
function WordNet(options) {
var WNdb, e;
if (!dataDir) {
if (typeof options === 'string') {
options = {
dataDir: options
};
} else {
if (options == null) {
options = {};
}
}
if (options.dataDir == null) {
try {
WNdb = require('wndb-with-exceptions');
} catch (_error) {
e = _error;
console.error("Please 'npm install wndb-with-exceptions' before using WordNet module or specify a dict directory.");
throw e;
}
dataDir = WNdb.path;
options.dataDir = WNdb.path;
}
if (!options.cache) {
this.cache = null;
} else {
if (options.cache === true) {
options.cache = {
max: 2000
};
}
if (typeof options.cache === 'object' && typeof options.cache.get === 'function') {
this.cache = options.cache;
} else {
this.cache = LRU(options.cache);
}
}
this.path = dataDir;
this.nounIndex = new IndexFile(dataDir, 'noun');
this.verbIndex = new IndexFile(dataDir, 'verb');
this.adjIndex = new IndexFile(dataDir, 'adj');
this.advIndex = new IndexFile(dataDir, 'adv');
this.nounData = new DataFile(dataDir, 'noun');
this.verbData = new DataFile(dataDir, 'verb');
this.adjData = new DataFile(dataDir, 'adj');
this.advData = new DataFile(dataDir, 'adv');
this.path = options.dataDir;
this.nounIndex = new IndexFile(this.path, 'noun');
this.verbIndex = new IndexFile(this.path, 'verb');
this.adjIndex = new IndexFile(this.path, 'adj');
this.advIndex = new IndexFile(this.path, 'adv');
this.nounData = new DataFile(this.path, 'noun');
this.verbData = new DataFile(this.path, 'verb');
this.adjData = new DataFile(this.path, 'adj');
this.advData = new DataFile(this.path, 'adv');
this.allFiles = [
{
index: this.nounIndex,
Expand All @@ -61,9 +86,21 @@ WordNet = (function() {
}

WordNet.prototype.get = function(synsetOffset, pos, callback) {
var dataFile;
dataFile = this.getDataFile(pos);
return dataFile.get(synsetOffset, callback);
var dataFile, hit, query, wordnet;
wordnet = this;
if (this.cache) {
query = "get:" + synsetOffset + ":" + pos;
if (hit = wordnet.cache.get(query)) {
return callback(hit);
}
}
dataFile = wordnet.getDataFile(pos);
return dataFile.get(synsetOffset, function(result) {
if (query) {
wordnet.cache.set(query, result);
}
return callback(result);
});
};

WordNet.prototype.getAsync = function(synsetOffset, pos) {
Expand All @@ -77,14 +114,25 @@ WordNet = (function() {
};

WordNet.prototype.lookup = function(input, callback) {
var lword, pos, selectedFiles, word, wordnet, _ref;
var hit, lword, pos, query, selectedFiles, word, wordnet, _ref;
wordnet = this;
_ref = input.split('#'), word = _ref[0], pos = _ref[1];
lword = word.toLowerCase().replace(/\s+/g, '_');
if (this.cache) {
query = "lookup:" + input;
if (hit = wordnet.cache.get(query)) {
return callback(hit);
}
}
selectedFiles = !pos ? wordnet.allFiles : wordnet.allFiles.filter(function(file) {
return file.pos === pos;
});
return wordnet.lookupFromFiles(selectedFiles, [], lword, callback);
return wordnet.lookupFromFiles(selectedFiles, [], lword, function(results) {
if (query) {
wordnet.cache.set(query, results);
}
return callback(results);
});
};

WordNet.prototype.lookupAsync = function(input, callback) {
Expand All @@ -98,9 +146,15 @@ WordNet = (function() {
};

WordNet.prototype.findSense = function(input, callback) {
var lword, pos, selectedFiles, sense, senseNumber, word, wordnet, _ref;
var hit, lword, pos, query, selectedFiles, sense, senseNumber, word, wordnet, _ref;
wordnet = this;
_ref = input.split('#'), word = _ref[0], pos = _ref[1], senseNumber = _ref[2];
if (this.cache) {
query = "findSense:" + input;
if (hit = wordnet.cache.get(query)) {
return callback(hit);
}
}
sense = parseInt(senseNumber);
if (Number.isNaN(sense)) {
throw new Error("Sense number should be an integer");
Expand All @@ -112,7 +166,12 @@ WordNet = (function() {
return file.pos === pos;
});
return wordnet.lookupFromFiles(selectedFiles, [], lword, function(response) {
return callback(response[sense - 1]);
var result;
result = response[sense - 1];
if (query) {
wordnet.cache.set(query, result);
}
return callback(result);
});
};

Expand All @@ -127,9 +186,15 @@ WordNet = (function() {
};

WordNet.prototype.querySense = function(input, callback) {
var pos, word, wordnet, _ref;
var hit, pos, query, word, wordnet, _ref;
wordnet = this;
_ref = input.split('#'), word = _ref[0], pos = _ref[1];
if (this.cache) {
query = "querySense:" + input;
if (hit = wordnet.cache.get(query)) {
return callback(hit);
}
}
return wordnet.lookup(input, function(results) {
var i, sense, senseCounts, senses;
senseCounts = {};
Expand All @@ -149,6 +214,9 @@ WordNet = (function() {
}
return _results;
})();
if (query) {
wordnet.cache.set(query, senses);
}
return callback(senses);
});
};
Expand Down Expand Up @@ -502,7 +570,20 @@ WordNet = (function() {
};

WordNet.prototype.validForms = function(string, callback) {
return _validFormsWithExceptions(this, string, callback);
var hit, query, wordnet;
wordnet = this;
if (this.cache) {
query = "validForms:" + string;
if (hit = wordnet.cache.get(query)) {
return callback(hit);
}
}
return _validFormsWithExceptions(this, string, function(result) {
if (query) {
wordnet.cache.set(query, result);
}
return callback(result);
});
};

WordNet.prototype.validFormsAsync = function(string) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "node-wordnet",
"version": "0.1.7",
"version": "0.1.8",
"description": "Node.js interface for Wordnet",
"main": "lib/wordnet.js",
"scripts": {
Expand Down Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"async": "^0.9.0",
"bluebird": "^2.6.0",
"es6-shim": "^0.22.1"
"es6-shim": "^0.22.1",
"lru-cache": "^2.5.0"
}
}

0 comments on commit 6b7529f

Please sign in to comment.