class Markov::Chain(LinkType)
- Markov::Chain(LinkType)
- Reference
- Object
Overview
A Chain is a vehicle for generating probable sequences of type LinkType
Included Modules
- JSON::Serializable
Defined in:
Constructors
-
.new(transition_table : TransitionTable(LinkType), seed : LinkType? = nil)
For larger processes, you'll want to externally train a
TransitionTablethen pass it in as an argument. -
.new(sample : Array(LinkType), seed : LinkType = (sample.sample(1)).first)
If you have a small (
Array-sized) data set, you can pass it assampleand aTransitionTablewill be constructed for you with the sample data. -
.new(pull : JSON::PullParser)
Makes it possible to use
#to_jsonand#from_json(see Crystal docs)
Instance Method Summary
-
#generate(count : Int32)
Generates a probable, sequential
ArrayofLinkTypeelements ofcountlength -
#generated : Array(LinkType)
Returns an ordered
Array(LinkType)of allLinkTypeelements generated -
#next : LinkType
Generates the next probable
LinkTypeelement -
#on_dead_end(&block : Proc(TransitionTable(LinkType), Chain(LinkType), Exception, LinkType)) : Proc(TransitionTable(LinkType), Chain(LinkType), Exception, LinkType)
Sets an exception handler for
EmptyTransitionMatrixExceptionwhenChaininstance reaches a dead end while usingChain#generateorChain#next. -
#seed : LinkType
Returns
#seedelement. -
#transition_table : TransitionTable(LinkType)
Returns the trained instance of
TransitionTable
Constructor Detail
For larger processes, you'll want to externally train a TransitionTable then
pass it in as an argument.
If #seed is not provided, it will default to a random item chosen with TransitionTable#random_key
If you have a small (Array-sized) data set, you can pass it as sample
and a TransitionTable will be constructed for you with the sample data.
#seed should be the element in sample which you would like to begin the sequence.
If no #seed is provided, a random element will be selected from sample.
Makes it possible to use #to_json and #from_json (see Crystal docs)
Instance Method Detail
Generates a probable, sequential Array of LinkType elements of count length
Returns an ordered Array(LinkType) of all LinkType elements generated
Sets an exception handler for EmptyTransitionMatrixException when Chain instance reaches a dead end
while using Chain#generate or Chain#next. Returned value is inserted as the next probable element.
Usage:
c = Markov::Chain(String).new sample: ["Koala", "Kangaroo"] of String, seed: "Kangaroo"
c.on_dead_end do |transition_table, chain, exception|
"Koala"
end
c.next # => "Koala"
c.next # => "Kangaroo"
c.next # => "Koala"