Twitter Sentiment Analysis in Python

NeuralNine
12 Oct 202022:24

Summary

TLDRIn this tutorial, viewers learn to create a Twitter sentiment analysis tool using Python. The video guides through setting up a Twitter app, obtaining API keys, and using libraries like Tweepy and TextBlob. It demonstrates connecting to the Twitter API, gathering tweets on a chosen topic, cleaning the data, and performing sentiment analysis. The script also discusses potential biases and inaccuracies in sentiment analysis and suggests ways to improve the analysis by adjusting polarity thresholds.

Takeaways

  • 🔑 **Twitter API Access**: To build a Twitter sentiment analysis tool, you need access to the Twitter API through a Twitter Developer App.
  • 📱 **App Verification**: A verified Twitter account with a confirmed phone number and email is required to create a Twitter Developer App.
  • 🔗 **API Keys**: Four keys are necessary for the Twitter API: API key, API secret key, Access token, and Access token secret.
  • 📚 **Libraries Needed**: The tutorial uses the libraries TextBlob for sentiment analysis and Tweepy (or Tweetpie) for interacting with the Twitter API.
  • 💾 **Key Management**: It's advisable to store API keys in a text file for security rather than hardcoding them into the script.
  • 🔍 **Search Tweets**: Use the Twitter API's search method to retrieve tweets based on a specified topic and language (English in this case).
  • 🗑️ **Data Cleaning**: Clean the tweets by removing unnecessary parts like 'RT', mentions, and possibly hyperlinks to improve sentiment analysis accuracy.
  • 📊 **Sentiment Analysis**: TextBlob is used to calculate the polarity of each tweet, which indicates the sentiment (positive, negative, or neutral).
  • 📈 **Threshold Adjustment**: Set a higher polarity threshold to account for a positive bias often observed in sentiment analysis results.
  • 📝 **Result Interpretation**: Analyze the overall sentiment by adding up the polarity scores and interpreting the results as positive, negative, or neutral based on the polarity values.
  • 🔍 **Topic Selection**: Choose appropriate topics for sentiment analysis and consider the impact of context on the accuracy of sentiment analysis results.

Q & A

  • What is the main focus of the video tutorial?

    -The main focus of the video tutorial is to build a Twitter sentiment analysis tool using the Twitter API and natural language processing tools.

  • What is required to set up a Twitter app for the project?

    -To set up a Twitter app, one needs to navigate to developer.twitter.com or apps.twitter.com, create a new project, and have a verified Twitter account with a specified phone number and email.

  • What are the keys and tokens needed to use the Twitter API?

    -The keys and tokens needed to use the Twitter API include an API key, a secret API key, access tokens, and a secret access token.

  • Which additional libraries are required for the Twitter sentiment analysis project?

    -The additional libraries required for the project are TextBlob for sentiment analysis and Tweepy (or Tweetpie) for interacting with the Twitter API.

  • How does one install the required libraries mentioned in the tutorial?

    -The required libraries can be installed using pip commands in the command line by activating the conda environment and then typing 'pip install text blob' and 'pip install tweepy'.

  • How are the API keys and tokens kept secure in the script?

    -In the script, the API keys and tokens are read from a text file to keep them secure, rather than being hard-coded directly into the Python code.

  • What is the purpose of using a cursor object in the script?

    -A cursor object is used in the script to search for tweets based on a specific term and to limit the number of tweets returned for analysis.

  • Why is the language parameter specified when searching for tweets?

    -The language parameter is specified to filter the results and only get tweets in English, as the sentiment analysis tool used, TextBlob, only works on English text.

  • How is the sentiment of each tweet determined using TextBlob?

    -The sentiment of each tweet is determined using TextBlob by creating a TextBlob object with the cleaned tweet text and then finding the polarity of the text.

  • What is the issue with considering tweets with a polarity of zero as neutral?

    -The issue with considering tweets with a polarity of zero as neutral is that TextBlob's sentiment analysis might not accurately determine the context, and many tweets perceived as negative might still be scored as slightly positive.

  • How can one increase the accuracy of the sentiment analysis results?

    -One can increase the accuracy of the sentiment analysis results by setting a higher threshold for what is considered positive, cleaning the tweets more thoroughly to remove noise like mentions and RTs, and using a more sophisticated natural language processing tool.

