Leçon 1, Chapitre 1
En cours

Language naturel – Défis Maison domotique python

Yann KIDSHAKER 17 mars 2025

In this activity, we will make a system through which we can switch On or Off different appliances at home using a series of different voice commands (not just one command). Such a system run on the basis of a text classifier, which is a machine learning model that needs to be trained.

For this, we will train a Natural Language Processing (NLP) model on a series of commands. The model will categorize these commands on the basis of whether they belong to the lights on category, lights off category or fan on category.

Lets Code

  1. Open the Pictoblox file given below, and choose the Python Coding environment.
    9 Blank_Home Automation System
  2. You will see that three sprites are already initiated (by default): Abby, Lamp, Fan as follows:
    sprite = Sprite('Abby')
    lamp = Sprite('Lamp')
    fan = Sprite('Fan')
  3. Let us import the time library. Let us also initiate the following three classes:
    1. Natural Language Processing: This class has functions to create and train your own text classifiers.
    2. Speech Recognition: This class has functions to recognize our speech, and perform actions accordingly.
    3. TexttoSpeech: This class has a function that converts text to speech
      import time
      
      nlp = NaturalLanguageProcessing()
      sr = SpeechRecognition()
      ts = TexttoSpeech()
  4. Firstly, we will use the reset() function from NaturalLanguageProcessing class to reset the NLP model. This is done to clear the NLP model of any past data that it might contain from the previous training.
    nlp.reset()

    Now our NLP model is clear and ready to be fed training data

  5. Let us use the pushdata() function from class to add training data to the NLP model. This function is used for labeling various text data to a particular class. The syntax is as follows:
    1. Syntax: pushdata(text_data, class_label)
    2. Parameters:
      1. text_data: The text data you want to label
      2. class_label: The particular class, you want to label on text_data
  6. The code for adding data to label them with the class ‘lights on’ is given below:
    # Data for lights On
    nlp.pushdata("turn on the light", "lights on")
    nlp.pushdata("lights on", "lights on")
    nlp.pushdata("light on", "lights on")
    nlp.pushdata("lied on", "lights on")
    nlp.pushdata("i need some sunshine", "lights on")
  7. Similarly, we can add code to label some text with the class ‘lights off’:
    # Data for lights off
    nlp.pushdata("turn off the light", "lights off")
    nlp.pushdata("lights off", "lights off")
    nlp.pushdata("light off", "lights off")
    nlp.pushdata("lied off", "lights off")
    nlp.pushdata("i need to rest", "lights off")
  8. Similarly, we can add code to label some text with the class ‘fan on’:
    # Data for fan on
    nlp.pushdata("switch on fan", "fan on")
    nlp.pushdata("fan on", "fan on")
    nlp.pushdata("climate hot", "fan on")
    nlp.pushdata("so hot here", "fan on")
    nlp.pushdata("turn on fan", "fan on")
  9. Now, we will train our NLP model on the text_data we added above. We will do this using the train() function of NaturalLanguageProcessing class.
    nlp.train()
  10. Now, we will make the Abby sprite say on the stage “Hello I am your AI assistant” for 3 seconds using the say() function of Sprite class. We will also make our device say this, for this we will use the speak() function from TexttoSpeech class.
    sprite.say("Hello I am your AI assistant", 3)
    ts.speak("Hello I am your AI assistant")
  11. To make our code run continuously we will use the while True loop. We will now make the sprite say on stage and speak “What do you want me to do today” just like we did on the above step.
    while True:
      sprite.say("What do you want me to do today", 3)
      ts.speak("What do you want me to do today")
  12. We have prepared the NLP model that we will use for home automation. Now we want to take in the speech spoken by us and analyse it. For this, we will use the analysespeech() function from SpeechRecognition class.
      #Recognize speech           
      sr.analysespeech(7, "en-US")

    Here the first parameter is for the amount of time, our speech will be recorded. We keep it to 7 seconds. The second parameter is the language of choice. We have chosen US english for the same.

  13. In the above function we recorded and analysed our speech. This speech is now stored in string format, somewhere within the code. To access this string we need to use the speechresult() function of SpeechRecognition class. We will assign this string to a variable called ‘command’ as follows:
      # store the speech result
      command = sr.speechresult()
      command = command.lower()
  14. We want the lamp to turn ON when we give any of the commands that we have listed in the class ‘lights on’. Hence whenever any of these commands are detected, we will run an if-condition, to check whether the command detected belongs to the ‘lights on’ class.In order to check, whether the specific text spoken belongs to a particular class, we will use the function analyse() from the NaturalLanguageProcessing class. This function takes in the text you spoke (stored in variable: command) and gives back the class_name that text belongs to. We then check whether it belongs to our class ex: ‘Lights On’ using the equality operator (==).
      if nlp.analyse(command) == 'lights on':
  15. If the above condition returns True, we will switch on the lamp using the following functions on the lamp object of the Sprite class.
    1. switchcostume(costume_name): switches the costume of lamp sprite to the one given as parameter.
    2. seteffect(effect_name, effect_value): Sets the specified effect on its sprite to the specified amount.
      1. effect_name = There are 7 different effects to choose from: color, fisheye, whirl, pixelate, mosaic, brightness, and ghost.
      2. effect_value = 0 to 100
          if nlp.analyse(command) == 'lights on':
            lamp.switchcostume('lamp-light-animation2')
            lamp.seteffect("BRIGHTNESS", 100)
  16. Similarly, we will switch to another costume, when the class ‘lights off’ is detected. Here, we will use costume ‘lamp-light-animation‘ =  and brightness = -20

        
      if nlp.analyse(command) == 'lights off':
        lamp.switchcostume('lamp-light-animation')
        lamp.seteffect("BRIGHTNESS", -20)
  17. In case the class ‘fan on’ is detected, we want the fan to run for some time and then turn off (save electricity!).For this, we will change the costume of the fan sprite using the function nextcostume() of fan object of the Sprite class. We will also use sleep()  function of the time class so that the costumes of fan are changed at a particular speed. This will make it look as if the fan is moving.
      if nlp.analyse(command) == 'fan on':
        for i in range(1000):
          fan.nextcostume()
          time.sleep(0.005)
  18. The final code is as follows:
    sprite = Sprite('Abby')
    lamp = Sprite('Lamp')
    fan = Sprite('Fan')
    
    import time
    
    nlp = NaturalLanguageProcessing()
    sr = SpeechRecognition()
    ts = TexttoSpeech()
    
    # Clear any residual data from the model (if any)
    nlp.reset()
    
    # Data for lights On
    nlp.pushdata("turn on the light", "lights on")
    nlp.pushdata("lights on", "lights on")
    nlp.pushdata("light on", "lights on")
    nlp.pushdata("lied on", "lights on")
    nlp.pushdata("i need some sunshine", "lights on")
    
    # Data for lights off
    nlp.pushdata("turn off the light", "lights off")
    nlp.pushdata("lights off", "lights off")
    nlp.pushdata("light off", "lights off")
    nlp.pushdata("lied off", "lights off")
    nlp.pushdata("i need to rest", "lights off")
    
    # Data for fan on
    nlp.pushdata("switch on fan", "fan on")
    nlp.pushdata("fan on", "fan on")
    nlp.pushdata("climate hot", "fan on")
    nlp.pushdata("so hot here", "fan on")
    nlp.pushdata("turn on fan", "fan on")
    
    # Train the nlp model
    nlp.train()
    
    sprite.say("Hello I am your AI assistant", 3)
    ts.speak("Hello I am your AI assistant")
    
    
    while True:
      sprite.say("What do you want me to do today", 3)
      ts.speak("What do you want me to do today")
      
      # Recognize speech
      sr.analysespeech(7, "en-US")
      # store the speech result
      command = sr.speechresult()
      command = command.lower()
    
      if nlp.analyse(command) == 'lights on':
        lamp.switchcostume('lamp-light-animation2')
        lamp.seteffect("BRIGHTNESS", 100)
        
      if nlp.analyse(command) == 'lights off':
        lamp.switchcostume('lamp-light-animation')
        lamp.seteffect("BRIGHTNESS", -20)
        
      if nlp.analyse(command) == 'fan on':
        for i in range(1000):
          fan.nextcostume()
          time.sleep(0.005)
  19. Press the Run button to test the script.

Assignment

Before you move on to the next lesson, a small assignment awaits you!

You must upload the PictoBlox program you created in this activity to the website. Submitting the assignment is a must in order to receive the certificate after completing the course.

Follow the steps below to upload your assignment:

  1. Click on Browse.
  2. Search and Select your saved Project file(.sb3) and Click Open.
  3. Click on Upload to submit the assignment.
evive Alert
The file type allowed is the SB3 file generated from the PictoBlox program. The maximum file size allowed is 5 MB.

Good luck!