class Markov::TransitionTable(LinkType)

Overview

A TransitionTable represents a mapping of keys to TransitionMatrix's.

Defined in:

Constructors

Instance Method Summary

Constructor Detail

def self.new(pull : JSON::PullParser) #

Makes it possible to use #to_json and #from_json (see Crystal docs)


def self.new #

Creates a new empty TransitionMatrix.


Instance Method Detail

def add(key : LinkType) #

Inserts key into last added key's TransitionMatrix, if applicable, and creates new TransitionMatrix for key if not already there.


def fill(table_with sample : Array(LinkType)) #

Sequentially fills TransitionTable with values in given Array using #add method. Just a shortcut for looping through array and #adding elements.

string_array = %w(some say the world will end in fire)
tt = Markov::TransitionTable(String).new
tt.fill table_with: string_array

def probable(after key : LinkType) : LinkType #

Returns probable transition from the TransitionMatrix associated with key provided. Will raise EmptyTransitionMatrixException if no probable transition is available.

string_array = %w(some say the world will end in fire)
tt = Markov::TransitionTable(String).new
tt.fill table_with: string_array

tt.probable? after: "world" # => "will"
tt.probable? after: "fire"  # raises `EmptyTransitionMatrixException`

def probable?(after key : LinkType) : LinkType? #

Returns probable transition from the TransitionMatrix associated with key provided. Returns nil if no probable transition is available.

string_array = %w(some say the world will end in fire)
tt = Markov::TransitionTable(String).new
tt.fill table_with: string_array

tt.probable? after: "world" # => "will"
tt.probable? after: "fire"  # => nil

def random_key : LinkType #

Returns random key. Will raise EmptyTransitionTableException if TransitionTable is empty.


def random_matrix : TransitionMatrix(LinkType) #

Returns random TransitionMatrix from table.


def reset #

tt.probable? after: "gatsby" #=> nil tt.probable? after: "great" #=> "expectations" or "gatsby"