keyphrase GitHub

Reference

Extract returns up to limit non-overlapping phrases, the count summary, and an error. The ExtractOptions argument is optional. Leave it out for DefaultExtractOptions. Adjusting the output shows how to pass one value.

kws, counts, err := extractor.Extract(text, "en", 10)

Keyword

Each result is a span of the caller's text. Text is text[StartByte:EndByte]. Offsets are zero-based, half-open UTF-8 byte indexes.

FieldMeaning
TextThe phrase, sliced from the input.
StartByteFirst byte of the span.
EndByteByte after the span.
ScoreRanking signal in [0, 1].

Score is relative to this document. The best phrase scores 1. It is not a probability, and scores are not comparable across documents.

Order

Results are ordered by score descending, then by earlier StartByte, then by Text. Spans never overlap. The returned text is always a slice of the input.

Limit

The default limit is 10. limit must be at least 1. There is no maximum: a limit greater than the number of phrases returns all of them. A limit below 1 returns ErrInvalidLimit.

Language

language is the code of a package the program imported: en, fr, de, es, it, or pt. A code whose package was not imported returns ErrUnsupportedLanguage.

Empty text

Empty or whitespace-only text is success and returns no phrases.

Counts

The count summary has these fields:

  • Words — words in the text. Punctuation is not counted. A hyphenated compound such as "e-mail" is one word.
  • Extracted — every candidate phrase.
  • Returned — how many phrases the call returned.
  • ExtractedMoreThanOneWord and ReturnedMoreThanOneWord — the same counts for phrases of two or more words, including connectors.
  • MoreThanOneWord — every extracted phrase of two or more words, highest score first. Its length equals ExtractedMoreThanOneWord.

The Try page always uses this summary. A 64 KiB clip applies there, and to text loaded from a URL. The library call itself does not reject longer text.

Extracted is larger than Returned even when the limit is high. A candidate that overlaps a returned phrase, or repeats its words, is skipped: "offshore wind farms" is not returned next to "Offshore wind".

Adjusting the output

Two rules leave out phrases that are rarely good keywords. Both are on in DefaultExtractOptions. Pass one ExtractOptions value to change them. Start from DefaultExtractOptions and change only these fields:

FieldDefaultMeaning
DropLoneNounstrueLeave out a single word that is not a name, such as "government". A capitalized name such as "Britain" is always kept.
LoneNounMinRarity0.6Keep a single common noun anyway when its rarity is at least this. Rarity runs from 0, an everyday word, to 1, a word never seen in the language's reference text. "government" is 0.32 and "electricity" 0.53, so both are left out. 0 drops every single common noun.
DropOneOffPhrasestrueLeave out a phrase of two or more words that occurs once and contains no name, such as "transmission network". A phrase that repeats, or contains a name, is always kept.
OneOffMinOthers10Apply the previous rule only when at least this many other candidates remain. In a short text every phrase occurs once, so it keeps them. Raise it to keep phrases seen once in longer texts; 0 always drops them.
options := extractor.DefaultExtractOptions
options.DropLoneNouns = false     // return "government", "electricity"
options.DropOneOffPhrases = false // return "transmission network", "steel prices"

kws, counts, err := extractor.Extract(text, "en", 20, options)

A LoneNounMinRarity outside 0 to 1, or a negative OneOffMinOthers, returns ErrInvalidExtractOptions.
Passing more than one ExtractOptions value returns ErrTooManyExtractOptions.

Try the extractor with your own text, and tweak these four settings to see how the results change.