WordCounts["string"]
gives an association whose keys are the distinct words identified in string, and whose values give the number of times those words appear in string.
WordCounts["string",n]
gives counts of the distinct n-grams consisting of runs of n words in string.
WordCounts[{"string1","string2",…},…]
gives the counts for each of the stringi.
WordCounts
WordCounts["string"]
gives an association whose keys are the distinct words identified in string, and whose values give the number of times those words appear in string.
WordCounts["string",n]
gives counts of the distinct n-grams consisting of runs of n words in string.
WordCounts[{"string1","string2",…},…]
gives the counts for each of the stringi.
Details and Options
- WordCounts[string,…] identifies words in string in the same way as TextWords.
- In WordCounts[string,n], words that are considered part of an n-gram must appear consecutively in string, not separated by nonword characters other than whitespace.
- WordCounts has the option IgnoreCase. With the setting IgnoreCase->True, letters are in effect all converted to lower case before being counted.
Examples
open all close allBasic Examples (3)
Count the distinct words in a string:
WordCounts["one fish, two fish, red fish, blue fish"]Count the distinct 2-gram word sequences in a string:
WordCounts["the fox jumped over the hare.", 2]Count the distinct words in each of a list of strings:
WordCounts[{"hello world!", "goodnight moon"}]Scope (1)
Options (2)
IgnoreCase (2)
The default setting IgnoreCase->False treats uppercase and lowercase characters as distinct:
WordCounts["Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo", IgnoreCase -> False]IgnoreCase->True treats words that differ only in case as the same:
WordCounts["Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo", IgnoreCase -> True]Count n-grams regardless of case:
WordCounts["And the fox jumped over the tortoise and the hare.", 2, IgnoreCase -> False]WordCounts["And the fox jumped over the tortoise and the hare.", 2, IgnoreCase -> True]Applications (2)
Find the number of times the main characters Sherlock Holmes and John Watson are mentioned in some novels of Arthur Conan Doyle:
novels = {Entity["Book", "TheSignOfTheFour1890"], Entity["Book", "TheAdventuresOfSherlockHolmes"], Entity["Book", "TheHoundOfTheBaskervilles"], Entity["Book", "TheReturnOfSherlockHolmes1904"]};texts = EntityValue[novels, "Plaintext"];
counts = AssociationThread[novels -> WordCounts /@ texts];holmes = counts[[All, "Holmes"]]watson = counts[[All, "Watson"]]callouts = Callout@@@KeyValueMap[{#2, #1}&, Merge[{holmes, watson}, Identity]];ListPlot[callouts, AxesLabel -> {"Holmes", "Watson"}, AspectRatio -> Automatic, AxesOrigin -> {0, 0}]Retrieve Miguel Cervantes's novel Don Quixote from ExampleData to test the empirical Zipf law:
text = ExampleData[{"Text", "DonQuixoteISpanish"}];Generate the frequency table of all words in this text:
Short[association = WordCounts[text]]Zipf's law asserts that the frequency of a word versus its rank in the frequency table follows approximately a linear relation in a log-log scale. Test this statement on the first 1,000 most frequent words:
counts = Take[Values@association, 1000];
f[x_] = Fit[Log[Transpose[{Range[1000], counts}]], {1, x}, x]The result is close to
. Visualize the fit together with the actual data:
dataplot = ListLogLogPlot[counts, PlotStyle -> PointSize[0.02]];
fitplot = LogLogPlot[Exp[f[Log[x]]], {x, 1, 1000}, PlotStyle -> Directive[DotDashed, Red]];
Show[dataplot, fitplot, AspectRatio -> 1, PlotRange -> All, AxesLabel -> {"rank", "frequency"}]Neat Examples (1)
Related Guides
Related Workflows
- Analyze the Text on a Webpage
Text
Wolfram Research (2015), WordCounts, Wolfram Language function, https://reference.wolfram.com/language/ref/WordCounts.html (updated 2024).
CMS
Wolfram Language. 2015. "WordCounts." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/WordCounts.html.
APA
Wolfram Language. (2015). WordCounts. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WordCounts.html
BibTeX
@misc{reference.wolfram_2026_wordcounts, author="Wolfram Research", title="{WordCounts}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/WordCounts.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_wordcounts, organization={Wolfram Research}, title={WordCounts}, year={2024}, url={https://reference.wolfram.com/language/ref/WordCounts.html}, note=[Accessed: 12-June-2026]}