public class MarkovChar
extends java.lang.Object
implements java.io.Serializable
analyze(CharSequence)
once on a large sample text, then you can
call chain(long)
many times to get odd-sounding "remixes" of the sample text. This is meant to allow easy
serialization of the necessary data to call chain(); if you can store the chars
and processed
arrays in some serialized form, then you can reassign them to the same fields to avoid calling analyze(). One way to
do this conveniently is to use serializeToString()
after calling analyze() once and to save the resulting
String; then, rather than calling analyze() again on future runs, you would call
deserializeFromString(String)
to create the MarkovText without needing any repeated analysis.
Modifier and Type | Field and Description |
---|---|
char[] |
chars
All chars (case-sensitive and only counting chars that are letters in Unicode) that this encountered during the
latest call to
analyze(CharSequence) . |
IntIntOrderedMap |
pairs
Map of all pairs of chars encountered to the position in the order they were encountered.
|
int[][] |
processed
Complicated data that mixes probabilities of chars using their indices in
chars and the indices of char
pairs in pairs , generated during the latest call to analyze(CharSequence) . |
Constructor and Description |
---|
MarkovChar() |
Modifier and Type | Method and Description |
---|---|
void |
analyze(java.lang.CharSequence corpus)
This is the main necessary step before using a MarkovText; you must call this method at some point before you can
call any other methods.
|
java.lang.String |
chain(long seed)
Generate a roughly-sentence-sized piece of text based on the previously analyzed corpus text (using
analyze(CharSequence) ) that terminates when stop punctuation is used (".", "!", "?", or "..."), or once
the length would be greater than 200 characters without encountering stop punctuation(it terminates such a
sentence with "." or "..."). |
java.lang.String |
chain(long seed,
int maxLength)
Generate a roughly-sentence-sized piece of text based on the previously analyzed corpus text (using
analyze(CharSequence) ) that terminates when stop punctuation is used (".", "!", "?", or "...") or once
the maxLength would be exceeded by any other words (it terminates such a sentence with "." or "..."). |
MarkovChar |
copy()
|
static MarkovChar |
deserializeFromString(java.lang.String data)
Recreates an already-analyzed MarkovText given a String produced by
serializeToString() . |
java.lang.String |
serializeToString()
Returns a representation of this MarkovText as a String; use
deserializeFromString(String) to get a
MarkovText back from this String. |
public char[] chars
analyze(CharSequence)
. Will be null if analyze(CharSequence)
was never called.public IntIntOrderedMap pairs
chars
indices placed into the most-significant bits for the first word and the
least-significant bits for the second word. The size of this IntIntOrderedMap is likely to be larger than the
char array chars
, but should be equal to processed.length
. Will be null if
analyze(CharSequence)
was never called.public int[][] processed
chars
and the indices of char
pairs in pairs
, generated during the latest call to analyze(CharSequence)
. This is a jagged 2D
array. Will be null if analyze(CharSequence)
was never called.public void analyze(java.lang.CharSequence corpus)
chars
and processed
, which allows other
methods to be called (they will throw a NullPointerException
if analyze() hasn't been called).corpus
- a typically-large sample text in the style that should be mimickedpublic java.lang.String chain(long seed)
analyze(CharSequence)
) that terminates when stop punctuation is used (".", "!", "?", or "..."), or once
the length would be greater than 200 characters without encountering stop punctuation(it terminates such a
sentence with "." or "...").seed
- the seed for the random decisions this makes, as a long; any long can be usedpublic java.lang.String chain(long seed, int maxLength)
analyze(CharSequence)
) that terminates when stop punctuation is used (".", "!", "?", or "...") or once
the maxLength would be exceeded by any other words (it terminates such a sentence with "." or "...").seed
- the seed for the random decisions this makes, as a long; any long can be usedmaxLength
- the maximum length for the generated String, in number of characterspublic java.lang.String serializeToString()
deserializeFromString(String)
to get a
MarkovText back from this String. The chars
and processed
fields must have been given values by
either direct assignment, calling analyze(CharSequence)
, or building this MarkovTest with the
aforementioned deserializeToString method. Uses spaces to separate words and a tab to separate the two fields.public static MarkovChar deserializeFromString(java.lang.String data)
serializeToString()
.data
- a String returned by serializeToString()
chain(long)
public MarkovChar copy()
chars
and the 2D jagged int array processed
into a new MarkovText.
None of the arrays will be equivalent references, but the Strings (being immutable) will be the same objects in
both MarkovText instances.Copyright © Eben Howard 2012–2022. All rights reserved.