liveBook · Manning

Multiword state

As explained in Fabulous Adventures in Data Structures and Algorithms

A multiword state is a Markov-model state composed of two or more consecutive words to condition the next-word distribution on additional context.

A multiword state makes two or more consecutive words the current state of a Markov text generator. Conditioning on this larger context gives the next-word distribution more information than a single-word state. section 16.2

The technique addresses implausible sentences caused by memoryless generation. Instead of using all words following his, a generator can use the narrower distribution following phrases such as of his or of France. section 16.2

Increasing the state size creates a clear trade-off:

  • Pairs or triples preserve more local context and can make word choices more plausible. section 16.2
  • Using an entire session as the state supplies still more context, but repeatedly analyzing longer text can make generation quadratic in document length. section 16.2

A narrower next-word distribution

When the generated ending is of his, prefer words observed after of his rather than words observed after his alone. section 16.2

Listing 16.2 A simple words-and-sentences parser
public static class Extensions
{
    static readonly char[] punct = "*()[]#:;_\"".ToCharArray();
    public static IEnumerable<List<string>> Sentences(
        this IEnumerable<string> lines)
    {
        List<string> current = [];
        foreach (var line in lines)
        {
            foreach (var word in line.Split())
            {
                var trimmed = word.Trim(punct);
                if (trimmed.Length > 0)
                {
                    current.Add(trimmed);
                    char c = trimmed[trimmed.Length - 1];
                    if (c == '.' || c == '!' || c == '?')
                    { 
                        yield return current;
                        current = [];
                    }
                }
            }
        }
    }
    public static string Space<T>(this IEnumerable<T> ts) => 
        string.Join(" ", ts);
    [...]
}

Inspect the sentence parser used to turn source text into ordered word sequences before constructing Markov states. section 16.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines pairs and triples, explains their effect, and contrasts them with whole-session context and its quadratic cost.
Examples
Uses of his, of France, and of the to show how phrase-specific distributions narrow next-word choices.
What it assumes
Assumes familiarity with Markov processes and next-state distributions, while contrasting them with memoryless generation.
What you can do after
Evaluate how increasing Markov state context affects plausibility, information, and generation cost.
How it connects
Connects Markov processes with conditional distributions, sentence generation, and language models.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.