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:
Here are the first 1,000 characters of the text:
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:
If you count the characters, you can verify that the first occurrence of “Alice” is indeed from characters 25 to 29:
Map First over the list to get a list of just the starting positions:
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:
Add a fill underneath the plot line:
Add a legend:
Add a legend with Legended:
Show “Alice” and the “Queen” at the same time:
Show the story flow for the Queen:
Show both Alice and the Queen at the same time. Enclose the two datasets in a list (indicated by curly braces):
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:
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:
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:
Test the function on three character names:
Run this first to define the function:
Plot the story flow using the function: