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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
documentsOrWords | Any | Yes | — | Tokenized documents, word vector, or unique vocabulary. |
uniqueWords | Any | Yes | — | Unique words. |
counts | Any | Yes | — | Word counts per document. |
Returns
| Name | Type | Description |
|---|---|---|
bag | Any | Bag-of-words model object. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:textAnalyticsDocuments:InvalidInput | Inputs do not match a supported Text Analytics document or model helper form. | Text Analytics document helper received invalid input |
How bagOfWords works
bagOfWordswith no input creates an empty model.bagOfWords(documents)accepts RunMattokenizedDocumentobjects created bytokenizedDocument.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.uniqueWordsmust not contain duplicate nonmissing words. Missing string entries are ignored along with their corresponding count columns.- The returned object stores
Vocabulary,Counts,NumWords, andNumDocumentsproperties. - 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
uniqueWordsinput.
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.
Related Strings functions
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · cosineSimilarity · doc2sequence · encode · extractFileText · extractHTMLText · fastTextWordEmbedding · findElement · getAttribute · htmlTree · ind2word · isVocabularyWord · normalizeWords · readWordEmbedding · removeLongWords · removeShortWords · removeStopWords · removeWords · stopWords · tokenDetails · tokenizedDocument · trainWordEmbedding · vaderSentimentScores · vec2word · word2ind · word2vec · wordEncoding · writeWordEmbedding
Transform
append · deblank · erase · eraseBetween · erasePunctuation · eraseURLs · extractAfter · extractBefore · extractBetween · insertAfter · insertBefore · join · lower · pad · replace · replaceBetween · reverse · split · splitlines · strcat · strip · strjoin · strjust · strrep · strsplit · strtrim · upper
Core
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · genvarname · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp · strncmpi · strtok · unicode2native
Search
contains · endsWith · matches · startsWith · strfind
Pattern
digitsPattern · lettersPattern · pattern · regexpPattern · textBoundary · wildcardPattern
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how bagOfWords is executed, line by line, in Rust.
- View the source for bagOfWords in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.