What is retext?
Retext is a natural language processing (NLP) framework for analyzing and manipulating text. It provides a variety of plugins to perform tasks such as spell checking, sentiment analysis, and readability scoring.
What are retext's main functionalities?
Spell Checking
This feature allows you to check the spelling of text. The code sample demonstrates how to use the `retext-spell` plugin with an English dictionary to identify spelling errors in a given text.
const retext = require('retext');
const retextSpell = require('retext-spell');
const dictionary = require('dictionary-en');
const report = require('vfile-reporter');
retext()
.use(retextSpell, dictionary)
.process('speling errror', function (err, file) {
console.error(report(err || file));
});
Sentiment Analysis
This feature allows you to analyze the sentiment of text. The code sample demonstrates how to use the `retext-sentiment` plugin to determine the sentiment polarity and valence of a given text.
const retext = require('retext');
const retextSentiment = require('retext-sentiment');
const report = require('vfile-reporter');
retext()
.use(retextSentiment)
.process('I love programming!', function (err, file) {
console.error(report(err || file));
console.log(file.data); // { polarity: 3, valence: 1 }
});
Readability Scoring
This feature allows you to score the readability of text. The code sample demonstrates how to use the `retext-readability` plugin to determine the readability age of a given text.
const retext = require('retext');
const retextReadability = require('retext-readability');
const report = require('vfile-reporter');
retext()
.use(retextReadability, { age: 18 })
.process('The cat sat on the mat.', function (err, file) {
console.error(report(err || file));
console.log(file.data); // { readability: { age: 6 } }
});
Other packages similar to retext
natural
Natural is a general natural language processing (NLP) library for Node.js. It provides functionalities such as tokenization, stemming, classification, and phonetics. Compared to retext, Natural offers a broader range of NLP tools but may not be as specialized in text analysis and manipulation.
compromise
Compromise is a lightweight NLP library for Node.js that focuses on fast and easy text processing. It provides features like part-of-speech tagging, named entity recognition, and text normalization. While it offers some similar functionalities to retext, it is designed to be more user-friendly and performant for common NLP tasks.
retext is an extensible natural language processor with support for
multiple languages. retext provides a pluggable system for analysing
and manipulating natural language in JavaScript. It works on Node and
in the Browser.
Rather than being a do-all library for Natural Language Processing
(such as NLTK or OpenNLP), retext aims to be useful for
more practical use cases (such as checking for insensitive words
or decoding emoticons) instead of more academic
goals (research purposes).
retext is inherently modular—it uses plugins (similar to
remark for markdown) instead of providing everything out of
the box (such as Natural). This makes retext a viable
tool for use on the web.
Installation
npm:
npm install retext
retext is also available as an AMD, CommonJS, and globals module,
uncompressed and compressed.
Usage
The following example uses retext-emoji to show
emoji and retext-smartypants for smart
punctuation.
Require dependencies:
var retext = require('retext');
var emoji = require('retext-emoji');
var smartypants = require('retext-smartypants');
Create an instance using retext-emoji and -smartypants:
var processor = retext().use(smartypants).use(emoji, {
'convert' : 'encode'
});
Process a document:
var doc = processor.process([
'The three wise monkeys [. . .] sometimes called the three mystic',
'apes--are a pictorial maxim. Together they embody the proverbial',
'principle to ("see no evil, hear no evil, speak no evil"). The',
'three monkeys are Mizaru (:see_no_evil:), covering his eyes, who',
'sees no evil; Kikazaru (:hear_no_evil:), covering his ears, who',
'hears no evil; and Iwazaru (:speak_no_evil:), covering his mouth,',
'who speaks no evil.'
].join('\n'));
Yields (you need a browser which supports emoji to see them):
The three wise monkeys […] sometimes called the three mystic
apes—are a pictorial maxim. Together they embody the proverbial
principle to (“see no evil, hear no evil, speak no evil”). The
three monkeys are Mizaru (🙈), covering his eyes, who
sees no evil; Kikazaru (🙉), covering his ears, who
hears no evil; and Iwazaru (🙊), covering his mouth,
who speaks no evil.
API
retext.use(plugin[, options])
Change the way retext works by using a plugin.
Signatures
processor = retext.use(plugin, options?)
;processor = retext.use(plugins)
.
Parameters
-
plugin
(Function
) — A Plugin;
-
plugins
(Array.<Function>
) — A list of Plugins;
-
options
(Object?
) — Passed to the plugin. Specified by its
documentation.
Returns
Object
— an instance of Retext: The returned object functions just like
retext (it has the same methods), but caches the use
d plugins. This
provides the ability to chain use
calls to use multiple plugins, but
ensures the functioning of the retext module does not change for other
dependents.
retext.process(value[, done])
Parse a text document, apply plugins to it, and compile it into
something else.
Parameters
Returns
string?
: A document. Formatted in whatever plugins generate. The result is
null
if a plugin is asynchronous, in which case the callback done
should’ve
been passed (don’t worry: plugin creators make sure you know its async).
function done(err, file, doc)
Callback invoked when the output is generated with either an error, or the
processed document (represented as a virtual file and a string).
Parameters
err
(Error?
) — Reason of failure;file
(VFile?
) — Virtual file;doc
(string?
) — Generated document.
Plugin
function attacher(retext[, options])
A plugin is a function, which takes the Retext instance a user attached
the plugin on as a first parameter and optional configuration as a second
parameter.
A plugin can return a transformer
.
function transformer(node, file[, next])
A transformer changes the provided document (represented as a node and a
virtual file).
Transformers can be asynchronous, in which case next
must be invoked
(optionally with an error) when done.
List of Plugins
List of Utilities
The following projects are useful when working with the syntax tree,
NLCST:
In addition, see wooorm/unist
for other utilities which
work with retext nodes, but also with other nodes.
And finally, see wooorm/vfile
for a list of utilities which
work with virtual files.
List of Products
The following products use retext:
CLI
wooorm/alex
— Catch insensitive, inconsiderate writing.
Node
Related
License
MIT © Titus Wormer