2014-11-05 Lab + HW

posted Nov 5, 2014, 5:00 AM by Samuel Konstantinovich   [ updated Nov 6, 2014, 6:31 AM ]
GOAL: Making your own random sentence generator

Practice Problems (for people that want test prep)
DN1 Evaluate:
a. (length '((1)(2)(3)))
b. (length '(()()()))
c. (length '((1 2) 3))
d. (length '((1 2 3)))
e. (length '((1 2 (3))))
f. (length '((1 2 3) 4))

DN2 Write an expression using a sequence of car/cdr to extract 
the value 3 from each of the following 3 lists:
g. (define super '(1 2 3))
h. (define fighting '((1 2 (3 4)(5 6))))
i. (define robot '( (1 (2 (3) 4)))) 

Sentence Generator: (Weighted ad: 2 labs + 2 hw, due Friday 11:59pm HW server)
-We will ignore case for sentences for now.
-In order to do this you need the random function in Pretty Big/Swindle. NOT R5RS!

-You should have the most awesome sentence generator in class on Friday. Be prepared to run it a few times to show me your awesome sentences. 

You need to be able to write a basic sentence generator first, after that works you can make it more elaborate/poetic/creative:

Noun1 <negate> verb1 Noun2, noun2 verb noun2.
Guns don't kill people, people kill people. 
Toasters don't toast toast. Toast toast toast. 

Think for a second:
What are the basic parts of a sentence? 
What is the simplest sentence you can make?

We would ideally start with a simple sentence like:
noun verb
kitty plays
toast runs
tea steams

BETTER: add 'the' to the sentence: 
the toast runs

;1. Add your random words lists. They should be in the format:
;(define nouns '( kitty toast tea) )
;(define verbs '( runs plays steams) ) 

;2. Add your code for getNth, and getRandomTopLevelElement.
;rename getRandomTopLevelElement -> getOne
;YOU SHOULD HAVE getNth before continuing 
;YOU SHOULD HAVE getOne before continuing  

;3a. We want to be able to append two words together, so we don't just want the word we want the word inside a list. This can be accomplished with the list command.  Lets make two functions using getOne and the lists you have made:
;getNoun is a function that takes no parameters, that gives you a single random noun as a list:  
;(getNoun) -> (kitty)
;(getNoun) -> (toast)
;getVerb gives you a single random verb as a list:  
;(getVerb) -> (runs)
;(getVerb) -> (steams)
;YOU SHOULD HAVE getNoun and getVerb before continuing 
;BOTH functions should return a LIST, did you check the output?

;3b. If you do that then it is trivial to make a simple sentence that chooses a verb and a noun and adds them to the same list. This is because 
to join (kitty) and (runs)  we can just append:    (append '(kitty) '(runs) )  -> (kitty runs)

;Here is the simple sentence generating function:

(define (sentence)  (append (getNoun) (getVerb) )) 

;Every time you run it you should get randomized results depending on your list of words.

;(sentence) -> ( kitty runs )

;(sentence) -> ( kitty plays )
;(sentence) -> ( toast steams )

;Now make it include the word "the"

;If you finished up to this point, you can try to make better sentences by replacing getNoun and getVerb with new functions. We can replace a noun with a noun phrase, and replace a verb with a verb phrase. You can experiment on your own to do so. Try using adjectives and adverbs.

YOUR JOB IS TO MAKE IT BETTER: After your basic generator works, you should implement as many of these as you can. We can show off the best sentences at the end of class or on Monday. 
Your generator should not always give the same type of sentence:
Sometimes noun verb
Sometimes adjective noun verb
Somtimes adjective adjective noun verb

Now you can take a look at some ways of doing this:

1. We can choose between different options randomly by using  a series of random in the cond statement as  follows:
  ( (= 0 (random 3))  _SENTENCETYPE1_ )   ;replace sentencetype with a function or some code that makes a sentence
  ( (= 0 (random 2))  _SENTENCETYPE2_ )
  ( else                     _SENTENCETYPE3_ )

2: Noun Phrases: (include 'the' when appropriate)
"the dog"            noun
"the happy dog"  adjective+ noun  ;SEE BELOW FOR ADJECTIVE BLOCK
"Michael"            proper noun (no 'the')   ;SEE BELOW FOR PROPER NOUN NOTES
"the slow frustrated person" adjective+ adjective+ noun

3. Proper nouns can be one option in your noun phrase list: 
(You would need a separate list of proper nouns, and a function that gets one for you)
Loki defies the world wrestling federation.
Dave walks.
Thor smashes
The mighty Thor sits ;THIS IS HARD! If your adjective block is empty you don't put 'the' before the proper noun... implement features like this last!
Tony Stark quickly shoots
The Hulk gleefully plays (notice 'the hulk' is a proper noun, the 'the' is attached to the name...) ;THIS IS HARD! Multi word 'words' are special cases!
4. AdjectiveBlock: (0 or more adjectives to use in your noun Phrases)
Notice you can have any number of adjectives connected! 
You can define a function AdjectiveBlock that uses the cond to return a random number of ajectives. 
(AdjectiveBlock) -> (slow tall)
(AdjectiveBlock) -> (fast silly furry)
(AdjectiveBlock) -> (fishy)
(AdjectiveBlock) -> ()
5. AdverbVerbBlock:   (0 or more adverbs + verb) 
it could return:
(quickly runs)
(slowly silently creeps)
(silently efficiently screams)

6. Some verbs require another noun.

Joe runs  //noun verb

Joe takes the ball  //noun verb noun2
This is because "take" is a transitive verb, it requires a target.

Make a 2nd list of transitiveVerbs that requires a 2nd noun/nounphrase. You can randomly select:
-Choose an intransitive verb like we just did before (the normal verb list)
-Choose a transitive verb then choose a noun phrase:
Thor pokes Loki  ;transitive verb + proper noun
The dog bites the little chair. ;transitive verb + noun phrase


Do Now Solutions:
a. 3
c. 2
d. 1
e. 1
f. 2
g. (caddr super)
h. (caaddr (car fighting))
i. (caadar(cdar robot))

Functions you should have written:

(define (getNth L n)
  (if (= n 0) (car L) (getNth (cdr L) (- n 1))))

(define (getOne L)
  (getNth L (random (length L))))