LetterCounts

LetterCounts["string"]

gives an association whose keys are the distinct letters in string, and whose values give the number of times those letters appear in string.

LetterCounts["string",n]

gives counts of the distinct n-grams consisting of runs of n letters in string.

LetterCounts["string",n,{"c1","c2",}]

allows the characters ci to appear in n-grams, in addition to ordinary letters.

Details and Options

  • LetterCounts[string,n] considers n-grams that start at every position in string and includes them in its counts if they contain only letters.
  • LetterCounts effectively uses LetterQ to determine whether to consider a character to be a letter.
  • LetterCounts has the option IgnoreCase. With the setting IgnoreCase->True, letters are in effect all converted to lower case before being counted.
  • LetterCounts[BioSequence["type","seq"]] will give the letters of the string "seq".

Examples

open allclose all

Basic Examples  (4)

Count the letters in a string:

Non-letter characters are excluded from letter counts:

Count 2-grams of letters:

Specify a list of non-letter characters to be counted:

Scope  (1)

Find the letters of a biomolecular sequence:

Options  (2)

IgnoreCase  (2)

With IgnoreCase -> False, uppercase and lowercase characters will be counted independently:

Use IgnoreCase ->True to count all occurrences of a letter, regardless of case:

Count n-grams regardless of case:

Neat Examples  (1)

Find the most frequently occurring letters in a piece of text:

Do the same for 3-letter sequences:

Wolfram Research (2015), LetterCounts, Wolfram Language function, https://reference.wolfram.com/language/ref/LetterCounts.html.

Text

Wolfram Research (2015), LetterCounts, Wolfram Language function, https://reference.wolfram.com/language/ref/LetterCounts.html.

CMS

Wolfram Language. 2015. "LetterCounts." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/LetterCounts.html.

APA

Wolfram Language. (2015). LetterCounts. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LetterCounts.html

BibTeX

@misc{reference.wolfram_2023_lettercounts, author="Wolfram Research", title="{LetterCounts}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/LetterCounts.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2023_lettercounts, organization={Wolfram Research}, title={LetterCounts}, year={2015}, url={https://reference.wolfram.com/language/ref/LetterCounts.html}, note=[Accessed: 19-March-2024 ]}