Chapter 6. Chatbot
userhead
RRoy 2020-04-08 10:34:06

Have you ever chatted with iphone’s Siri or interact with a smart robot? If not, that’s ok, cause now we can directly program an intelligent chatbot by ourselves with mind+ and micro:bit. Isn’t that sounds cool! Well, let’s begin. 

The AI translations will be involved in this chapter, also we will get to know about the temperature sensor on the micro:bit board. 

6.1 Get to know list 


We have already get known about variable, you may find that there is a “create a list” block in the variable module. What is a list ? we know that a variable can be used to store number, char etc and can be refereed repeatedly. Of course, we can also update a variable. In fact, a list is a collection variable, just like a queue. For example, we create a list and name it as “present”, then place four items in the present list: crystal ball, music box, lego, model car. We can add items into the list, here we add an item to the end of the list, and then the list would be like this “crystal ball, music box, lego, model car, doll house”. Besides, the items in the list can be revised, for instance, change the first item into dinosaur model, then the list would be “dinosaur model, music box, lego, model car, doll house”. Apparently, we can delete items in the list. Well now, let’s create a list and see what functions will be found in blocks. As figure 6-1 shows, we can add or delete a specified item to a list; replace an item, hide or show a list. 


Case 1: Santa Claus gives gifts 


In this case, we are gonna create three sprites: Santa Claus, a boy and a girl, as figure 6-2 shows. The boy and girl asked Santa Claus respectively “what can I get for my Christmas present?” Santa Claus thinks for a while and then determines to pick a gift randomly from his pocket. But here comes a problem: there are crystal ball and Transformer in his pocket, so a situation like this could happen: the crystal ball was sent to a boy and the Transformer went to a girl, then it would be awkward! To deal with that problem, we need to classify our gifts into For Girl and For Boy. Cause it could be much reasonable to pick a present from For Girl list when a girl asked for her gift. 

projectImage
projectImage
projectImage

In this case we learned how to create a list, add items for the list, hide and acquire a certain item form the list and so on. We will use a new block 

projectImage

here . It can be found in “Operators” module. The block concatenates or “links” the two values together and reports the result. 

Case 2: Chat Language Detection 


We will use list to detect chat language in this case. The functions the programs need to realize: obtain a speech the user input, divide it into single character and put them into a list, then check every item in the list. 


  First of all, we need to learn how to get the number of characters. 

 

projectImage

Secondly, divide a speech into single characters and place the characters into a list. 

  Input a speech “I am Micko”, the number of characters is 3, and it needs three times to get all the characters. Set a variable “i” to represent the number of cycles. Get the first character in the first loop and so on. The value of i will increase by 1 in every loop. When it comes to the last loop, the value of i will be 4 after getting the third character. Then stops the loop and display the content of the list.  

projectImage

Character Detection 


Input “I am Micko” and check the result. So far, we can detect some particular characters of the chatting language. 

With the knowledge of list we can make much more fun programs, detect language, chatting room and so on. Ok, now let’s start to make the chatbot. 

projectImage

6.2 Algorithms Analysis 


- Create a list “Chatbot” and show it; 

- Repeatedly run the program “ask … and wait”, and “answer”. Add “answer” to the end of the list to make it like an effect of asking question.

- Embed the “if then…else” repeatedly and detect if the words like “time”, “joke”, “song”, “translation”, and “temperature” includes in the “answer”.  

6.3 Programming 


We can ask the chatbot to tell us time, temperature, or let him tell jokes, play music and do translations. The scripts are shown in figure 6-7. 

You can play the sound in the sound library, record your own sound or upload a sound file from your computer. We use the sound “dance around” in the loops of the sound library.  

projectImage
projectImage

Load the google translate: click “Extensions”-> “Function”-> “Google Translate”. Then we can use the related google translate module. 

projectImage

6.4 Temperature Sensor 


Connect micro:bit and the computer with micro USB. The temperature sensor on the micro:bit board will detect the current temperature of the device. (unit: °/℃) 

projectImage

6.5 Programming Result 


Click the green flag to start the program, and check the effect. We can drag the frame of the list to expand the display area. 

projectImage
projectImage

6.6 Training Camp 


Now the chatbot can only realize the function of chatting, but we can combine it with the micro:bit to simulate the smart home, for example, to give orders like “turn on/off the light” to control the LED light on the micro:bit board. 

License
1
All Rights
Reserved
[[i.text]]
licensBg
0
1
COMMENTS ([[commentTotla]])
[[c.user_name]] [[c.create_time]]
[[c.parent_comment.count]]
[[c.comment_content]]