Sarcasm Detection: Step towards Sentiment Analysis | by DIGVIJAY SINGH | Towards Data Science

Humans have a social nature. Social nature means that we interact with each other in positive, friendly ways, and it also means that we know how to manipulate others in a very negative way.
Sarcasm, which is both positively funny and negatively nasty, plays an important part in human social interaction.
But sometimes it is difficult to detect whether someone is making fun of us with some irony. So to make it easy we built something which helps you in detecting sarcastic text, but before getting into much more detail, let’s define sarcasm:-
Sarcasm is “a sharp, bitter, or cutting expression or remark”. The use of irony to mock or convey contempt.
So now it’s time to charge up and start the journey of sarcasm detection.
Let’s start by defining the problem:-
Problem description:-
We have given the News Headlines Dataset and our goal is to predict whether a given text is sarcastic or not.
This News Headlines Dataset for Sarcasm Detection is collected from The Onion website which aims at producing sarcastic versions of current events and HuffPost website which collects real news headlines.
You can get this Dataset easily from the amazing website Kaggle, here is the link below:-
Click here to download dataset
Our dataset has three columns, where the
article_link (type: Object): contains links to the news articles.
headline (type: Object): contains headlines of the news articles.
is_sarcastic (type: int64): contains 0(for nonsarcastic text) and 1(for sarcastic text).
Source Code(Python):-
This sarcasm detector was built-in Python language, so it’s time to import essential libraries and modules and don’t worry if you don’t know about these modules, we will learn about these as we will move ahead:-
import pandas as pd, numpy as np, re, timefrom nltk.stem.porter import PorterStemmer
Now it’s time to get our models and some dependencies:-
from sklearn.model_selection import train_test_splitfrom sklearn.metrics import confusion_matrixfrom sklearn.svm import LinearSVCfrom sklearn.model_selection import cross_val_scorefrom sklearn.naive_bayes import GaussianNBfrom sklearn.linear_model import LogisticRegressionfrom sklearn.ensemble import RandomForestClassifier
and don’t worry, if you don’t have any of the modules, you can use:-
pip install <module/library Name>
to install any of the dependencies.
After getting all the dependencies it’s time to load our dataset into Pandas Dataframe.
# Loading data from json filedata = pd.read_json(‘Sarcasm_Headlines_Dataset.json’, lines = True)
Now let’s see the first five rows of our dataset using data.head() method of Pandas Dataframe.
It is always better to check the null values in the dataset first.
print(data.isnull().any(axis = 0))# Output :-# ----------------------# article_link    False# headline        False# is_sarcastic    False# dtype: bool# ----------------------
Since our dataset doesn’t have any null value. So, we can proceed to clean the data.
Cleaning the data:-
Although, it looks that our data is clean but the headline column has some special symbols that have to be eliminated.
So, we are using Regular Expression to eliminate special symbols.
# Relacing special symbols and digits in headline column# re stands for Regular Expressiondata['headline'] = data['headline'].apply(lambda s : re.sub('[^a-zA-Z]', ' ', s))
Feature and label extraction:-
It’s time to extract features and labels out of our data. It seems that we may consider article_link and headline as features. But, article_link doesn’t play an important role in predicting the label. So, the only feature that we have is the headline column. And is_sarcastic is the only label.
# getting features and labelsfeatures = data['headline']labels = data['is_sarcastic']
Stemming of features:-
Stemming is the process of reducing a word to its word stem that affixes to suffixes and prefixes or the roots of words known as a lemma. Stemming is important in natural language understanding (NLU) and natural language processing (NLP).
for example:-
original word: “reading” and “reader”.
after stemming: read (for all the above two words).
# Stemming our dataps = PorterStemmer()features = features.apply(lambda x: x.split())features = features.apply(lambda x : ‘ ‘.join([ps.stem(word) for word in x]))
Vectorization of features using TF-IDF Vectorizer:-
TF-IDF is an abbreviation for Term Frequency-Inverse Document Frequency and is a very common algorithm to transform the text into a meaningful representation of numbers. The technique is widely used to extract features across various NLP applications.
# vectorizing the data with maximum of 5000 featuresfrom sklearn.feature_extraction.text import TfidfVectorizertv = TfidfVectorizer(max_features = 5000)features = list(features)features = tv.fit_transform(features).toarray()
Training and Testing data:-
After all the important stuff, it’s time to split our data into training and testing chunks.
# getting training and testing datafeatures_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size = .05, random_state = 0)
Training and testing of models:-
Now it’s showtime, we are going to train our model using different Machine Learning algorithms.
# model 1:-# Using linear support vector classifierlsvc = LinearSVC()# training the modellsvc.fit(features_train, labels_train)# getting the score of train and test dataprint(lsvc.score(features_train, labels_train)) # 90.93print(lsvc.score(features_test, labels_test))   # 83.75# model 2:-# Using Gaussuan Naive Bayesgnb = GaussianNB()gnb.fit(features_train, labels_train)print(gnb.score(features_train, labels_train))  # 78.86print(gnb.score(features_test, labels_test))    # 73.80# model 3:-# Logistic Regressionlr = LogisticRegression()lr.fit(features_train, labels_train)print(lr.score(features_train, labels_train))   # 88.16print(lr.score(features_test, labels_test))     # 83.08# model 4:-# Random Forest Classifierrfc = RandomForestClassifier(n_estimators = 10, random_state = 0)rfc.fit(features_train, labels_train)print(rfc.score(features_train, labels_train))  # 98.82print(rfc.score(features_test, labels_test))    # 79.71
After applying all the above models, the score is as follows:-
It can be seen that the best scorer is the Linear Support Vector Classifier with an accuracy of 83.75%. And remaining models have accuracies nearer to Linear Support Vector Classifier. Therefore, we have chosen the best one.
User-Interface:-
For making it user-friendly, we also built a user-interface using HTML, CSS, and Bootstrap, which is supported by The Flask.
This is how our user interface looks like:-
Based on the input text, our model predicts it to be sarcastic or not.
Summary:-
It can be seen that how well we can use the power of Machine Learning algorithms to make this sentiment analysis an easier task.
Special Thanks:-
The realization of this project was done at Forsk-Labs under the direction of amazing mentors, who helped us a lot in building this wonderful project. I will always be grateful to them for their support and kindness.
About Us:-
We (as a team of two members- DIGVIJAY SINGH and Sourabh Prajapati) built this project during our summer internship at Forsk Technologies.
QR Code to GitHub Repo:-