Package org.cis1200
Class MarkovChainIterator
java.lang.Object
org.cis1200.MarkovChainIterator
This inner class represents a "walk" through the Markov Chain as an
Iterator
that yields each token encountered during the walk.
A walk through the chain is determined by the given NumberGenerator
,
which picks from among the choices of tokens according to their
probability distributions.
For example, given
ILLUSTRATIVE EXAMPLE MARKOV CHAIN: startTokens: { "a":2 } bigramFrequencies: "!": { "and":1 } "?": { "<END>":1 } "a": { "banana":2 "chair":1 "table":1 } "and": { "a":2 } "banana": { "!":1 "?":1 } "chair": { "<END>":1 } "table": { "and":1 }The sequence of numbers 0 2 0 determines the (valid) walk consisting of the three tokens "a", "chair", and
END_TOKEN
as follows:
- The first 0 picks out "a" from among the
startTokens
. (Since "a" occurred with frequency 2, either 0 or 1 would yield "a".) - Next, the 2, picks out "chair" from the probability distribution over bigrams associated with "a" (0-1 map to "banana", 2 maps to "chair", and 3 maps to "table").
- Finally, the last 0 picks out
END_TOKEN
from the bigrams associated with "chair".
pick
in ProbabilityDistribution.pick(int)
for more details.-
Constructor Summary
ConstructorsConstructorDescriptionMarkovChainIterator
(ProbabilityDistribution<String> startTokens, Map<String, ProbabilityDistribution<String>> bigramFrequencies, NumberGenerator ng) Constructs an iterator that follows the path specified by the givenNumberGenerator
. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Constructor Details
-
MarkovChainIterator
MarkovChainIterator(ProbabilityDistribution<String> startTokens, Map<String, ProbabilityDistribution<String>> bigramFrequencies, NumberGenerator ng) Constructs an iterator that follows the path specified by the givenNumberGenerator
. The first token of the walk is chosen fromstartTokens
by picking from that distribution using ng's first number. If the number generator can not provide a valid start index, or if there are no start tokens, any exceptions should be caught and the constructed Iterator should be empty (i.e., hasNext is always false).- Parameters:
startTokens
- from the MarkovChain (assumed not null)bigramFrequencies
- from the MarkovChain (assumed not null)ng
- the number generator to use for this walk (assumed not null)
-
-
Method Details
-
hasNext
public boolean hasNext()This method determines whether there is a next token in the Markov Chain based on the current state of the walk. Remember that the end of a sentence is denoted by the tokenEND_TOKEN
.Your solution should be very short.
-
next
- Specified by:
next
in interfaceIterator<String>
- Returns:
- the next token in the MarkovChain's walk
- Throws:
NoSuchElementException
- if there are no more tokens on the walk through the chain (i.e. it has reachedEND_TOKEN
), or if the number generator provides an invalid choice (e.g, an illegal argument forpick
).
-