markov-strings
Advanced tools
Comparing version 1.3.5 to 1.4.0
36
index.js
'use strict'; | ||
const _ = require('lodash'), | ||
debug = require('debug')('markov-strings'); | ||
debug = require('debug'), | ||
warn = debug('markov-strings:warning'); | ||
@@ -13,2 +14,4 @@ class Generator { | ||
constructor(data, options = {}) { | ||
this._checkOptions(options, 'constructor'); | ||
this.data = data; | ||
@@ -35,2 +38,14 @@ | ||
_checkOptions(options, methodName) { | ||
if (options && typeof options.checker !== 'undefined') { | ||
warn( | ||
`You've passed an 'options' object with 'checker' ` + | ||
`property set to 'Generator.${methodName}'. ` + | ||
`'checker(sentence)' property is deprecated and will be removed ` + | ||
`in future versions of the library. ` + | ||
`Please use 'filter(result)' property instead.` | ||
); | ||
} | ||
} | ||
formatData(data) { | ||
@@ -112,2 +127,4 @@ if (_.isString(data[0])) { | ||
generateSentence(options) { | ||
this._checkOptions(options, 'generateSentence'); | ||
return new Promise((resolve, reject) => { | ||
@@ -127,2 +144,5 @@ try { | ||
} | ||
this._checkOptions(options, 'generateSentenceSync'); | ||
const newOptions = {}; | ||
@@ -168,2 +188,8 @@ _.assignIn(newOptions, this.options, options); | ||
const sentence = _.map(arr, 'words').join(' ').trim(); | ||
const result = { | ||
string: sentence, | ||
score: score, | ||
scorePerWord: scorePerWord, | ||
refs: _.uniqBy(_.flatten(_.map(arr, 'refs')), 'string') | ||
}; | ||
@@ -173,2 +199,3 @@ // Sentence is not ended or incorrect | ||
typeof options.checker === 'function' && !options.checker(sentence) || // checker cb returns false | ||
typeof options.filter === 'function' && !options.filter(result) || | ||
options.minWords > 0 && sentence.split(' ').length < options.minWords || | ||
@@ -183,8 +210,3 @@ options.maxWords > 0 && sentence.split(' ').length > options.maxWords || | ||
return { | ||
string: sentence, | ||
score: score, | ||
scorePerWord: scorePerWord, | ||
refs: _.uniqBy(_.flatten(_.map(arr, 'refs')), 'string') | ||
}; | ||
return result; | ||
} | ||
@@ -191,0 +213,0 @@ throw new Error('Cannot build sentence with current corpus and options'); |
{ | ||
"name": "markov-strings", | ||
"version": "1.3.5", | ||
"version": "1.4.0", | ||
"description": "A Markov string generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,2 +29,3 @@ [![Build Status](https://travis-ci.org/scambier/markov-strings.svg?branch=master)](https://travis-ci.org/scambier/markov-strings) | ||
- [checker(sentence)](#checkersentence) | ||
- [filter(result)](#filterresult) | ||
- [markov.buildCorpus()](#markovbuildcorpus) | ||
@@ -80,4 +81,4 @@ - [markov.generateSentence([options])](#markovgeneratesentenceoptions) | ||
minScore: 25, | ||
checker: sentence => { | ||
return sentence.endsWith('.'); // I want my tweets to end with a dot. | ||
filter: result => { | ||
return result.string.endsWith('.'); // I want my tweets to end with a dot. | ||
} | ||
@@ -133,3 +134,3 @@ }; | ||
Examples: | ||
Examples: | ||
`[ 'lorem ipsum', 'dolor sit amet' ]` or `[ { string: 'lorem ipsum', attr: 'value' }, { string: 'dolor sit amet', attr: 'other value' } ]` | ||
@@ -153,5 +154,5 @@ | ||
The number of words for each state. | ||
`1` will output gibberish sentences without much sense. | ||
`2` is a sensible default. | ||
The number of words for each state. | ||
`1` will output gibberish sentences without much sense. | ||
`2` is a sensible default. | ||
`3` and more could create good sentences, at the expense of randomness. You'll need a good corpus, though. | ||
@@ -199,8 +200,15 @@ | ||
##### checker(sentence) | ||
Type: `function` | ||
Type: `function` | ||
**Deprecated**, please use `filter(result)` instead. | ||
In addition to all previous options, you can define your own checking function that will be called once the sentence is generated. | ||
If this callback returns `false`, the sentence is rejected and a new one is generated. | ||
##### filter(result) | ||
Type: `function` | ||
You can use this option to filters out possible results. A possible result is an object of type `{string, score, scorePerWord, refs}`. If the function returns `false`, the result in question is rejected and a new one is generated. | ||
### markov.buildCorpus() | ||
@@ -210,3 +218,3 @@ Return a Promise that will resolve to nothing. | ||
This function **must** be called to build the corpus for Markov generation. | ||
This function **must** be called to build the corpus for Markov generation. | ||
It will iterate over all words for all strings from your `data` parameter, so it can take some time depending on its size. | ||
@@ -227,14 +235,20 @@ | ||
### 1.3.3 | ||
**1.4.0** | ||
- New `filter()` method, thanks @flpvsk | ||
**1.3.4 - 1.3.5** | ||
- Dependencies update | ||
**1.3.3** | ||
- Updated README. Version bump for npm | ||
### 1.3.2 | ||
**1.3.2** | ||
- Fixed an infinite loop bug | ||
- Performance improvement | ||
### 1.3.1 | ||
**1.3.1** | ||
- Updated README example | ||
- Removed a useless line | ||
### 1.3.0 | ||
**1.3.0** | ||
- New feature: the generator now accepts arrays of objects, and tells the user which objects were used to build a sentence | ||
@@ -241,0 +255,0 @@ - Fixed all unit tests |
@@ -113,13 +113,63 @@ 'use strict'; | ||
it('should reject the sentence', function (done) { | ||
it(`should invoke 'filter'`, function (done) { | ||
let wasFilterCalled = false; | ||
const options = { | ||
filter: () => { | ||
wasFilterCalled = true; | ||
return true; | ||
} | ||
}; | ||
generator.generateSentence(options) | ||
.then(() => { | ||
expect(wasFilterCalled).to.equal(true); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
it(`should pass the result object to 'filter(result)'`, function (done) { | ||
const options = { | ||
minWords: 5, | ||
maxTries: 10, | ||
checker: result => result.string.split(' ').length < 5 | ||
filter: (result) => { | ||
expect(result).to.have.property('string'); | ||
expect(result).to.have.property('score'); | ||
expect(result).to.have.property('scorePerWord'); | ||
expect(result).to.have.property('refs'); | ||
expect(result.refs).to.be.an('array'); | ||
return true; | ||
} | ||
}; | ||
generator.generateSentence(options) | ||
.then(() => done()) | ||
.catch(done); | ||
}); | ||
it(`should reject the sentence based on 'filter'`, function (done) { | ||
const options = { | ||
minWords: 5, | ||
maxTries: 10, | ||
filter: result => result.string.split(' ').length < 5 | ||
}; | ||
generator.generateSentence(options) | ||
.then(result => { | ||
done(new Error( | ||
`Sentence was generated when no result was expected. ` + | ||
`Sentence: ${result.string}` | ||
)); | ||
}) | ||
.catch(e => { | ||
expect(e).to.be.an('error'); | ||
expect(e.message).to.equal( | ||
'Cannot build sentence with current corpus and options' | ||
); | ||
// Test passed | ||
done(); | ||
}) | ||
// Assertions in previous "catch" block failed - test failed | ||
.catch(done); | ||
}); | ||
@@ -132,3 +182,4 @@ | ||
done(); | ||
}); | ||
}) | ||
.catch(done); | ||
}); | ||
@@ -135,0 +186,0 @@ |
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
23337
390
253
7