Outlines

00:00

🔑 Setting Up Twitter Developer App

The paragraph introduces the process of setting up a Twitter Developer App to access the Twitter API for a sentiment analysis project. It explains the necessity of having a verified Twitter account with a phone number and email to create a developer app. The video creator walks through the steps of navigating to developer.twitter.com, creating a new project, and specifying the purpose and details of the app. It also discusses the importance of obtaining the API key, secret API key, access token, and secret access token, which are essential for the tutorial's script.

05:01

📚 Installing Required Libraries

This section covers the installation of necessary Python libraries for the Twitter sentiment analysis tool. The video instructs viewers to install 'textblob' for sentiment analysis and 'tweepy' for accessing the Twitter API. It provides instructions on how to install these libraries using pip in a command line interface, assuming the user has a conda environment set up. The paragraph also mentions the potential need for additional libraries if visualization is included in the project.

10:03

🔗 Authenticating and Connecting to Twitter API

The paragraph explains how to authenticate and connect to the Twitter API using the keys obtained from the developer portal. It details creating an authentication handler with the consumer key and secret, then setting the access token and access token secret to connect to the API. The process involves defining an API object that utilizes the authentication handler. The video also discusses how to define a search term and the number of tweets to analyze for sentiment, emphasizing the importance of specifying the language to filter out irrelevant results.

15:04

🔍 Searching and Cleaning Tweets

Here, the video script describes the process of searching for tweets using the Twitter API and cleaning the data to improve the accuracy of sentiment analysis. It involves using a cursor object to search for tweets based on a specified term and language. The paragraph also covers removing unnecessary elements like retweets ('rt'), mentions, and ads from the tweets to focus on the content that is relevant for analysis. The cleaned tweets are then printed to the screen for further processing.

20:07

📊 Analyzing Sentiment and Counting Tweets

The final paragraph discusses performing sentiment analysis on the cleaned tweets using the TextBlob library. It explains creating a polarity score for each tweet and accumulating these scores to determine the overall sentiment. The video creator also suggests setting a higher threshold for considering a tweet as positive due to a perceived positive bias in the analysis tool. Additionally, it introduces the idea of counting the number of positive, negative, and neutral tweets and adjusting the criteria for classification based on the desired accuracy.

Mindmap

Keywords

💡Twitter API

The Twitter API is a set of tools and endpoints that allows developers to access and interact with Twitter data programmatically. In the video, the presenter uses the Twitter API to gather tweets based on a specific topic for sentiment analysis. The API provides a way to search for tweets, which is fundamental to the project's goal of analyzing public sentiment on Twitter.

💡Sentiment Analysis

Sentiment analysis, also known as opinion mining, is the process of determining the emotional tone behind words to understand the attitudes, opinions, and emotions of a speaker or writer. In the video, sentiment analysis is the main focus, where the presenter aims to analyze the overall sentiment of tweets about a given topic using Natural Language Processing (NLP) tools.

💡Natural Language Processing (NLP)

NLP is a field of computer science and artificial intelligence that is concerned with the interaction between computers and humans through natural language. In the context of the video, NLP tools are used to process and analyze the text of tweets to determine the sentiment behind them, which is central to the project's objective.

💡TextBlob

TextBlob is a simple library in Python that is designed to provide a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, and more. In the video, TextBlob is used for performing sentiment analysis on the tweets gathered via the Twitter API.

💡Tweepy

Tweepy is an easy-to-use Python library for accessing the Twitter API. It provides a convenient interface for the Twitter API, necessary for the video's project to interact with Twitter's data. The script uses Tweepy to authenticate with Twitter and to fetch tweets based on a search query.

💡API Key

An API key is a code passed in by computer programs calling an API to identify the calling program, its developer, or its user. In the video, the presenter mentions the need for an API key, a secret API key, access tokens, and a secret access token to authenticate and authorize the app to use the Twitter API.

💡Authentication

Authentication is the process of verifying the identity of a user or system. In the video script, the presenter discusses setting up authentication with the Twitter API using the keys and tokens obtained from the Twitter Developer portal. This step is crucial for gaining access to the API and ensuring that the requests are coming from a verified source.

💡Cursor

