keywords: Extract keywords from text

View source: R/keywords.R

keywordsR Documentation

Extract keywords from text

Description

Extract TF-IDF keywords from one or more in-memory strings with a keyword worker created by worker(). This is separate from textrank(), which uses TextRank weighting.

Usage

keywords(
  x,
  worker,
  ...,
  format = c("vector", "data.frame", "legacy"),
  batch = c("list", "flatten"),
  code = deprecated(),
  jiebar = deprecated()
)

Arguments

x

A non-empty character vector to analyze. Each string is one document.

worker

A jieba_worker object created with worker(type = "keywords").

...

Must be empty. This enforces that optional arguments such as format and batch are supplied with explicit names.

format

Output format for each document. "vector" returns a named numeric vector, "data.frame" returns a data frame with term and weight columns, and "legacy" returns the old jiebaR style character vector with weights in names(). Default is "vector".

batch

Aggregation mode for multi-string input. Must be one of "list" or "flatten". Default is "list".

code, jiebar

[Deprecated] Use x and worker instead.

Details

Each string is treated as one document, and up to topn keywords (see worker()) are extracted from each document independently. Multiple strings are processed in parallel.

format controls the shape of each single-document result:

  • "vector": a named numeric vector of weights, named by keyword.

  • "data.frame": a data frame with term and weight columns.

  • "legacy": the old jiebaR layout with keyword values and weights in names().

When x contains multiple strings, batch controls how the per-document results are aggregated:

  • "list": one single-document result per input string.

  • "flatten": concatenate all results into one. "vector"/"legacy" produce one named vector, while "data.frame" produces a combined data frame with a leading doc_id column.

A single input string always returns a single-document result, and batch is ignored after being validated.

Value

Keyword results in the requested format.

Examples

keys <- worker(type = "keywords", topn = 2)
text1 <- "\u5317\u4eac\u70e4\u9e2d\u771f\u597d\u5403\uff0c\u5317\u4eac\u70e4\u9e2d"
text2 <- "\u7ebd\u7ea6\u7684\u5929\u6c14\u771f\u597d\uff0c\u7ebd\u7ea6\u7684\u5929\u6c14"
keywords(text1, keys)
keywords(c(text1, text2), keys)
keywords(c(text1, text2), keys, format = "data.frame", batch = "flatten")

jiebaRS documentation built on Dec. 3, 2026, 5:09 p.m.