Count total and unique words in text
How word counters find total and unique words, which tokenization choices change the result, and when to clean styled or pasted text before counting.
A word count looks like a single objective number, but every counter makes choices first. It decides where one word ends and the next begins, and whether "The" and "the" are the same word. It also decides how to treat "don't" or "well-known". Those choices barely matter for a quick length check. They matter a great deal when you compare documents, measure vocabulary, or count text that has been reversed, restyled or pasted from somewhere else. This guide explains how total and unique word counts are produced, what can make them diverge between tools, and when to clean the text before counting.
Total words versus unique words
The total word count, often called the token count, counts every occurrence of every word. The unique word count, sometimes called the number of different words, the type count or the vocabulary size, counts each distinct word only once, however often it repeats.
Take the sentence "The cat saw the other cat." It has six words in total: The, cat, saw, the, other, cat. The unique count depends on case handling. If case matters, there are five distinct words, because "The" and "the" are counted separately. If case is ignored, there are four: the, cat, saw, other. The total does not change, but the unique count does. That is why unique counts are more sensitive to a tool's rules than totals are.
Is a word counter suitable for what you need?
For most everyday purposes, any reasonable counter is good enough. Examples include checking an essay against a limit, sizing a description field or getting a rough sense of length. Its main benefit is speed. Its decisive tradeoff is that the number is only meaningful under the counter's own rules, and those rules are often not stated.
Be more careful in these situations:
- You are comparing counts produced by different tools, because each may split words differently.
- You need a unique word count for vocabulary or style analysis, where case, punctuation and word-form decisions change the result.
- The text is in a language that does not separate words with spaces, such as Chinese, Japanese or Thai.
- The text contains styled Unicode letters, upside-down or mirrored characters, emoji, or invisible formatting characters.
- A specific rule set is required, such as a publisher's, a school's or a platform's own definition of a word.
Plain, normalized text is the better input whenever accuracy matters. If the text has been decorated with "fancy font" characters or reversed for visual effect, count the original plain version instead of the transformed one.
What counting actually does to the text
Counting is a read-only analysis. A counter should report numbers without altering the text you supplied. Most counters do, however, transform a working copy of the text internally before counting. Those internal steps are what make results differ.
Tokenization: deciding where words begin and end
The simplest approach splits on whitespace. That approach treats "cat." and "cat" as different words unless punctuation is stripped. Better counters strip surrounding punctuation and then apply rules for the harder cases:
- Contractions and possessives: "don't" may count as one word or as two. Straight and curly apostrophes may be handled differently.
- Hyphenated forms: "well-known" may be one word or two.
- Numbers: "2026" or "3.5" may or may not count as words.
- Email-like strings, URLs, hashtags and code identifiers may be split at punctuation or kept whole.
- Dashes without spaces, as in "word—word", may join or separate the two words.
Spaces are not reliable word boundaries in every language. The Unicode Standard describes default word-boundary rules in its text segmentation annex. Even so, languages written without spaces between words generally need dictionary- or model-based segmentation to give meaningful counts. A counter built only around spaces may treat a whole Chinese or Japanese sentence as one word.
Normalization: deciding which words are "the same"
Unique counts depend on how aggressively words are merged:
- Case folding treats "Cat", "cat" and "CAT" as one word. This is usually what readers expect, but it also merges a proper noun with a common word, such as "Bill" and "bill".
- Unicode normalization makes visually identical text compare as equal. For example, an accented letter can be stored as one precomposed character or as a base letter plus a combining mark, and normalization reconciles the two. Compatibility normalization (NFKC) goes further. It maps many styled letters, such as mathematical bold or full-width letters, back to plain ones. It also flattens some distinctions you might want to keep, such as superscript digits and ligatures.
- Stemming or lemmatization merges word forms, such as "run", "runs" and "running". Ordinary word counters usually do not do this, so a unique word count is normally a count of distinct spellings, not distinct dictionary words.
An illustrative implementation
The short Python function below shows these decisions explicitly. It applies compatibility normalization and case folding. It then treats runs of word characters, optionally joined by an apostrophe or hyphen, as words. For "The cat saw the other cat." it returns six total words and four unique words, matching the case-insensitive count above. It is a sketch for Latin-script text, not a general solution. Python's word-character class does not match every combining mark used in scripts such as Devanagari, so words in those scripts can be split incorrectly. It also does nothing to segment languages written without spaces.
import re
import unicodedata
def count_words(text):
text = unicodedata.normalize("NFKC", text).casefold()
words = re.findall(r"\w+(?:['’-]\w+)*", text)
return len(words), len(set(words))
print(count_words("The cat saw the other cat.")) # (6, 4)
Changing any one line changes the results on real text. Dropping the case folding raises the unique count. Removing the hyphen from the pattern splits hyphenated words. Skipping normalization lets styled letters count as distinct words. A trustworthy counter states choices like these, or at least behaves consistently enough that you can work them out.
Reversed, mirrored and styled text
Text transformations change the stored characters in ways that directly affect counting, even when the result looks readable on screen:
- Character reversal, as in "olleh" for "hello", keeps the same characters in the opposite order. Word totals may survive, but the counted words no longer match their originals. Careless reversal can also detach accents from their base letters or break emoji built from several code points, which can create stray tokens.
- Word-order reversal keeps each word intact. Total and unique counts should match the original, provided punctuation handling is consistent.
- Upside-down and "mirror" text generators usually substitute look-alike characters from other Unicode blocks. A counter sees those as different words from their plain spellings, and normalization generally does not map them back.
- Styled "font" text, such as bold, italic, script or double-struck letters, also substitutes characters from other Unicode blocks. It may be merged with plain text only if the counter applies compatibility normalization. Some decorative characters are classified as symbols rather than letters, so a counter may skip them entirely.
- Visual-only effects, such as CSS transforms, right-to-left display or font styling, do not change the stored text. Counting the underlying text gives the same result as the plain original.
If you need an accurate count of a transformed passage, count the plain source text. If only the transformed version is available, treat any count of it as approximate.
Will counts stay consistent after copying or sharing?
The count is just a number, so it travels without problems. The text you paste into a counter, however, may not be what it appears to be. Copying from web pages, PDFs, word processors and chat apps can introduce characters that change the result without any visible difference:
- Zero-width spaces and joiners can split one visible word into two tokens, or join two words where a space was expected.
- Non-breaking and other special spaces are treated as separators by some counters and as part of a word by others.
- Soft hyphens and line-break hyphens copied from PDFs can split words or leave fragments behind.
- Smart quotes and apostrophes may be handled differently from straight ones in contractions.
When two tools disagree on the same passage, these hidden characters and differing tokenization rules are the usual causes. Pasting the text through a plain-text editor first, or using a tool that reveals invisible characters, makes the comparison fairer. If a count must meet a particular platform's limit, use that platform's own counter where one exists. Another tool's rules may differ from the platform's, and no destination-specific documentation was available for this article.
Accessibility matters for the text being counted, not the count itself. Styled Unicode letters and substituted look-alike characters can be announced oddly or skipped by screen readers. Behavior varies by assistive technology and version, so test with the specific tools your readers use rather than assuming. A plain-text version is the safe choice for anything that must be read aloud, searched or edited.
Interpreting the unique word count
Dividing unique words by total words gives the type-token ratio, a common rough measure of vocabulary variety. It falls as texts get longer, because common words keep repeating while new words appear more slowly. Comparing ratios across texts of very different lengths is therefore misleading. Compare texts of similar length, or use length-adjusted measures intended for that purpose.
What this article could not verify
No evidence about specific word-counting tools was available. This article therefore does not describe any particular product's tokenization rules, language support, limits or accuracy. It also makes no claims about how any tool handles privacy or stores pasted text. A tool's appearance or marketing is not evidence of either, so check its documentation before pasting sensitive material. The behavior described here follows general, verifiable principles of text processing. The code example's output was worked through by hand for the single sentence shown. It is not a benchmark of real-world text.