SlideShare a Scribd company logo
1 of 29
NLTK Sentiment Analysis
CHAPTER ā€“ 4
THE BASICS OF SEARCH ENGINE FRIENDLY DESIGN & DEVELOPMENT
NLTK Sentiment Analysis
About NLTK :
The Natural Language Toolkit, or more commonly NLTK, is a suite of libraries and
programs for symbolic and statistical natural language processing (NLP) for English
written in the Python programming language.
It was developed by Steven Bird and Edward Loper in the Department of Computer
and Information Science at the University of Pennsylvania.
Copyright @ 2019 Learntek. All Rights Reserved.
Copyright @ 2019 Learntek. All Rights Reserved. 4
Sentiment Analysis :
Sentiment Analysis is a branch of computer science, and overlaps heavily with
Machine Learning, and Computational Linguistics Sentiment Analysis is the
most common text classification tool that analyses an incoming message and
tells whether the underlying sentiment is positive, negative our neutral.
It the process of computationally identifying and categorizing opinions
expressed in a piece of text, especially in order to determine whether the
writerā€™s attitude towards a particular topic, product, etc. is positive, negative,
or neutral.
Copyright @ 2019 Learntek. All Rights Reserved. 5
Sentiment Analysis is a concept of Natural Language Processing and Sometimes
referred to as opinion mining, although the emphasis in this case is on extraction
Copyright @ 2019 Learntek. All Rights Reserved. 6
Examples of the sentimental analysis are as follows :
ā€¢Is this product review positive or negative?
ā€¢Is this customer email satisfied or dissatisfied?
ā€¢Based on a sample of tweets, how are people responding to this ad
campaign/product release/news item?
ā€¢How have bloggersā€™ attitudes about the president changed since the election?
ā€¢The purpose of this Sentiment Analysis is to automatically classify a tweet as a
positive or Negative Tweet Sentiment wise
Copyright @ 2019 Learntek. All Rights Reserved. 7
ā€¢Given a movie review or a tweet, it can be automatically classified in categories.
These categories can be user defined (positive, negative) or whichever classes you
want.
ā€¢Sentiment Analysis for Brand Monitoring
ā€¢Sentiment Analysis for Customer Service
ā€¢Sentiment Analysis for Market Research and Analysis
Copyright @ 2019 Learntek. All Rights Reserved. 8
Copyright @ 2015 Learntek. All Rights Reserved.
9
Sample Positive Tweets
ā€¢I love this car
ā€¢This view is amazing
ā€¢I feel great this morning
ā€¢I am so excited about the concert
ā€¢He is my best friend
Sample Negative Tweets
ā€¢I do not like this car
ā€¢This view is horrible
ā€¢I feel tired this morning
ā€¢I am not looking forward to the concert
ā€¢He is my enemy
Copyright @ 2019 Learntek. All Rights Reserved. 10
Sentimental Analysis Process
ā€¢The list of word features need to be extracted from the tweets.
ā€¢It is a list with every distinct word ordered by frequency of appearance.
ā€¢The use of Feature Extractor to decide which features are more relevant.
ā€¢The one we are going to use returns a dictionary indicating that words are
contained in the input passed.
Copyright @ 2019 Learntek. All Rights Reserved. 11
Copyright @ 2019 Learntek. All Rights Reserved. 12
Naive Bayes Classifier
ā€¢It uses the prior probability of each label ā€“ which is the frequency of each label in
the training set and the contribution from each feature.
ā€¢In our case, the frequency of each label is the same for ā€˜positiveā€™ and ā€˜negativeā€™.
ā€¢Word ā€˜amazingā€™ appears in 1 of 5 of the positive tweets and none of the negative
tweets.
ā€¢This means that the likelihood of the ā€˜positiveā€™ label will be multiplied by 0.2 when
this word is seen as part of the input
Copyright @ 2019 Learntek. All Rights Reserved. 13
Sentiment Analysis Example 1 :
Training Data
1.This is a good book! Positive
2.This is a awesome book! Positive
3.This is a bad book! Negative
4.This is a terrible book! Negative
Testing Data
ā€¢This is a good article
ā€¢This is a bad article
Copyright @ 2019 Learntek. All Rights Reserved. 14
We will train the model with the help of training data by using NaĆÆve Bayes
Classifier.
And then test the model on testing data.
Copyright @ 2019 Learntek. All Rights Reserved. 15
>>> def form_sent(sent):
...return {word: True for word in nltk.word_tokenize(sent)}
...
>>> form_sent("This is a good book")
{'This': True, 'is': True, 'a': True, 'good': True, 'book': True}
>>> s1='This is a good bookā€™
>>> s2='This is a awesome bookā€™
>>> s3='This is a bad bookā€™
>>> s4='This is a terrible book'
>>> training_data=[[form_sent(s1),'pos'],[form_sent(s2),'pos'],[form_sent(s3),'neg'],[form_sent(s4),'neg']]
>>> for t in training_data:print(t)
...
[{'This': True, 'is': True, 'a': True, 'good': True, 'book': True}, 'posā€™]
[{'This': True, 'is': True, 'a': True, 'awesome': True, 'book': True}, 'pos']
Copyright @ 2019 Learntek. All Rights Reserved. 16
[{'This': True, 'is': True, 'a': True, 'bad': True, 'book': True}, 'negā€™]
[{'This': True, 'is': True, 'a': True, 'terrible': True, 'book': True}, 'negā€™]
>>> from nltk.classify import NaiveBayesClassifier
>>> model = NaiveBayesClassifier.train(training_data)
>>>model.classify(form_sent('This is a good articleā€™))
'posā€™
>>>model.classify(form_sent('This is a bad articleā€™))
'negā€™
>>>
Copyright @ 2019 Learntek. All Rights Reserved. 17
Copyright @ 2019 Learntek. All Rights Reserved. 18
Accuracy
NLTK has a built-in method that computes the accuracy rate of our model:
>>> from nltk.classify.util import accuracy
Sentiment Analysis Example 2 :
Gender Identification: ā€“ we know that male and female names have some distinctive
characteristics. Generally, Names ending in a, e and i are likely to be female, while
names ending in k, o, r, s and t are likely to be male.
We build a classifier to model these differences more precisely.
Copyright @ 2019 Learntek. All Rights Reserved. 19
>>> def gender_features(word):
... return {'last_letter': word[-1]}
>>> gender_features('Shrekā€™)
{'last_letter': 'k'}
Now that weā€™ve defined a feature extractor, we need to prepare a list of examples
and corresponding class labels.
>>> from nltk.corpus import names
>>> labeled_names = ([(name, 'male') for name in
names.words('male.txt')] +
... [(name, 'female') for name in names.words('female.txt')])
>>> import random
>>> random.shuffle(labeled_names)
Copyright @ 2019 Learntek. All Rights Reserved. 20
Next, the feature extractor is using to process the names data and divide the
resulting list of feature sets into a training set and a test set. The training set is
used to train a new ā€œnaive Bayesā€ classifier.
>>> featuresets = [(gender_features(n), gender) for (n, gender) in labeled_names]
>>> train_set, test_set = featuresets[500:], featuresets[:500]
>>> classifier = nltk.NaiveBayesClassifier.train(train_set)
Copyright @ 2019 Learntek. All Rights Reserved. 21
Copyright @ 2019 Learntek. All Rights Reserved.
22
Letā€™s just test it out on some names that did not appear in its training data:
>>> classifier.classify(gender_features('Neoā€™))
'maleā€™
>>> classifier.classify(gender_features('olvinā€™))
'maleā€™
>>> classifier.classify(gender_features('rickyā€™))
'femaleā€™
>>> classifier.classify(gender_features('serenaā€™))
'female'
Copyright @ 2019 Learntek. All Rights Reserved. 23
>>> classifier.classify(gender_features('cyraā€™))
'femaleā€™
>>> classifier.classify(gender_features('leetaā€™))
'femaleā€™
>>> classifier.classify(gender_features('rockā€™))
'maleā€™
>>> classifier.classify(gender_features('jackā€™))
'male'
Copyright @ 2019 Learntek. All Rights Reserved. 24
We can systematically evaluate the classifier on a much larger quantity of unseen
data:
>>> print(nltk.classify.accuracy(classifier, test_set))
0.77
Finally, we can examine the classifier to determine which features it found most
effective for distinguishing the namesā€™ genders:
Copyright @ 2019 Learntek. All Rights Reserved. 25
>>> classifier.show_most_informative_features(20)
Most Informative Features
last_letter = 'a' female : male = 35.5 : 1.0
last_letter = 'k' male : female = 30.7 : 1.0
last_letter = 'p' male : female = 20.8 : 1.0
last_letter = 'f' male : female = 15.9 : 1.0
last_letter = 'd' male : female = 11.5 : 1.0
last_letter = 'v' male : female = 9.8 : 1.0
Copyright @ 2019 Learntek. All Rights Reserved. 26
last_letter = 'o' male : female = 8.7 : 1.0
last_letter = 'w' male : female = 8.4 : 1.0
last_letter = 'm' male : female = 8.2 : 1.0
last_letter = 'r' male : female = 7.0 : 1.0
last_letter = 'g' male : female = 5.1 : 1.0
last_letter = 'b' male : female = 4.4 : 1.0
last_letter = 's' male : female = 4.3 : 1.0
Copyright @ 2019 Learntek. All Rights Reserved. 27
last_letter = 'z' male : female = 3.9 : 1.0
last_letter = 'j' male : female = 3.9 : 1.0
last_letter = 't' male : female = 3.8 : 1.0
last_letter = 'i' female : male = 3.8 : 1.0
last_letter = 'u' male : female = 3.0 : 1.0
last_letter = 'n' male : female = 2.1 : 1.0
last_letter = 'e' female : male = 1.8 : 1.0
Copyright @ 2019 Learntek. All Rights Reserved. 28
Copyright @ 2019 Learntek. All Rights Reserved. 29
For more Training Information , Contact Us
Email : info@learntek.org
USA : +1734 418 2465
INDIA : +40 4018 1306
+7799713624

More Related Content

Similar to Nltk sentiment analysis

1 Exploratory Data Analysis (EDA) by Melvin Ott, PhD.docx
1 Exploratory Data Analysis (EDA) by Melvin Ott, PhD.docx1 Exploratory Data Analysis (EDA) by Melvin Ott, PhD.docx
1 Exploratory Data Analysis (EDA) by Melvin Ott, PhD.docxhoney725342
Ā 
Programming Style Guidelines for Java Programming The fo.docx
Programming Style Guidelines for Java Programming The fo.docxProgramming Style Guidelines for Java Programming The fo.docx
Programming Style Guidelines for Java Programming The fo.docxwkyra78
Ā 
Buy Custom Essays Online Buyessaycheaper Com
Buy Custom Essays Online Buyessaycheaper ComBuy Custom Essays Online Buyessaycheaper Com
Buy Custom Essays Online Buyessaycheaper ComAmy Isleb
Ā 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonJanu Jahnavi
Ā 
Sample Essay Question And Answer
Sample Essay Question And AnswerSample Essay Question And Answer
Sample Essay Question And AnswerCarolyn Smith
Ā 
Sentiment Analysis: A comparative study of Deep Learning and Machine Learning
Sentiment Analysis: A comparative study of Deep Learning and Machine LearningSentiment Analysis: A comparative study of Deep Learning and Machine Learning
Sentiment Analysis: A comparative study of Deep Learning and Machine LearningIRJET Journal
Ā 
A FILM SYNOPSIS GENRE CLASSIFIER BASED ON MAJORITY VOTE
A FILM SYNOPSIS GENRE CLASSIFIER BASED ON MAJORITY VOTEA FILM SYNOPSIS GENRE CLASSIFIER BASED ON MAJORITY VOTE
A FILM SYNOPSIS GENRE CLASSIFIER BASED ON MAJORITY VOTEkevig
Ā 
NLP based Mining on Movie Critics
NLP based Mining on Movie Critics NLP based Mining on Movie Critics
NLP based Mining on Movie Critics supraja reddy
Ā 
Business intelligence analytics using sentiment analysis-a survey
Business intelligence analytics using sentiment analysis-a surveyBusiness intelligence analytics using sentiment analysis-a survey
Business intelligence analytics using sentiment analysis-a surveyIJECEIAES
Ā 
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxNLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxBoston Institute of Analytics
Ā 
Can I Pay Someone To Write My Research Paper - The
Can I Pay Someone To Write My Research Paper - TheCan I Pay Someone To Write My Research Paper - The
Can I Pay Someone To Write My Research Paper - TheLaura Smith
Ā 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonJanu Jahnavi
Ā 
Live Twitter Sentiment Analysis and Interactive Visualizations with PyLDAvis ...
Live Twitter Sentiment Analysis and Interactive Visualizations with PyLDAvis ...Live Twitter Sentiment Analysis and Interactive Visualizations with PyLDAvis ...
Live Twitter Sentiment Analysis and Interactive Visualizations with PyLDAvis ...IRJET Journal
Ā 
Slides
SlidesSlides
Slidesbutest
Ā 

Similar to Nltk sentiment analysis (20)

Aman chaudhary
 Aman chaudhary Aman chaudhary
Aman chaudhary
Ā 
1 Exploratory Data Analysis (EDA) by Melvin Ott, PhD.docx
1 Exploratory Data Analysis (EDA) by Melvin Ott, PhD.docx1 Exploratory Data Analysis (EDA) by Melvin Ott, PhD.docx
1 Exploratory Data Analysis (EDA) by Melvin Ott, PhD.docx
Ā 
402 w2
402 w2402 w2
402 w2
Ā 
Programming Style Guidelines for Java Programming The fo.docx
Programming Style Guidelines for Java Programming The fo.docxProgramming Style Guidelines for Java Programming The fo.docx
Programming Style Guidelines for Java Programming The fo.docx
Ā 
Buy Custom Essays Online Buyessaycheaper Com
Buy Custom Essays Online Buyessaycheaper ComBuy Custom Essays Online Buyessaycheaper Com
Buy Custom Essays Online Buyessaycheaper Com
Ā 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk python
Ā 
Sample Essay Question And Answer
Sample Essay Question And AnswerSample Essay Question And Answer
Sample Essay Question And Answer
Ā 
Sentiment Analysis: A comparative study of Deep Learning and Machine Learning
Sentiment Analysis: A comparative study of Deep Learning and Machine LearningSentiment Analysis: A comparative study of Deep Learning and Machine Learning
Sentiment Analysis: A comparative study of Deep Learning and Machine Learning
Ā 
A FILM SYNOPSIS GENRE CLASSIFIER BASED ON MAJORITY VOTE
A FILM SYNOPSIS GENRE CLASSIFIER BASED ON MAJORITY VOTEA FILM SYNOPSIS GENRE CLASSIFIER BASED ON MAJORITY VOTE
A FILM SYNOPSIS GENRE CLASSIFIER BASED ON MAJORITY VOTE
Ā 
NLP based Mining on Movie Critics
NLP based Mining on Movie Critics NLP based Mining on Movie Critics
NLP based Mining on Movie Critics
Ā 
Business intelligence analytics using sentiment analysis-a survey
Business intelligence analytics using sentiment analysis-a surveyBusiness intelligence analytics using sentiment analysis-a survey
Business intelligence analytics using sentiment analysis-a survey
Ā 
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxNLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
Ā 
Can I Pay Someone To Write My Research Paper - The
Can I Pay Someone To Write My Research Paper - TheCan I Pay Someone To Write My Research Paper - The
Can I Pay Someone To Write My Research Paper - The
Ā 
Dc Ielts Essays
Dc Ielts EssaysDc Ielts Essays
Dc Ielts Essays
Ā 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk python
Ā 
Live Twitter Sentiment Analysis and Interactive Visualizations with PyLDAvis ...
Live Twitter Sentiment Analysis and Interactive Visualizations with PyLDAvis ...Live Twitter Sentiment Analysis and Interactive Visualizations with PyLDAvis ...
Live Twitter Sentiment Analysis and Interactive Visualizations with PyLDAvis ...
Ā 
Introduction To Pc Security
Introduction To Pc SecurityIntroduction To Pc Security
Introduction To Pc Security
Ā 
Introduction To Pc Security
Introduction To Pc SecurityIntroduction To Pc Security
Introduction To Pc Security
Ā 
Introduction To Pc Security
Introduction To Pc SecurityIntroduction To Pc Security
Introduction To Pc Security
Ā 
Slides
SlidesSlides
Slides
Ā 

More from Janu Jahnavi

Analytics using r programming
Analytics using r programmingAnalytics using r programming
Analytics using r programmingJanu Jahnavi
Ā 
Software testing
Software testingSoftware testing
Software testingJanu Jahnavi
Ā 
Software testing
Software testingSoftware testing
Software testingJanu Jahnavi
Ā 
Stack skills
Stack skillsStack skills
Stack skillsJanu Jahnavi
Ā 
Apache flink
Apache flinkApache flink
Apache flinkJanu Jahnavi
Ā 
Apache flink
Apache flinkApache flink
Apache flinkJanu Jahnavi
Ā 
Mysql python
Mysql pythonMysql python
Mysql pythonJanu Jahnavi
Ā 
Mysql python
Mysql pythonMysql python
Mysql pythonJanu Jahnavi
Ā 
Ruby with cucmber
Ruby with cucmberRuby with cucmber
Ruby with cucmberJanu Jahnavi
Ā 
Apache kafka
Apache kafkaApache kafka
Apache kafkaJanu Jahnavi
Ā 
Apache kafka
Apache kafkaApache kafka
Apache kafkaJanu Jahnavi
Ā 
Google cloud platform
Google cloud platformGoogle cloud platform
Google cloud platformJanu Jahnavi
Ā 
Google cloud Platform
Google cloud PlatformGoogle cloud Platform
Google cloud PlatformJanu Jahnavi
Ā 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8Janu Jahnavi
Ā 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8Janu Jahnavi
Ā 
Python multithreading
Python multithreadingPython multithreading
Python multithreadingJanu Jahnavi
Ā 
Python multithreading
Python multithreadingPython multithreading
Python multithreadingJanu Jahnavi
Ā 

More from Janu Jahnavi (20)

Analytics using r programming
Analytics using r programmingAnalytics using r programming
Analytics using r programming
Ā 
Software testing
Software testingSoftware testing
Software testing
Ā 
Software testing
Software testingSoftware testing
Software testing
Ā 
Spring
SpringSpring
Spring
Ā 
Stack skills
Stack skillsStack skills
Stack skills
Ā 
Ui devopler
Ui devoplerUi devopler
Ui devopler
Ā 
Apache flink
Apache flinkApache flink
Apache flink
Ā 
Apache flink
Apache flinkApache flink
Apache flink
Ā 
Angular js
Angular jsAngular js
Angular js
Ā 
Mysql python
Mysql pythonMysql python
Mysql python
Ā 
Mysql python
Mysql pythonMysql python
Mysql python
Ā 
Ruby with cucmber
Ruby with cucmberRuby with cucmber
Ruby with cucmber
Ā 
Apache kafka
Apache kafkaApache kafka
Apache kafka
Ā 
Apache kafka
Apache kafkaApache kafka
Apache kafka
Ā 
Google cloud platform
Google cloud platformGoogle cloud platform
Google cloud platform
Ā 
Google cloud Platform
Google cloud PlatformGoogle cloud Platform
Google cloud Platform
Ā 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8
Ā 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8
Ā 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
Ā 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
Ā 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
Ā 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
Ā 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
Ā 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
Ā 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
Ā 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
Ā 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
Ā 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
Ā 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
Ā 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
Ā 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
Ā 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
Ā 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
Ā 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
Ā 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
Ā 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
Ā 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
Ā 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
Ā 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
Ā 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Ā 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Ā 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
Ā 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
Ā 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
Ā 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
Ā 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
Ā 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
Ā 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
Ā 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
Ā 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
Ā 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
Ā 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
Ā 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
Ā 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
Ā 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
Ā 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
Ā 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Ā 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
Ā 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
Ā 

Nltk sentiment analysis

  • 2. CHAPTER ā€“ 4 THE BASICS OF SEARCH ENGINE FRIENDLY DESIGN & DEVELOPMENT
  • 3. NLTK Sentiment Analysis About NLTK : The Natural Language Toolkit, or more commonly NLTK, is a suite of libraries and programs for symbolic and statistical natural language processing (NLP) for English written in the Python programming language. It was developed by Steven Bird and Edward Loper in the Department of Computer and Information Science at the University of Pennsylvania. Copyright @ 2019 Learntek. All Rights Reserved.
  • 4. Copyright @ 2019 Learntek. All Rights Reserved. 4 Sentiment Analysis : Sentiment Analysis is a branch of computer science, and overlaps heavily with Machine Learning, and Computational Linguistics Sentiment Analysis is the most common text classification tool that analyses an incoming message and tells whether the underlying sentiment is positive, negative our neutral. It the process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writerā€™s attitude towards a particular topic, product, etc. is positive, negative, or neutral.
  • 5. Copyright @ 2019 Learntek. All Rights Reserved. 5 Sentiment Analysis is a concept of Natural Language Processing and Sometimes referred to as opinion mining, although the emphasis in this case is on extraction
  • 6. Copyright @ 2019 Learntek. All Rights Reserved. 6 Examples of the sentimental analysis are as follows : ā€¢Is this product review positive or negative? ā€¢Is this customer email satisfied or dissatisfied? ā€¢Based on a sample of tweets, how are people responding to this ad campaign/product release/news item? ā€¢How have bloggersā€™ attitudes about the president changed since the election? ā€¢The purpose of this Sentiment Analysis is to automatically classify a tweet as a positive or Negative Tweet Sentiment wise
  • 7. Copyright @ 2019 Learntek. All Rights Reserved. 7 ā€¢Given a movie review or a tweet, it can be automatically classified in categories. These categories can be user defined (positive, negative) or whichever classes you want. ā€¢Sentiment Analysis for Brand Monitoring ā€¢Sentiment Analysis for Customer Service ā€¢Sentiment Analysis for Market Research and Analysis
  • 8. Copyright @ 2019 Learntek. All Rights Reserved. 8
  • 9. Copyright @ 2015 Learntek. All Rights Reserved. 9 Sample Positive Tweets ā€¢I love this car ā€¢This view is amazing ā€¢I feel great this morning ā€¢I am so excited about the concert ā€¢He is my best friend Sample Negative Tweets ā€¢I do not like this car ā€¢This view is horrible ā€¢I feel tired this morning ā€¢I am not looking forward to the concert ā€¢He is my enemy
  • 10. Copyright @ 2019 Learntek. All Rights Reserved. 10 Sentimental Analysis Process ā€¢The list of word features need to be extracted from the tweets. ā€¢It is a list with every distinct word ordered by frequency of appearance. ā€¢The use of Feature Extractor to decide which features are more relevant. ā€¢The one we are going to use returns a dictionary indicating that words are contained in the input passed.
  • 11. Copyright @ 2019 Learntek. All Rights Reserved. 11
  • 12. Copyright @ 2019 Learntek. All Rights Reserved. 12 Naive Bayes Classifier ā€¢It uses the prior probability of each label ā€“ which is the frequency of each label in the training set and the contribution from each feature. ā€¢In our case, the frequency of each label is the same for ā€˜positiveā€™ and ā€˜negativeā€™. ā€¢Word ā€˜amazingā€™ appears in 1 of 5 of the positive tweets and none of the negative tweets. ā€¢This means that the likelihood of the ā€˜positiveā€™ label will be multiplied by 0.2 when this word is seen as part of the input
  • 13. Copyright @ 2019 Learntek. All Rights Reserved. 13 Sentiment Analysis Example 1 : Training Data 1.This is a good book! Positive 2.This is a awesome book! Positive 3.This is a bad book! Negative 4.This is a terrible book! Negative Testing Data ā€¢This is a good article ā€¢This is a bad article
  • 14. Copyright @ 2019 Learntek. All Rights Reserved. 14 We will train the model with the help of training data by using NaĆÆve Bayes Classifier. And then test the model on testing data.
  • 15. Copyright @ 2019 Learntek. All Rights Reserved. 15 >>> def form_sent(sent): ...return {word: True for word in nltk.word_tokenize(sent)} ... >>> form_sent("This is a good book") {'This': True, 'is': True, 'a': True, 'good': True, 'book': True} >>> s1='This is a good bookā€™ >>> s2='This is a awesome bookā€™ >>> s3='This is a bad bookā€™ >>> s4='This is a terrible book' >>> training_data=[[form_sent(s1),'pos'],[form_sent(s2),'pos'],[form_sent(s3),'neg'],[form_sent(s4),'neg']] >>> for t in training_data:print(t) ... [{'This': True, 'is': True, 'a': True, 'good': True, 'book': True}, 'posā€™] [{'This': True, 'is': True, 'a': True, 'awesome': True, 'book': True}, 'pos']
  • 16. Copyright @ 2019 Learntek. All Rights Reserved. 16 [{'This': True, 'is': True, 'a': True, 'bad': True, 'book': True}, 'negā€™] [{'This': True, 'is': True, 'a': True, 'terrible': True, 'book': True}, 'negā€™] >>> from nltk.classify import NaiveBayesClassifier >>> model = NaiveBayesClassifier.train(training_data) >>>model.classify(form_sent('This is a good articleā€™)) 'posā€™ >>>model.classify(form_sent('This is a bad articleā€™)) 'negā€™ >>>
  • 17. Copyright @ 2019 Learntek. All Rights Reserved. 17
  • 18. Copyright @ 2019 Learntek. All Rights Reserved. 18 Accuracy NLTK has a built-in method that computes the accuracy rate of our model: >>> from nltk.classify.util import accuracy Sentiment Analysis Example 2 : Gender Identification: ā€“ we know that male and female names have some distinctive characteristics. Generally, Names ending in a, e and i are likely to be female, while names ending in k, o, r, s and t are likely to be male. We build a classifier to model these differences more precisely.
  • 19. Copyright @ 2019 Learntek. All Rights Reserved. 19 >>> def gender_features(word): ... return {'last_letter': word[-1]} >>> gender_features('Shrekā€™) {'last_letter': 'k'} Now that weā€™ve defined a feature extractor, we need to prepare a list of examples and corresponding class labels. >>> from nltk.corpus import names >>> labeled_names = ([(name, 'male') for name in names.words('male.txt')] + ... [(name, 'female') for name in names.words('female.txt')]) >>> import random >>> random.shuffle(labeled_names)
  • 20. Copyright @ 2019 Learntek. All Rights Reserved. 20 Next, the feature extractor is using to process the names data and divide the resulting list of feature sets into a training set and a test set. The training set is used to train a new ā€œnaive Bayesā€ classifier. >>> featuresets = [(gender_features(n), gender) for (n, gender) in labeled_names] >>> train_set, test_set = featuresets[500:], featuresets[:500] >>> classifier = nltk.NaiveBayesClassifier.train(train_set)
  • 21. Copyright @ 2019 Learntek. All Rights Reserved. 21
  • 22. Copyright @ 2019 Learntek. All Rights Reserved. 22 Letā€™s just test it out on some names that did not appear in its training data: >>> classifier.classify(gender_features('Neoā€™)) 'maleā€™ >>> classifier.classify(gender_features('olvinā€™)) 'maleā€™ >>> classifier.classify(gender_features('rickyā€™)) 'femaleā€™ >>> classifier.classify(gender_features('serenaā€™)) 'female'
  • 23. Copyright @ 2019 Learntek. All Rights Reserved. 23 >>> classifier.classify(gender_features('cyraā€™)) 'femaleā€™ >>> classifier.classify(gender_features('leetaā€™)) 'femaleā€™ >>> classifier.classify(gender_features('rockā€™)) 'maleā€™ >>> classifier.classify(gender_features('jackā€™)) 'male'
  • 24. Copyright @ 2019 Learntek. All Rights Reserved. 24 We can systematically evaluate the classifier on a much larger quantity of unseen data: >>> print(nltk.classify.accuracy(classifier, test_set)) 0.77 Finally, we can examine the classifier to determine which features it found most effective for distinguishing the namesā€™ genders:
  • 25. Copyright @ 2019 Learntek. All Rights Reserved. 25 >>> classifier.show_most_informative_features(20) Most Informative Features last_letter = 'a' female : male = 35.5 : 1.0 last_letter = 'k' male : female = 30.7 : 1.0 last_letter = 'p' male : female = 20.8 : 1.0 last_letter = 'f' male : female = 15.9 : 1.0 last_letter = 'd' male : female = 11.5 : 1.0 last_letter = 'v' male : female = 9.8 : 1.0
  • 26. Copyright @ 2019 Learntek. All Rights Reserved. 26 last_letter = 'o' male : female = 8.7 : 1.0 last_letter = 'w' male : female = 8.4 : 1.0 last_letter = 'm' male : female = 8.2 : 1.0 last_letter = 'r' male : female = 7.0 : 1.0 last_letter = 'g' male : female = 5.1 : 1.0 last_letter = 'b' male : female = 4.4 : 1.0 last_letter = 's' male : female = 4.3 : 1.0
  • 27. Copyright @ 2019 Learntek. All Rights Reserved. 27 last_letter = 'z' male : female = 3.9 : 1.0 last_letter = 'j' male : female = 3.9 : 1.0 last_letter = 't' male : female = 3.8 : 1.0 last_letter = 'i' female : male = 3.8 : 1.0 last_letter = 'u' male : female = 3.0 : 1.0 last_letter = 'n' male : female = 2.1 : 1.0 last_letter = 'e' female : male = 1.8 : 1.0
  • 28. Copyright @ 2019 Learntek. All Rights Reserved. 28
  • 29. Copyright @ 2019 Learntek. All Rights Reserved. 29 For more Training Information , Contact Us Email : info@learntek.org USA : +1734 418 2465 INDIA : +40 4018 1306 +7799713624