In the context of the video, a cursor is used to paginate through the results when searching for tweets with the Twitter API. The presenter uses Tweepy's cursor functionality to iterate over tweets and gather a specific number of tweets for analysis.

💡Polarity

Polarity, in the context of sentiment analysis, refers to the degree to which the sentiment expressed is positive or negative. The presenter uses the polarity scores provided by TextBlob to determine the sentiment of each tweet. A positive polarity score indicates a positive sentiment, while a negative score indicates a negative sentiment.

💡Threshold

A threshold is a value or level that determines when a particular action or state will be triggered. In the video, the presenter discusses setting a threshold for polarity scores to differentiate between positive, negative, and neutral sentiments. For example, setting a threshold of 0.00 could classify scores above it as positive and below as negative.

💡Context

Context refers to the circumstances or facts that surround a particular event or situation, providing meaning or understanding. The script mentions that sentiment analysis might not always be accurate because it considers individual words without their context in the sentence. For example, the word 'not' can change the sentiment of a phrase, which simple word-based analysis might miss.

Highlights

Introduction to building a Twitter sentiment analysis tool using Python and the Twitter API.

Overview of setting up a Twitter developer app, including the need for a verified Twitter account.

Importance of obtaining API keys and tokens from the Twitter developer portal to interact with the Twitter API.

Installation of two critical Python libraries for this project: TextBlob (for sentiment analysis) and Tweepy (for interacting with the Twitter API).

Guide on how to read API keys from a file for security, or hard-code them directly for simpler use.

Establishing a connection to the Twitter API using OAuth and API keys, followed by setting access tokens.

Defining a search term (such as 'stocks') and a tweet limit to specify how many tweets will be analyzed.

Explanation of using Tweepy’s cursor object to fetch tweets based on search terms and filter them by language.

Cleaning up tweet text by removing retweets (RT) and mentions (@usernames) to enhance analysis accuracy.

Using TextBlob for sentiment analysis by computing the polarity of each tweet's text.

Summing up the polarity of all tweets to determine overall sentiment on a specific topic.

Handling biases in sentiment analysis, such as positive bias, and introducing a threshold to determine positive polarity.

The ability to categorize tweets into positive, neutral, and negative based on their polarity score.

Demonstration of modifying the polarity threshold to get more accurate sentiment results.

Final sentiment analysis results for different topics (e.g., stocks, war, death, happiness) and understanding biases in the results.

Transcripts

play00:04

[Music]

play00:09

what is going on guys welcome back to

play00:10

another ai project tutorial in python in

play00:13

today's video we're going to build a

play00:14

twitter sentiment analysis

play00:16

tool so we're going to use the twitter

play00:17

api in order to get a bunch of tweets

play00:20

based on a topic that we provide

play00:21

and then we're going to use natural

play00:23

language processing tools in order to

play00:26

analyze the overall sentiment for that

play00:28

topic so let us get right into it

play00:31

now before we can get into the actual

play00:32

coding we need to make sure we have a

play00:34

twitter app set up

play00:35

so um if you've never worked with

play00:38

twitter developer apps you probably

play00:39

don't have that so you need to navigate

play00:41

to developer.twitter.com or

play00:43

apps.twitter.com

play00:44

and it's going to get you here to the

play00:46

developer portal and here you can create

play00:48

new projects now this only works if you

play00:50

have a um

play00:51

an account where you have a phone number

play00:53

i think and a confirmed email or

play00:55

something you cannot just create a

play00:57

a new twitter account without specifying

play00:59

or verifying your phone number and then

play01:01

do this

play01:02

uh you need to have an actual twitter

play01:04

account that

play01:06

is verified citizen like not verified in

play01:08

terms of celebrity verified but

play01:10

verified in terms of you have specified

play01:12

a phone number a valid phone number and

play01:13

an email

play01:14

then you can use these developer apps

play01:16

here and the only thing that you need to

play01:17

do is you need to create

play01:18

an app and um you you will need to have

play01:22

to specify

play01:23

what you need it for who you are like

play01:25

are your student or your company what

play01:27

are you doing with this for example i

play01:28

specified i'm going to do youtube

play01:30

tutorials with this app so

play01:31

depending on what you plan on doing with

play01:33

that you need to specify what you're

play01:34

going to use it for

play01:35

