# Sentiment Analysis for Earnings Calls with AssemblyAI

## Метаданные

- **Канал:** AssemblyAI
- **YouTube:** https://www.youtube.com/watch?v=kBoe56CfugY
- **Дата:** 12.12.2021
- **Длительность:** 19:52
- **Просмотры:** 5,075

## Описание

Transcribing audio is cool but what about drawing insights from that unstructured data? With AssemblyAI you can get much more out of your audio or video files.

In this video, we make a web app that does sentiment analysis on earnings calls of companies. Just input the recording of an earnings call and get your insights in terms of the sentiment of the call. Let’s learn how to make it!

Get your own AssemblyAI API token here 👇
https://www.assemblyai.com/?utm_source=youtube&utm_medium=referral&utm_campaign=yt_mis_11

Find the code for this tutorial here: https://github.com/AssemblyAI/youtube-tutorials/tree/main/Sentiment%20Analysis

## Содержание

### [0:00](https://www.youtube.com/watch?v=kBoe56CfugY) <Untitled Chapter 1>

can we understand how well a company is doing based on their earnings call would it be possible to understand if they're optimistic or pessimistic about their future well in this video that's exactly what we're going to do using assembly ai sentiment analysis feature this is what the app will look like we will have a title and some descriptions for the user they will be able to give us a youtube link to the recording of this earnings call we will show the title to understand what company and which quarter and what year it belongs to we will show the transcription on the sidebar and as an addition we will have visualizations of this earnings call sentiment analysis we will have a bar chart showing how many negative positive or neutral sentences there are we will calculate a sentiment score but i'll show you how to calculate that later and we will also see on each sentence of what sentiment group they belong to all right let's jump right into it all right so let's get started i will not start from all the way from scratch this time because we've already done a video where i show you how to transcribe youtube videos and that's also where i'm going to start here because we've already done that code if you're not familiar with it go ahead and watch that video and then come back here and then we'll uh go further from where we were because in that video we only gave a link to a youtube link youtube video and then we transcribed it and then we displayed it on a stream with application but today we're going to go further we're also going to do sentiment analysis on it and then we're going to create some visualizations to show this sentiment analysis so this is what we have so far the only difference from the other application is that this time i put the transcription on the sidebar instead of showing it in the actual body

### [1:46](https://www.youtube.com/watch?v=kBoe56CfugY&t=106s) Code

let's look at our code so i'll just quickly walk you through what i have so far um i am basically defining the transcription and upload endpoints for assembly ai and i'm also creating the headers for authentication later again for the

### [2:02](https://www.youtube.com/watch?v=kBoe56CfugY&t=122s) Authentication

authentication we just need to configure that pi file where we have the assembly ai api key that we have i'll quickly show you how to get that also it's very simple all you have to do is either follow the link in the description or go to assemblyai. com and then you just say start now for free and once you said that you can sign in and in the page that welcomes you have an api key and you can just click this one and copy it in the configure. pi file and then that's all you need all right uh what i have is a title where i give a title for my app and a caption just describing what this app does and just a sub header uh to show that this is the place where users need to uh put a link or input a link um i am getting the input from the user this is a default value and then i am as we did in the previous video we saved this file this the audio version of this video that is on youtube on our local system and then we read it from upload it to assembly ai like we did last time and then we start the transcription job using this audio file that we just uploaded to assembly ai by passing it the audio url that we have and this difference this time is that we are saying sentiment analysis we are passing sentiment analysis as true and that means we also want together with a transcript we also want the sentiment analysis results on this transcription and that's going to come sentence by sentence but we will see how the transcription response will look like in a second um and that's all basically we send the post request to assembly ai to create this transcription job and then we are getting as a result a polling endpoint to which we can ask uh to see if transcription has been done already or not and then we are checking it while it's still being processed we need to check it once in a while to see if the transcription is complete or not and once we have that once we know the status is complete then we stop asking and that's it and then we have the transcript in the polling responses text section let's say but let's look at how this polling response looks like and how we can extract the sentiment analysis from it and for that on top of showing the transcript on the application i'm going to print it the json version of it as you can see here in like a pretty printed way on my terminal so let's run this and see what the response looks like all right so now we got the response from assembly ai here at the end all the way at the end we have the words that were detected so i need to actually i don't have to scroll i can just look for sentiment analysis results yes so um in this json response that we get from assembly ai we have something

### [5:08](https://www.youtube.com/watch?v=kBoe56CfugY&t=308s) Sentiment Analysis Results

