LearnBuild toward a chatbotLooking back

Looking back

Aim. At the end of this lesson your program looks back at four earlier word pieces, decides how much each one counts, and mixes what they have to say into one number.

Where the lessons are going

The sum so far was given its four inputs. In a model they come from the text before: here the words of "The capital of France is", cut into word pieces, which are words or parts of words. To go on, the model must look back at those pieces.

Decide which piece counts

Try it: four earlier word pieces have a score each, and each says one number. Move the scores until " France" is the only piece that counts: its count 15, the other three 0:

What the pieces say, mixed by count
The same, from the brainfuck program

Figure 1. Four earlier word pieces with a score each. The counts are read from the tape of a brainfuck program. (interactive)

A list of ready-made answers

Try it: a piece that is far enough behind the best gets count 0. Find the smallest distance at which that happens:

Figure 2. The list of ready-made counts. The marked bar is the entry your distance picks. (interactive)

Watch the program walk past the pieces

Try it: choose a fast speed and press Play, or go to the last step. The pointer passes the four pieces twice:

It uses the scores you set in step 1. The program is characters long, so only the part around the lit-up command is shown.

Figure 3. The attention program. The strip on top shows which word piece the pointer is at. (interactive)

What you can do now

Look back along the tape, weigh the earlier word pieces and mix them. The numbers that go into the sum now come from the text itself.

One part is left. The model has a score for every word piece it knows, and the highest must win. Brainfuck cannot compare. What then?

How the real program differs

The cost of a lookup. In the chat program, looking up one of 256 values takes about 68 steps with the peel (step 3 of lesson 5), against about 500 by counting down.

Where the scores come from. Here you set them. In the chat program each score is itself a sum of products between the current word piece and an earlier one. The bits of the current piece are peeled once and used for all the earlier pieces (the note "A bonus" of lesson 6).

A difference with the original. The original model never cuts a piece off completely; it lets tiny counts through. This is one of the places where the whole-number model differs a little. The chat model uses a finer table than this lesson: 256 entries, and the best piece gets count 255.

What a piece "says" is in reality a whole row of numbers, not one. The same counts are used for every number of the row.