Given the following string:
sentence = "Never in the delirious dream of a disordered brain could
anything more savage, more appalling, more hellish, be conceived than
that dark form and savage face which broke upon us out of the wall of
fog";
I am trying to find the frequency of occurrence of each letter. Working towards this, I have find the occurrence of each letter by:
CountLettersSorted[y_] :=
Reverse[SortBy[
Tally[Characters[
StringReplace[
ToLowerCase[y], {"," -> "", " " -> "", "." -> "",
"-" -> ""}]]], Last]];
Giving:
{{"e", 39}, {"a", 35}, {"s", 26}, {"n", 25}, {"o", 24}, {"l",
24}, {"t", 23}, {"d", 21}, {"u", 17}, {"i", 17}, {"h", 17}, {"r",
14}, {"m", 13}, {"w", 11}, {"c", 11}, {"y", 9}, {"b", 9}, {"p",
7}, {"k", 7}, {"v", 6}, {"g", 6}, {"f", 3}}
Now I'm trying to define a function f that can be mapped to this list ie:
f:= #/Length[string[s]]
findingfrequency=Map[f,#]
But this is not working, any ideas?
#
in your code, you also need&
(this marks the end of the anonymous function). For your specific case, I would suggestReverseSort[Counts[Characters[…]]]
to get an association of the counts. Then all you need iscounts/Length["..."]
to get the frequencies. (The advantage of an association here is that the keys (the letters) are ignored for many operations, so dividing by the length leaves them alone) $\endgroup$