UNPKG

nounfinder

Version:

Extracts nouns from chunks of text, using the [Wordnik API](http://developer.wordnik.com/docs.html).

274 lines (263 loc) β€’ 5.59 kB
var assert = require('assert'); var createNounfinder = require('../nounfinder'); var queue = require('queue-async'); var config = require('../config'); var nounfinder = createNounfinder({ wordnikAPIKey: config.wordnikAPIKey }); var textsAndNouns = [ { text: 'Facebook running studies to see if it can manipulate users\' emotional states by skewing their feeds.', nouns: [ 'facebook', 'it', 'running', 'state', 'feed' ], interestingNouns: [] }, { text: 'During this work, the street will be closed to motorists, except for abutters.', nouns: [ 'this', 'work', 'street', 'will', 'motorist', 'abutter' ], interestingNouns: [ 'motorist', 'abutter' ] }, { text: 'Property owners will maintain access to their driveways.', nouns: [ 'will', 'property', 'owner', 'access', 'driveway' ], interestingNouns: [ 'driveway' ] }, { text: 'In addition to the street closure, there will be no parking on either side of the street.', nouns: [ 'addition', 'street', 'closure', 'will', 'parking', 'either', 'side' ], interestingNouns: [ 'closure' ] }, { text: 'If voting didn\'t matter Republicans wouldn\'t be trying so hard to stop people from doing it.', nouns: [ 'matter', 'republican', 'person', 'doing', 'it' ], interestingNouns: [ 'republican' ] }, { text: 'Hello, dystopia. You\'re a little early, but I\'ve been expecting you.', nouns: [ 'dystopium', 'you' ], interestingNouns: [ ] }, { text: 'So @seventeenmag\'s app has shared my stripes vs. polkadots ' + '@polarpolls poll. http://polarb.com/65517 They know I\'m a dude in my forties, right?', nouns: [ 'app', 'stripe', // 'v', 'poll', 'dude', // 'forties' // Should leave out v and forties., 'they' ], interestingNouns: [ 'stripe', 'dude', // '40s' // should leave out 40s ] }, { text: 'So @seventeenmag\'s app has shared my stripes vs. polkadots ' + '@polarpolls poll. http://polarb.com/65517 They know I\'m a dude in my 40s, right?', nouns: [ 'app', 'stripe', // 'v', 'poll', 'dude', // '40s' // Should leave out v and 40., 'they' ], interestingNouns: [ 'stripe', 'dude', // '40s' // should leave out 40s ] }, { text: 'PLS don\'t use pls in an exhortation', nouns: [ 'exhortation' ], interestingNouns: [ 'exhortation' ] }, { text: 'w/e. tl;dr capitalism ruins everything. i’m going to bed.', nouns: [ 'capitalism', 'ruin', 'everything', 'going', 'bed' ], interestingNouns: [ 'capitalism', 'ruin' ] }, { text: 'Back when some stuff is done. 😊 πŸΈπŸ“ˆπŸΈ', nouns: [ 'back', 'stuff', '😊', '🐸', 'πŸ“ˆ' ], interestingNouns: [ '🐸', 'πŸ“ˆ' ] }, { text: 'Read a really cool AMA with the Zodiac Killer!', nouns: [ 'zodiac', 'killer' ], interestingNouns: [ 'zodiac' ] }, { text: 'Alas. I seem to have missed the boat.', nouns: [ 'boat' ], interestingNouns: [ ] } ]; var expectedNounCache = { facebook: 151, running: 1102, study: 880, user: 373, state: 2719, work: 4804, street: 569, will: 18228, motorist: 5, abutter: 0, property: 649, owner: 387, access: 936, driveway: 34, addition: 532, closure: 56, no: 9127, parking: 201, side: 1657, voting: 377, matter: 1382, republican: 51, person: 1675, doing: 2149, hello: 87, app: 163, stripe: 8, poll: 387, dude: 76, 'don\'t': 5157, exhortation: 1, capitalism: 88, ruin: 65, going: 6355, bed: 519, back: 6613, stuff: 1107, zodiac: 1, killer: 150, boat: 231 }; suite('Noun getting', function gettingSuite() { this.timeout(30000); function testTextAndNouns(text, expectedNouns, done) { nounfinder.getNounsFromText(text, function checkNouns(error, nouns) { assert.ok(!error); assert.deepEqual(nouns.sort(), expectedNouns.sort()); done(error, nouns) } ); } test('Test getting nouns from text.', function testGetNouns(testDone) { var q = queue(1); textsAndNouns.forEach(function addNounsTest(textAndNouns) { q.defer(testTextAndNouns, textAndNouns.text, textAndNouns.nouns); }); q.awaitAll(testDone); }); }); suite('Noun frequencies', function frequenciesSuite() { this.timeout(30000); function testInterestingFilter(nouns, expectedNouns, done) { nounfinder.filterNounsForInterestingness(nouns, 100, function checkNouns(error, filteredNouns) { assert.ok(!error); assert.deepEqual(filteredNouns.sort(), expectedNouns.sort()); done(error, filteredNouns) } ); } test('Test filtering nouns', function testFilteringSetsOfNouns(testDone) { function addFilterTest(textAndNouns) { q.defer(testInterestingFilter, textAndNouns.nouns, textAndNouns.interestingNouns ); } var q = queue(1); textsAndNouns.forEach(addFilterTest); q.awaitAll(testDone); }); });