<jupyter_start><jupyter_text>Tone Analysis<jupyter_code># Import the necessary packages

import pandas as pd
from ibm_watson import ToneAnalyzerV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson import ApiException<jupyter_output><empty_output><jupyter_text>IBM Tone analyzer allows us to analyse only 2500 tweets for free. So the tweets are split into 5 parts (corresponding to each phases of lockdown) with 500 tweets each.<jupyter_code># Read the data

df = pd.read_csv("Processed Data.csv",sep='\t')
df.head() #First 5 data
# Divide the data on the basis of lockdown phases and take 500 samples from each of them

lockdown1 = df[(df['date'] >= '2020-03-25') & (df['date'] <= '2020-04-14')].sample(500)

lockdown2 = df[(df['date'] >= '2020-04-15') & (df['date'] <= '2020-05-03')].sample(500)

lockdown3 = df[(df['date'] >= '2020-05-04') & (df['date'] <= '2020-05-17')].sample(500)

lockdown4 = df[(df['date'] >= '2020-05-18') & (df['date'] <= '2020-05-31')].sample(500)

unlock1 = df[(df['date'] >= '2020-06-01') & (df['date'] <= '2020-06-14')].sample(500)
lockdown1
combined_df = pd.concat([lockdown1,lockdown2,lockdown3,lockdown4,unlock1], ignore_index=True)  # Combine the data to easily analyse the tone
combined_df
tone_analyzer = ToneAnalyzerV3(version='2017-09-21',authenticator=IAMAuthenticator("Api Key")) #Initialise Tone Analyzer

tone_analyzer.set_service_url("Api Url")  # Replace Api Url & Api Key with your credentials 
tone_analyzer.set_disable_ssl_verification(False)
tones = []
for item in combined_df['processed_text']:
    try:
        tone_analysis = tone_analyzer.tone({'text': str(item)}, content_type='application/json').get_result()
        tones.append(tone_analysis)  # Use IBM Tone Analyzer to find the tone and append this data to a list
    except ApiException as ex:         
        tones.append(None)
        print ("Method failed with status code " + str(ex.code) + ": " + ex.message)  # Print the failure reason (If any occurs)
tones
tone_df = pd.DataFrame(tones) # Create a pandas dataframe from the data
tone_df['document_tones'] = tone_df.document_tone.apply(lambda x: x['tones'])  #Split the tones into individual rows
tone_df.drop('document_tone',axis = 1,inplace = True)
tone_df.head()
# split sentiments in corresponding columns

length_df = len(tone_df)
for i in range(length_df):
    for j in range(len(tone_df.loc[i, 'document_tones'])):
        doc_tone = tone_df.loc[i, 'document_tones'][j]
        source = doc_tone['tone_id']
        tone_df.loc[i, source] = 1
        
tone_df.head()
result = pd.concat([combined_df, tone_df], axis=1)  # Combine the data
result
# Save the data

result.to_csv("Sentiment Data.csv", sep='\t', encoding='utf-8',index=False)
# Split the data and save it for different lockdown phases

lockdown1tone = result[(result['date'] >= '2020-03-25') & (result['date'] <= '2020-04-14')].sort_values(by=['date', 'time'])
lockdown1tone.to_csv("Lockdown 1 Tone.csv", sep='\t', encoding='utf-8',index=False)

lockdown2tone = result[(result['date'] >= '2020-04-15') & (result['date'] <= '2020-05-03')].sort_values(by=['date', 'time'])
lockdown2tone.to_csv("Lockdown 2 Tone.csv", sep='\t', encoding='utf-8',index=False)

lockdown3tone = result[(result['date'] >= '2020-05-04') & (result['date'] <= '2020-05-17')].sort_values(by=['date', 'time'])
lockdown3tone.to_csv("Lockdown 3 Tone.csv", sep='\t', encoding='utf-8',index=False)

lockdown4tone = result[(result['date'] >= '2020-05-18') & (result['date'] <= '2020-05-31')].sort_values(by=['date', 'time'])
lockdown4tone.to_csv("Lockdown 4 Tone.csv", sep='\t', encoding='utf-8',index=False)

unlock1tone = result[(result['date'] >= '2020-06-01') & (result['date'] <= '2020-06-14')].sort_values(by=['date', 'time'])
unlock1tone.to_csv("Unlock 1 Tone.csv", sep='\t', encoding='utf-8',index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17437354688710927, 'fraction_numerical': 0.05993283389305089, 'mean_word_length': 5.55160744500846, 'pattern_counts': {'":': 1, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '208282', 'license': 'MIT', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1358, 'n_words': 308, 'path': 'Jupyter Notebook/Tone Analysis.ipynb', 'repo_name': 'RahulR19/Covid19-Public-Sentiment-Insights', 'size': '3871'}