Summary
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.
- Type
- Chapter
- Information
- Programming in Ada 2012 , pp. 695 - 698Publisher: Cambridge University PressPrint publication year: 2014