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 ask [] and wait 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.

when gf clicked
forever
ask [type a message] and wait
set [letter # v] to [1] //so the iteration begins with the first letter of the answer
delete all of [words v] //clears the list of words; the first step is separating the words into a list
insert [] at (1 v) of [words v] //it needs a blank item to start out
repeat (length of (answer)) //one repetition for each letter
if <(letter (letter #) of (answer)) = [ ]> then //note that a space is inserted into this string, not nothing
insert [] at (1 v) of [words v] //since it is a space, create a new item
else
replace item (1 v) of [words v] with (join (item (1 v) of [words v]) (letter (letter #) of (answer))) //otherwise, add the letter to the current word
end
change [letter # v] by [1] //move on to the next letter
end
if <[words v] contains [hello]> then //word detection
say [Hello to you, too!] for (2) secs//response
end
if <[words v] contains [like]> then
set  [pick v] to (pick random (1) to (2)) //for use in alternate responses
if <(pick) = [1]> then
say [I like that, too!] for (2) secs
else
say [Really? I don't like that too much.] for (2) secs
end
end
end

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:

if <[words v] contains [like]> then
set  [pick v] to (pick random (1) to (2)) //for use in alternate responses
if <(pick) = [1]> then
add (item (join (last) ()) of [words v]) to [Likes v]
else
add (item (join (last) ()) of [words v]) to [Dislikes v]
end
if <(pick) = [1]> then
if <not <[Likes v] contains (item (last v) of [words v])>> then
say [I like that, too!] for (2) secs
else
delete (last v) of [Likes v]
say [Didn't I already tell you I liked it?] for (2) secs
end
else
if <not <[Dislikes v] contains (item (last v) of [words v])>> then
say [Really? I don't like it too much.] for (2) secs
else
delete (join (last) ()) of [Dislikes v]
say [Didn't I already tell you I didn't like it?] for (2) secs


Шаблон:Note

Final Product

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

when gf clicked
forever
ask [type a message] and wait
set [letter # v] to [1] //so the iteration begins with the first letter of the answer
delete all of [words v] //clears the list of words; the first step is separating the words into a list
insert [] at (1 v) of [words v] //it needs a blank item to start out
repeat (length of (answer)) //one repetition for each letter
if <(letter (letter #) of (answer)) = [ ]> then //note that a space is inserted into this string, not nothing
insert [] at (1 v) of [words v] //since it is a space, create a new item
else
replace item (1 v) of [words v] with (join (item (1 v) of [words v]) (letter (letter #) of (answer))) //otherwise, add the letter to the current word
end
change [letter # v] by [1] //move on to the next letter
end
if <[words v] contains [hello]> then //word detection
say [Hello to you, too!] for (2) secs//response
end
if <[words v] contains [like]> then
set  [pick v] to (pick random (1) to (2)) //for use in alternate responses
if <(pick) = [1]> then
add (item (join (last) ()) of [words v]) to [Likes v]
else
add (item (join (last) ()) of [words v]) to [Dislikes v]
end
if <(pick) = [1]> then
if <not <[Likes v] contains (item (join (last) ()) of [words v])>> then
say [I like that, too!] for (2) secs
else
delete (last v) of [Likes v]
say [Didn't I already tell you I liked it?] for (2) secs
end
else
if <not <[Dislikes v] contains (item (join (last) ()) of [words v])>> then
say [Really? I don't like it too much.] for (2) secs
else
delete (join (last) ()) of [Dislikes v]
say [Didn't I already tell you I didn't like it?] for (2) secs

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:

if <[words v] contains [Word you would like bot to understand]> then //word detection
say [Response you would like your bot to say] for (2) secs//response
end

See Also