Computer Science 111 Fundamentals of Programming I A




















- Slides: 20

Computer Science 111 Fundamentals of Programming I A Sentence Generator

Application: Generate Sentences • Given a vocabulary and grammar rules, one can generate some random and perhaps rather silly sentences • Vocabulary - the set of words belonging to the parts of speech (nouns, verbs, articles, prepositions) • Grammar - the set of rules for building phrases in a sentence (noun phrase, verb phrase, prepositional phrase)

The Structure of a Sentence sentence noun phrase verb phrase A sentence is a noun phrase followed by a verb phrase

The Structure of a Sentence sentence noun phrase article verb phrase noun A noun phrase is an article followed by a noun

The Structure of a Sentence sentence noun phrase article the verb phrase noun girl Similar to the behavior of strings so far Pick actual words for those parts of speech at random

The Structure of a Sentence sentence noun phrase article the noun verb phrase verb girl Similar noun phrase prepositional phrase to the behavior of strings so far A verb phrase is a verb followed by a noun phrase and a prepositional phrase

The Structure of a Sentence sentence noun phrase article the noun verb phrase verb noun phrase girl Similarthrew to the prepositional phrase behavior of strings so far Pick a verb at random

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article the girl Similarthrew to the prepositional phrase noun behavior of strings so far Expand a noun phrase again

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article the prepositional phrase noun girl the pieof Similarthrew to the behavior strings so far Pick an article and a noun at random

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article the noun girl the pieof Similarthrew to the behavior prepositional phrase preposition noun phrase strings so far A prepositional phrase is a preposition followed by a noun phrase

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article the noun girl the pieof Similarthrew to the behavior Pick a preposition at random prepositional phrase preposition stringsatso far noun phrase

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article noun prepositional phrase preposition noun phrase article the girl the pieof Similarthrew to the behavior Expand another noun phrase stringsatso far noun

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article noun prepositional phrase preposition noun phrase article the girl the pieof Similarthrew to the behavior stringsatso far More random words from the parts of speech a noun boy

Representing the Vocabulary nouns = ['bat', 'boy', 'girl', 'dog', 'cat', 'chair', 'fence', 'table', 'computer', 'cake', 'field’, 'pie'] verbs = ['hit', 'threw', 'pushed', 'ate', 'dragged', 'jumped'] prepositions = ['with', 'to', 'from', 'on', 'below', 'above', 'beside', 'at'] articles = ['a', 'the'] Use a list of words for each part of speech (lexical category)

Picking a Word at Random nouns = ['bat', 'boy', 'girl', 'dog', 'cat', 'chair', 'fence', 'table', 'computer', 'cake', 'field’, 'pie'] verbs = ['hit', 'threw', 'pushed', 'ate', 'dragged', 'jumped'] prepositions = ['with', 'to', 'from', 'on', 'below', 'above', 'beside', 'at'] articles = ['a', 'the'] import random print(random. choice(verbs)) # Prints a randomly chosen verb The random module includes functions to select numbers, sequence elements, etc. , at random

Grammar Rules sentence = nounphrase verbphrase nounphrase = article noun verbphrase = verb nounphrase prepositionalphrase preopositonalphrase = preposition nounphrase A sentence is a noun phrase followed by a verb phrase Etc. , etc.

Define a Function for Each Rule # sentence = nounphrase verbphrase def sentence(): return nounphrase() + ' ' + verbphrase() Each function builds and returns a string that is an instance of the phrase Separate phrases and words with a space

Define a Function for Each Rule # sentence = nounphrase verbphrase def sentence(): return nounphrase() + verbphrase() # nounphrase = article noun def nounphrase(): return random. choice(articles) + ' ' + random. choice(nouns) When a part of speech is reached, select an instance at random from the relevant list

Call sentence() to Try It Out # sentence = nounphrase verbphrase def sentence(): return nounphrase() + verbphrase() # nounphrase = article noun def nounphrase(): return random. choice(articles) + ' ' + random. choice(nouns) … for x in range(10): print(sentence()) # Display 10 sentences You can also generate examples of the other phrases by calling their functions

For Monday Continue reading Chapter 5 on dictionaries