0% found this document useful (0 votes)
9 views8 pages

Python Sentiment Analysis Project

The document outlines a project to create a Python program that uses machine learning to respond to user messages with either happiness or sadness based on the sentiment of the input. Users will train the model by providing examples of kind and mean messages, and then implement the program to display corresponding images based on the input sentiment. The project emphasizes the importance of providing sufficient examples for accurate sentiment analysis.

Uploaded by

saiful fajar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Python Sentiment Analysis Project

The document outlines a project to create a Python program that uses machine learning to respond to user messages with either happiness or sadness based on the sentiment of the input. Users will train the model by providing examples of kind and mean messages, and then implement the program to display corresponding images based on the input sentiment. The project emphasizes the importance of providing sufficient examples for accurate sentiment analysis.

Uploaded by

saiful fajar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Make me happy

In this project you will make a Python program that can react to what
messages that you send to it.

If you compliment it, it will act happy.


If you insult it, it will act sad.

You’ll use machine learning to train the computer to recognise the


difference between kind and mean messages, by giving it examples of
each.

This project worksheet is licensed under a Creative Commons Attribution Non-Commercial Share-Alike License
[Link]

Page 1 of 8 Adapted by Dhiresh Nathwani Last updated: 9 May 2020


1. You need two pictures: a picture of a happy face, and a picture of a
sad face. Draw them, or if you prefer, find pictures on the Internet instead
Save the two pictures somewhere on your computer and remember where
you put them. You’ll need them later.

2. Go to [Link] in a web browser

3. Click on “Get started”

4. Click on “Log In” and type in your username and password


If you don’t have a username, ask your teacher or group leader to create
one for you.
If you can’t remember your username or password, ask your teacher or
group leader to reset it for you.

5. Click on “Projects” on the top menu bar

6. Click the “+ Add a new project” button.

7. Name your project “make me happy” and set it to learn how to


recognise “text”.
Click the “Create” button

8. You should now see “make me happy” in the list of your projects.
Click on it.

Page 2 of 8 Adapted by Dhiresh Nathwani Last updated: 9 May 2020


9. You need examples to train the computer. Click the Train button.

10. Click on “+ Add new label” and call it “kind things”.


Do that again, and create a second bucket called “mean things”.

11. Click the “Add example” button in the “kind things” bucket, and
type in the nicest, kindest compliment that you can think of.

Page 3 of 8 Adapted by Dhiresh Nathwani Last updated: 9 May 2020


12. Click on the “Add example” button in the “mean things” bucket,
and type in the meanest, cruellest message that you can think of.

13. Repeat steps 11 & 12 until you’ve got at least six examples of each.

14. Click on the “< Back to project” link.


Then click on the “Learn & Test” button.

15. Click on the “Train new machine learning model” button.


As long as you’ve collected enough examples, the computer should start
to learn how to recognise messages from the examples you’ve given to it.

Page 4 of 8 Adapted by Dhiresh Nathwani Last updated: 9 May 2020


16. Wait for the training to complete. This might take a few minutes.

17. Once the training has completed, a Test box will be displayed.
Try testing your machine learning model to see what the computer has
learned.
Type something kind, and press enter. It should be recognised as kind.
Type something mean, and press enter. It should be recognised as mean.

Test it with examples that you haven’t shown the computer before.
If you’re not happy with how the computer recognises the messages, go
back and add some more examples.

18. Click the “< Back to project” link

19. Next, we’ll use Python. Click “Make”

Page 5 of 8 Adapted by Dhiresh Nathwani Last updated: 9 May 2020


20. Click on the “Python” button

21. Click on the “Copy code to run on your computer”

22. Copy the skeleton code displayed into a text editor.


Save it as a file called [Link]

23. Delete the bottom half of the code


Everything from # CHANGE THIS to the end of the file, highlighted below.
Delete it, as you won’t need it.

Page 6 of 8 Adapted by Dhiresh Nathwani Last updated: 9 May 2020


24. You will be using a Python library called Pillow to display your
pictures. If you don’t already have it installed, you need to do this now.
Ask your teacher or group leader to help. Instructions can be found at
[Link]

25. Add “from PIL import Image” to the top of your code

26. Add this to the end of your code.


It will ask you to type in a message.
The message you type in will be stored in a variable called “input”.
That variable will be given to the machine learning model that you have
trained so it can try to recognize it.
If the machine learning model recognizes it as a compliment, your
program will display a “You’re so nice” message and show your picture of
a happy face.

