2012-05-24 08:11:55 +00:00
|
|
|
/**
|
|
|
|
* wordpos_spec.js
|
|
|
|
*
|
|
|
|
* spec file for main wordpos functionality
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* npm install jasmine-node -g
|
|
|
|
* jasmine-node wordpos_spec.js --verbose
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 mooster@42at.com
|
|
|
|
* https://github.com/moos/wordpos
|
|
|
|
*
|
|
|
|
* Released under MIT license
|
2012-05-04 19:23:28 +00:00
|
|
|
*/
|
2012-05-24 08:11:55 +00:00
|
|
|
var WordPOS = require('../src/wordpos'),
|
2012-05-06 09:44:21 +00:00
|
|
|
wordpos = new WordPOS();
|
2012-05-02 23:18:10 +00:00
|
|
|
|
|
|
|
var str = "The angry bear chased the frightened little squirrel",
|
2012-05-04 19:23:28 +00:00
|
|
|
expected = {
|
|
|
|
nouns: [ 'bear', 'squirrel', 'little', 'chased' ],
|
|
|
|
verbs: [ 'bear' ],
|
|
|
|
adjectives: [ 'little', 'angry', 'frightened' ],
|
|
|
|
adverbs: [ 'little' ],
|
|
|
|
rest: [ 'the' ]
|
|
|
|
},
|
|
|
|
garble = 'garblegarble'; // expect not to find word
|
2012-05-02 23:18:10 +00:00
|
|
|
|
|
|
|
|
2012-05-24 08:11:55 +00:00
|
|
|
describe('getX()...', function() {
|
2012-05-02 23:18:10 +00:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2012-05-04 19:23:28 +00:00
|
|
|
this.addMatchers({
|
|
|
|
// unordered (multiset) comparison -- NOTE: doesn't handle deep!
|
|
|
|
toEqualUnordered: function(expected) {
|
|
|
|
var mismatchKeys=[],
|
|
|
|
mismatchValues=[],
|
|
|
|
result = this.env.compareObjects_(this.actual, expected, mismatchKeys, mismatchValues);
|
|
|
|
return result || (mismatchKeys.length == 0 && mismatchValues.length > 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should get all POS', function(done) {
|
2012-05-02 23:18:10 +00:00
|
|
|
wordpos.getPOS(str, function(result) {
|
|
|
|
expect(result.nouns).toEqualUnordered(expected.nouns);
|
|
|
|
expect(result.verbs).toEqualUnordered(expected.verbs);
|
|
|
|
expect(result.adjectives).toEqualUnordered(expected.adjectives);
|
|
|
|
expect(result.adverbs).toEqualUnordered(expected.adverbs);
|
2012-05-04 19:23:28 +00:00
|
|
|
expect(result.rest).toEqualUnordered(expected.rest);
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-04 19:23:28 +00:00
|
|
|
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should get nouns', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.getNouns(str, function(result) {
|
|
|
|
expect(result).toEqualUnordered(expected.nouns);
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
2012-05-04 19:23:28 +00:00
|
|
|
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should get verbs', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.getVerbs(str, function(result) {
|
|
|
|
expect(result).toEqualUnordered(expected.verbs);
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
2012-05-04 19:23:28 +00:00
|
|
|
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should get adjectives', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.getAdjectives(str, function(result) {
|
|
|
|
expect(result).toEqualUnordered(expected.adjectives);
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
2012-05-04 19:23:28 +00:00
|
|
|
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should get adverbs', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.getAdverbs(str, function(result) {
|
|
|
|
expect(result).toEqualUnordered(expected.adverbs);
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-05-24 08:11:55 +00:00
|
|
|
describe('isX()...', function() {
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should check if noun', function(done) {
|
2012-05-02 23:18:10 +00:00
|
|
|
wordpos.isNoun(expected.nouns[0], function(result) {
|
|
|
|
expect(result).toBeTruthy();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should check if verb', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.isVerb(expected.verbs[0], function(result) {
|
|
|
|
expect(result).toBeTruthy();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should check if adjective', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.isAdjective(expected.adjectives[0], function(result) {
|
|
|
|
expect(result).toBeTruthy();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should check if adverb', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.isAdverb(expected.adverbs[0], function(result) {
|
|
|
|
expect(result).toBeTruthy();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-05-24 08:11:55 +00:00
|
|
|
describe('!isX()...', function() {
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should check if !noun', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.isNoun(garble, function(result) {
|
|
|
|
expect(result).not.toBeTruthy();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should check if !verb', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.isVerb(garble, function(result) {
|
|
|
|
expect(result).not.toBeTruthy();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should check if !adjective', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.isAdjective(garble, function(result) {
|
|
|
|
expect(result).not.toBeTruthy();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should check if !adverb', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.isAdverb(garble, function(result) {
|
|
|
|
expect(result).not.toBeTruthy();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
|
|
|
|
2012-05-24 08:11:55 +00:00
|
|
|
describe('lookupX()...', function() {
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should lookup noun', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.lookupNoun('squirrel', function(result) {
|
|
|
|
expect(result[0].pos).toBe('n');
|
|
|
|
expect(result[0].lemma).toBe('squirrel');
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should lookup verb', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.lookupVerb('bear', function(result) {
|
|
|
|
expect(result[0].pos).toBe('v');
|
|
|
|
expect(result[0].lemma).toBe('have_a_bun_in_the_oven');
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should lookup adjective', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.lookupAdjective('angry', function(result) {
|
|
|
|
expect(result[0].pos).toBe('s');
|
|
|
|
expect(result[0].lemma).toBe('angry');
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should lookup adverb', function(done) {
|
2012-05-04 19:23:28 +00:00
|
|
|
wordpos.lookupAdverb('little', function(result) {
|
|
|
|
expect(result[0].pos).toBe('r');
|
|
|
|
expect(result[0].lemma).toBe('little');
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-04 19:23:28 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-02 23:18:10 +00:00
|
|
|
});
|
|
|
|
|
2012-05-08 04:41:46 +00:00
|
|
|
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});
|
|
|
|
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should return time argument for isX()', function(done){
|
2012-05-24 08:11:55 +00:00
|
|
|
wp.isNoun(garble, function(result, word, time) {
|
|
|
|
expect(word).toEqual(garble);
|
2012-05-08 04:41:46 +00:00
|
|
|
expect(time).toBeDefined();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-08 04:41:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should return time argument for getX()', function(done){
|
2012-05-08 04:41:46 +00:00
|
|
|
wp.getNouns(garble, function(result, time) {
|
|
|
|
expect(time).toBeDefined();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-08 04:41:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-05-25 18:09:37 +00:00
|
|
|
it('should return time argument for lookupX()', function(done){
|
2012-05-08 04:41:46 +00:00
|
|
|
wp.isNoun(garble, function(result, time) {
|
|
|
|
expect(time).toBeDefined();
|
2012-05-25 18:09:37 +00:00
|
|
|
done();
|
2012-05-08 04:41:46 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-30 21:58:18 +00:00
|
|
|
|
|
|
|
it('should disable stopword filtering', function(){
|
|
|
|
var wp = new WordPOS({stopwords : false}),
|
|
|
|
strWithStopwords = 'about after all'; // 3 adjective stopwords
|
|
|
|
expect(wp.getAdjectives(strWithStopwords, noop)).toBe(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use custom stopwords', function(){
|
|
|
|
var wp = new WordPOS({stopwords : ['all']}),
|
|
|
|
strWithStopwords = 'about after all'; // 3 adjective stopwords
|
|
|
|
// 'all' should be filtered
|
|
|
|
expect(wp.getAdjectives(strWithStopwords, noop)).toBe(2);
|
|
|
|
});
|
|
|
|
|
2012-05-08 04:41:46 +00:00
|
|
|
});
|
|
|
|
|
2012-05-30 21:58:18 +00:00
|
|
|
|
|
|
|
function noop(){}
|
|
|
|
|