Created
February 12, 2017 21:46
-
-
Save tommyhuang1/e25572356da699bb5c9444c49cfce469 to your computer and use it in GitHub Desktop.
Review Polarity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Polarity for Gabapentin | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf8') | |
from textblob import TextBlob | |
wiki = map(lambda x: TextBlob(x),B.Review) | |
result=[] | |
for i in range(len(wiki)): | |
k = wiki[i].sentiment.polarity | |
result.append(k) | |
Positive = len([x for x in result if x>0])/float(len(result)) *100 | |
Neutral = len([x for x in result if x==0])/float(len(result)) *100 | |
Negative = len([x for x in result if x<0])/float(len(result)) *100 | |
# Polarity for Clonazepam | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf8') | |
from textblob import TextBlob | |
wiki1 = map(lambda x: TextBlob(x),clonazepam.Review) | |
result2=[] | |
for i in range(len(wiki1)): | |
k = wiki1[i].sentiment.polarity | |
result2.append(k) | |
Positive1 = len([x for x in result2 if x>0])/float(len(result2)) *100 | |
Neutral1 = len([x for x in result2 if x==0])/float(len(result2)) *100 | |
Negative1 = len([x for x in result2 if x<0])/float(len(result2)) *100 | |
# Polarity for Topiramate | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf8') | |
from textblob import TextBlob | |
wiki2 = map(lambda x: TextBlob(x),Topiramate.Review) | |
result4=[] | |
for i in range(len(wiki2)): | |
k = wiki2[i].sentiment.polarity | |
result4.append(k) | |
Positive2 = len([x for x in result4 if x>0])/float(len(result4)) *100 | |
Neutra12 = len([x for x in result4 if x==0])/float(len(result4)) *100 | |
Negative2 = len([x for x in result4 if x<0])/float(len(result4)) *100 | |
#Polarity for Pregabalin | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf8') | |
from textblob import TextBlob | |
wiki3 = map(lambda x: TextBlob(x),Pregabalin.Review) | |
#positive/negative | |
result6=[] | |
for i in range(len(wiki3)): | |
k = wiki3[i].sentiment.polarity | |
result6.append(k) | |
Positive3 = len([x for x in result6 if x>0])/float(len(result6)) *100 | |
Neutra13 = len([x for x in result6 if x==0])/float(len(result6)) *100 | |
Negative3 = len([x for x in result6 if x<0])/float(len(result6)) *100 | |
#Generate Polarity dataframe | |
d = {'Positive':[Positive,Positive1,Positive2,Positive3], | |
'Neutral': [Neutral,Neutral1,Neutra12,Neutra13], | |
'Negative': [Negative,Negative1,Negative2,Negative3], | |
} | |
polarity = pd.DataFrame(d, index = ['Gabapentin','Clonazepam','Topiramate','Pregabalin']) | |
#bar plot for polarity | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
n_groups = 4 | |
Neg = polarity.Negative | |
Neu = polarity.Neutral | |
Pos = polarity.Positive | |
fig, ax = plt.subplots() | |
index = np.arange(n_groups) | |
bar_width = 0.3 | |
opacity = 0.7 | |
error_config = {'ecolor': '0.8'} | |
rects1 = plt.bar(index, Neg, bar_width, | |
alpha=opacity, | |
color='r', | |
error_kw=error_config, | |
label='Negative') | |
rects2 = plt.bar(index + bar_width, Neu, bar_width, | |
alpha=opacity, | |
color='y', | |
error_kw=error_config, | |
label='Neutral') | |
rects3 = plt.bar(index + 2 * bar_width, Pos, bar_width, | |
alpha=opacity, | |
color='g', | |
error_kw=error_config, | |
label='Positive') | |
plt.xlabel('Drugs') | |
plt.ylabel('Polarity Percent') | |
plt.title('Review Polarity') | |
plt.xticks(index + 1.5*bar_width, ('Gabapentin', 'Clonazepam', 'Topiramate','Pregabalin')) | |
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0) | |
#plt.tight_layout() | |
plt.figure(figsize=(4,10)) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment