markov-strings
Advanced tools
Comparing version 0.0.2 to 0.0.3
20
index.js
@@ -23,3 +23,3 @@ 'use strict'; | ||
this.options = this.defaultOptions; | ||
_.extend(this.options, options); | ||
_.assignIn(this.options, options); | ||
@@ -70,6 +70,7 @@ this.buildCorpus(); | ||
options = options ? options : {}; | ||
_.assignIn(options, this.options); | ||
_.assignIn(this.options, options); | ||
options = this.options; | ||
let corpus = _.cloneDeep(this.corpus); | ||
let max = 10000, iter = 0; | ||
const max = 10000; | ||
@@ -124,17 +125,4 @@ // Loop for maximum tries | ||
} | ||
_parseOption(options, name) { | ||
return options[name] ? options[name] : this.defaultOptions[name]; | ||
} | ||
parseOptions(options) { | ||
options.stateSize = this._parseOption(options, 'stateSize'); | ||
options.maxLength = this._parseOption(options, 'maxLength'); | ||
options.minWords = this._parseOption(options, 'minWords'); | ||
options.minScore = this._parseOption(options, 'minScore'); | ||
return options; | ||
} | ||
} | ||
module.exports = Generator; |
{ | ||
"name": "markov-strings", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A Markov string generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,11 +11,9 @@ [![Build Status](https://travis-ci.org/scambier/markov-strings.svg?branch=master)](https://travis-ci.org/scambier/markov-strings) | ||
##Getting started | ||
###Prerequisites | ||
##Prerequisites | ||
This module makes use of ES6 features. | ||
###Installing | ||
##Installing | ||
`npm install --save markov-strings` | ||
###Basic usage | ||
##Usage | ||
@@ -25,11 +23,48 @@ ```javascript | ||
let data = []; // An array of strings | ||
let options = {}; // An optional object of options | ||
let generator = new Markov(data, options); | ||
// An array of strings | ||
let data = [/* insert a few hundreds sentences here */]; | ||
let refinedOptions = {}; // An optional object of options, for this particular generation | ||
generator.generateSentence(refinedOptions); // Outputs an object, containing a string and a score | ||
// Some options to generate Twitter-ready strings | ||
let options = { | ||
maxLength: 140, | ||
minWords: 10, | ||
minScore: 25, | ||
}; | ||
// Instantiate the generator | ||
let markov = new Markov(data, options); | ||
// Generate some tweets | ||
let tweets = []; | ||
for (let i = 0; i < 10; i++) { | ||
tweets.push(markov.generateSentence()); | ||
} | ||
// Generate a shorter tweet to add a link | ||
let shorterTweet = markov.generateSentence({ | ||
maxLength: 140-24 | ||
}); | ||
shorterTweet += ' https://github.com/scambier/markov-strings'; // Links always take 23 characters in a tweet | ||
console.log(shorterTweet); | ||
/* | ||
Possible output: | ||
{ | ||
string: 'lorem ipsum dolor sit amet (etc.) https://github.com/scambier/markov-strings', | ||
score: 42 | ||
} | ||
*/ | ||
``` | ||
## API | ||
### Markov(data, [options]) | ||
Create a generator instance. | ||
#### data | ||
Type: `array` | ||
###Options | ||
`data` is an array of strings (sentences). The bigger the array, the better and more various the results. | ||
#### options | ||
Type: `object` | ||
You can provide options during the generator instantiation, and/or while calling `generateSentence()`. | ||
@@ -42,3 +77,3 @@ | ||
#####stateSize | ||
##### stateSize | ||
Type: `integer` | ||
@@ -52,3 +87,3 @@ Default: `2` | ||
#####maxLength | ||
##### maxLength | ||
Type: `integer` | ||
@@ -59,3 +94,3 @@ Default: `0` | ||
#####minWords | ||
##### minWords | ||
Type: `integer` | ||
@@ -66,3 +101,3 @@ Default: `5` | ||
#####maxWords | ||
##### maxWords | ||
Type: `integer` | ||
@@ -73,3 +108,3 @@ Default: `0` | ||
#####minScore | ||
##### minScore | ||
Type: `integer` | ||
@@ -82,3 +117,14 @@ Default: `0` | ||
#### markov.generateSentence([options]) | ||
Generate a random sentence. | ||
##### options | ||
Type: `object` | ||
If set, these options will take precedence over those set in the constructor. | ||
## Todo | ||
The generator should return a Promise, to not hang the thread. | ||
## Running the tests | ||
`npm test` |
@@ -14,3 +14,4 @@ 'use strict'; | ||
"Egestas bibendum eros nisi ut lacus", | ||
"Sony avait annoncé une rupture avec le Xperia X: il n'en est rien…" | ||
"fringilla dui avait annoncé une rupture avec le erat vel: il n'en est rien…", | ||
"Fusce tincidunt tempor, erat vel lacinia vel ex pharetra pretium lacinia imperdiet" | ||
]; | ||
@@ -43,3 +44,3 @@ | ||
it('should have the right length', function() { | ||
expect(generator.startWords).to.have.lengthOf(6); | ||
expect(generator.startWords).to.have.lengthOf(7); | ||
}); | ||
@@ -108,2 +109,38 @@ }); | ||
it('should reject all sentences', function() { | ||
let options = {minWords:5}; | ||
for (let i = 0; i < 10; i++) { | ||
expect(() => { | ||
generator.generateSentence(options, result => result.split(' ').length < 5) | ||
}).to.throw(Error); | ||
} | ||
}); | ||
it('should accept all sentences', function() { | ||
for (let i = 0; i < 10; i++) { | ||
expect(() => { | ||
generator.generateSentence({}, result => true) | ||
}).to.exist; | ||
} | ||
}); | ||
it('should reject if the options values are not met', function() { | ||
expect(() => { | ||
generator.generateSentence({maxLength:1, minWords:0, maxWords:0}) | ||
}).to.throw(Error); | ||
expect(() => { | ||
generator.generateSentence({minWords:100}) | ||
}).to.throw(Error); | ||
expect(() => { | ||
generator.generateSentence({minScore: 20}) | ||
}).to.throw(Error); | ||
expect(() => { | ||
generator.generateSentence({maxWords: 1, minWords:0}) | ||
}).to.throw(Error); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12847
218
123