Skip to content

Traverser

charlie-wt edited this page Sep 1, 2017 · 2 revisions

The traverser module allows you to simulate readings of a story - either using your own functions to decide where to go next, or following where people went in a log.

traverse
traverse(story, ranker=ranker.rand, decider=decider.rand, n=1, cache=None, max_steps=50, prnt=False)

Do a simulated reading of the Story story.

ranker is a function that takes the list of pages that can be moved to, and outputs a dictionary of 'scores' for each page, adding up to 1. It defaults to ranker.rand, which gives an equal chance to every page.

decider is a function that takes the output of ranker and uses it to output the index of the page to choose. It defaults to dc.rand, which generates a random number and uses it to choose the page to go to.

n is the number of traversals to perform. It defaults to 1 - one reading. This will output a store - a list of Records describing the traversal. If n is greater than 1, a list of stores will be output.

cache is a Cache of heuristic values, allowing you to only have to calculate them once. It is recommended to use a cache, and keep it across the length of the program. If it is not set, a temporary internal one will be created for the duration of the n readings, then discarded.

max_steps defines the maximum number of steps between pages that can be done before the traversal stops. This prevents possible infinite loops, but setting it too low may prevent a legitimate reading of a long story from finishing.

traverse_log

traverse_log(story, paths_per_reading, max_steps=50, allow_quitting=False, prnt=False)

Do a log-based reading, where at each page the next page to go to will be chosen from the set of visible pages as the one most visited from this location in the logs.

paths_per_reading is a dictionary as output by importer.path_pages_from_json or importer.filtered_paths_from_json. It is used as the base of the decision making.

Setting allow_quitting to True allows the traversal to quit the story early, if that was the most popular choice. Leaving it as False eliminates this option from decision making, forcing a complete reading to the finish.

Other arguments are the same as for traverse.


auxiliary & helper functions

reset
reset(story, reading, user)

After a reading (which internally relies on a User to track the path, and a Reading to track variables), its User and Reading contain data that is no longer needed. When doing many readings, instead of creating a new User and Reading each time, reset is used to reset these objects to their starting position, ready for another reading.

get path distribution

get_path_distribution(page, ppr, prnt=False)

From all the readings in ppr, count up the pages taken from page and return an options dictionary (similar to a ranker) that contains the proportion of times each page was taken from page. Used in traverse_log's decision making.

get path distribution discourage loops
get_path_distribution_discourage_loops(page, ppr, path, prnt=False)

Similar to get_path_distribution, reduce chances for a page for each time it was visited before.

pick most likely

pick_most_likely(options)

Return the Page with the highest likelihood from options.