This program simulates the familiar attempt by a chimpanzee to type one of the works of Shakespeare. It illustrates a number of aspects of the use of the predefined library for string handling and generating random numbers.
The idea is that the chimp types away basically at random. However, the magic word processor knows of certain characteristics of English text such as the frequency of letters.
Various checks ensure that only certain oneand two-letter words are possible; and of course certain four-letter words are forbidden. Words are also rejected if they start with an unacceptable pattern, have no vowels or have no consonants.
The text is processed as a series of unjustified lines of words with parameters such as a maximum line length of 70 characters. The text is structured into sentences and paragraphs. The stop is the only punctuation mark.
After a set number of lines have been typed, the program searches for names that might occur in a play such as Hamlet, Lear, Puck and so on. If successful it types a friendly message for the chimp and then prints the story, otherwise it types a less friendly message and tries again.
Characters are drawn from a conceptual infinite bag containing characters in the correct proportion. Word, sentence and paragraph length are given by rolling an associated pair of conceptual dice as required. After a word is completed it is checked for feasibility. If accepted, it is added to the story.
The program has two main packages, Bag which contains the notional stock of characters, which are accessed by calling the procedure Draw, and Story which contains the text being created and also the constant Max_Lines.
Other packages are Lines which contains the instantiation for bounded string handling and Rules which contains information regarding word formation such as the letter frequencies and a list of forbidden words.
with Ada.Strings.Bounded;
use Ada.Strings.Bounded;
package Lines is
Line_Length: constant := 70;
package Bounded_Lines is
new Generic_Bounded_Length(Line_Length);
use Bounded_Lines;
subtype B_String is Bounded_String;
type BS_Array is
array (Positive range) of B_String;
function “+” (S: String) return B_String;
end;
package body Lines is
function “+” (S: String) return B_String is
begin
return To_Bounded_String(S);
end “+“;
end Lines;
To save this book to your Kindle, first ensure [email protected] is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.