chomsky-phrase
v1.0.1
Published
Generates memorable phrases using the Wordnik API. Phrases have sentence structure similar to Noam Chomsky's famous 'Colorless green ideas sleep furiously' and are not guaranteed to make sense
Downloads
14
Readme
Chomsky-Phrase
Generates memorable phrases using the Wordnik API getRandomWords method. Phrases have sentence structure similar to Noam Chomsky's famous 'Colorless green ideas sleep furiously' and are not guaranteed to make sense.
Can be installed globally as a CLI tool or locally as a project dependency. Has no third-party dependencies.
Brief Implementation Details
For each word in a phrase, chomsky-phrase sends a request to the Wordnik API getRandomWords method. A random word from the response is then selected. This implementation was chosen to let users obtain proper word tenses since the Wordnik API useCanonicalForm option seems to be broken.
Local
Installation
npm install chomsky-phrase
Default Usage
var chomsky = require('chomsky-phrase');
var phraseFactory = chomsky.PhraseFactory.createDefault();
phraseFactory.create((phrase) => {
console.log(phrase);
});
Custom Phrases
You can build a custom phrase factory to output a phrase of any number of words and with any structure.
WordnikConfig
Each WordFactory takes a WordnikConfig that defines the API options for getting a random word. At this time, the following url options are configurable:
- wordType
- minCorpusCount
- minLength
- maxLength
Example:
var chomsky = require('chomsky-phrase');
// Get Adjectives between 1 and 25 characters long with minCorpusCount = 10000
var adjectiveConfig = new chomsky.WordnikConfig('adjective', 10000, 1, 25);
See the Wordnik API getRandomWords documentation for more information
WordFactory
A WordFactory creates words. Each WordFactory takes a WordnikConfig and optional filters to be used on the getRandomWords API response. Filter functions are used with the Array.prototype.filter function. Below is an example filter function that keeps only words that end with 'ing'
function onlyIng(word) {
return word.endsWith('ing');
}
Filters used in the default PhraseFactory can be found here
Example:
var chomsky = require('chomsky-phrase');
var http = require('http');
var adjectiveConfig = new chomsky.WordnikConfig('adjective');
var adjectiveFactory = new chomsky.WordFactory(http, adjectiveConfig, [
chomsky.resultFilters.filterWithEndings([ 'y', 'ing' ]),
chomsky.resultFilters.filterContractions
]);
console.log(adjectiveFactory.create());
>>> 'fiery'
PhraseFactory
Inject a WordFactory array that will be used to create the phrase. Each WordFactory is used in order to form the phrase.
IE - The final phrase will be
[
WordFactoryArray[0].create(),
WordFactoryArray[1].create(),
...
WordFactoryArray[n].create()
]
See PhraseFactory.createDefault() to see how the default factory is created.
Global
Installing chomsky-phrase globally will allow you to create phrases with the default PhraseFactory from the CLI. It is not configurable at this time.
Installation
npm install chomsky-phrase -g
Usage
chomsky-phrase
>>> [ 3, 'sultry', 'clogs', 'persist', 'soundly' ]