Skip to content

Commit

Permalink
Merge branch 'release/0.1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
morungos committed Jan 11, 2015
2 parents d98d257 + facbbf8 commit de9b433
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -77,6 +77,20 @@ wordnet.validForms('axes#n', console.log);

Similar to `validForms(word, callback)` but returning a promise.

### querySense(query, callback)

Queries WordNet to find all the senses of a given word, optionally with a
part-of-speech.

```javascript
var wordnet = new WordNet()
wordnet.querySense('axes#n', console.log);
```

### querySenseAsync(query)

Similar to `querySense(query, callback)` but returning a promise.


Future work
-----------
Expand Down
37 changes: 37 additions & 0 deletions lib/wordnet.js
Expand Up @@ -126,6 +126,43 @@ WordNet = (function() {
});
};

WordNet.prototype.querySense = function(input, callback) {
var pos, word, wordnet, _ref;
wordnet = this;
_ref = input.split('#'), word = _ref[0], pos = _ref[1];
return wordnet.lookup(input, function(results) {
var i, sense, senseCounts, senses;
senseCounts = {};
senses = (function() {
var _i, _len, _results;
_results = [];
for (i = _i = 0, _len = results.length; _i < _len; i = ++_i) {
sense = results[i];
pos = sense.pos;
if (pos === 's') {
pos = 'a';
}
if (senseCounts[pos] == null) {
senseCounts[pos] = 1;
}
_results.push(word + "#" + pos + "#" + senseCounts[pos]++);
}
return _results;
})();
return callback(senses);
});
};

WordNet.prototype.querySenseAsync = function(input) {
var wordnet;
wordnet = this;
return new Promise(function(resolve, reject) {
return wordnet.querySense(input, function(data) {
return resolve(data);
});
});
};

WordNet.prototype.lookupFromFiles = function(files, results, word, callback) {
var file, wordnet;
wordnet = this;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "node-wordnet",
"version": "0.1.4",
"version": "0.1.5",
"description": "Node.js interface for Wordnet",
"main": "lib/wordnet.js",
"scripts": {
Expand Down
20 changes: 20 additions & 0 deletions src/wordnet.coffee
Expand Up @@ -112,6 +112,26 @@ class WordNet
wordnet.findSense input, (data) -> resolve(data)


querySense: (input, callback) ->
wordnet = @
[word, pos] = input.split('#')

wordnet.lookup input, (results) ->
senseCounts = {}
senses = for sense, i in results
pos = sense.pos
pos = 'a' if pos == 's'
senseCounts[pos] ?= 1
word + "#" + pos + "#" + senseCounts[pos]++

callback(senses)

querySenseAsync: (input) ->
wordnet = @
new Promise (resolve, reject) ->
wordnet.querySense input, (data) -> resolve(data)


lookupFromFiles: (files, results, word, callback) ->
wordnet = @

Expand Down
34 changes: 34 additions & 0 deletions test/wordnet_test.coffee
Expand Up @@ -78,6 +78,40 @@ describe 'wordnet', () ->
.notify(done)


describe 'querySense', () ->
it 'should succeed for node', (done) ->
wordnet.querySense 'node', (results) ->
should.exist(results)
results.should.be.an.instanceOf(Array)
results.should.have.length(8)
done()

it 'should succeed for ghostly#a', (done) ->
wordnet.querySense 'ghostly#a', (results) ->
should.exist(results)
results.should.be.an.instanceOf(Array)
results.should.have.length(1)
results.should.eql(['ghostly#a#1'])
done()


describe 'querySenseAsync', () ->
it 'should succeed for node', (done) ->
wordnet.querySenseAsync 'node'
.should.eventually.exist
.should.eventually.be.an.instanceOf(Array)
.should.eventually.have.length(8)
done()

it 'should succeed for ghostly#a', (done) ->
wordnet.querySenseAsync 'ghostly#a'
.should.eventually.exist
.should.eventually.be.an.instanceOf(Array)
.should.eventually.have.length(1)
.should.eventually.eql(['ghostly#a#1'])
done()


describe 'findSense', () ->

it 'should succeed for lie#v#1', (done) ->
Expand Down

0 comments on commit de9b433

Please sign in to comment.