10/23/2014

Using Python to generate humorous memos

The Python programming language lets you generate humorous memos automatically. This document includes an example Python 3 program that generates random humorous memos. This code includes word lists (arrays), function definitions, and nested while loops. The word lists contain various verbs, adverbs, objects, adjectives and subjects for use in sentences. The program code defines functions that select random words to generate sentences. The nested while loops generate paragraphs and sentences. 

Python is free to download, install, and use. It's easy to program and delightful to run. Before trying the example program in this document, you need to install Python 3 on your computer. For more information, see Getting started with Python programmingFor information about random functions, see Using Python random functions to create new words.

Example Python program for automatic memos

Figures 1 through 3 show example Python code that can generate automatic memos. To run this code on your computer, do the following:
  1. Install Python 3 if you've not already done so.
  2. Run the interface development environment (IDE) for your Python 3.
  3. Copy AutomaticWriter and then paste it into an appropriate Python 3 window.
  4. Run the program and, at the prompt, type the number of paragraphs you want in your memo, such as 4.
  5. Press Enter to generate and display a random memo with a random title, such as 'Radically Downsize Plaintiffs."
  6. Repeat steps 4 and 5 until the program generates a sufficiently humorous memo. You can save your memo by copying its text to a word processor.
  7. Experiment with this program by modifying your copy of it. For example, you can add words and phrases most appropriate to your friends. 

Word lists 

The Python lists in Figure 1 contain the possible words and phrases that can appear in a memo generated by this Python program. The name of each list describes the grammatical function of its items:
  • ucverb -- upper-case verbs, such as Exploit or Ridicule
  • lcverb -- lower-case verbs, such as exploit or ridicule
  • ucadvrb -- upper-case adverbs, such as Carefully or Instantly
  • lcadvrb -- lower-case adverbs, such as carefully or instantly 
  • objct -- objects, such a customers or safety inspectors
  • adjctv -- adjectives, such as uninspired or repugnant
  • sbjct -- subjects, such as We or Human resources
  • auxlry -- auxiliaries, such as should always or must
  • ttlobj -- title objects, such as Regulators or Wall Street Brokers
Figure 1 - These lists contain words and phrases as verbs, adverbs, objects,
adjectives, subjects, auxiliaries, and title objects. When the the Python code in
Figure 3 calls the 
title () function, it randomly chooses title words,
concatenates them, and then returns the title to the function caller.
The 
sntnc01 () function similarly defines sentence type 01.

Function definitions

The functions defined in Figures 1 and 2 make random.choice () function calls to return random words or phrases for the title and six sentence types. For example, the title () function is:
def title (ucadverb,ucverb,ttlobj):
    a = random.choice (ucadvrb)
    b = 
random.choice (ucverb)
    c = random.choice (ttlobj)
    r = a + ' ' + b + ' ' + c
    return
(r)

When the code in Figure 3 calls the title () function, it makes three random.choice () function calls that select:
  • One upper-case adverb from the ucadvrb list
  • One verb from the ucverb list
  • One title object from the ttlobj list
The title () function then concatenates these words and returns them as the title of the automatic memo. Likewise, when the code in Figure 3 calls one of the sentence-type functions below, it returns an appropriate sentence:
  • The sntnc01 () function makes three random.choice () function calls that select three words for sentence type 01, concatenates those words, and then returns them as a sentence type 01 within the automatic memo. Note: Figure 1 includes the sntnc01 () function. Figure 2 includes the sntnc02 () through sntnc06 () functions.
  • The sntnc02 () function makes three random.choice () function calls that select three words for sentence type 02, concatenates those words, and then returns them as a sentence type 02 in the automatic memo.
  • The sntnc03 () function selects, concatenates and returns four words for sentence type 03.
  • The sntnc04 () function selects, concatenates and returns five words for sentence type 04.
  • The sntnc05 () function selects, concatenates and returns four words for sentence type 05.
  • The sntnc06 () function selects, concatenates and returns five words for sentence type 06.
Figure 2 - These functions define sentence types 02 through 06. [Note: Figure 1 includes the sntnc01 () function.] When the Python code in Figure 3 calls any sentence-type function, sntnc01 () through sntnc06 (), it randomly chooses sentence words or phrases, concatenates them, and then returns them as a sentence.

Nested while loops

The Python code in Figure 3 generates a random memo as follows:
  1. The title () function generates a random title.
  2. The outer while loop generates a user-specified number of paragraphs. Each pass through the outer (paragraph) loop:
    1. Calls the random.randint () function to specify between three to nine sentences as the paragraph length. 
    2. The inner loop generates sentences. Each pass through the inner (sentence) loop:
      1. Calls the the range (1,6) function to set the switch value to 1 to 6.
      2. Calls the random.choice (switch) function to call a sentence-type function,  sntnc01 () to sntnc06 (). The sentence-type function generates its random sentence.
      3. Concatenates the sentence to the paragraph.
    3. Calls the print () function to display the paragraph.
  3. Various print () functions display ending text, such as corporate guidance and manager names.
Figure 3 - These nested while loops create paragraphs and sentences. The user specifies the number of paragraphs; the outer while loop runs for each paragraph. The inner while loop calls 3 to 9 sentence-definition functions for each paragraph.

Example reformatted memo

If you wish to format any random memo generated by your Python 3 program, simply copy its text and then paste it into a word processor document as shown in Figure 4.
Figure 4 - This automatic memo was copied to a  word-processor
document, and then exported as a PDF.

No comments :

Post a Comment