LIMERICKS

Although it will be a long time before computers can generate sentences of their own accord, it is possible to program a computer to generate sentences that will pass as meaningful provided we restrict ourselves to a small number of possibilities. To illustrate, the following light-hearted program will construct limericks according to a set of fairly simple rules. Nevertheless, the results are sometimes surprising, and at worst amusing.

Some examples produced by the program are given below:

A GRACEFUL BLAND GROCER FROM KINGS
ONCE DEMOLISHED SOME CAKES AND GREW WINGS
HE DEMOLISHED SO LATE
THAT HE LOOKED FOR A PLATE
THIS GRACEFUL BLAND GROCER OF KINGS
A VICIOUS YOUNG LAUNDRESS FROM SPAIN
ONCE WANTED SOME CAKES ON A TRAIN
SHE WANTED SO SLOW
THAT SHE WANTED SOME DOUGH
THIS VICIOUS YOUNG LAUNDRESS OF SPAIN

Each word or phrase in the limerick is selected, at random, from six alternatives, each of which has the same number of syllables so that the final limerick will scan correctly. The structure of the limerick is defined as follows, whare lower-case words in angled brackets, such as <adjective>, are to be replaced by the actual words selected at random by the computer:

A <adjective w> <adjective x> <noun y> FROM <place z>
Once <verb g> <noun> <qualifier z>
<pronoun y> <verb g> SO <adverb t>
THAT <pronoun y> <verb> <noun t>
THIS <adjective w> <adjective x> <noun y> OF <place z>

Parts of speech, such as <noun>, are replaced by a word or phrase selected at random from six possibilities. Where two parts of speech are labelled with a letter, as in <noun y> and <pronoun y>, the two words are chosen as a pair; for example, if <noun y> were "LAUNDRESS", <pronoun y> would be "SHE". Similarly, the <qualifier z> in line 2 is chosen to rhyme with <place z> in lines 1 and 5; for example, if <place z> is chosen as "FROM KINGS", the second line must end with "AND GREW WINGS". Similarly the <noun t> in line 4 is chosen to rhyme with the <adverb t> in line 3. These simple constraints are sufficient to : ensure that the random limericks will rhyme and scan.


BBC Computer Version

For the BBC Computer version the program uses mode 5, available on the model A, which gives 4 colours at a resolution of 160x256. Integer variables are used, for maximum speed:

10 REM ... Limericks

First line of limerick.

.20 P=1000:PRINT "A ";
30 PROCRND:W=R:PROCRND:X=R:PROCRND:Y=R
40 PRINT "FROM ";
50 PROCRND:Z=R:PRINT

Second line.

60 PRINT "ONCE ";:PROCRND:G$=C$
70 PROCRND:R=Z:PROCWORD:PRINT

Third and fourth lines.

80 R=Y:PROCWORD:H$=C$:PRINTG$;"SO ";
90 PROCRND:T=R:PRINT'"THAT "'H$;:PROCRND:R=T:PROCWORD: PRINT

Last line.

110 PRINT"THIS ";:P=1000:R=W:PROCWORD: R=X:PROCWORD: R=Y:PROCWORD
120 PRINT"OF ";:R=Z:PROCWORD:PRINT"."
140 END

PROCRND - Choose random phrase.

200 DEF PROCRND:R=ABSRND MOD 6:PROCWORD:ENDPROC

PROCWORD - Select Rth word in $C and print it.

220 DEF PROCWORD:RESTORE P
230 FOR N=0 TO R:READ C$:NEXT:C$=C$+"" 
250 PRINTC$;:P=P+100:ENDPROC

Strings of phrases.

1000 DATASORDID,GRACEFUL,WILY,VICIOUS,SPARKLING,REALLY
1100 DATAGREEN,YOUNG,VILE,BLAND,OLD,WILD
1200 DATADUCHESS,GROCER,GLUTTON,FLAUTIST,LAUNDRESS, SAILOR
1300 DATAWEMBLEY,SPAIN,CHAD,SPEKE,KINGS,FRANCE
1400 DATAWANTED,FOLLOWED,COUNTED,DEMOLISHED, COLLECTED,SWALLOWED
1500 DATASOME STAMPS,A STOAT,A NUDE,SOME CAKES, A FROG,SOME MOULD
1600 DATAAND FELT TREMBLY,ON A TRAIN,AND WENT MAD, TWICE A WEEK,AND GREW WINGS,IN A TRANCE
1700 DATASHE,HE,SHE,HE,SHE,HE
1800 DATAQUICK,SLOW,FEW,HARD,LATE,LONG
1900 DATANOTICED,FOLLOWED,ASKED FOR,LOOKED FOR, WANTED,LONGED FOR
2000 DATAA BRICK,SOME DOUGH,A SCREW,SOME LARD,A PLATE,KING KONG

