-
Notifications
You must be signed in to change notification settings - Fork 2
/
WordProblems.nb
4508 lines (4316 loc) · 181 KB
/
WordProblems.nb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Content-type: application/vnd.wolfram.mathematica *)
(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
(* CreatedBy='Mathematica 10.0' *)
(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[ 158, 7]
NotebookDataLength[ 184643, 4499]
NotebookOptionsPosition[ 177121, 4264]
NotebookOutlinePosition[ 177594, 4283]
CellTagsIndexPosition[ 177551, 4280]
WindowFrame->Normal*)
(* Beginning of Notebook Content *)
Notebook[{
Cell[CellGroupData[{
Cell[BoxData["Today"], "Input",
CellChangeTimes->{{3.734286438781232*^9, 3.734286439203684*^9}}],
Cell[BoxData[
TemplateBox[{RowBox[{"\"Sat 5 May 2018\""}],RowBox[{"DateObject", "[",
RowBox[{"{",
RowBox[{"2018", ",", "5", ",", "5"}], "}"}], "]"}]},
"DateObject",
Editable->False]], "Output",
CellChangeTimes->{3.734286440469399*^9, 3.734469979803186*^9,
3.734505311461104*^9, 3.734514216744005*^9, 3.734545607385673*^9,
3.734545857152451*^9, 3.734548437073133*^9, 3.734560335341251*^9}]
}, Open ]],
Cell[CellGroupData[{
Cell["Those Dreaded Word Problems", "Title",
CellChangeTimes->{{3.734286468695182*^9, 3.734286476166688*^9}},
TextJustification->1.,
FontSize->36],
Cell[TextData[{
StyleBox["Ilkka Kokkarinen, ",
FontSize->14],
StyleBox["[email protected]",
FontFamily->"Courier New"],
StyleBox["\n\nThe original \[OpenCurlyDoubleQuote]Big Data\
\[CloseCurlyDoubleQuote] of its era, Leo Tolstoy\[CloseCurlyQuote]s epic \
masterpiece ",
FontSize->14],
StyleBox["War and Peace ",
FontSize->14,
FontSlant->"Italic"],
StyleBox["that has been loved and dreaded for its ginormous length, is now \
old enough to be available for free on Project Gutenberg so that we can \
perform some computations on it. The raw text file contains about three \
megabytes of Unicode character data encoded with the ",
FontSize->14],
StyleBox["UTF-8",
FontSize->14,
FontSlant->"Italic"],
StyleBox[" standard character encoding that allows all the familiar English \
characters and punctuation marks to be stored using only one byte of storage, \
but the characters on the higher planes of Unicode can fly along encoded in \
multibyte combinations whose highest order bits are set to one. The ",
FontSize->14],
StyleBox["Mathematica",
FontSize->14,
FontSlant->"Italic"],
StyleBox[" function ",
FontSize->14],
StyleBox["Import",
FontSize->14,
FontWeight->"Bold"],
StyleBox[" is a powerful Swiss army knife that can read in the contents of \
any file of any common data format, either from the local filesystem or over \
an URL.",
FontSize->14]
}], "Text",
CellChangeTimes->{{3.7342865100028687`*^9, 3.734286580992635*^9}, {
3.734286924083089*^9, 3.734287007833777*^9}, {3.7342884400168133`*^9,
3.7342884436083117`*^9}, {3.734293179705562*^9, 3.7342932739582577`*^9}, {
3.734432876385848*^9, 3.7344329765692873`*^9}, {3.734559412139058*^9,
3.734559424985229*^9}},
TextJustification->1.,
FontSize->14],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"wap", " ", "=", " ",
RowBox[{
"Import", "[", "\"\<http://www.gutenberg.org/files/2600/2600-0.txt\>\"",
"]"}]}], ";"}], "\[IndentingNewLine]",
RowBox[{"StringLength", "[", "wap", "]"}]}], "Input",
CellChangeTimes->{3.734286547637602*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["3227578"], "Output",
CellChangeTimes->{3.734286552072324*^9, 3.734469989077305*^9,
3.734505314172943*^9, 3.734512867101101*^9, 3.734514219332274*^9,
3.7345456107669077`*^9, 3.734545859820071*^9, 3.734548439762742*^9,
3.7345603381894197`*^9},
TextJustification->1.,
FontSize->14]
}, Open ]],
Cell[TextData[{
"Even though the file contains line break characters, ",
StyleBox["Import",
FontWeight->"Bold"],
" does not treat them any differently from other characters, and for now the \
entire book is one string of more than three million Unicode characters. The \
function ",
StyleBox["StringSplit",
FontWeight->"Bold"],
" can easily break down this mass into the list of its individual lines. \
However, since the text file available on Project Gutenberg contains several \
lines of legal boilerplate prologue and epilogue around the actual meat of \
the text, we use ",
StyleBox["Take",
FontWeight->"Bold"],
" and ",
StyleBox["Drop",
FontWeight->"Bold"],
" to extract only the actual body of the text, from which the empty lines \
and chapter number lines are furthermore discarded. The remaining body of the \
text then contains 51,111 lines of actual content. We can take a peek of the \
last ten lines to find out how that entire story finally ends."
}], "Text",
CellChangeTimes->{{3.734286614658532*^9, 3.7342867231825247`*^9}, {
3.734286835589786*^9, 3.734286852980466*^9}, {3.734288825040975*^9,
3.734288932479149*^9}, {3.734293293405346*^9, 3.734293322997127*^9}, {
3.734336210780677*^9, 3.734336456392112*^9}, {3.734432988042371*^9,
3.73443304284798*^9}},
TextJustification->1.,
FontSize->14],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"wapLines", " ", "=", " ",
RowBox[{"ToLowerCase", "[",
RowBox[{"Select", "[",
RowBox[{
RowBox[{"Take", "[",
RowBox[{
RowBox[{"Drop", "[",
RowBox[{
RowBox[{"StringSplit", "[",
RowBox[{"wap", ",", " ",
RowBox[{"RegularExpression", "[", "\"\<\\\\n\>\"", "]"}]}], "]"}],
",", "835"}], "]"}], ",", "64860"}], "]"}], ",",
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{
RowBox[{"StringLength", "[", "#", "]"}], ">", "0"}], " ", "&&", " ",
RowBox[{"Not", "[",
RowBox[{"StringMatchQ", "[",
RowBox[{"#", ",",
RowBox[{"RegularExpression", "[", "\"\<^CHAPTER.*\>\"", "]"}]}],
"]"}], "]"}]}], ")"}], "&"}]}], "]"}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"Length", "[", "wapLines", "]"}], "\[IndentingNewLine]",
RowBox[{"Scan", "[",
RowBox[{"Print", ",",
RowBox[{"wapLines", "[",
RowBox[{"[",
RowBox[{
RowBox[{
RowBox[{"Length", "[", "wapLines", "]"}], "-", "10"}], " ", ";;",
RowBox[{"Length", "[", "wapLines", "]"}]}], "]"}], "]"}]}],
"]"}]}], "Input",
CellChangeTimes->{
3.734286664548379*^9, {3.734286731249248*^9, 3.734286823392435*^9}, {
3.73428835894731*^9, 3.73428837133706*^9}, {3.734336173694154*^9,
3.734336205201951*^9}},
TextJustification->1.,
FontSize->14],
Cell[BoxData["51111"], "Output",
CellChangeTimes->{{3.734286767703299*^9, 3.734286824123043*^9},
3.73428685623685*^9, 3.73428837355481*^9, 3.7343362070331163`*^9,
3.7343377471050243`*^9, 3.734469989698863*^9, 3.7345053148311663`*^9,
3.7345142199485903`*^9, 3.7345456113916388`*^9, 3.734545860392948*^9,
3.7345484403424273`*^9, 3.7345603387377357`*^9},
TextJustification->1.,
FontSize->14],
Cell[CellGroupData[{
Cell[BoxData["\<\"do not feel the movement of the earth, but by admitting its \
immobility\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.73456033874671*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"we arrive at absurdity, while by admitting its motion \
(which we do not\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.7345603387546787`*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"feel) we arrive at laws,\[CloseCurlyDoubleQuote] so also in \
history the new view says: \[OpenCurlyDoubleQuote]it is\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338766384*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"true that we are not conscious of our dependence, but by \
admitting our\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338778413*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"free will we arrive at absurdity, while by admitting our \
dependence on\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338789234*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"the external world, on time, and on cause, we arrive at \
laws.\[CloseCurlyDoubleQuote]\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338799715*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"in the first case it was necessary to renounce the \
consciousness of an\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338811449*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"unreal immobility in space and to recognize a motion we did \
not feel;\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338822001*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"in the present case it is similarly necessary to renounce a \
freedom\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338833249*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"that does not exist, and to recognize a dependence of which \
we are not\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338844191*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["\<\"conscious.\"\>"], "Print",
CellChangeTimes->{3.7342883735667467`*^9, 3.734336207049646*^9,
3.7343377471180763`*^9, 3.734469989708123*^9, 3.734505314841323*^9,
3.734514219959463*^9, 3.7345456114028463`*^9, 3.734545860403592*^9,
3.734548440352228*^9, 3.734560338855785*^9},
TextJustification->1.,
FontSize->14]
}, Open ]]
}, Open ]],
Cell[TextData[{
"For the purpose of analyzing the individual words that make up this text, \
we split the lines into individual words. So that we don\[CloseCurlyQuote]t \
distinguish between capitalized words that begin a sentence and the lowercase \
words inside a sentence, the entire text is first converted to lowercase, and \
all the contractions are turned into equivalent separate words. The function ",
StyleBox["removeContractions",
FontWeight->"Bold"],
" removes the contractions from the line, and the function ",
StyleBox["intoWords",
FontWeight->"Bold"],
" splits the result into the individual words using the sequences of \
non-letter characters as separator sequences. The function ",
StyleBox["StringReplace",
FontWeight->"Bold"],
" can replace every occurrence of a string pattern inside that string, with \
the operator ",
StyleBox["~~",
FontWeight->"Bold"],
" combining parts of a regular expression that identify the pattern to be \
replaced."
}], "Text",
CellChangeTimes->{{3.734286880693119*^9, 3.734286915395442*^9}, {
3.7342870344421263`*^9, 3.734287070592956*^9}, {3.734287128001021*^9,
3.734287181311346*^9}, {3.734288939441737*^9, 3.734289067509872*^9}},
TextJustification->1.,
FontSize->14],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{
RowBox[{"removeContractions", "[", "line_", "]"}], " ", ":=", " ",
RowBox[{"StringReplace", "[",
RowBox[{"line", ",",
RowBox[{"{", "\[IndentingNewLine]",
RowBox[{
RowBox[{
"\"\<you\[CloseCurlyQuote]re\>\"", " ", "\[Rule]", " ",
"\"\<you are\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
"\"\<i\[CloseCurlyQuote]m\>\"", " ", "\[Rule]", " ", "\"\<i am\>\""}],
",", "\[IndentingNewLine]",
RowBox[{
"\"\<we\[CloseCurlyQuote]re\>\"", " ", "\[Rule]", " ",
"\"\<we are\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
"\"\<they\[CloseCurlyQuote]re\>\"", " ", "\[Rule]", " ",
"\"\<they are\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
"\"\<shan\[CloseCurlyQuote]t\>\"", " ", "\[Rule]", " ",
"\"\<shall not\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
"\"\<won\[CloseCurlyQuote]t\>\"", " ", "\[Rule]", " ",
"\"\<will not\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
"\"\<shouldn\[CloseCurlyQuote]t\>\"", " ", "\[Rule]", " ",
"\"\<should not\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
"\"\<don\[CloseCurlyQuote]t\>\"", " ", "\[Rule]", " ",
"\"\<do not\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
"\"\<doesn\[CloseCurlyQuote]t\>\"", " ", "\[Rule]", " ",
"\"\<does not\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
"\"\<mustn\[CloseCurlyQuote]t\>\"", " ", "\[Rule]", " ",
"\"\<must not\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{
"\"\<\[CloseCurlyQuote]ll\>\"", " ", "~~", " ", "WordBoundary"}], " ",
"\[Rule]", " ", "\"\< will\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{
"\"\<\[CloseCurlyQuote]s\>\"", " ", "~~", " ", "WordBoundary"}], " ",
"\[Rule]", " ", "\"\<\>\""}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{
"\"\<\[CloseCurlyQuote]t\>\"", " ", "~~", " ", "WordBoundary"}], " ",
"\[Rule]", " ", "\"\<\>\""}]}], "\[IndentingNewLine]", "}"}]}],
"]"}]}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"intoWords", "[", "line_", "]"}], " ", ":=", " ",
RowBox[{"StringSplit", "[",
RowBox[{
RowBox[{"removeContractions", "[", "line", "]"}], ",", " ",
RowBox[{
RowBox[{"Except", "[", "LetterCharacter", "]"}], ".."}]}], " ", "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"intoWords", "[",
"\"\<i\[CloseCurlyQuote]m sure that we won\[CloseCurlyQuote]t stand for Bob\
\[CloseCurlyQuote]s nonsense!\>\"", "]"}]}], "Input",
CellChangeTimes->{
3.734287080965753*^9, {3.7343366734313602`*^9, 3.734336677820459*^9}, {
3.734337182710677*^9, 3.734337210039455*^9}},
TextJustification->1.,
FontSize->14],
Cell[BoxData[
RowBox[{"{",
RowBox[{"\<\"i\"\>", ",", "\<\"am\"\>", ",", "\<\"sure\"\>",
",", "\<\"that\"\>", ",", "\<\"we\"\>", ",", "\<\"will\"\>",
",", "\<\"not\"\>", ",", "\<\"stand\"\>", ",", "\<\"for\"\>",
",", "\<\"Bob\"\>", ",", "\<\"nonsense\"\>"}], "}"}]], "Output",
CellChangeTimes->{3.734287082243835*^9, 3.7343362392787247`*^9,
3.734336682097307*^9, 3.734337213982286*^9, 3.734337437861617*^9,
3.734337751143858*^9, 3.734469989930785*^9, 3.734505315050275*^9,
3.7345142201711073`*^9, 3.734545611618733*^9, 3.734545860608012*^9,
3.734548440543911*^9, 3.734560338935155*^9},
TextJustification->1.,
FontSize->14]
}, Open ]],
Cell[TextData[{
"To count the number of occurrences of elements that naturally map into \
positive integers, some kind of Table of counters would be the best way to \
keep track of how many times you have seen each element. The same approach \
does not work for counting things for which most possible things are not \
things at all, such as the words of the English language. For the purpose of \
mapping a set of keys into their corresponding values, ",
StyleBox["associations",
FontSlant->"Italic"],
" are a much more efficient mapping data structure of ",
StyleBox["Mathematica",
FontSlant->"Italic"],
", similar to hashmaps of various other languages such as Java. Given the \
list of individual lines of strings, the function ",
StyleBox["countWords",
FontWeight->"Bold"],
" creates and returns a pair ",
StyleBox["{wordMap, wordCount}",
FontWeight->"Bold"],
" where ",
StyleBox["wordMap",
FontWeight->"Bold"],
" is the association of individual words to their occurrence counts, and ",
StyleBox["wordCount",
FontWeight->"Bold"],
" is the mapping from the lines of the text into the total number of words \
that have been seen up to that line. "
}], "Text",
CellChangeTimes->{{3.7342872077915277`*^9, 3.734287264613842*^9}, {
3.734287300998705*^9, 3.734287490087658*^9}},
TextJustification->1.,
FontSize->14],
Cell[BoxData[{
RowBox[{
RowBox[{"ClearAll", "[", "countWords", "]"}],
";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"countWords", "[", "lines_", "]"}], " ", ":=", " ",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{
"wordMap", ",", " ", "wordCount", ",", "i", ",", "j", ",", "words", ",",
" ", "word", ",", " ", "totalWords"}], "}"}], ",",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"wordMap", " ", "=", " ",
RowBox[{"Association", "[",
RowBox[{"{", "}"}], "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"wordCount", " ", "=", " ",
RowBox[{"Table", "[",
RowBox[{"0", ",",
RowBox[{"{",
RowBox[{"i", ",", " ", "1", ",", " ",
RowBox[{"Length", "[", "lines", "]"}]}], "}"}]}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"totalWords", " ", "=", " ", "0"}], ";", "\[IndentingNewLine]",
RowBox[{"Do", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"words", " ", "=", " ",
RowBox[{"intoWords", "[",
RowBox[{"lines", "[",
RowBox[{"[", "i", "]"}], "]"}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"Do", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"word", " ", "=", " ",
RowBox[{"words", "[",
RowBox[{"[", "j", "]"}], "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{
RowBox[{
RowBox[{"StringLength", "[", "word", "]"}], " ", ">", " ",
"1"}], " ", "||", " ",
RowBox[{"word", " ", "\[Equal]", " ", "\"\<i\>\""}]}], ",",
"\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{"KeyExistsQ", "[",
RowBox[{"wordMap", ",", "word"}], "]"}], ",",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"wordMap", "[", "word", "]"}], " ", "=", " ",
RowBox[{
RowBox[{"wordMap", "[", "word", "]"}], " ", "+", " ",
"1"}]}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"wordMap", "[", "word", "]"}], " ", "=", " ", "1"}],
";", "\[IndentingNewLine]",
RowBox[{"totalWords", "++"}]}]}], "\[IndentingNewLine]",
"]"}]}], "]"}]}], ",",
RowBox[{"{",
RowBox[{"j", ",", " ", "1", ",", " ",
RowBox[{"Length", "[", "words", "]"}]}], "}"}]}], "]"}], ";",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"wordCount", "[",
RowBox[{"[", "i", "]"}], "]"}], " ", "=", " ", "totalWords"}]}],
"\[IndentingNewLine]", ",",
RowBox[{"{",
RowBox[{"i", ",", "1", ",",
RowBox[{"Length", "[", "lines", "]"}]}], "}"}]}], "]"}], ";",
"\[IndentingNewLine]",
RowBox[{"{",
RowBox[{"wordMap", ",", " ", "wordCount"}], "}"}]}]}],
"\[IndentingNewLine]", "]"}]}], ";"}]}], "Input",
CellChangeTimes->{
3.734287500109976*^9, {3.734337044216466*^9, 3.734337093774849*^9}, {
3.734337249103759*^9, 3.73433724921986*^9}, {3.734337313500762*^9,
3.73433732950989*^9}, {3.734337383043601*^9, 3.734337418124096*^9},
3.734337449617305*^9, {3.734337481953661*^9, 3.73433754864755*^9}, {
3.734337605274603*^9, 3.7343376101626596`*^9}, {3.734337659610817*^9,
3.73433766006535*^9}, 3.734337699974354*^9, {3.7343378361753607`*^9,
3.734337886302372*^9}, {3.734337930574843*^9, 3.7343379466354513`*^9}, {
3.73433799781894*^9, 3.734338040315014*^9}},
TextJustification->1.,
FontSize->14],
Cell[TextData[{
"With the previous function defined, we can generate the association of the \
individual words to their occurrence counts, from which the function ",
StyleBox["Keys",
FontWeight->"Bold"],
" produces the list of keys that have some value associated with them. The \
first problem that having this association available makes trivial to solve \
is finding the top hundred most frequent words in the text, along with their \
occurrence counts. The results are not really that surprising, but of course \
computational linguists would normalize these counts with respect to their \
corresponding frequencies in the average English text, which would be \
available in the ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" function ",
StyleBox["WordFrequencyData",
FontWeight->"Bold"],
". Even without having ever read the epic story, just based on their \
occurrence counts seem like Pierre, Nat\[AAcute]sha and Andrew might be its \
three main characters."
}], "Text",
CellChangeTimes->{{3.734287516600403*^9, 3.734287597501792*^9}, {
3.73428769975599*^9, 3.734287843497905*^9}, {3.734288089918386*^9,
3.734288115725552*^9}, {3.734289536810886*^9, 3.734289555347158*^9}, {
3.734336987599698*^9, 3.7343369993832903`*^9}, {3.7343378181485662`*^9,
3.734337819979086*^9}, {3.734338312811819*^9, 3.734338380756043*^9}},
TextJustification->1.,
FontSize->14],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{
RowBox[{"{",
RowBox[{"result", ",", " ", "wordCount"}], "}"}], " ", "=", " ",
RowBox[{"countWords", "[", "wapLines", "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"Length", "[",
RowBox[{"Keys", "[", "result", "]"}], "]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"sorted", " ", "=", " ",
RowBox[{"Reverse", "[",
RowBox[{"SortBy", "[",
RowBox[{
RowBox[{"Map", "[",
RowBox[{
RowBox[{
RowBox[{"{",
RowBox[{"#", ",",
RowBox[{"result", "[", "#", "]"}]}], "}"}], "&"}], ",", " ",
RowBox[{"Keys", "[", "result", "]"}]}], "]"}], ",",
RowBox[{
RowBox[{"#", "[",
RowBox[{"[", "2", "]"}], "]"}], "&"}]}], "]"}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"Take", "[",
RowBox[{"sorted", ",", "100"}], "]"}]}], "Input",
CellChangeTimes->{{3.734287545891787*^9, 3.7342875771470633`*^9}, {
3.7342877156292543`*^9, 3.7342877159405403`*^9}, {3.7342880643116302`*^9,
3.734288068258408*^9}},
TextJustification->1.,
FontSize->14],
Cell[BoxData["17423"], "Output",
CellChangeTimes->{
3.734287586066513*^9, 3.734287721138482*^9, 3.7342880741998577`*^9,
3.7343364677652187`*^9, 3.7343366942953444`*^9, 3.7343371041584682`*^9,
3.734337225689329*^9, 3.734337258062283*^9, 3.734337348789167*^9,
3.734337393881446*^9, 3.7343374276841097`*^9, 3.734337459193228*^9,
3.734337525946965*^9, 3.734337560719199*^9, 3.7343376296175003`*^9,
3.7343376689883833`*^9, 3.734337710324102*^9, 3.734337764615119*^9, {
3.734337870896172*^9, 3.734337898330984*^9}, {3.734337948601417*^9,
3.7343379585683413`*^9}, 3.734338007941154*^9, 3.734338050685808*^9,
3.734469996041655*^9, 3.734505321380258*^9, 3.73451422579136*^9,
3.734545616856415*^9, 3.734545865856331*^9, 3.7345484480902042`*^9,
3.734560344410927*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData[
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"\<\"the\"\>", ",", "34549"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"and\"\>", ",", "22230"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"to\"\>", ",", "16675"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"of\"\>", ",", "14889"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"he\"\>", ",", "10004"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"in\"\>", ",", "8979"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"that\"\>", ",", "8191"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"his\"\>", ",", "7984"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"was\"\>", ",", "7359"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"with\"\>", ",", "5663"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"it\"\>", ",", "5602"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"not\"\>", ",", "5426"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"had\"\>", ",", "5365"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"her\"\>", ",", "4725"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"him\"\>", ",", "4637"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"at\"\>", ",", "4532"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"i\"\>", ",", "4507"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"but\"\>", ",", "4050"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"as\"\>", ",", "4024"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"on\"\>", ",", "4003"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"you\"\>", ",", "3801"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"for\"\>", ",", "3529"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"she\"\>", ",", "3489"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"is\"\>", ",", "3321"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"said\"\>", ",", "2842"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"all\"\>", ",", "2795"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"from\"\>", ",", "2694"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"be\"\>", ",", "2438"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"by\"\>", ",", "2437"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"were\"\>", ",", "2425"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"what\"\>", ",", "2398"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"they\"\>", ",", "2253"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"who\"\>", ",", "2166"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"one\"\>", ",", "2128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"this\"\>", ",", "2075"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"which\"\>", ",", "2057"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"have\"\>", ",", "1975"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"pierre\"\>", ",", "1963"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"prince\"\>", ",", "1928"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"so\"\>", ",", "1901"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"an\"\>", ",", "1629"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"will\"\>", ",", "1594"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"up\"\>", ",", "1579"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"do\"\>", ",", "1570"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"there\"\>", ",", "1560"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"them\"\>", ",", "1529"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"or\"\>", ",", "1525"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"when\"\>", ",", "1496"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"did\"\>", ",", "1485"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"been\"\>", ",", "1476"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"their\"\>", ",", "1440"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"no\"\>", ",", "1401"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"are\"\>", ",", "1394"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"would\"\>", ",", "1366"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"now\"\>", ",", "1332"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"only\"\>", ",", "1298"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"if\"\>", ",", "1294"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"me\"\>", ",", "1275"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"out\"\>", ",", "1239"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"my\"\>", ",", "1226"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"nat\[AAcute]sha\"\>", ",", "1213"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"man\"\>", ",", "1189"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"andrew\"\>", ",", "1143"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"could\"\>", ",", "1115"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"we\"\>", ",", "1069"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"more\"\>", ",", "1057"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"himself\"\>", ",", "1020"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"about\"\>", ",", "1011"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"how\"\>", ",", "1006"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"into\"\>", ",", "1004"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"then\"\>", ",", "941"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"time\"\>", ",", "929"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"princess\"\>", ",", "916"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"face\"\>", ",", "893"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"french\"\>", ",", "881"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"went\"\>", ",", "862"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"some\"\>", ",", "846"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"know\"\>", ",", "846"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"after\"\>", ",", "841"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"old\"\>", ",", "833"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"before\"\>", ",", "831"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"eyes\"\>", ",", "827"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"your\"\>", ",", "811"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"very\"\>", ",", "803"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"men\"\>", ",", "792"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"rost\[OAcute]v\"\>", ",", "776"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"room\"\>", ",", "771"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"thought\"\>", ",", "767"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"go\"\>", ",", "755"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"like\"\>", ",", "751"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"well\"\>", ",", "746"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"see\"\>", ",", "731"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"count\"\>", ",", "726"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"moscow\"\>", ",", "722"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"began\"\>", ",", "718"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"again\"\>", ",", "709"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"has\"\>", ",", "707"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"down\"\>", ",", "700"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"come\"\>", ",", "683"}], "}"}], ",",
RowBox[{"{",
RowBox[{"\<\"came\"\>", ",", "683"}], "}"}]}], "}"}]], "Output",
CellChangeTimes->{
3.734287586066513*^9, 3.734287721138482*^9, 3.7342880741998577`*^9,
3.7343364677652187`*^9, 3.7343366942953444`*^9, 3.7343371041584682`*^9,
3.734337225689329*^9, 3.734337258062283*^9, 3.734337348789167*^9,
3.734337393881446*^9, 3.7343374276841097`*^9, 3.734337459193228*^9,
3.734337525946965*^9, 3.734337560719199*^9, 3.7343376296175003`*^9,
3.7343376689883833`*^9, 3.734337710324102*^9, 3.734337764615119*^9, {
3.734337870896172*^9, 3.734337898330984*^9}, {3.734337948601417*^9,
3.7343379585683413`*^9}, 3.734338007941154*^9, 3.734338050685808*^9,
3.734469996041655*^9, 3.734505321380258*^9, 3.73451422579136*^9,
3.734545616856415*^9, 3.734545865856331*^9, 3.7345484480902042`*^9,
3.7345603444668922`*^9},
TextJustification->1.,
FontSize->14]
}, Open ]],
Cell[TextData[{
"As you read through any real world text, you will quickly encounter the \
majority of the words that it is ever going to contain, and the rest of the \
text will be basically those same words but together in different \
combinations and permutations. Or is it? The ",
StyleBox["wordCount",
FontWeight->"Bold"],
" list returned by the previous function shows the total count of different \
words encountered up to the particular line. Since there are over fifty \
thousands lines of text in ",
StyleBox["War and Peace",
FontSlant->"Italic"],
", to keep the following graph small enough, the plot shows the cumulative \
word counts for every thousandth line. This makes the growth curve clear \
enough for visual inspection, and after the initial growth spurt, new words \
keep coming out all the way to the end of the book in an almost linear pace. \
"
}], "Text",
CellChangeTimes->{{3.734287624294654*^9, 3.7342876947244053`*^9}, {
3.734287865874742*^9, 3.734287942616056*^9}, {3.734288039655879*^9,
3.7342880454545403`*^9}, {3.734288167252159*^9, 3.734288172076241*^9}, {
3.7342887026266108`*^9, 3.7342887062107964`*^9}, {3.734293346231682*^9,
3.734293390622335*^9}, {3.734336563053829*^9, 3.734336639524683*^9}, {
3.7343367232192297`*^9, 3.734336737691537*^9}, {3.734338093311414*^9,
3.7343381817007303`*^9}},
TextJustification->1.,
FontSize->14],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"ListLinePlot", "[",
RowBox[{"Map", "[",
RowBox[{"Last", ",", " ",
RowBox[{"Partition", "[",
RowBox[{"wordCount", ",", "1000"}], "]"}]}], "]"}], "]"}]], "Input",
CellChangeTimes->{{3.734287954884708*^9, 3.7342879569208813`*^9}, {
3.7344700091148167`*^9, 3.734470013159328*^9}},
TextJustification->1.,
FontSize->14],
Cell[BoxData[
GraphicsBox[{{}, {{}, {},
{RGBColor[0.368417, 0.506779, 0.709798], PointSize[0.016666666666666666`],
AbsoluteThickness[1.6],
StyleBox[LineBox[CompressedData["
1:eJw1zM8rw3Ecx/FvcnCQHBwcHGYhLclsMz+37/x2pNyUWu3gJOc5fP4BVycH
B0k5DCUSGfNz82NX/gQ5KEvKBXu+3p/69OzxfX36tqaX5zI1nudF/u5/OR/J
aqKbvj7Q7R25jq7n5EZaOJCb6Oyh3Ey/j+QWmjqRAzR9Kgfpy5ncRoPncke1
biEvd9KKOcS+dSF34fil3K3/m3vocUEO837xSu6ln+YI+/61HKWBGznGvmfu
o4lbOc6eM/fj9jt5AK+YB/GXeQiv3svD9N08wp4tyglcMSdxqiT7dFd2cv2D
9hTNyA67V9tHaehRu+xkb4z3b7ZjL/akfZx9TXbYK9k+QWuftWO3JHuTtGw7
9hvK2qeqzYdlh71526dp1nbsb9g+g4u2Y/dT9n8Bq455FA==
"]],
FontFamily->"Verdana"]}}, {}},
AspectRatio->NCache[GoldenRatio^(-1), 0.6180339887498948],
Axes->{True, True},
AxesLabel->{None, None},
AxesOrigin->{0, 0},
BaseStyle->{FontFamily -> "Verdana"},
DisplayFunction->Identity,
Frame->{{False, False}, {False, False}},
FrameLabel->{{None, None}, {None, None}},
FrameTicks->{{Automatic, Automatic}, {Automatic, Automatic}},
GridLines->{None, None},
GridLinesStyle->Directive[
GrayLevel[0.5, 0.4]],
Method->{},
PlotRange->{{0., 51.}, {0, 17390.}},
PlotRangeClipping->True,
PlotRangePadding->{{
Scaled[0.02],
Scaled[0.02]}, {
Scaled[0.02],
Scaled[0.05]}},
Ticks->{Automatic, Automatic}]], "Output",
CellChangeTimes->{3.734287958380142*^9, 3.734336473764697*^9,
3.734469996877082*^9, 3.7345053217929897`*^9, 3.734514226252613*^9,
3.734545617293181*^9, 3.734545866162375*^9, 3.734548448493979*^9,
3.734560344875244*^9},
TextJustification->1.,
FontSize->14]
}, Open ]],
Cell[TextData[{
"How many lines do we need to read into ",
StyleBox["War and Peace",
FontSlant->"Italic"],
" to encounter half of the different words that the entire book contains? \
The correct answer of 8,740 lines is significantly lower than the quick but \
wrong answer of one half of the lines! In fact, after reading only the first \
1,747 lines out of the entirety of 51,476 lines of raw text, you have already \
encountered one tenth of the total set of words of this entire tome. Merely \
reading the first hundred lines would already give you 474 different words."
}], "Text",
CellChangeTimes->{{3.734288176412868*^9, 3.7342881919874897`*^9}, {
3.734288242230225*^9, 3.734288318345533*^9}, {3.734288458990735*^9,
3.7342884931664047`*^9}, {3.734288523797923*^9, 3.734288683507135*^9}, {
3.734288732899472*^9, 3.734288768953514*^9}, {3.734289143272863*^9,
3.734289170768511*^9}, {3.734289488462368*^9, 3.7342894899881477`*^9}, {
3.734293399358265*^9, 3.734293426427677*^9}, {3.734293496945971*^9,
3.734293503689891*^9}},
TextJustification->1.,
FontSize->14],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{"Table", "[",
RowBox[{
RowBox[{"Last", "[",
RowBox[{"Select", "[",
RowBox[{"wordCount", ",", " ",
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{"k", "#"}], " ", "\[LessEqual]", " ",
RowBox[{"Last", "[", "wordCount", "]"}]}], ")"}], "&"}]}], "]"}],
"]"}], ",",
RowBox[{"{",
RowBox[{"k", ",", "10", ",", "2", ",",
RowBox[{"-", "1"}]}], "}"}]}], "]"}], "\[IndentingNewLine]",
RowBox[{"wordCount", "[",
RowBox[{"[", "100", "]"}], "]"}]}], "Input",
CellChangeTimes->{{3.734288204965062*^9, 3.734288238899428*^9}, {
3.7342885050169353`*^9, 3.734288514255026*^9}, {3.734288644948658*^9,
3.73428866949165*^9}, {3.7342934704826307`*^9, 3.7342934713075666`*^9}},
TextJustification->1.,
FontSize->14],
Cell[BoxData[
RowBox[{"{",
RowBox[{
"1742", ",", "1934", ",", "2177", ",", "2488", ",", "2903", ",", "3484",
",", "4354", ",", "5806", ",", "8711"}], "}"}]], "Output",
CellChangeTimes->{
3.734288264482662*^9, 3.734288515737768*^9, {3.7342886515112543`*^9,
3.734288670505094*^9}, 3.734293472434642*^9, 3.734469997522225*^9,
3.7345053225198174`*^9, 3.734514226951758*^9, 3.73454561797678*^9,
3.734545866845879*^9, 3.734548449212237*^9, 3.734560345548587*^9},
TextJustification->1.,
FontSize->14],
Cell[BoxData["473"], "Output",
CellChangeTimes->{
3.734288264482662*^9, 3.734288515737768*^9, {3.7342886515112543`*^9,
3.734288670505094*^9}, 3.734293472434642*^9, 3.734469997522225*^9,
3.7345053225198174`*^9, 3.734514226951758*^9, 3.73454561797678*^9,
3.734545866845879*^9, 3.734548449212237*^9, 3.7345603455571947`*^9},
TextJustification->1.,
FontSize->14]
}, Open ]],
Cell[TextData[{
"Using ",
StyleBox["War and Peace",
FontSlant->"Italic"],
" as the source corpus, we can have some fun with the ",
StyleBox["Markovian random text generation",
FontSlant->"Italic"],
", also more colloquially known as ",
StyleBox["Dissociated Press",
FontSlant->"Italic"],
". To demonstrate the extremely important ",
StyleBox["tradeoff between space and time",