Skip to main content

Function: countWords()

countWords(text): number

Defined in: packages/agentos/src/cognition/library/tokens.ts:239

The words a text holds, as a person counts them: the segments Intl.Segmenter cuts at the word granularity and marks word-like (letters, numbers, ideographs), so don't and 1.2M are one word each and punctuation is none. Chinese and Japanese are cut by a dictionary, so two runtimes can differ by a word or two on such a text. Where the runtime has no Intl.Segmenter, or no Intl at all, the runs between white space.

Parameters​

text​

string

Returns​

number