Variables:

G - String containing verb used in line 2
H - String containing HE/SHE
P - Pointer to next selection of phrases
R - Random number 0 to 4
S - String of phrase options
T - Word selected in second line
W,X,Y,Z - Words selected in first line

Atom Version

The Atom version uses a string, $S, to store the six alternatives for a particular phrase. This string is set to the list of alternatives by a GOSUB to a line which assigns the string to $S. Note that where the string will not fit onto one line the second half is concatenated onto the end of $S by executing:

$S+LENS="string"

as on lines 2000 and 2001.

For every point on the screen plot a point whose colour depends on the function.

10 REM ... LIMERICKS ...l5 DIM S(100),G(32),H(32)

First line of limerick.

20 P=1000;PRINT "A "
30 GOSUB s;W=R;GOSUB s;X=R;GOSUB s;Y=R
40 PRINT "FROM "
50 GOSUB s;Z=R;PRINT

Second line.

60 PRINT "ONCE ";GOSUB s;$G=$C
70 GOSUB s;R=Z;GOSUB t;PRINT

Third and fourth ines.

80 R=Y;GOSUB t;$H=$C;PRINT $G,"SO"
90 GOSUB s;T=R;PRINT '"THAT ",$H;GOSUB s;R=T; GOSUB t; PRINT

.Last line.

110 PRINT "THIS ";P=1000;R=W;GOSUB t;R=X;GOSUB tj R=Y;GOSUB t
120 PRINT "OF ";R=Z;GOSUB t;PRINT "."'
140 END

s - Make a random choice by extracting a substring from within the string $S.

200sR=ABSRND%6

t - Select the substring corresponding to the value of R. Scan past R commas, put the string from there to the next comma, or 'return’, in $C, and print it.

210tGOSUB (P);A=0;IF R=0 G.u
220 FOR N=l TO R;GOSUB c;NEXT;A=A+1
240uC=A+S;GOSUB c;$S+A=""
250 PRINT $C;P=P+100; RETURN

c - Search for comma or end of string.

300cDO A=A+1;UNTIL S?A=CH","OR S?A=CH""; RETURN

Strings of phrases.

1000 REM WORDS
1001 $S="SORDID,GRACEFUL,WILY,VICIOUS,SPARKLING, REALLY"; RETURN
1100 $S="GREEN,YOUNG,VILE,BLAND,OLD,WILD", RETURN
1200 $S="DUCHESS,GROCER,GLUTTON,FLAUTIST,LAUNDRESS,SAILOR"; RETURN
1300 $S="WEMBLEY,SPAIN,CHAD,SPEKE,KINGS,FRANCE"; RETURN
1400 $S="WANTED,FOLLOWED,COUNTED,"
1401 $S+LENS="DEMOLISHED,COLLECTED,SWALLOWED"; RETURN
1500 $S="SOME STAMPS,A STOAT,A NUDE,"
1501 $S+LENS="SOME CAKES,A FROG,SOME MOULD"; RETURN
1600 $S="AND FELT TREMBLY,ON A TRAIN,AND WENT MAD,"
1601 $S+LENS="TWICE A WEEK,AND GREW WINGS,IN A TRANCE"; RETURN
1700 $S="SHE,HE,SHE,HE,SHE,HE"; RETURN
1800 $S="QUICK,SLOW,FEW,HARD,LATE,LONG"; RETURN
1900 $S="NOTICED,FOLLOWED,ASKED FOR,"
1901 $S+LENS="LOOKED FOR,WANTED,LONGED FOR”; RETURN
2000 $S="A BRICK,SOME DOUGH/A SCREW,"
2001 $S+LENS="SOME LARD,A PLATE,KING KONG", RETURN

Variables:

A - Pointer to find commas
G - String containing verb used in line 2
H - String containing HE/SHE
P - Pointer to next selection of phrases
R - Random number 0 to 4
S - String of phrase options
T - Word selected in second line.
W,X,Y,Z - Words selected in first line.