StringTools
CharacterFrequencies
compute the number of occurrences of each character in a string
Calling Sequence
Parameters
Description
Examples
CharacterFrequencies( s, filter )
s
-
Maple string
filter
(optional) name or string; character class filter specifying frequencies returned
The CharacterFrequencies(s) command returns an expression sequence of equations of the form character = frequency, where character is a single character string, and frequency is the number of times the corresponding character occurs in the string s.
The expression CharacterFrequencies( s ) is equivalent to seq( ch = CountCharacterOccurrences( s, ch ), ch = Support( s ) ), but computes the latter result more efficiently.
The frequencies appear in ASCII order; that is, in order of the numeric byte value of the character on the left-hand side of each equation. For an example illustrating how to sort by frequency, see the examples below.
To specify that the frequencies of only certain characters be returned, use an optional character class filter parameter. The parameter can be a string of characters to return, for example, "abcd" or one of the following character class names.
alpha
alphabetic characters
alnum
alphabetic characters and digits
ascii
ASCII (7-bit) characters
binary
"0" and "1"
cntrl
control characters
digit
decimal digits
dna
A,C,G or T
hdigit
hexadecimal digits (both cases)
ident
identifier characters
ident1
leading identifier characters
lower
lowercase letters
odigit
octal digits (0-7)
space
whitespace characters
upper
uppercase letters
vowel
vowels (both cases)
All of the StringTools package commands treat strings as (null-terminated) sequences of -bit (ASCII) characters. Thus, there is no support for multibyte character encodings, such as unicode encodings.
We can sort the results by frequency, as follows.
Use a filter to restrict attention to a limited class of characters.
See Also
character classes
sort
string
StringTools[CountCharacterOccurrences]
StringTools[Random]
with
Download Help Document