and then you can just create it and then

play01:37

what you have is you have a bunch of

play01:39

keys here so you

play01:40

have an api key and a secret api key

play01:43

and you will have access tokens and a

play01:44

secret access token now i'm not going to

play01:46

go through the whole process here

play01:48

i think it's very intuitive and also you

play01:49

can go ahead and just google

play01:51

how to set up a twitter app developer

play01:53

app whatever

play01:54

it's not too complicated um this

play01:56

tutorial is more focused on the learning

play01:58

but what you need for this tutorial

play02:00

is you need uh to definitely know your

play02:01

api key your secret api key

play02:04

your access token and your secret access

play02:06

token now you can regenerate them revoke

play02:08

them at all times i'm not going to show

play02:10

my keys in this tutorial but you need to

play02:12

use your keys your four keys

play02:14

you need to know them and to use them in

play02:16

the script

play02:17

so for this video we're going to need

play02:19

two additional libraries that are not

play02:20

part of the core python stack and those

play02:22

are

play02:23

text blob and of course

play02:26

tweepy or tweetpie i'm not sure how it's

play02:28

pronounced

play02:29

now you can just go ahead and install

play02:31

them using a command line so you just go

play02:33

ahead

play02:34

activate your condo environment if you

play02:36

have one in my case it's called

play02:38

main and then you just go ahead and type

play02:40

pip install

play02:41

text blob in my case already satisfied

play02:45

and then you also go ahead and say pip

play02:47

install to ep or tweetpie

play02:51

which is the twitter api uh library that

play02:53

we're going to use

play02:54

so when you have that you need to import

play02:56

um

play02:58

three libraries uh maybe four if we're

play03:00

going to do a visualization in the end

play03:01

but for now we're going to import three

play03:03

libraries

play03:04

the first one is from text blob we're

play03:06

going to import

play03:07

text blob this is the library that we're

play03:09

going to use for the actual sentiment

play03:11

analysis

play03:12

uh then we also need of course tweetpie

play03:14

tweetp whatever

play03:16

i'm going to call it pie here uh because

play03:18

i don't want to say tweet pie tweepy

play03:20

whatever all the time

play03:22

so tweet pie um and then we're going to

play03:25

import

play03:26

sis as well which is part of the core

play03:29

python stack

play03:31

so what we do then is we need to

play03:34

somehow connect to our app so we need to

play03:37

use those four keys in order to

play03:39

make a connection to the app that we

play03:42

just created in the twitter

play03:43

developer portal so what we're first

play03:46

going to do is we're going to somehow

play03:47

get the keys now in my case since i'm

play03:49

not going to show them to you

play03:50

i save the keys in this text file here

play03:52

on the left and i'm going to read them

play03:54

into my script

play03:55

if you don't have anyone watching and if

play03:57

you if you don't want to hide it

play03:58

from anyone you can just go ahead and

play04:00

write them clear text into your python

play04:02

code so the only thing you need to do is

play04:03

you need to say

play04:04

api key equals whatever

play04:08

api key secret equals whatever and you

play04:11

just

play04:11

paste the strings from the twitter

play04:13

developer pool in my case i'm just going

play04:15

to read them from a file so i'm going to

play04:17

say

play04:18

my keys equals open

play04:21

twitter keys.txt you can also do it like

play04:25

that if you want just

play04:26

keep in mind that you need to have the

play04:27

right order and then we're going to say

play04:29

dot

play04:29

reads and then we're going to say split

play04:31

lines

play04:32

and that's essentially it and now i'm

play04:34

just going to say api

play04:36

key and here you paste your api key

play04:39

in my case i'm going to say my keys

play04:42

0 so the first one then we're going to

play04:45

say api key

play04:46

secret or api secret key

play04:49

equals my keys 1

play04:53

then we're going to say access token

play04:58

equals my keys 2

play05:01

and access token secret equals

play05:05

my keys my keys

play05:08

three as i said you don't need to do it

play05:11

like that you just write

play05:12

all the keys you can just copy paste

play05:14

them directly out of the

play05:16

developer portal if you want to so once

play05:19

we have done that the next thing that we

play05:20

need to do is we need to use those keys

play05:22

because those are just strings right now

play05:24

uh we need to use those keys in order to

play05:25

connect to the app so what we're going

