aboutsummaryrefslogtreecommitdiff
path: root/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md
diff options
context:
space:
mode:
Diffstat (limited to 'posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md')
-rw-r--r--posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md88
1 files changed, 88 insertions, 0 deletions
diff --git a/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md b/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md
new file mode 100644
index 0000000..831b490
--- /dev/null
+++ b/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md
@@ -0,0 +1,88 @@
1---
2Title: Using sentiment analysis for clickbait detection in RSS feeds
3Description: Using Python with sentiment analysis to detect if titles in RSS feeds are clickbait
4Slug: using-sentiment-analysis-for-clickbait-detection-in-rss-feeds
5Listing: true
6Created: 2019-10-19
7Tags: []
8---
9
10## Initial thoughts
11
12One of the things that interested me for a while now is if major well established news sites use click bait titles to drive additional traffic to their sites and generate additional impressions.
13
14Goal is to see how article titles and actual content of article differ from each other and see if titles are clickbaited.
15
16## Preparing and cleaning data
17
18For this example I opted to just use RSS feed from a new website and decided to go with [The Guardian](https://www.theguardian.com) World news. While this gets us limited data (~40) articles and also description (actual content) is trimmed this really doesn't reflect the actual article contents.
19
20To get better content I could use web scraping and use RSS as link list and fetch contents directly from website, but for this simple example this will suffice.
21
22There are couple of requirements we need to install before we continue:
23
24- `pip3 install feedparser` (parses RSS feed from url)
25- `pip3 install vaderSentiment` (does sentiment polarity analysis)
26- `pip3 install matplotlib` (plots chart of results)
27
28So first we need to fetch RSS data and sanitize HTML content from description.
29
30```python
31import re
32import feedparser
33
34feed_url = "https://www.theguardian.com/world/rss"
35feed = feedparser.parse(feed_url)
36
37# sanitize html
38for item in feed.entries:
39 item.description = re.sub('<[^<]+?>', '', item.description)
40```
41
42## Perform sentiment analysis
43
44Since we now have cleaned up data in our `feed.entries` object we can start with performing sentiment analysis.
45
46There are many sentiment analysis libraries available that range from rule-based sentiment analysis up to machine learning supported analysis. To keep things simple I decided to use rule-based analysis library [vaderSentiment](https://github.com/cjhutto/vaderSentiment) from [C.J. Hutto](https://github.com/cjhutto). Really nice library and quite easy to use.
47
48```python
49from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
50analyser = SentimentIntensityAnalyzer()
51
52sentiment_results = []
53for item in feed.entries:
54 sentiment_title = analyser.polarity_scores(item.title)
55 sentiment_description = analyser.polarity_scores(item.description)
56 sentiment_results.append([sentiment_title['compound'], sentiment_description['compound']])
57```
58
59Now that we have this data in a shape that is compatible with matplotlib we can plot results to see the difference between title and description sentiment of an article.
60
61```python
62import matplotlib.pyplot as plt
63
64plt.rcParams['figure.figsize'] = (15, 3)
65plt.plot(sentiment_results, drawstyle='steps')
66plt.title('Sentiment analysis relationship between title and description (Guardian World News)')
67plt.legend(['title', 'description'])
68plt.show()
69```
70
71## Results and assets
72
731. Because of the small sample size further conclusions are impossible to make.
742. Rule-based approach may not be the best way of doing this. By using deep learning we would be able to get better insights.
753. **Next step would be to** periodically fetch RSS items and store them over a longer period of time and then perform analysis again and use either machine learning or deep learning on top of it.
76
77![Relationship between title and description](/assets/sentiment-analysis/guardian-sa-title-desc-relationship.png)
78
79Figure above displays difference between title and description sentiment for specific RSS feed item. 1 means positive and -1 means negative sentiment.
80
81[ยป Download Jupyter Notebook](/assets/sentiment-analysis/sentiment-analysis.ipynb)
82
83## Going further
84
85- [Twitter Sentiment Analysis by Bryan Schwierzke](https://github.com/bswiss/news_mood)
86- [AFINN-based sentiment analysis for Node.js by Andrew Sliwinski](https://github.com/thisandagain/sentiment)
87- [Sentiment Analysis with LSTMs in Tensorflow by Adit Deshpande](https://github.com/adeshpande3/LSTM-Sentiment-Analysis)
88- [Sentiment analysis on tweets using Naive Bayes, SVM, CNN, LSTM, etc. by Abdul Fatir](https://github.com/abdulfatir/twitter-sentiment-analysis)