• Stars
    star
    322
  • Rank 130,398 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

plugin to extract keywords and key-phrases

retext-keywords

Build Coverage Downloads Size Sponsors Backers Chat

retext plugin to extract keywords and key phrases.

Contents

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.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install retext-keywords

In Deno with esm.sh:

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

In browsers with esm.sh:

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

Use

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 contains:

import {toString} from 'nlcst-to-string'
import {retext} from 'retext'
import retextKeywords from 'retext-keywords'
import retextPos from 'retext-pos'
import {read} from 'to-vfile'

const file = await retext()
  .use(retextPos) // Make sure to use `retext-pos` before `retext-keywords`.
  .use(retextKeywords)
  .process(await read('example.txt'))

console.log('Keywords:')

if (file.data.keywords) {
  for (const keyword of file.data.keywords) {
    console.log(toString(keyword.matches[0].node))
  }
}

console.log()
console.log('Key-phrases:')

if (file.data.keyphrases) {
  for (const phrase of file.data.keyphrases) {
    console.log(toString(phrase.matches[0].nodes))
  }
}

…then running node example.js yields:

Keywords:
term
extraction
Terminology
web
domain

Key-phrases:
terminology extraction
terms
term extraction
knowledge domain
communities

API

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.keyphrases (Array<Keyphrase>) and file.data.keywords (Array<Keyword>).

Parameters
  • options (Options, optional) β€” configuration
Returns

Transform (Transformer).

Keyphrase

Info on a key phrase (TypeScript type).

Fields
  • matches (Array<PhraseMatch>) β€” all matches
  • score (number) β€” score of phrase, for one match
  • stems (Array<string>) β€” stems of phrase
  • weight (number) β€” score of phrase, for all matches

Keyword

Info on a keyword (TypeScript type).

Fields
  • matches (Array<WordMatch>) β€” all matches
  • score (number) β€” score of word, for all matches
  • stem (string) β€” stems of word

Options

Configuration (TypeScript type).

Fields
  • maximum (number, default: 5) β€” try to detect at most maximum words and phrases; actual counts may differ, for example, when two words have the same score, both will be returned; when too few words exist, less will be returned

PhraseMatch

Match (TypeScript type).

Fields

WordMatch

Match (TypeScript type).

Fields
  • node (Node) β€” matched node
  • index (number) β€” index of node in parent
  • parent (Node) β€” parent

Types

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

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 this is `Array<Keyword> | undefined`.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, retext-keywords@^8, compatible with Node.js 16.

Contribute

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.

License

MIT Β© Titus Wormer

More Repositories

1

retext

natural language processor powered by plugins part of the @unifiedjs collective
JavaScript
2,334
star
2

retext-equality

plugin to check for possible insensitive, inconsiderate language
JavaScript
156
star
3

awesome-retext

Curated list of awesome retext resources
108
star
4

retext-sentiment

plugin to detect sentiment
JavaScript
97
star
5

retext-simplify

plugin to check phrases for simpler alternatives
JavaScript
91
star
6

retext-readability

plugin to check readability
JavaScript
89
star
7

retext-spell

plugin to check spelling
JavaScript
68
star
8

retext-smartypants

plugin to implement SmartyPants
JavaScript
55
star
9

retext-profanities

plugin to check for profane and vulgar wording
JavaScript
44
star
10

retext-emoji

plugin to support emoji, gemoji, and emoticons
JavaScript
34
star
11

retext-pos

plugin to add part-of-speech (POS) tags
JavaScript
21
star
12

retext-indefinite-article

plugin to check if indefinite articles (a and an) are used correctly
JavaScript
20
star
13

retext-intensify

plugin to check for weak and mitigating wording
JavaScript
17
star
14

retext-passive

plugin to check for passive voice
JavaScript
15
star
15

retext-repeated-words

plugin to check for for repeated words
JavaScript
13
star
16

retext-syntax-mentions

plugin to classify @mentions as syntax, not natural language
JavaScript
10
star
17

retext-sentence-spacing

plugin to check spacing between sentences
JavaScript
10
star
18

retext-quotes

plugin to check quotes and apostrophes
JavaScript
10
star
19

retext-syntax-urls

plugin to classify url-like values as syntax, not natural language
JavaScript
9
star
20

retext-contractions

plugin to check apostrophes in elided contractions
JavaScript
8
star
21

retext-diacritics

plugin to check for proper use of diacritics
JavaScript
6
star
22

retext-redundant-acronyms

plugin to check for redundant acronyms (such as ATM machine to ATM)
JavaScript
5
star
23

ideas

Share ideas for new utilities and tools built with @retextjs
3
star
24

.github

Community health files for retext
2
star