play05:27

to do is we're going to

play05:28

define an authentication handler so

play05:30

we're going to say off

play05:32

handler equals twi.offhandler

play05:37

actually o off handler and what we

play05:40

specify here is the consumer key

play05:43

which is the api key and then we specify

play05:47

the uh i think it's called

play05:50

what is it called it's called consumer

play05:52

secret

play05:54

and here we're going to specify the api

play05:57

key secret

play05:59

then we're now connected to the uh api

play06:02

and now we need to just set the access

play06:04

token so that we know which app

play06:06

we're working on and we're going to say

play06:07

offhandler dot

play06:09

set access token and here we set the

play06:11

access token

play06:12

and the access token secret

play06:15

and then last but not least we create

play06:17

the actual api so we say api

play06:19

equals twi dot api

play06:24

using the auth handler that we just

play06:26

created

play06:28

and that is how you build the connection

play06:29

to the app

play06:31

so now we can actually go ahead and

play06:33

define the search term that we're

play06:34

interested in so the topic so to say

play06:36

uh that we want to analyze the sentiment

play06:38

for and in this case i'm just going to

play06:40

pick

play06:41

stocks for that and then the second

play06:43

thing that we need to do is we need to

play06:44

define

play06:44

or we can define uh the amount of tweets

play06:47

that we're interested in so we can base

play06:49

our analysis on 10 tweets on 100 tweets

play06:51

on a thousand tweets

play06:53

and of course the more tweets we use for

play06:55

the analysis

play06:57

the more quote-unquote accurate it will

play06:59

get now of course

play07:00

accurate is to be taken with a grain of

play07:02

salt here because we're using text blob

play07:04

and essentially what we're doing is

play07:06

we're looking at each word

play07:07

and then determining if this word is a

play07:10

positive one or negative one and then

play07:11

just adding up all these sentiments for

play07:13

the individual words

play07:14

which is not always uh in the right

play07:17

context of course

play07:19

so if i have for example not happy happy

play07:22

is still a positive word and it's not

play07:23

the most accurate thing to do

play07:25

however the more the more tweets we use

play07:27

the more quote-unquote accurate we get

play07:29

so we're going to say

play07:30

twitter or actually sorry tweet

play07:34

amount equals and we're just going to

play07:38

go for 200 uh in the beginning

play07:41

and then what we need to do is we need

play07:43

to use a cursor object

play07:45

in order to search for the term so we're

play07:47

going to say

play07:48

our results or actually that's let's

play07:51

call it tweets

play07:53

equals and then we're going to use twi

play07:56

dot cursor and

play08:00

here we need to specify the method the

play08:02

argument for the method

play08:04

and the language is an optional argument

play08:06

that we're definitely going to specify

play08:08

because otherwise we got all kinds of

play08:10

results here so the first thing is we're

play08:12

going to use api.search so the api that

play08:15

we already created here with

play08:17

our access token with our secret key

play08:19

with our api key

play08:20

we're going to use its search method

play08:23

then we're going to specify

play08:25

the parameter q here which is the actual

play08:27

term that we're going to search for so

play08:28

we're just going to pass a search term

play08:30

and then we're going to specify a

play08:32

language in this case

play08:34

english so lowercase e n

play08:38

is how you specify the english language

play08:40

because if you don't do that

play08:41

you'll get all kinds of hyperlinks and

play08:43

all kinds of

play08:44

uh random auto-generated messages

play08:48

uh maybe you get spanish french posts uh

play08:51

and the sentiment analysis and textblob

play08:53

as far as i know

play08:54

only works on english text so you'll get

play08:57

not the most accurate results

play08:59

and then we're going to use this object

play09:01

here sorry

play09:03

in order to call the items function and

play09:06

the items function

play09:07

essentially just specifies how many

play09:09

items we're interested in so

play09:11

we're going to get all the items uh and

play09:13

we can pass

play09:14

the tweet amount that we just created in

play09:16

order to limit it to 200

play09:18

items to 200 tweets so

play09:22

i think we should be able to see the

play09:24

tweets now so for tweet and tweets

play09:28

we should be able to print them out onto

play09:31

the screen

play09:32

let's see if this is the case so

play09:36

actually i need to run this thing run

play09:39

main

play09:42

