StringTools - Maple Programming Help

Home : Support : Online Help : Programming : Names and Strings : StringTools Package : Miscellaneous Utilities : StringTools/CharacterFrequencies

StringTools

 CharacterFrequencies
 compute the number of occurrences of each character in a string

 Calling Sequence CharacterFrequencies( s, filter )

Parameters

 s - Maple string filter - (optional) name or string; character class filter specifying frequencies returned

Description

 • 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)

Examples

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{CharacterFrequencies}\left(""\right)$
 > $\mathrm{CharacterFrequencies}\left("aaaa"\right)$
 ${"a"}{=}{4}$ (1)
 > $\mathrm{CharacterFrequencies}\left("abcadaeb"\right)$
 ${"a"}{=}{3}{,}{"b"}{=}{2}{,}{"c"}{=}{1}{,}{"d"}{=}{1}{,}{"e"}{=}{1}$ (2)
 > $\mathrm{CharacterFrequencies}\left("abracadabra"\right)$
 ${"a"}{=}{5}{,}{"b"}{=}{2}{,}{"c"}{=}{1}{,}{"d"}{=}{1}{,}{"r"}{=}{2}$ (3)
 > $\mathrm{CharacterFrequencies}\left(\mathrm{Random}\left(1000000,'\mathrm{lower}'\right)\right)$
 ${"a"}{=}{38715}{,}{"b"}{=}{38351}{,}{"c"}{=}{38615}{,}{"d"}{=}{38449}{,}{"e"}{=}{38194}{,}{"f"}{=}{38657}{,}{"g"}{=}{38420}{,}{"h"}{=}{38624}{,}{"i"}{=}{37981}{,}{"j"}{=}{38519}{,}{"k"}{=}{38495}{,}{"l"}{=}{38382}{,}{"m"}{=}{38407}{,}{"n"}{=}{38522}{,}{"o"}{=}{38175}{,}{"p"}{=}{38798}{,}{"q"}{=}{38050}{,}{"r"}{=}{38676}{,}{"s"}{=}{38658}{,}{"t"}{=}{38402}{,}{"u"}{=}{38559}{,}{"v"}{=}{38293}{,}{"w"}{=}{38421}{,}{"x"}{=}{38494}{,}{"y"}{=}{38644}{,}{"z"}{=}{38499}$ (4)
 > $\mathrm{CharacterFrequencies}\left(\mathrm{Random}\left(1000000,'\mathrm{dna}'\right)\right)$
 ${"A"}{=}{251057}{,}{"C"}{=}{249668}{,}{"G"}{=}{249658}{,}{"T"}{=}{249617}$ (5)
 > $\mathrm{CharacterFrequencies}\left(\mathrm{Random}\left(1000000,'\mathrm{binary}'\right)\right)$
 ${"0"}{=}{499805}{,}{"1"}{=}{500195}$ (6)
 > $\mathrm{Shakespeare}≔"When in disgrace with Fortune and men\text{'}s eyes, I all alone beweep my outcast state, And trouble deaf heaven with my bootless cries, And look upon my self and curse my fate, Wishing me like to one more rich in hope, Featured like him, like him with friends possessed, Desiring this man\text{'}s art, and that man\text{'}s scope, With what I most enjoy contented least, Yet in these thoughts my self almost despising, Haply I think on thee, and then my state, \left(Like to the lark at break of day arising From sullen earth\right) sings hymns at heaven\text{'}s gate, For thy sweet love remembered such wealth brings, That then I scorn to change my state with kings.":$
 > $\mathrm{cf}≔\mathrm{CharacterFrequencies}\left(\mathrm{Shakespeare}\right)$
 ${\mathrm{cf}}{≔}{""}{=}{13}{,}{""}{=}{106}{,}{"\text{'}"}{=}{4}{,}{"\left("}{=}{1}{,}{"\right)"}{=}{1}{,}{","}{=}{15}{,}{"."}{=}{1}{,}{"A"}{=}{2}{,}{"D"}{=}{1}{,}{"F"}{=}{4}{,}{"H"}{=}{1}{,}{"I"}{=}{4}{,}{"L"}{=}{1}{,}{"T"}{=}{1}{,}{"W"}{=}{3}{,}{"Y"}{=}{1}{,}{"a"}{=}{35}{,}{"b"}{=}{6}{,}{"c"}{=}{10}{,}{"d"}{=}{15}{,}{"e"}{=}{66}{,}{"f"}{=}{6}{,}{"g"}{=}{11}{,}{"h"}{=}{31}{,}{"i"}{=}{31}{,}{"j"}{=}{1}{,}{"k"}{=}{9}{,}{"l"}{=}{19}{,}{"m"}{=}{20}{,}{"n"}{=}{38}{,}{"o"}{=}{28}{,}{"p"}{=}{7}{,}{"r"}{=}{21}{,}{"s"}{=}{42}{,}{"t"}{=}{48}{,}{"u"}{=}{9}{,}{"v"}{=}{3}{,}{"w"}{=}{8}{,}{"y"}{=}{13}$ (7)