called sentiment analysis results and that's where we have the sentiment analysis for each sentence so each sentence is basically one dictionary um kind of or one little json segment and in each of them we have the confidence so what is the confidence that we are uh detecting or classifying the sentence as positive um and then the start of the sentence the end of the sentence and the sentiment that was the classified as if there is a speaker the speaker specified that we will also see it here and we also see the specific text that was uh classified as positive or with the specific sentiment we get positive negative or neutral with assembly ai's sentiment analysis and as i said we get a confidence and we get it for every single sentence i think this is quite clear how we can read this i'm just going to save this to a different variable and all we have to do is instead of getting the text we are going to get the as i just saw sentiment analysis result results yes and it is quite hard to work with when we have a json file like this a json variable or a dictionary so i'm going to turn this actually into a python pandas data frame so that it will be easier to work with a change um you know or create insights with or even visualize i will call this a sentiment data frame and it's very simple if we already imported pandas we just need to say data frame sentiment analysis results and then i'm also going to print the first five rows of this data frame just to see if everything looks correct okay and this looks quite good actually we have the text in one column all the texts the sentences in one column the start and end points of the sentences whether it's positive negative or neutral and the confidence in separate columns so this is quite nice and what i want to do next is to actually use this information and start visualizing it all right the first thing is i want to show the title of this video because when people paste a new link maybe they'll forget what uh earnings whole they were actually aiming to see the analysis of the sentiment analysis of so i want to extract the title from this url that we're getting and then show it to the user and we can actually do this by using beautiful soup uh what we do is we just get the html code from this url and then in this html code we are reading the title uh of this html file and the title is the youtube name of this video so let's see that quickly nice so we have the name of this earnings call so we know it's for the company amazon as for q3 of 2021 so that's perfect and the next thing that i want to do is to see how many sentences there are in this earnings call and then also show it to the user so it would be quite simple i'll just show it stream with markdown so once i say this then i can also go and check out how it looks all right nice so apparently we had 388 sentences in amazon's q3 earnings call of 2021. uh this is good to know so the next thing that i want to do is to

### [8:44](https://www.youtube.com/watch?v=kBoe56CfugY&t=524s) Visualizations

create actual visualizations and the first visualization that i want to create is a bar chart showing me how many positive negative and neutral sentences there are in this earnings call so for that the first thing that i want to do is to actually group um the sentiment different sentiment values and see how many sentences there are in our data frame so it is quite simple with pandas because i'm going to use the value counts function what i do is in the sentiment column i just say count the different values how many times they occur in this data set and i'll show you what this looks like right so this small data frame looks quite simple it just in one column shows me uh the value for the sentiment and the second column shows me how many of them there are in this the data frame the bigger data frame that we had uh the next thing that we want to do is to create a plotly graph and uh show this in a bar chart but before that i actually want to have two columns to show my visualizations in the first column i'll have a bar chart in the second column i will have a score as i showed you in the beginning of the video too so for that very simply just need to say stream the columns create two columns the first one i'll call column one the second two to create a bar chart i just need to say plotly express this is the library that i'm using for this specific one create a bar chart with the grouped data frame and the x-axis is going to be the sentiment so positive negative or neutral y-axis is going to have the count the width of it is going to be 500 but i will actually also specify this in my update layout i'll show you in a second so i can remove that one the color is going to depend on the sentiment and here is the color map that

### [10:38](https://www.youtube.com/watch?v=kBoe56CfugY&t=638s) Color Map

we're going to use and for the negative one i have a red cold fire brick for the neutral one i have a neutral color called navajo white and for positive i have dark green i'm using kind of fancier ones because i don't like the normal red blue or green they are kind of ugly so we also want something that kind of you know looks nice right all right the second thing that i want to do is something that i always copy and paste when i'm working with portly just so that i have some control over how my graph looks um so i am i'm using the upside layout function they have i want the legend to be false i don't want there to be any legend because most of the time it just is redundant information that you can already see inside the graph so that's why i'm not showing it you can specify the width and the height and also the margin of the padding if you like to so let's save this and lastly because i want to show this on the app i will say column one plotly chart and show figure and that's it so let's go ahead and see what it looks like awesome okay this is exactly what i wanted i will close the sidebar for now because you know it's just taking up space if you want you can pull it back up and then close it uh all right so it looks like we had two 213 neutral sentences which is good i guess they're quite neutral and most other sentences are positive 157 and negative sentences are 18. so i guess this was overall a more positive than negative uh call so the next thing that i want to do is actually to kind of give like one number or one score to this call overall and i

### [12:24](https://www.youtube.com/watch?v=kBoe56CfugY&t=744s) The Sentiment Score