and uh actually it works but i think we

play09:46

don't get the text so let me just see

play09:48

what i did here

play09:49

okay we need to say tweet dot text

play09:53

and then we should be able to see the

play09:54

tweets

play09:57

there you go as you can see there are a

play09:59

lot of things here

play10:00

a lot of noises so to say that we are

play10:03

not

play10:03

necessarily interested in so you can see

play10:05

we have a lot of rt

play10:06

rt we have a lot of ad we have a lot of

play10:10

mentions and all kinds of stuff so uh

play10:13

maybe we'll get rid of those as well to

play10:15

make the results more accurate

play10:18

so let's go ahead and clean up the

play10:19

tweets a little bit we're going to save

play10:21

for tweet and tweets and now we're going

play10:23

to

play10:24

um delete all these rt tags and also

play10:27

we're going to try to

play10:29

get rid of all these mentions here of

play10:32

all these add

play10:33

some account occurrences so we're going

play10:35

to first say

play10:36

final text you can also choose a more

play10:39

reasonable name here i'm just going to

play10:41

call it final text is essentially the

play10:43

tweet

play10:43

text that is remaining in the end and

play10:45

we're going to say it's just tweets

play10:47

dot text that we have but we're going to

play10:49

replace

play10:51

the rt with nothing so we're going to

play10:54

just get rid of the rt

play10:57

occurrences here so all of those here

play10:59

because they're essentially not

play11:00

important for the sentiment

play11:02

um also we're going to to remove all

play11:05

these

play11:06

at whatever at least at the start so

play11:09

we're not going to make two complicated

play11:11

expressions here but every time that add

play11:13

something appears at the start

play11:15

we're going to delete it so since we

play11:17

removed the rt we have

play11:18

a blank space here a white space and

play11:21

then the add

play11:22

username so what we're going to do is

play11:24

we're going to say if

play11:26

final text that we already removed the

play11:28

rt from

play11:29

if this text starts with

play11:34

with if it starts with blank space

play11:38

at what we're going to do is we're going

play11:40

to say find

play11:41

the colon colon because what we have

play11:43

here is we have an

play11:44

add then a username and it ends with a

play11:46

colon here

play11:47

so we're going to find the index of the

play11:49

colon so we're going to say

play11:51

position equals final text.index

play11:56

of the colon and then after that we're

play11:59

going to say final text

play12:01

equals final text but it starts

play12:04

from the position that we have um

play12:08

plus from then on

play12:11

so we're going to uh cut off the first

play12:14

part

play12:14

where the username is so this is the

play12:17

actual final text we're not going to

play12:20

we can actually go ahead and just print

play12:22

the final texture

play12:23

we're not going to clean up all the mess

play12:26

here we could also remove the hype links

play12:27

and all that

play12:28

uh actually it seems to not work

play12:32

every time i think because there are

play12:34

also user names that are not starting

play12:36

with rt so we ignored those

play12:38

we can actually also try the same thing

play12:40

for those i think

play12:43

if final text starts with that directly

play12:48

we could do the same thing i think

play12:51

then we should be able to get rid of

play12:53

those

play12:55

uh what do we have here substring not

play12:58

found

play12:58

oh i think that's the problem because um

play13:01

when they start

play13:02

with at username we don't get a colon

play13:05

there

play13:07

yeah as you can see let's just wait a

play13:10

little bit

play13:10

till it's done as you can see wherever

play13:14

we start with something like that we

play13:15

don't have a colon so we could actually

play13:17

go ahead

play13:18

uh and repeat the same thing but we

play13:20

would have to look for the white space

play13:23

like that and like that

play13:27

so this should work in order to remove

play13:29

those usernames as well

play13:33

most of the time at least yeah seems to

play13:36

work

play13:36

so we clean up uh we we have cleaned up

play13:39

the messages here and or the tweets here

play13:42

and now we can go ahead and do the

play13:43

sentiment analysis for this we're just

play13:45

going to create a textbook object we're

play13:47

going to say analysis

play13:49

equals text blob of the final text so

play13:52

we're going to not pass the tweet text

play13:54

but the final text the cleaned up text

play13:57

and then we're just going to find the

play14:00

polarity of it

play14:01

and overall we're going to create a

play14:03

polarity object starting at zero so

