This task explores systems that use NLP to classify a reviewer’s online text as positive, neutral or negative, based on words that might appear in the text. This is often referred to as ‘sentiment analysis’.
Sentiment analyses aren’t always reliable. When training an AI, words and combinations of words would be classified as positive, neutral or negative through the process of supervised learning. Supervised learning is the process of the human providing the program with lots of examples of what it is we are wanting it to identify along with a label.
What combinations of words may produce an inaccurate sentiment analysis?
Use these suggested activities to explore NLP and sentiment analysis.
The activity has been levelled to enable differentiation.
This task enables students to explore sentiment analysis via an online tool that uses NLP to classify text as positive, neutral or negative, based on words that appear in the text.
In this task, students code their own program using a visual programming language such as Scratch to demonstrate sentiment analysis. The program identifies a word contained in the user’s input (string). If that word is recognised then a particular response is given else provide a generic response. The program incorporates:
One possible solution is as follows:
To start the program, use ‘When flag clicked’.
Use an ask block to initiate user input.
The user’s input is classed as ‘answer’.
Show the operator block ‘apple contains a?’
Explain how this block can be changed, by adding text, e.g. if the user’s answer contains the word ‘like’.
Use the if/then/else block:
If the user’s text contains ‘like’, then use ‘say’ block to say, ‘That is positive’ for ‘2’ seconds:
For else say something generic such as ‘I’m not sure if you liked it or not’. The block would look something like:
Repeat the process for a word that is classified as negative language such as ‘hate’. The subsequent if/then/else blocks can each be nested in the previous block’s ‘else’ position.
Image: Screen capture of Scratch program showing nested if/then/else blocks
Challenge students to remix the existing Scratch programs provided:
For students with:
Students can remix the program and:
Question: Do you think this simple keyword system is foolproof?
Answer: While this system can make some guesses as to a positive or negative tone, it is not foolproof. For a start, it has a very limited vocabulary, and it does not deal with the use of modifiers like ‘not’ and ‘very’.
In the ‘Make me happy’ project, students explore the use of an AI to determine if a sentence is kind or mean. The project worksheet for students provides step-by-step instructions for students to draw a happy and sad sprite in Scratch, develop a knowledge base, train it and use it in their Scratch program.
Visit the worksheet page on ‘Machine learning for kids’ and select the ‘Make me happy’ project. (Although the guide is labelled ‘easy’ on their site, relative to our activities it is medium to high.)
Image: Make me happy in Machine Learning for Kids (licensed under CC BY NC SA 4.0)
You may decide to run the project as is with your students.
As an extension, students could add a third category ‘neutral’ to the dataset. This requires a retraining of the knowledge base, the design of a new smiley and an extension of the Scratch code’s if statement to account for a third outcome besides ‘kind’ and ‘mean’.
Image: Classifying words as positive, neutral or negative in Machine Learning for Kids
Refer to the related lesson plan on the topic of Anti-bullying with AI and visit the My computer brain’s anti-bullying experiment to explore the inner workings of the AI for this scenario. It maps well to the ‘Make me Happy’ activity.
Have students share what they have learned about AI and how ‘smart’ a computer can be.
This lesson focuses on:
Algorithms and programming are essential to developing machines powered by AI.
In conventional programming, the computer is provided with a set of instructions for a defined set of scenarios. In this lesson, students hardcode a program that is based on identifying a word contained in the user’s input (string). If that word is recognised, then a particular response is given. Else provides a generic response.
The downside with this programming is that every possible option needs to be hardcoded to ensure the text is correctly categorised. Also, the order that the word appears in the text affects the categorising. In an AI, a similar problem arises. We need to teach the AI each individual word, however the AI is good at solving all sorts of word combinations. So, we really want to point out that the conventional programming method struggles with the myriad of possible word combinations. An AI-based tool using NLP is better able to provide a correct categorisation as it has been trained on large amounts of data.