will call it the sentiment score uh so i'll show you how i calculate that now so for that one what i decided would work well to see if a call was overall positive or negative is to give it a score from zero to a hundred if it's zero it was completely negative it was terrible it was a terrible call if it's 50 it was a neutral call you know nothing really special if it's higher than 50 then it means that it was a positive call so 100 would be perfectly positive so what i did for that is to calculate the percentages of all the sentences that are positive compared to all the other sentences that are in this call uh so really quickly what i do is just say give me the count of all the sentences that were positive and calculate the percentage of that based on all the sentences that i have in this call and do it for negative and neutral too and what i do is i add up the number of neutral sentences with number of or the percentage of neutral sentences of the percentage of positive sentences and subtract the percentage of negative sentences this one will be a little bit different to show because i'm going to use the graph objects uh plotly library this time instead of the express library because this is the only way i could find to show kind of like a gauge uh if you know if it's like positive or negative it will show different colors so for that i'll create a plotly graph again and inside this graph i will add something i guess these are called the uh indicators so we're quick adding a trace of an indicator from the graph objects library this one is called delta the specific they have a couple different options you can go check them out i am going to show the sentiment score also my update layout function is a bit more full this time again i have the width height and margin and everything but on top of that i'm also specifying what the title text needs to be what is the reference value that i'm taking so everything will be shown as a reference to 50 because as i said 50 is like the neutral if everything was neutral is 50 and if it's higher than that is positive it's lower than it's negative and that's what i want to show and uh yeah that's it so now we can just display this in column two nice so apparently the score we had for this one is 90. 7 because we got a 40. 7 positive score if we had if the sentiment score we got was let's say 20 that means that this call is actually quite negative so we would have gotten a red looking number that was with the arrow pointing down that would have said 30. so all right these are quite nice and the last thing that i want to show you is uh where in the sentence the negativity happens where in this whole call not in a sentence but the negativity happens so for that i'm just going to use a very simple scatter plot that shows me uh where the positive sentences are where the neutral sentences are and what the negative sentences are luckily for this one i don't have to create any special data set i can directly use the sentiment analysis response that i got from assembly ai so that json or dictionary whatever you want to call it a variable that i have uh i just need to specify that y is going to be the sentiment so i want them kind of layered on top of each other um and the x-axis is just going to be the index so i don't have to specify it again i want the sentiment to dictate the color of this and i also want the size to show me how confident assembly ai's api is based on you know what sentiment this sentence belongs to um when i hover it i want to be able to see the text so the sentence that is in question and the color map is again negative is going to be fabric neutral navajo white and positive dark green uh i again will have my update layout function here uh this time i'm going to make it a bit more narrow and longer so you know we will have a bit more of space so it will spend the whole width of the app that we have instead of just being in one column so we'll be able to see it a bit more clearly uh and that's it actually so let me show that on the app screen and we can go and read some options or sentences and how they are we'll see how they're classified all right and here it is as you can see we have on the top side we have the positive sentences and the neutral and negative ones and we see that the negative ones are kind of grouped together here and then here again like in the beginning a little bit before the middle right after the middle and just two sentences towards the end uh and whereas the whole call was more or less positive and generally a lot of neutral remarks too uh so you know let's look at some positive ones thank you for standing by uh some of them are a little bit too long to read uh you know better price performance than current x86 processors is positive uh great someone says that's positive of course uh we're getting closer yeah definitely i do so these are things that were specified as positive uh what about the negative ones let's look at a couple this one says our capacity constraints actually labor which is new and not welcome this definitely sounds like something negative is going on there we're dealing with labor risks and supply chain interruptions that also sounds quite uh negative to me it was a larger loss than in prior quarters so it looks like the sentiment analysis actually uh go works quite well here because we see that all of these sentences are actually things that have a negative uh feeling to them so can't forecast this any further at the segment level so it's quite good um and you know this was amazon's uh q3 2021 earnings call if you find uh well i mean it's very easy to find other earnings calls on youtube uh for example i tried netflix or facebook you can also try them out here and see you know what their sentiment analysis is what their score is and how the sentences kind of are distributed towards the whole call the code you can find in the github repository as always and you can just download it plug in your own assembly ai api which you can get through the link in the description try different earnings calls and see how their sentiment is and maybe you can even come up with some visualizations yourself and you know add some new value to this application if you come up with something like that definitely let us know in the comments below thank you for watching and i hope you enjoyed this tutorial if you want to make this app yourself you will need two things the code and the assembly ai api token and the link for both will be in the description below if you liked it don't forget to give us a like and subscribe to be one of the first people to know when we publish a new video and also if you have any questions or comments or if you want to let us know what kind of videos you want to see next leave a comment and let us know for now have a nice day

---
*Источник: https://ekstraktznaniy.ru/video/13299*