From Test-Scratch-Wiki
A chat bot is a program which allows you to talk to the computer or application. This can be developed by solely using Scratch; it involves many lists, operators, and the 询问 [] 并等待
block in particular. This tutorial shows how to make one yourself.
Brief Description
Basically, to create a chat bot you need to use the "询问并等待" block to enter a message. Then, the 专案 takes that message, breaks it apart into words, and scans the list of words for specific words. Then, if your message contains those specific words, the chat bot can respond a pre-set message back to you. To break apart the 询问的答案 变量 into words, you have to repetitively add each letter to a list until you reach a space, and then you create a new item in the list and continue adding the letters, forming words. To program this, you will need only one 清单:
- words
and one or two 变量:
(letter #)
(pick)
Note: | the variables and lists can be named anything you want them to |
Programming the Bot
The following code replication can be used to make the bot respond to your messages. All these scripts can go in any sprite.
當 @greenflag 被點擊 重複無限次 詢問 [type a message] 並等待 變數 [letter # v] 設為 [1] // so the iteration begins with the first letter of the answer 刪除第 (全部 v) 項 \( [words v] \) // clears the list of words; the first step is separating the words into a list 添加 [] 為第 (1 v) 項\( [words v] \) // it needs a blank item to start out 重複 (字串長度\( (詢問的答案) \)) 次 如果 <(字串中第 (letter #) 字\( (詢問的答案) \)) = [ ]> 那麼 添加 [] 為第 (1 v) 項\( [words v] \) // since it is a space, create a new item 替換第 (1 v) 項於 [words v] 成 (字串組合 (清單第 (1 v) 項項目\( [words v] \) :: list) 和 (字串中第 (letter #) 字\( (詢問的答案) \))) // otherwise, add the letter to the current word end // note that a space is inserted into this string, not nothing 變數 [letter # v] 改變 [1] // move on to the next letter end // one repetition for each letter 如果 <清單 [words v] 包含 [hello] ?> 那麼 說出 [Hello to you, too!] (2) 秒 // response end // word detection 如果 <清單 [words v] 包含 [like] ?> 那麼 變數 [pick v] 設為 (隨機取數 (1) 到 (2)) // for use in alternate responses 如果 <(pick) = [1]> 那麼 說出 [I like that, too!] (2) 秒 說出 [Really? I don't like that too much.] (2) 秒 end end end
For example, if you entered in "I like Scratch" 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, for instance, you type in "I like Scratch", and the bot may respond "I like that too!", but if you type "I like Scratch" again, it might say "Really? I don't like that too much.". To prevent this, you can make some lists to store words that the bot already responded to.
Make two 清单:
- Likes
- Dislikes
Then change this part of the script:
當 @greenflag 被點擊 重複無限次 詢問 [type a message] 並等待 變數 [letter # v] 設為 [1] // so the iteration begins with the first letter of the answer 刪除第 (全部 v) 項 \( [words v] \) // clears the list of words; the first step is separating the words into a list 添加 [] 為第 (1 v) 項\( [words v] \) // it needs a blank item to start out 重複 (字串長度\( (詢問的答案) \)) 次 如果 <(字串中第 (letter #) 字\( (詢問的答案) \)) = [ ]> 那麼 添加 [] 為第 (1 v) 項\( [words v] \) // since it is a space, create a new item 替換第 (1 v) 項於 [words v] 成 (字串組合 (清單第 (1 v) 項項目\( [words v] \) :: list) 和 (字串中第 (letter #) 字\( (詢問的答案) \))) // otherwise, add the letter to the current word end // note that a space is inserted into this string, not nothing 變數 [letter # v] 改變 [1] // move on to the next letter end // one repetition for each letter 如果 <清單 [words v] 包含 [hello] ?> 那麼 說出 [Hello to you, too!] (2) 秒 // response end // word detection 如果 <清單 [words v] 包含 [like] ?> 那麼 變數 [pick v] 設為 (隨機取數 (1) 到 (2)) // for use in alternate responses 如果 <(pick) = [1]> 那麼 說出 [I like that, too!] (2) 秒 說出 [Really? I don't like that too much.] (2) 秒 end end end
Note: | This might not work if you have more than one word after "like" |
Final Product
Excluding scripts in the next section, the whole script would be something like this:
當 @greenflag 被點擊 重複無限次 詢問 [type a message] 並等待 變數 [letter # v] 設為 [1] // so the iteration begins with the first letter of the answer 刪除第 (全部 v) 項 \( [words v] \) // clears the list of words; the first step is separating the words into a list 添加 [] 為第 (1 v) 項\( [words v] \) // it needs a blank item to start out 重複 (字串長度\( (詢問的答案) \)) 次 如果 <(字串中第 (letter #) 字\( (詢問的答案) \)) = [ ]> 那麼 添加 [] 為第 (1 v) 項\( [words v] \) // since it is a space, create a new item 替換第 (1 v) 項於 [words v] 成 (字串組合 (清單第 (1 v) 項項目\( [words v] \) :: list) 和 (字串中第 (letter #) 字\( (詢問的答案) \))) // otherwise, add the letter to the current word end // note that a space is inserted into this string, not nothing 變數 [letter # v] 改變 [1] // move on to the next letter end // one repetition for each letter 如果 <清單 [words v] 包含 [hello] ?> 那麼 說出 [Hello to you, too!] (2) 秒 // response end // word detection 如果 <清單 [words v] 包含 [like] ?> 那麼 變數 [pick v] 設為 (隨機取數 (1) 到 (2)) // for use in alternate responses 如果 <(pick) = [1]> 那麼 新增項目 (清單第 (last v) 項項目\( [words v] \) :: list) \( [Likes v] \) 新增項目 (清單第 (last v) 項項目\( [words v] \) :: list) \( [Dislikes v] \) end 如果 <(pick) = [1]> 那麼 如果 <<清單 [Likes v] 包含(清單第 (末 v) 項項目\( [words v] \) :: list) ?> 不成立> 那麼 說出 [I like that, too!] (2) 秒 刪除第 (last v) 項 \( [Likes v] \) 說出 [Didn't I already tell you I liked it?] (2) 秒 end 如果 <<清單 [Dislikes v] 包含 (清單第 (末 v) 項項目\( [words v] \) :: list) ?> 不成立> 那麼 說出 [Really? I don't like it too much.] (2) 秒 刪除第 (last v) 項 \( [Dislikes v] \) 說出 [Didn't I already tell you I didn't like it?] (2) 秒 end end end end
Taking it Further
To make the chat bot more realistic, you can create a separate list of previously stored words from your old entered messages, and make the bot refer to those later in the conversation. For example, if you tell it that you like programming, and then later say you like 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".
You could also change the script to make the bot understand even more words, by using this:
如果 <清單 [words v] 包含 [Word you would like bot to understand] ?> 那麼 說出 [Response you would like your bot to say] (2) 秒 // response end // word detection