Section 1: Generating Sentences

We have generated language in several ways in this class already.

One way was with the simple language generator. We typed in a model (a sentence pattern) and a dictionary of words by parts.

../_images/SentenceGenerator.png

We have also done sentence generation in Snap. We have a language generation project that lets us generate sentences by parts, just like in the simple language generator.

../_images/picking-words-from-variables.png

The Python Version V1

The below program does the same thing in Python. Click ‘Run’ to generate a random sentence.

Try answering these questions about the code above.

The Python Version V2

The below program does the same thing in Python. Click ‘Run’ to generate a random sentence.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

Try answering these questions about the code above.

Section 2: Creating a little Python chatbot

You have built Chatbots in both Snap! and Charla-bots. Here’s an example on a little one:

../_images/Snap-kinda-chatbot.png

Here is a (very) little Python chatbot. This one is a little more sophisticated than our Snap chatbot – it can pick out a name from an input sentence, and it can do the equivalent of respond randomly that we saw in Charla-bot.

Python here in a Runestone ebook can’t receive user input, so let’s just change the inputSentence variable to represent what the user says. Press Run to see what the chat bot says.

This code is recognizing words in the inputSentence then deciding what to print in response.

You have attempted 1 of 14 activities on this page