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