YouTube Sentiment Analysis
Sentiment analysis, also known as opinion mining, is the process of determining the emotional tone and sentiment expressed in a piece of text. It involves classifying the sentiment as positive, negative, or neutral.
In this article, we’ll delve into the world of sentiment analysis and explore how it can be a game-changer for YouTube channel owners. I’ll also guide you through the step-by-step process of implementing sentiment analysis for YouTube video comments using TensorFlow.js.
Youtube Sentiment Analysis Demo
Enter YouTube video id below to visualize Sentiment of comments.
GitHub repository
You can download the complete code of the above demo in the link below:
How Does Sentiment Analysis Help YouTube Channels?
YouTubers often struggle to engage with their audience effectively. This is where sentiment analysis comes into play. Sentiment analysis helps you understand how viewers feel about your content. It can provide valuable insights into audience engagement, content quality, and help creators tailor their content to meet their viewers’ expectations.
- Audience Insights: Sentiment analysis allows YouTubers to gain a deeper understanding of their audience’s feelings and opinions. By analyzing comments, creators can identify common themes, sentiments, and preferences, which can inform content strategy.
- Content Optimization: Knowing the sentiment of comments can help creators identify which parts of their videos resonate most with viewers and which need improvement. This data-driven approach can lead to more engaging and successful content.
- Brand Reputation Management: For businesses and brands on YouTube, sentiment analysis can be vital for managing their online reputation. Identifying negative sentiments early on allows for timely responses and reputation repair.
- Filtering and Moderation: Sentiment analysis can be used to automatically filter out offensive or inappropriate comments, ensuring a more positive and safe community for viewers.
Implementation
Now, let’s dive into the practical side of things. Here’s a step-by-step guide on how to implement sentiment analysis for YouTube video comments using Tensorflow.js Sentiment Model.
# Step 1 : Create YouTube API Key
As we would like to pull comments from YouTube to analyze the sentiment, therefore, we need to create an API Key in the Google’s developer platform.
- Go to https://developers.google.com/ and log in or create an account, if necessary.
- After logging in go to this link https://console.developers.google.com/project and click on the blue CREATE PROJECT button as depicted in the photo below. Wait a moment as google prepares your project.
- Fill in whatever Project Name you want.
- Select the project that you just created, visit the API Library page. In the list of APIs, search for YouTube Data API v3.
- Select and Enable YouTube Data API v3
- Navigate to Credentials on the left side menu, then click on CREATE CREDENTIALS -> API Key
- Wait for google to create your new project and you should see the screen below where you can copy your API Key.
# Step 2 : Get Comments from YouTube
Once you generated the YouTube API Key, it’s time to utilize YouTube Data API to pull down details and comments for a specific YouTube video.
PHP
Google provided APIs Client Library for PHP, to install that, you’ll need:
- PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed.
- The Composer dependency management tool installed globally
- Run below command line to install Google APIs Client Library
composer require google/apiclient:^2.0
After the Google APIs Client Library is installed, you should see a “vendor” folder which contains all the API Client Library PHP files.
Create a youtubeGetComments.php
file, follow below steps to utilize the YouTube API
- Set up Google Client object with the API Key you generated in Step 1
require_once dirname(__DIR__) . '/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName('YouTube Comment Sentiment Analysis');
$client->setDeveloperKey('"YOUR-API-KEY"');
// Define service object for making API requests.
$youtube = new Google_Service_YouTube($client);
- Utilize YouTube API’s
listVideos
function to get video details
$VIDEO_ID = $_GET['v'];
$videoDetail = $youtube->videos->listVideos('snippet,statistics', array(
'id' => $VIDEO_ID,
'maxResults' => 1,
));
- Utilize YouTube API’s
listCommentThreads
function to get video comments
$videoComments = $youtube->commentThreads->listCommentThreads('snippet,replies', array(
'videoId' => $VIDEO_ID,
'maxResults' => 100,
'textFormat' => 'plainText',
'pageToken' => $pageToken
));
JavaScript
Then we write javascript function to pass a request to the above php page and retrieve the YouTube video details and comments.
function getYouTubeComments(videoId, callback) {
$.getJSON( urls.ytGetComments + videoId, function(result) {
if(result !== null && result.comments !== null){
callback(result);
}
});
}
# Step 3 : Load the Sentiment model
Simply include the scripts for tfjs
and in the <head> section of the html file.
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
</head>
Or you can install it via npm for use in a TypeScript / ES6 project
npm install @tensorflow/tfjs
In order to perform sentiment analysis, we first need to load the pre-trained sentiment model and metadata, by calling the API of tf.loadLayersModel(url)
.
const urls = {
model: 'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/model.json',
metadata: 'https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/metadata.json'
};
async function loadModel(url) {
try {
const model = await tf.loadLayersModel(url);
return model;
} catch (err) {
console.log(err);
}
}
async function loadMetadata(url) {
try {
const metadataJson = await fetch(url);
const metadata = await metadataJson.json();
return metadata;
} catch (err) {
console.log(err);
}
}
The model was trained on a set of 25,000 movie reviews from IMDB, labelled as having positive or negative sentiment. This dataset is provided by Python Keras, and the models were trained in Keras as well, based on the imdb_cnn examples.
# Step 4 : Sentiment Analysis YouTube Comments
For each comment, we call the model.predict(input)
API in Tensorflow.js. This would perform a Sentiment Analysis on each comment text, returning a score between 0 and 1, which indicate whether it is Neutral, Positive or Negative.
function processYouTubeComments(ytVideo){
setupSentimentModel().then(
result => {
displayVideoDetails(ytVideo);
const comments = ytVideo.comments;
const commentData = [];
$.each(comments, function( index, comment ) {
const comment_text = comment.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
const sentiment_score = getSentimentScore(comment_text);
let comment_sentiment = '';
if(sentiment_score > SentimentThreshold.Positive){
comment_sentiment = 'positive'
}else if(sentiment_score > SentimentThreshold.Neutral){
comment_sentiment = 'neutral'
}else if(sentiment_score >= SentimentThreshold.Negative){
comment_sentiment = 'negative'
}
commentData.push({
sentiment: comment_sentiment,
score: sentiment_score.toFixed(2),
comment: comment_text
});
});
//console.log(commentData);
$('.spinner-border').addClass('d-none');
displayComments(commentData.filter(t => t.sentiment == 'positive'), 'positive');
displayComments(commentData.filter(t => t.sentiment == 'neutral'), 'neutral');
displayComments(commentData.filter(t => t.sentiment == 'negative'), 'negative');
$('#comment-list').removeClass('d-none');
displayPieChart(commentData);
}
)
}
function getSentimentScore(text) {
const inputText = text.trim().toLowerCase().replace(/(\.|\,|\!)/g, '').split(' ');
// Convert the words to a sequence of word indices.
const sequence = inputText.map(word => {
let wordIndex = metadata.word_index[word] + metadata.index_from;
if (wordIndex > metadata.vocabulary_size) {
wordIndex = OOV_INDEX;
}
return wordIndex;
});
// Perform truncation and padding.
const paddedSequence = padSequences([sequence], metadata.max_len);
const input = tf.tensor2d(paddedSequence, [1, metadata.max_len]);
const predictOut = model.predict(input);
const score = predictOut.dataSync()[0];
predictOut.dispose();
return score;
}
# Step 5 : Display result in table and chart
Now we had the sentiment result of the comments. To make it look nice and easy to capture the information, we put that into a table and pie chart.
function displayComments(commentData, sentiment){
var tbl = document.createElement('table');
var tr = tbl.insertRow();
for( var j in commentData[0] ) {
if(j !=='sentiment'){
var td = tr.insertCell();
td.appendChild(document.createTextNode(j));
}
}
for( var i = 0; i < commentData.length; i++) {
var tr = tbl.insertRow();
for( var j in commentData[i] ) {
if(j !=='sentiment'){
var td = tr.insertCell();
var text = commentData[i][j];
td.appendChild(document.createTextNode(text));
}
}
}
tbl.setAttribute('class', 'comment-table')
$('#'+sentiment).append(tbl);
$('#'+sentiment+'-counter').html('('+ commentData.length +')');
}
For the pie chart, I am using a jquery chart library canvasjs
function displayPieChart(commentData){
var sentimentsCounter = {"Negative": 0, "Neutral": 0, "Positive": 0};
for( var i = 0; i < commentData.length; i++) {
switch(commentData[i].sentiment) {
case 'positive':
sentimentsCounter["Positive"] += 1;
break;
case 'negative':
sentimentsCounter["Negative"] += 1;
break;
case 'neutral':
sentimentsCounter["Neutral"] += 1;
break;
}
}
var chart = new CanvasJS.Chart("chartContainer", {
theme: "light2",
exportEnabled: true,
animationEnabled: true,
data: [{
type: "pie",
startAngle: 25,
toolTipContent: "<b>{label}</b>: {y}%",
showInLegend: "true",
legendText: "{label}",
indexLabelFontSize: 16,
indexLabel: "{label} - {y}%",
dataPoints: [
{ y: (sentimentsCounter["Positive"] * 100.00/commentData.length).toFixed(2), label: "Positive" },
{ y: (sentimentsCounter["Neutral"] * 100.00/commentData.length).toFixed(2), label: "Neutral" },
{ y: (sentimentsCounter["Negative"] * 100.00/commentData.length).toFixed(2), label: "Negative" },
]
}]
});
chart.render();
}
And when the enter is press or the search button is click, the above functions are call
$("#videoId").on('keyup', function (e) {
if (e.keyCode === 13) {
youtubeSentiment();
}
});
$(".btn-search").click(function () {
youtubeSentiment();
});
function youtubeSentiment(){
$('#comment-list').addClass('d-none');
$('#video-detail').addClass('d-none');
$('#positive').empty();
$('#neutral').empty();
$('#negative').empty();
$('#chartContainer').empty();
$('.spinner-border').removeClass('d-none');
getYouTubeComments($("#videoId").val(), processYouTubeComments);
}
That’s it for the code, and congratulations, you had built your YouTube Sentiment Analysis app.
Conclusion
Sentiment analysis is a powerful tool for YouTube channel owners. It provides insights into audience sentiment, helps optimize content, and can be used for brand reputation management and comment moderation. By implementing sentiment analysis with TensorFlow.js, Youtubers can better understand their audience and improve the overall quality of their content.
Beyond YouTube, sentiment analysis has applications in various domains, including social media monitoring, customer feedback analysis, and market research. Its versatility makes it a valuable tool for anyone looking to gain deeper insights into text data and leverage those insights for decision-making. So, whether you’re a content creator or a business owner, sentiment analysis can be a valuable addition to your toolkit.
Thank you for reading. If you like this article, please share on Facebook or Twitter. Let me know in the comment if you have any questions. Follow me on Medium, GitHub and Linkedin. Support me on Ko-fi.