bagOfWords — Create bag-of-words model objects.

bagOfWords(documents) creates a lightweight RunMat bagOfWords object from a tokenizedDocument compatibility object. bagOfWords(uniqueWords, counts) creates a model from an explicit vocabulary and count matrix.

Syntax

bag = bagOfWords
bag = bagOfWords(documents)
bag = bagOfWords(uniqueWords, counts)

Inputs

NameTypeRequiredDefaultDescription
documentsOrWordsAnyYesTokenized documents, word vector, or unique vocabulary.
uniqueWordsAnyYesUnique words.
countsAnyYesWord counts per document.

Returns

NameTypeDescription
bagAnyBag-of-words model object.

Errors

IdentifierWhenMessage
RunMat:textAnalyticsDocuments:InvalidInputInputs do not match a supported Text Analytics document or model helper form.Text Analytics document helper received invalid input

How bagOfWords works

  • bagOfWords with no input creates an empty model.
  • bagOfWords(documents) accepts RunMat tokenizedDocument objects created by tokenizedDocument.
  • bagOfWords(words) accepts a word vector as a single-document model.
  • bagOfWords(uniqueWords, counts) accepts a word vector and a numeric count matrix whose columns match the vocabulary length.
  • uniqueWords must not contain duplicate nonmissing words. Missing string entries are ignored along with their corresponding count columns.
  • The returned object stores Vocabulary, Counts, NumWords, and NumDocuments properties.
  • Counts are stored as a dense host tensor with one row per document and one column per vocabulary word. Very large dense count matrices are rejected with a controlled error until native sparse bag storage is implemented.
  • Native sparse bag storage and sparse-preserving GPU residency remain outside this slice.
  • Vocabulary order follows first token appearance for document input and the supplied order for explicit uniqueWords input.

GPU memory and residency

bagOfWords stores host text-model metadata and dense host counts in this slice.

Examples

Create A Bag From Documents

documents = tokenizedDocument(["alpha beta"; "beta gamma"]);
bag = bagOfWords(documents)

Expected output:

`bag.NumDocuments` is `2`, and `bag.Counts` has one column for each unique token.

Create A Bag From Counts

bag = bagOfWords(["alpha" "beta"], [2 1; 0 3])

Expected output:

`bag.Vocabulary` is `["alpha" "beta"]`, and `bag.Counts` is the supplied `2 x 2` count matrix.

Using bagOfWords with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how bagOfWords changes the result.

Run a small bagOfWords example, explain the result, then change one input and compare the output.

FAQ

Does bagOfWords use sparse matrices?

Not in this slice. The compatibility object stores dense host counts and rejects very large dense count matrices. Sparse storage and provider-resident sparse paths remain broader runtime/model work.

Can bagOfWords consume raw document strings directly?

No. Raw document arrays should first be converted with tokenizedDocument. A raw word vector is accepted as a single-document model.

Does bagOfWords execute on the GPU?

No. It builds a host text-model object and does not use a runmat-accelerate provider path.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how bagOfWords is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.