Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Programming I A Sentence Generator

Similar presentations


Presentation on theme: "Fundamentals of Programming I A Sentence Generator"— Presentation transcript:

1 Fundamentals of Programming I A Sentence Generator
Computer Science 111 Fundamentals of Programming I A Sentence Generator

2 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)

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

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

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

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

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

8 The Structure of a Sentence
noun phrase verb phrase article noun verb noun phrase prepositional phrase article noun the girl threw Similar to the behavior of strings so far Expand a noun phrase again

9 The Structure of a Sentence
noun phrase verb phrase article noun verb noun phrase prepositional phrase article noun the girl threw the pie Similar to the behavior of strings so far Pick an article and a noun at random

10 The Structure of a Sentence
noun phrase verb phrase article noun verb noun phrase prepositional phrase article noun preposition noun phrase the girl threw the pie Similar to the behavior of strings so far A prepositional phrase is a preposition followed by a noun phrase

11 The Structure of a Sentence
noun phrase verb phrase article noun verb noun phrase prepositional phrase article noun preposition noun phrase the girl threw the pie at Similar to the behavior of strings so far Pick a preposition at random

12 The Structure of a Sentence
noun phrase verb phrase article noun verb noun phrase prepositional phrase article noun preposition noun phrase article noun the girl threw the pie at Similar to the behavior of strings so far Expand another noun phrase

13 The Structure of a Sentence
noun phrase verb phrase article noun verb noun phrase prepositional phrase article noun preposition noun phrase article noun the girl threw the pie at a boy Similar to the behavior of strings so far More random words from the parts of speech

14 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)

15 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

16 Grammar Rules A sentence is a noun phrase followed by a verb phrase
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.

17 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

18 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

19 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

20 Continue reading Chapter 5 on dictionaries
For Monday Continue reading Chapter 5 on dictionaries


Download ppt "Fundamentals of Programming I A Sentence Generator"

Similar presentations


Ads by Google