Wolfram Archive
Wolfram Programming Lab is a legacy product.
All the same functionality and features, including access to Programming Lab Explorations, are available with Wolfram|One.
Start programming now. »

Story Flow in Alice

Get a sense of the flow of Alice in Wonderland by plotting where Alice and Queen occur in the story.

Run the code to get the text of Alice in Wonderland. Try other texts, like TheRaven or Hamlet:

This text is very long; give it the name text and suppress the output with a semicolon (;) at the end of the expression:

text = ExampleData[{"Text", "AliceInWonderland"}];

Here are the first 1,000 characters of the text:

StringTake[text, 1000]

text = ExampleData[{"Text", "AliceInWonderland"}];

Find the positions of Alice in the text. Find the positions of other words:

Get a list of the start and end character positions of the name Alice in the text:

StringPosition[text, "Alice"]

If you count the characters, you can verify that the first occurrence of Alice is indeed from characters 25 to 29:

StringTake[text, 100]

Map First over the list to get a list of just the starting positions:

First /@ StringPosition[text, "Alice"]

First /@ StringPosition[text, "Alice"]

Plot where Alice occurs in the story. Make plots for other words:

Make a smooth histogram showing how often Alice occurs in various parts of the story:

SmoothHistogram[First /@ StringPosition[text, "Alice"]]

Add a fill underneath the plot line:

SmoothHistogram[First /@ StringPosition[text, "Alice"], Filling -> Axis]

SmoothHistogram[First /@ StringPosition[text, "Alice"], Filling -> Axis]

SmoothHistogram[ Legended[First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Alice"], "Alice"], Filling -> Axis]

SmoothHistogram[ Legended[First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Alice"], "Alice"], Filling -> Axis]

Show Alice and the Queen at the same time:

Show the story flow for the Queen:

SmoothHistogram[ Legended[First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Queen"], "Queen"], Filling -> Axis]

Show both Alice and the Queen at the same time. Enclose the two datasets in a list (indicated by curly braces):

SmoothHistogram[{Legended[ First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Alice"], "Alice"], Legended[ First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Queen"], "Queen"]}, Filling -> Axis]

SmoothHistogram[{Legended[ First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Alice"], "Alice"], Legended[ First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Queen"], "Queen"]}, Filling -> Axis]

Make it a function. Try other character names:

This expression can be written more concisely using a pure function so that the parts of the Legended subexpressions that are similar are not duplicated:

SmoothHistogram[{Legended[ First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Alice"], "Alice"], Legended[ First /@ StringPosition[ExampleData[{"Text", "AliceInWonderland"}], "Queen"], "Queen"]}, Filling -> Axis]

The only parts of the Legended subexpressions that differ are the names Alice and Queen. Replace them with # in a pure function (indicated by &) that is applied to a list of the names:

SmoothHistogram[ Legended[First /@ StringPosition[ ExampleData[{"Text", "AliceInWonderland"}], #], #] & /@ {"Alice", "Queen"}, Filling -> Axis]

This form of the expression is easy to turn into a function that will work with any number of names. Replace the names list with the variable names and define the function storyflowinalice with the parameter names:

storyflowinalice[names_] := SmoothHistogram[ Legended[First /@ StringPosition[ ExampleData[{"Text", "AliceInWonderland"}], #], #] & /@ names, Filling -> Axis]

Test the function on three character names:

storyflowinalice[{"Alice", "Queen", "Hatter"}]

Run this first to define the function:

storyflowinalice[names_] := SmoothHistogram[ Legended[First /@ StringPosition[ ExampleData[{"Text", "AliceInWonderland"}], #], #] & /@ names, Filling -> Axis]

Plot the story flow using the function:

storyflowinalice[{"Alice", "Queen", "Hatter"}]