Python 100 project #16: Generate sentiment plot

I used a few libraries in this project, to create a plot chart from the transcripts I retrieved in the previous project.

  • google-cloud-language … to retrieve the sentiment score from google cloud natural language
  • pandas … to create a dataframe (might not be necessary, but I will use this heavily later)
  • seaborn … to create plot chart.

 

Here is the output result. It is visible the Doctor become excited as it goes towards the end.

 

This code uses google-cloud-language library, and it returns the sentiment of the sentence it received.

def get_sentiment(content):
    from google.cloud import language
    
    client = language.LanguageServiceClient(credentials=credentials)
    
    document = language.types.Document(
        content=content,
        type='PLAIN_TEXT',
    )

    response = client.analyze_sentiment(
        document=document,
        encoding_type='UTF32',
    )
    
    sentiment = response.document_sentiment
    
    return sentiment.magnitude, sentiment.score

 

Using the sentence list I retrieved in the previous project, assuming “speeches” is the list of conversations, and “names” are literally the names who spoke that sentence in respective index.

This is to retrieve all the sentiments for all the speeches.

for speech in speeches:
    
    magnitude, score = get_sentiment(speech)
    
    magnitudes.append(magnitude)
    scores.append(score)

 

This is data wrangling to make the format easier to process. It actually doesn’t process data much, maybe in the later project I will dive into more statistics using these transcripts or Japanese statistics I used on project#5.

import pandas as pd

conversation_df = pd.DataFrame(
    {'name': names,
     'magnitude': magnitudes,
     'score': scores,
    })

conversation_df['index_label'] = conversation_df.index
conversation_df['magnitude_score'] = conversation_df.magnitude * conversation_df.score

 

This is the actual code to display the plot using the data retrieved.

import seaborn as sns

sns.lmplot(x="index_label", y="magnitude_score", data=conversation_df, hue="name", fit_reg=False, size=10, aspect=1.5)