From d16a506ff1a40bfd27bea348d610f441a4ef04d8 Mon Sep 17 00:00:00 2001 From: moos Date: Sun, 20 May 2012 10:19:55 -0700 Subject: [PATCH] added return value for getX(), expose parse() and WNdb object. --- README.md | 17 ++++++++++++++--- package.json | 2 +- wordpos.js | 39 ++++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 91077d4..1505f21 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Please note: all API are async since the underlying WordNet library is async. WordPOS is a subclass of natural's [WordNet class](https://github.com/NaturalNode/natural#wordnet) and inherits all its methods. -### getX() +### getX()... Get POS from text. @@ -76,6 +76,8 @@ than getPOS() which looks up the word in all index files. NB: [stopwords] (https://github.com/NaturalNode/natural/blob/master/lib/natural/util/stopwords.js) are stripped out from str before lookup. +All getX() functions return the number of parsed words that will be looked up (less duplicates and stopwords). + Example: ```js @@ -108,7 +110,7 @@ would be considered nouns. (see http://nltk.googlecode.com/svn/trunk/doc/book/c squirrel / NN -### isX() +### isX()... Determine if a word is a particular POS. @@ -138,7 +140,7 @@ wordpos.isAdverb('fishly', console.log); // false ``` -### lookupX() +### lookupX()... These calls are similar to natural's [lookup()](https://github.com/NaturalNode/natural#wordnet) call, except they can be faster if you already know the POS of the word. @@ -179,6 +181,15 @@ Or use WordNet's inherited method: wordpos.lookup('great', console.log); // ... ``` + +### Other methods + +``` +WordPOS.WNdp -- access to the WNdb object + +wordpos.parse(str) -- returns tokenized array of words, less duplicates and stopwords. This method is called on all getX() calls internally. +``` + ### Options ```js diff --git a/package.json b/package.json index 87515ab..1c46ccf 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "author": "Moos ", "keywords": ["natural", "language", "wordnet", "pos"], "description": "wordpos is a set of part-of-speech utilities for Node.js using natural's WordNet module.", - "version": "0.1.2", + "version": "0.1.3", "homepage": "https://github.com/moos/wordpos", "engines": { "node": ">=0.4.10" diff --git a/wordpos.js b/wordpos.js index f246f2c..c584334 100644 --- a/wordpos.js +++ b/wordpos.js @@ -68,18 +68,19 @@ function get(isFn) { i = 0, self = this, results = [], - args = [results]; - profile && args.push(0); - if (!n) return callback.apply(null, args); + args = [results], + done = function(){ + profile && (args[1] = new Date() - start); + callback.apply(null, args) + }; + if (!n) return (process.nextTick(done),0); words.forEach(function(word,j){ self[isFn](word, function(yes){ yes && results.push(word); - if (++i==n) { - profile && (args[1] = new Date() - start); - callback.apply(null, args); - } + (++i==n) && done(); }, /*_noprofile*/ true); }); + return n; }; } @@ -146,6 +147,8 @@ wordposProto.getAdverbs = get('isAdverb'); wordposProto.getNouns = get('isNoun'); wordposProto.getVerbs = get('isVerb'); +wordposProto.parse = prepText; + if (!wordposProto.getIndexFile) { wordposProto.getIndexFile = function getIndexFile(pos) { switch(pos) { @@ -181,16 +184,18 @@ wordposProto.getPOS = function(text, callback) { nTests = testFns.length, nWords = words.length, self = this, - c = 0; + c = 0, + done = function(){ + profile && (args[1] = new Date() - start); + callback.apply(null, args) + }; - profile && args.push(0); - if (!nWords) return callback.apply(null, args); + if (!nWords) return (process.nextTick(done),0); words.forEach(lookup); function lookup(word){ var any = false, t=0; - word = normalize(word); testFns.forEach(lookupPOS); function lookupPOS(isFn,i,list){ @@ -204,17 +209,13 @@ wordposProto.getPOS = function(text, callback) { function donePOS() { if (++t == nTests) { !any && data['rest'].push(word); - done(); + (++c == nWords) && done(); } } } - - function done(){ - if (++c == nWords) { - profile && (args[1] = new Date() - start); - callback.apply(null, args); - } - } + return nWords; }; +WordPOS.WNdb = WNdb; + module.exports = WordPOS;