Creating a Chat Bot

Материал из Поле цифровой дидактики
Версия от 11:33, 21 июля 2022; Patarakin (обсуждение | вклад) (1 версия импортирована)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)

Шаблон:April Fools A chat bot is a program which allows mock communication with the computer or application. This can be developed by solely using Scratch; it involves many lists, operators, and the askandwait block in particular. This tutorial shows how to make one.

Brief Description

To create a chat bot, the "ask" block needs to be used to enter a message. Then, the project takes that message, breaks it apart into words, and scans the list of words for specific words. Then, if the message contains those specific words, the chat bot can respond a pre-set message. To break apart the answer variable into words, each letter must be repetitively added to a list until a space is reached, and then create a new item in the list and continue adding the letters, forming words. To program this, only one list will be needed:

  • words

and one or two variables:

  • letter#
  • pick

Шаблон:Note

Programming the Bot

The following code replication can be used to make the bot respond to messages. All these scripts can go in any sprite.

whenclickedforeverasktypeamessageandwaitsetletter#to1sotheiterationbeginswiththefirstletteroftheanswerdeleteallofwordsclearsthelistofwords;thefirststepisseparatingthewordsintoalistinsertat1ofwordsitneedsablankitemtostartoutrepeatlengthofansweronerepetitionforeachletterifletterletter#ofanswer=thennotethataspaceisinsertedintothisstring,notnothinginsertat1ofwordssinceitisaspace,createanewitemelsereplaceitem1ofwordswithjoinitem1ofwordsletterletter#ofanswerotherwise,addthelettertothecurrentwordendchangeletter#by1moveontothenextletterendifwordscontainshellothenworddetectionsayHellotoyou,too!for2secsresponseendifwordscontainslikethensetpicktopickrandom1to2foruseinalternateresponsesifpick=1thensayIlikethat,too!for2secselsesayReally?Idon'tlikethattoomuch.for2secsendendend

For example, if "I like Scratch" is entered, the bot may respond "I like that too!" or "Really? I don't like that too much" because the list of words contained "like". The script can be made more complex by breaking it down into individual likes and more responses. The "pick" variable is used solely for making it respond one of multiple possible messages. However, if for instance, "I like Scratch" is typed in, the bot may respond "I like that too!", but if it is typed in again, it might say "Really? I don't like that too much.". To prevent this, some lists can be made to store words that the bot already responded to.

Make two lists:

  • Likes
  • Dislikes

Then change this part of the script:

ifwordscontainslikethensetpicktopickrandom1to2foruseinalternateresponsesifpick=1thenadditemjoinlastofwordstoLikeselseadditemjoinlastofwordstoDislikesendifpick=1thenifnotLikescontainsitemlastofwordsthensayIlikethat,too!for2secselsedeletelastofLikessayDidn'tIalreadytellyouIlikedit?for2secsendelseifnotDislikescontainsitemlastofwordsthensayReally?Idon'tlikeittoomuch.for2secselsedeletejoinlastofDislikessayDidn'tIalreadytellyouIdidn'tlikeit?for2secs


Шаблон:Note

Final Product

Excluding scripts in the next section, the whole script would be something like this:

whenclickedforeverasktypeamessageandwaitsetletter#to1sotheiterationbeginswiththefirstletteroftheanswerdeleteallofwordsclearsthelistofwords;thefirststepisseparatingthewordsintoalistinsertat1ofwordsitneedsablankitemtostartoutrepeatlengthofansweronerepetitionforeachletterifletterletter#ofanswer=thennotethataspaceisinsertedintothisstring,notnothinginsertat1ofwordssinceitisaspace,createanewitemelsereplaceitem1ofwordswithjoinitem1ofwordsletterletter#ofanswerotherwise,addthelettertothecurrentwordendchangeletter#by1moveontothenextletterendifwordscontainshellothenworddetectionsayHellotoyou,too!for2secsresponseendifwordscontainslikethensetpicktopickrandom1to2foruseinalternateresponsesifpick=1thenadditemjoinlastofwordstoLikeselseadditemjoinlastofwordstoDislikesendifpick=1thenifnotLikescontainsitemjoinlastofwordsthensayIlikethat,too!for2secselsedeletelastofLikessayDidn'tIalreadytellyouIlikedit?for2secsendelseifnotDislikescontainsitemjoinlastofwordsthensayReally?Idon'tlikeittoomuch.for2secselsedeletejoinlastofDislikessayDidn'tIalreadytellyouIdidn'tlikeit?for2secs

Taking it Further

To make the chat bot more realistic, a separate list of previously stored words can be created from old entered messages, and make the bot refer to those later in the conversation. For example, if it is told that the user likes programming, and then is later told that the user likes Scratch, it can relate the two using the list of old words and say "Of course you like Scratch, you like programming" by sensing if the list of old words contains "programming".

The script could also be changed to make the bot understand even more words, using this:

ifwordscontainsWordyouwouldlikebottounderstandthenworddetectionsayResponseyouwouldlikeyourbottosayfor2secsresponseend

See Also