added profile option. bumped to 0.1.2
This commit is contained in:
parent
5f7a72cf44
commit
7d5d213675
18
README.md
18
README.md
|
@ -180,6 +180,24 @@ Or use WordNet's inherited method:
|
||||||
wordpos.lookup('great', console.log);
|
wordpos.lookup('great', console.log);
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
|
### Options
|
||||||
|
|
||||||
|
```js
|
||||||
|
WordPOS.defaults = {
|
||||||
|
/**
|
||||||
|
* enable profiling, time in msec returned as second argument in callback
|
||||||
|
*/
|
||||||
|
profile: false
|
||||||
|
};
|
||||||
|
```
|
||||||
|
To override, pass an options hash to the constructor:
|
||||||
|
|
||||||
|
```js
|
||||||
|
wordpos = new WordPOS({profile: true});
|
||||||
|
```
|
||||||
|
|
||||||
|
With the `profile` option, all callbacks receive a second argument that is the execution time in msec of the call.
|
||||||
|
|
||||||
|
|
||||||
Benchmark
|
Benchmark
|
||||||
----------
|
----------
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
"author": "Moos <mooster@42at.com>",
|
"author": "Moos <mooster@42at.com>",
|
||||||
"keywords": ["natural", "language", "wordnet", "pos"],
|
"keywords": ["natural", "language", "wordnet", "pos"],
|
||||||
"description": "wordpos is a set of part-of-speech utilities for Node.js using natural's WordNet module.",
|
"description": "wordpos is a set of part-of-speech utilities for Node.js using natural's WordNet module.",
|
||||||
"version": "0.1.1",
|
"version": "0.1.2",
|
||||||
"homepage": "https://github.com/moos/wordpos",
|
"homepage": "https://github.com/moos/wordpos",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.4.10"
|
"node": ">=0.4.10"
|
||||||
|
|
65
wordpos.js
65
wordpos.js
|
@ -29,51 +29,81 @@ function prepText(text) {
|
||||||
|
|
||||||
function lookup(pos) {
|
function lookup(pos) {
|
||||||
return function(word, callback) {
|
return function(word, callback) {
|
||||||
|
var profile = this.options.profile,
|
||||||
|
start = profile && new Date(),
|
||||||
|
args = [];
|
||||||
word = normalize(word);
|
word = normalize(word);
|
||||||
this.lookupFromFiles([
|
this.lookupFromFiles([
|
||||||
{index: this.getIndexFile(pos), data: this.getDataFile(pos)}
|
{index: this.getIndexFile(pos), data: this.getDataFile(pos)}
|
||||||
], [], word, callback);
|
], [], word, function(results){
|
||||||
|
args.push(results);
|
||||||
|
profile && args.push(new Date() - start);
|
||||||
|
callback.apply(null, args);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function is(pos){
|
function is(pos){
|
||||||
return function(word, callback) {
|
return function(word, callback, _noprofile) {
|
||||||
var index = this.getIndexFile(pos);
|
// disable profiling when isX() used internally
|
||||||
|
var profile = this.options.profile && !_noprofile,
|
||||||
|
start = profile && new Date(),
|
||||||
|
args = [],
|
||||||
|
index = this.getIndexFile(pos);
|
||||||
word = normalize(word);
|
word = normalize(word);
|
||||||
index.lookup(word, function(record) {
|
index.lookup(word, function(record) {
|
||||||
callback(!!record);
|
args.push(!!record);
|
||||||
|
profile && args.push(new Date() - start);
|
||||||
|
callback.apply(null, args);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function get(isFn) {
|
function get(isFn) {
|
||||||
return function(text, callback) {
|
return function(text, callback) {
|
||||||
var words = prepText(text),
|
var profile = this.options.profile,
|
||||||
|
start = profile && new Date(),
|
||||||
|
words = prepText(text),
|
||||||
n = words.length,
|
n = words.length,
|
||||||
i = 0,
|
i = 0,
|
||||||
self = this,
|
self = this,
|
||||||
results = [];
|
results = [],
|
||||||
|
args = [results];
|
||||||
if (!n) return callback(results);
|
profile && args.push(0);
|
||||||
|
if (!n) return callback.apply(null, args);
|
||||||
words.forEach(function(word,j){
|
words.forEach(function(word,j){
|
||||||
self[isFn](word, function(yes){
|
self[isFn](word, function(yes){
|
||||||
yes && results.push(word);
|
yes && results.push(word);
|
||||||
(++i==n) && callback(results);
|
if (++i==n) {
|
||||||
});
|
profile && (args[1] = new Date() - start);
|
||||||
|
callback.apply(null, args);
|
||||||
|
}
|
||||||
|
}, /*_noprofile*/ true);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
var WordPOS = function() {
|
* @class WordPOS
|
||||||
if (arguments.length == 0) {
|
* @constructor
|
||||||
|
*/
|
||||||
|
var WordPOS = function(options) {
|
||||||
|
if (arguments.length == 0 || _.isObject(options)) {
|
||||||
WordPOS.super_.call(this, WNdb.path);
|
WordPOS.super_.call(this, WNdb.path);
|
||||||
} else {
|
} else {
|
||||||
WordPOS.super_.apply(this, arguments);
|
WordPOS.super_.apply(this, arguments);
|
||||||
}
|
}
|
||||||
|
this.options = _.defaults({}, _.isObject(options) && options || {}, WordPOS.defaults);
|
||||||
};
|
};
|
||||||
util.inherits(WordPOS, WordNet);
|
util.inherits(WordPOS, WordNet);
|
||||||
|
|
||||||
|
WordPOS.defaults = {
|
||||||
|
/**
|
||||||
|
* enable profiling, time in msec returned as second argument in callback
|
||||||
|
*/
|
||||||
|
profile: false
|
||||||
|
};
|
||||||
|
|
||||||
var wordposProto = WordPOS.prototype;
|
var wordposProto = WordPOS.prototype;
|
||||||
|
|
||||||
// fast POS lookups (only look in specified file)
|
// fast POS lookups (only look in specified file)
|
||||||
|
@ -142,6 +172,9 @@ if (!wordposProto.getIndexFile) {
|
||||||
*/
|
*/
|
||||||
wordposProto.getPOS = function(text, callback) {
|
wordposProto.getPOS = function(text, callback) {
|
||||||
var data = {nouns:[], verbs:[], adjectives:[], adverbs:[], rest:[]},
|
var data = {nouns:[], verbs:[], adjectives:[], adverbs:[], rest:[]},
|
||||||
|
profile = this.options.profile,
|
||||||
|
start = profile && new Date(),
|
||||||
|
args = [data],
|
||||||
testFns = 'isNoun isVerb isAdjective isAdverb'.split(' '),
|
testFns = 'isNoun isVerb isAdjective isAdverb'.split(' '),
|
||||||
parts = 'nouns verbs adjectives adverbs'.split(' '),
|
parts = 'nouns verbs adjectives adverbs'.split(' '),
|
||||||
words = prepText(text),
|
words = prepText(text),
|
||||||
|
@ -150,7 +183,8 @@ wordposProto.getPOS = function(text, callback) {
|
||||||
self = this,
|
self = this,
|
||||||
c = 0;
|
c = 0;
|
||||||
|
|
||||||
if (!nWords) return callback(data);
|
profile && args.push(0);
|
||||||
|
if (!nWords) return callback.apply(null, args);
|
||||||
words.forEach(lookup);
|
words.forEach(lookup);
|
||||||
|
|
||||||
function lookup(word){
|
function lookup(word){
|
||||||
|
@ -177,7 +211,8 @@ wordposProto.getPOS = function(text, callback) {
|
||||||
|
|
||||||
function done(){
|
function done(){
|
||||||
if (++c == nWords) {
|
if (++c == nWords) {
|
||||||
callback(data);
|
profile && (args[1] = new Date() - start);
|
||||||
|
callback.apply(null, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -175,3 +175,48 @@ describe('lookup POS', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('options passed to constructor', function() {
|
||||||
|
var wp, origProfile = WordPOS.defaults.profile;
|
||||||
|
|
||||||
|
it('should override default option', function(){
|
||||||
|
wp = new WordPOS({profile:123});
|
||||||
|
expect(wp.options.profile).toEqual(123);
|
||||||
|
expect(WordPOS.defaults.profile).toEqual(origProfile);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not erase default option', function(){
|
||||||
|
wp = new WordPOS({aaa:123});
|
||||||
|
expect(wp.options.aaa).toEqual(123);
|
||||||
|
expect(wp.options.profile).toEqual(WordPOS.defaults.profile);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('profile option', function() {
|
||||||
|
|
||||||
|
var wp = new WordPOS({profile : true});
|
||||||
|
|
||||||
|
it('should return time argument for isX()', function(){
|
||||||
|
wp.isNoun(garble, function(result, time) {
|
||||||
|
expect(time).toBeDefined();
|
||||||
|
asyncSpecDone();
|
||||||
|
});
|
||||||
|
asyncSpecWait();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return time argument for getX()', function(){
|
||||||
|
wp.getNouns(garble, function(result, time) {
|
||||||
|
expect(time).toBeDefined();
|
||||||
|
asyncSpecDone();
|
||||||
|
});
|
||||||
|
asyncSpecWait();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return time argument for lookupX()', function(){
|
||||||
|
wp.isNoun(garble, function(result, time) {
|
||||||
|
expect(time).toBeDefined();
|
||||||
|
asyncSpecDone();
|
||||||
|
});
|
||||||
|
asyncSpecWait();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue