Nth Phonetic Mnemonic

    TypeScript icon, indicating that this package has built-in type declarations

    7.2.0 • Public • Published


    Build Coverage Downloads Size Sponsors Backers Chat

    retext plugin to extract keywords and key phrases.


    What is this?

    This package is a unified (retext) plugin to extract keywords and key phrases from a document, and exposes that metadata on the file.

    When should I use this?

    You can use this plugin any time you’re dealing with unified or retext already, and are interested in keywords and key phrases. Importantly, keywords extraction in NLP is a rather heavy and sometimes fragile process, so you might be better off manually providing a list of keywords.


    This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with npm:

    npm install retext-keywords

    In Deno with esm.sh:

    import retextKeywords from 'https://esm.sh/retext-keywords@7'

    In browsers with esm.sh:

    <script type="module">
      import retextKeywords from 'https://esm.sh/retext-keywords@7?bundle'


    Say our document example.txt contains (from Wikipedia):

    Terminology mining, term extraction, term recognition, or glossary extraction, is a subtask of information extraction. The goal of terminology extraction is to automatically extract relevant terms from a given corpus.
    In the semantic web era, a growing number of communities and networked enterprises started to access and interoperate through the internet. Modeling these communities and their information needs is important for several web applications, like topic-driven web crawlers, web services, recommender systems, etc. The development of terminology extraction is essential to the language industry.
    One of the first steps to model the knowledge domain of a virtual community is to collect a vocabulary of domain-relevant terms, constituting the linguistic surface manifestation of domain concepts. Several methods to automatically extract technical terms from domain-specific document warehouses have been described in the literature.
    Typically, approaches to automatic term extraction make use of linguistic processors (part of speech tagging, phrase chunking) to extract terminological candidates, i.e. syntactically plausible terminological noun phrases, NPs (e.g. compounds "credit card", adjective-NPs "local tourist information office", and prepositional-NPs "board of directors" - in English, the first two constructs are the most frequent). Terminological entries are then filtered from the candidate list using statistical and machine learning methods. Once filtered, because of their low ambiguity and high specificity, these terms are particularly useful for conceptualizing a knowledge domain or for supporting the creation of a domain ontology. Furthermore, terminology extraction is a very useful starting point for semantic similarity, knowledge management, human translation and machine translation, etc.

    …and our module example.js looks as follows:

    import {read} from 'to-vfile'
    import {toString} from 'nlcst-to-string'
    import {retext} from 'retext'
    import retextPos from 'retext-pos'
    import retextKeywords from 'retext-keywords'
    const file = retext()
      .use(retextPos) // Make sure to use `retext-pos` before `retext-keywords`.
      .process(await read('example.txt'))
    file.data.keywords.forEach((keyword) => {
    file.data.keyphrases.forEach((phrase) => {
      console.log(phrase.matches[0].nodes.map((d) => toString(d)).join(''))

    …now running node example.js yields:

    terminology extraction
    term extraction
    knowledge domain


    This package exports no identifiers. The default export is retextKeywords.

    unified().use(retextKeywords[, options])

    Extract keywords and key phrases.

    The results are stored on file.data.{keywords,keyphrases}. Both are lists.

    A single keyword looks as follows:

      stem: 'term',
      score: 1,
      matches: [
        {node: Node, index: 5, parent: Node},
        // …
      // …

    …and a key phrase:

      score: 1,
      weight: 11,
      stems: ['terminolog', 'extract'],
      value: 'terminolog extract',
      matches:  [
        {nodes: [Node, Node, Node], parent: Node},
        // …

    Configuration (optional).


    Try to detect at most maximum words and phrases (number, default: 5).

    Note that actual counts may differ. For example, when two words have the same score, both will be returned. Or when too few words exist, less will be returned. The same goes for phrases.


    This package is fully typed with TypeScript. It exports the additional types Options, Keyphrase, PhraseMatch, Keyword, KeywordMatch.

    It also registers the file.data fields with vfile. If you’re working with the file, make sure to import this plugin somewhere in your types, as that registers the new fields on the file.

     * @typedef {import('retext-keywords')}
    import {VFile} from 'vfile'
    const file = new VFile()
    console.log(file.data.keywords) //=> TS now knows the type of this.


    Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. Our projects sometimes work with older versions, but this is not guaranteed.


    See contributing.md in retextjs/.github for ways to get started. See support.md for ways to get help.

    This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.


    MIT © Titus Wormer


    npm i retext-keywords

    DownloadsWeekly Downloads






    Unpacked Size

    22.1 kB

    Total Files


    Last publish


    • wooorm