You need to update [Link] and [Link] to match the names of


your pictures of faces.

input = raw_input("What do you want to tell me? > ")

recognized = classify(input)

label = recognized["class_name"]

if label == "kind_things":
print ("You're so nice!")
img = [Link]("[Link]")
[Link]()
else:
print ("You're so mean!")
img = [Link]("[Link]")
[Link]()

Page 7 of 8 Adapted by Dhiresh Nathwani Last updated: 9 May 2020


27. Run your program with the command: python [Link]

What have you done?

You’ve made a Python program that asks for the users input. It will then
use the machine learning model that you trained to recognize whether
what the user inputted was a compliment or an insult. This is often
described as “sentiment analysis”.

The more examples you give it, the better it should get at recognising
messages correctly.

Page 8 of 8 Adapted by Dhiresh Nathwani Last updated: 9 May 2020

Common questions

Powered by AI

The project exemplifies a feedback loop in machine learning by allowing users to test the model's accuracy and return to improve it. After initial training, users input messages to test the model's sentiment classification. If the model doesn't perform adequately, users can return to the training phase to add more or varied examples, thus refining the model. This iterative process of testing and retraining forms a feedback loop that enhances model performance over time .

Supervised learning facilitates the 'Make Me Happy' program's objectives by enabling the creation of a model that learns from labeled data. By categorizing training data into 'kind things' and 'mean things', the program builds a predictive model to accurately classify new, unlabeled messages. This technique ensures the program can interpret sentiments in real-time and respond appropriately, fulfilling the project's goal to recognize and react to user messages .

Users might face challenges such as selecting appropriate examples that adequately represent kind and mean messages, which could affect the model's accuracy. Another challenge is ensuring the availability and awareness of using the necessary software tools, like the Pillow library. These challenges can be addressed by carefully curating balanced and varied training examples, following clear documentation or online tutorials for setting up the software environments, and seeking guidance from educators or online communities if issues persist .

In the 'Make Me Happy' project, sentiment analysis is applied by using a trained machine learning model to classify user input as either kind or mean. When a user inputs a message, the model analyzes the sentiment and classifies it accordingly. If the message is determined to be kind, the program displays a positive response and an image of a happy face. Conversely, if the message is mean, the program responds negatively with a sad face image .

User interaction is enabled through a Python program that prompts the user to input a message. The Python code collects the user’s input, and the trained machine learning model classifies the message based on its sentiment. The Python code responds by displaying a message and an image corresponding to the classification. This interaction is facilitated by using commands from the Pillow library to handle image display .

The Pillow library is used to display images within the project. It is integrated into the Python code by importing the Image module (“from PIL import Image”). Once imported, the Image.open() function is used to open the appropriate image file (either 'happy.png' or 'sad.png') based on the classification result from the machine learning model, allowing the program to visually communicate the sentiment of the message .

Adding more examples to the training data can improve the model’s ability to generalize and recognize patterns in different kinds of messages, leading to better accuracy. A diverse range of examples helps the model distinguish subtle differences between slightly positive and negative sentiments, reducing biases and improving its robustness when classifying unseen inputs .

Using sentiment analysis raises ethical considerations such as ensuring privacy and transparency in data handling and preventing misuse of personal information. Developers must consider potential biases in training data that could skew results, perpetuate stereotypes, or lead to unfair treatment. Ethical implementation involves documenting model limitations and ensuring user consent to prevent unintended consequences and foster trust in the technology .

Training the machine learning model involves several steps: naming the project, adding labels for classification categories ('kind things' and 'mean things'), and providing examples for each label to train the model. At least six examples of each category are recommended to ensure the model has enough data to learn effectively. Sufficient examples are crucial because they allow the model to better understand and differentiate between the features of kind and mean messages, directly influencing its accuracy and performance .

The purpose of using machine learning in the 'Make Me Happy' project is to enable the software to discern between kind and mean messages by training it with examples of each type. This is implemented by creating a machine learning model that learns from labeled examples categorized as 'kind things' and 'mean things'. When a user inputs a message, the trained model classifies it as either kind or mean. If the message is classified as kind, the software displays a 'You're so nice!' message and shows a happy face. If it's classified as mean, it shows a 'You're so mean!' message with a sad face .

You might also like