play14:05

polarity equals zero

play14:08

and we're just going to add the polarity

play14:10

of the individual tweets

play14:11

to that polarity uh variable so we're

play14:14

going to say analysis

play14:15

dot polarity because if a text has a

play14:18

polarity of 10

play14:20

um it's probably a very positive text if

play14:23

it has a polarity of -10 it's a very

play14:25

negative text

play14:26

so if you combine them you end up with

play14:28

zero because you have one very negative

play14:30

one very positive text so it's neutral

play14:32

if you have a very very positive text

play14:34

like a

play14:34

polarity of let's say 100 then you have

play14:36

-10 you have a very positive text

play14:40

and a somewhat negative text and when

play14:42

you combine them you have still

play14:44

positive polarity overall so this is how

play14:46

it works it's enough to just

play14:48

add them up um and then what we can do

play14:52

is we can actually just go ahead and

play14:54

print

play14:54

the polarity itself if that is enough

play14:59

uh so we're not going to see all the

play15:00

tweets right now since we're not

play15:02

printing them we're just going to get an

play15:03

overall polarity at the end

play15:05

hopefully and you can see it's positive

play15:09

because everything above

play15:10

0 is positive the more it's above zero

play15:12

the more positive it is

play15:14

um i'm not sure i i figured that

play15:17

whenever i use the twitter api or

play15:18

whenever i use text blob in general

play15:20

i have a positive bias even if i look

play15:23

for topics like war or disease or

play15:25

something i still get

play15:26

very uh not very but at least slightly

play15:30

positive

play15:31

sentiment but we can try let's see what

play15:33

happens when we look for war

play15:35

um i'm not sure why this is but i think

play15:38

because

play15:38

most words are actually positive and

play15:40

there are very few words that are

play15:42

negative and

play15:43

people don't use them because people

play15:44

often times use as you can see we have a

play15:46

polarity of 10 which is positive

play15:48

i think people oftentimes use stuff like

play15:51

not good not happy

play15:53

or something like that and it makes the

play15:56

analysis

play15:58

tend a little bit to be more positive

play16:00

however

play16:01

what we can do as well is we can

play16:04

actually count

play16:04

the amount of positive tweets the amount

play16:06

of negative tweets and the amount of

play16:07

neutral tweets

play16:08

i don't think that we will have any

play16:10

completely neutral tweets

play16:12

but we can actually just go ahead and

play16:13

say positive equals zero

play16:16

neutral equals zero negative equals zero

play16:20

and then whenever we have um we can say

play16:25

uh tweet polarity

play16:28

equals analysis dot polarity and we can

play16:32

say

play16:33

if tweet polarity

play16:37

is larger than zero

play16:40

positive plus equals one

play16:45

um alif tweet polarity

play16:50

less than zero

play16:53

positive no sorry negative plus equals

play16:57

one and else if it's exactly zero we're

play17:00

just going to say neutral equals

play17:02

plus equals one and then here we add

play17:06

the tweak polarity as well so we can

play17:09

print the polarity we can

play17:10

um print f string

play17:14

amount of positive

play17:18

tweets is positive

play17:23

and then we can just go ahead

play17:26

copy that for negative and

play17:30

neutral

play17:33

and then just exchange or swap the

play17:36

values here so

play17:37

neutral there you go

play17:42

um and one thing that you could do since

play17:46

we have a positive bias is you can

play17:48

actually say if you really want

play17:50

something to be considered positive

play17:52

you have to demand that it crosses at

play17:54

least like 15 or 20 or something

play17:56

because if even topics like war or

play17:58

actually maybe war

play17:59

is is really a positive topic because we

play18:02

have um

play18:03

so little war so people maybe talk about

play18:05

how little war we have

play18:06

uh this is also a possibility so maybe

play18:08

that's really uh

play18:09

the case here but actually you could

play18:12

also just set a higher standard so you

play18:14

can say we don't consider something to

play18:16

be positive

play18:17

unless it has at least a polarity of uh

play18:20

15 or something so because i very very

play18:24

rarely get something below zero and if

play18:26

it's below zero it's like

play18:27

minus five or something you rarely get

play18:29

something like -10

play18:30

because as i said most words are

play18:32

probably positive but we can try for a

play18:34

different topic here

