Generate readability statistics with localized options.
npm install localized-readabilitybash
npm install --save localized-readability
`
In the browser:
`html
`
$3
Before including the module itself:
`html
`
To get an idea of what it does, see the interactive demo and it's source for a sample implementation in the browser.
Usage
The module exports a Parser and a Highlighter. To generate statistics, pass a plain text string, an instance of Hypher, and applicable hyphenation patterns to Parser.setup(). Further, pass the results to Parser.count() to get descriptive statistics, the result of that to Parser.statistics() as well as a language-string to get readability statistics. The result of that to Parser.interpretations() as well as annotations to get interpreted readability statistics, and finally the result of that to Parser.consensus() to get an aggregated score on age and grade.
The language-string corresponds to the patterns defined by the Fluid
Project, specifically the name of the file
without the extension. Annotations follow the same pattern, a simple string of text representing the language-file.
For example:
`js
const Parser = require("localized-readability").parser;
const message = {};
message.setup = Parser.setup(data.input, Hypher, HyphenationPatterns);
message.count = Parser.count(message.setup);
message.statistics = Parser.statistics(message.count, data.lang);
message.interpretations = Parser.interpretations(
message.statistics,
Annotations
);
message.consensus = Parser.consensus(message.interpretations);
console.log(message);
`
$3
The Highlighter takes a Natural Language Concrete Syntax Tree, given by Parser.setup() as the nlcst-property, through the Highlighter.highlight()-function and formats it with optional paragraphs, highlighted sentences, and highlighted words. The second parameter is an object of options, wherein words: true also requires an instance of Hypher and hyphenation patterns, as shown below:
`js
const Highlighter = require("localized-readability").highlighter;
const nlcst = message.setup.nlcst;
const highlight = Highlighter.highlight(nlcst, {
paragraphs: true,
sentences: true,
words: true,
Hypher: Hypher,
HyphenationPatterns: HyphenationPatterns,
});
console.log(highlight);
`
This returns a string of highlighted text, wherein the p-tag is used for paragraphs, and the mark-tag is used for sentences and words. The class sentence denotes sentences, and word words, as well as the class and a number between 0 and 4 — higher numbers indicating higher difficulty. For example, Hi!.
#### Performance
It is advised not to run the Highlighter synchronously in a browser, and to consider offsetting each type of highlighting if possible. The paragraph- and sentence-highlighting is much simpler in this regard, and can fairly safely be ran together on medium-length inputs. Word-highlighting is much more resource intensive, as each word has to have its syllables counted, and can take several seconds even on short-length inputs.
In the /test/browser folder there are an html-files which demonstrate running the Parser and Highlighter asynchronously, for various languages. The interactive demo does the same, but with added controls.
Development
Install dependencies:
npm install
Build module:
npm run build`