Count syllables with retext.
npm:
npm install retext-syllable
retext-syllable is also available for bower, component, and duo, and as an AMD, CommonJS, and globals module, uncompressed and compressed.
var retext = require('retext');
var inspect = require('unist-util-inspect');
var syllable = require('retext-syllable');
retext().use(syllable).use(function () {
return function (cst) {
console.log(inspect(cst));
};
}).process('A yellow fresh banana.');
Yields:
RootNode[1] [data={"syllableCount":7}]
└─ ParagraphNode[1] [data={"syllableCount":7}]
└─ SentenceNode[8] [data={"syllableCount":7}]
├─ WordNode[1] [data={"syllableCount":1}]
│ └─ TextNode: 'A'
├─ WhiteSpaceNode: ' '
├─ WordNode[1] [data={"syllableCount":2}]
│ └─ TextNode: 'yellow'
├─ WhiteSpaceNode: ' '
├─ WordNode[1] [data={"syllableCount":1}]
│ └─ TextNode: 'fresh'
├─ WhiteSpaceNode: ' '
├─ WordNode[1] [data={"syllableCount":3}]
│ └─ TextNode: 'banana'
└─ PunctuationNode: '.'
None, retext-syllable automatically detects the syllables of each
WordNode
(using
wooorm/syllable), and stores the
count in node.data.syllableCount
.
All parents (such as sentences, paragraphs, root) also receive a
syllableCount
property (parent.data.syllableCount
).