play18:35

let's uh see something like um

play18:38

what is a negative topic that will not

play18:40

get demonetized here i know what you're

play18:42

all thinking but i cannot do that

play18:43

because

play18:44

if i use the word that you're all

play18:46

thinking about i probably

play18:48

uh will get demonetized here so i'm

play18:50

going to go with

play18:51

something like death because death is

play18:53

not never a good topic

play18:55

and let's also go ahead and print the

play18:58

tweets

play18:58

so that we can see what's happening here

play19:02

not tweet final text and then we can see

play19:07

the results

play19:10

prison death benefit death

play19:13

immediately death death death there you

play19:16

go

play19:17

um still positive for some reason we

play19:19

have 63 positive

play19:20

uh tweets then we have 40 negative

play19:24

and 97 neutral tweets i'm not sure if

play19:27

this is actually

play19:29

maybe we need to work with with floating

play19:31

point numbers

play19:33

so maybe we should say larger than 0.00

play19:38

less than 0 0 0 and then neutral

play19:42

is only we're not going to do else we're

play19:44

going to alif

play19:45

tweet polarity only if it is exactly

play19:49

0.00 so maybe that is a problem here

play19:51

because i don't think that we have

play19:53

97 tweets that have exactly zero as

play19:56

polarity

play19:56

i mean could be but it seems unrealistic

play19:59

to me

play20:03

so let's see oh

play20:06

we still have a lot more so yeah i think

play20:09

the only way to

play20:10

to really do that i mean we could also

play20:12

go ahead and read through all the tweets

play20:13

here maybe they

play20:14

really are positive tweets or neutral

play20:16

tweets uh but i think if they are

play20:18

actually

play20:19

negative in your perception and the tool

play20:22

still outputs that they're positive you

play20:24

can just set the standards the threshold

play20:26

higher

play20:26

you can say i only consider something to

play20:28

be positive if it's above

play20:30

20 for example so we can actually try to

play20:33

to go with a topic like

play20:34

happy or happiness because i think there

play20:37

we will get a much higher number than

play20:39

eight

play20:39

maybe i'm also wrong i don't know i'm

play20:41

always surprised by this uh

play20:43

this sentiment analysis here but

play20:46

actually i think if something is really

play20:48

really positive yeah as you can see we

play20:49

get 81

play20:50

so we can actually consider something to

play20:52

be positive uh only if it is above

play20:55

20 or something or 15 at least or

play20:57

anything like that

play20:58

uh you can do that however you want but

play21:00

that's how you analyze the sentiment for

play21:02

a specific topic

play21:04

on twitter so that's it for this we hope

play21:06

you enjoyed it i hope you'll learn

play21:07

something if so let me know by hitting

play21:09

the like button and leaving a comment in

play21:10

the comment section down below

play21:11

uh also feel free to make any

play21:14

suggestions in the comment section down

play21:15

below

play21:16

for projects that you would like to see

play21:17

in the future um

play21:19

i know that i not don't always do the

play21:21

projects that you suggest or

play21:22

ask for but sometimes it is just because

play21:25

i'm not capable of

play21:26

yet because a lot of you guys have

play21:28

requested django tutorials for example

play21:31

but i'm simply not good at django i've

play21:33

not used django enough i've not worked

play21:35

with django i'm not good at python web

play21:37

development yet

play21:38

because i haven't educated myself in

play21:40

that area

play21:41

so in order to make django tutorials i

play21:43

need to first learn

play21:44

django myself for example and this is

play21:46

also true for a lot of other projects

play21:48

but feel free to suggest any ai projects

play21:51

networking projects in the comment

play21:53

section down below

play21:54

if i think they're a good idea and if

play21:55

i'm capable of implementing them i'm

play21:57

definitely going to make a video

play21:59

on them other than that make sure you

play22:01

subscribe to this channel in order to

play22:02

see more future

play22:04

videos for free and thank you very much

play22:05

for watching see you next video

play22:10

bye

play22:20

[Music]

play22:23

you

Rate This

5.0 / 5 (0 votes)

関連タグ
Python TutorialSentiment AnalysisTwitter APITextBlobTweepyNLP ToolsData AnalysisSocial MediaCoding ProjectAI TutorialWeb Development
英語で要約が必要ですか?