We can sort the results by frequency, as follows.

 > $\mathrm{sort}\left(\left[\mathrm{cf}\right],\left(u,v\right)→\mathrm{evalb}\left(\mathrm{rhs}\left(u\right)\le \mathrm{rhs}\left(v\right)\right)\right)$
 $\left[{"\left("}{=}{1}{,}{"\right)"}{=}{1}{,}{"."}{=}{1}{,}{"D"}{=}{1}{,}{"H"}{=}{1}{,}{"L"}{=}{1}{,}{"T"}{=}{1}{,}{"Y"}{=}{1}{,}{"j"}{=}{1}{,}{"A"}{=}{2}{,}{"W"}{=}{3}{,}{"v"}{=}{3}{,}{"\text{'}"}{=}{4}{,}{"F"}{=}{4}{,}{"I"}{=}{4}{,}{"b"}{=}{6}{,}{"f"}{=}{6}{,}{"p"}{=}{7}{,}{"w"}{=}{8}{,}{"k"}{=}{9}{,}{"u"}{=}{9}{,}{"c"}{=}{10}{,}{"g"}{=}{11}{,}{""}{=}{13}{,}{"y"}{=}{13}{,}{","}{=}{15}{,}{"d"}{=}{15}{,}{"l"}{=}{19}{,}{"m"}{=}{20}{,}{"r"}{=}{21}{,}{"o"}{=}{28}{,}{"h"}{=}{31}{,}{"i"}{=}{31}{,}{"a"}{=}{35}{,}{"n"}{=}{38}{,}{"s"}{=}{42}{,}{"t"}{=}{48}{,}{"e"}{=}{66}{,}{""}{=}{106}\right]$ (8)

Use a filter to restrict attention to a limited class of characters.

 > $\mathrm{CharacterFrequencies}\left(\mathrm{Shakespeare},"ABCYZ"\right)$
 ${"A"}{=}{2}{,}{"Y"}{=}{1}$ (9)
 > $\mathrm{CharacterFrequencies}\left(\mathrm{Shakespeare},'\mathrm{upper}'\right)$
 ${"A"}{=}{2}{,}{"D"}{=}{1}{,}{"F"}{=}{4}{,}{"H"}{=}{1}{,}{"I"}{=}{4}{,}{"L"}{=}{1}{,}{"T"}{=}{1}{,}{"W"}{=}{3}{,}{"Y"}{=}{1}$ (10)
 > $\mathrm{CharacterFrequencies}\left(\mathrm{Random}\left(1000000\right),'\mathrm{lower}'\right)$
 ${"a"}{=}{3972}{,}{"b"}{=}{3990}{,}{"c"}{=}{3839}{,}{"d"}{=}{3885}{,}{"e"}{=}{3851}{,}{"f"}{=}{3949}{,}{"g"}{=}{3921}{,}{"h"}{=}{4017}{,}{"i"}{=}{3926}{,}{"j"}{=}{4025}{,}{"k"}{=}{4089}{,}{"l"}{=}{3815}{,}{"m"}{=}{3881}{,}{"n"}{=}{3962}{,}{"o"}{=}{3950}{,}{"p"}{=}{3801}{,}{"q"}{=}{3910}{,}{"r"}{=}{3877}{,}{"s"}{=}{3789}{,}{"t"}{=}{3820}{,}{"u"}{=}{3918}{,}{"v"}{=}{4004}{,}{"w"}{=}{3948}{,}{"x"}{=}{3851}{,}{"y"}{=}{3855}{,}{"z"}{=}{3809}$ (11)