Average length (in words) of paragraphs in document
How picky are you about what counts as a word?:
ExampleData[{"Text", "PrideAndPrejudice"}];
assoc = Association@MapIndexed[First@#2 -> # &, StringSplit[%, "."]];
WordCount /@ assoc // Total // AbsoluteTiming
(* {7.93687, 121851} *)
Length@*StringSplit /@ assoc // Total // AbsoluteTiming
(* {0.102869, 122541} *)
Divide by Length@assoc
to get the average (adds negligible time).
No need for averegL[#] &
and WordCount[#] &
. averegL
and WordCount
are already functions. (Not pure functions, though, but this is not of interest for Map
.)
Here, I construct a pure function with \[Function]
, the infix form of Function
.
ParallelMap[
text \[Function] Total[WordCount /@ text]/N[Length[text]],
data
]
{0.015057, <|923 -> 40., 924 -> 52., 925 -> 32.|>}