import matplotlib.pyplot as plt
import numpy as np,pandas as pd

def show(d,t,x,r,w):
    d.plot.bar(color=['red','gray','green'],ec='k',width=w,rot=r,title=t,xlabel=x,ylabel='Tweet Count')
    plt.tight_layout()
    plt.show()

df=pd.read_csv('twitter_training.csv',usecols=[1,3],names=['b','t']).dropna()
p='good|great|love|best|awesome|amazing|happy|nice|win|fun|thanks'
n='bad|worst|hate|terrible|awful|shit|poor|fuck|fail|bugs|delay|cancel'
P,N=[df.t.str.contains(r,case=0) for r in (p,n)]
df['S']=np.select([P>N,N>P],['Positive','Negative'],'Neutral')
O=['Negative','Neutral','Positive']
B=['Amazon','Facebook','Google','Microsoft','Nvidia']
show(pd.crosstab(df[df.b.isin(B)].b,df.S)[O],'Twitter Sentiment Analysis by Brand/Product','Brand',45,0.6)
for b in ['Amazon','Google']:
    show(df[df.b==b].S.value_counts().reindex(O),f'Twitter Sentiment Analysis for {b}','Sentiment',90,0.35)