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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from gotLibrary import GotLib
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
class plot_type:
|
| 8 |
+
def __init__(self,data):
|
| 9 |
+
self.data = data
|
| 10 |
+
self.fig=None
|
| 11 |
+
self.update_layout=None
|
| 12 |
+
|
| 13 |
+
def bar(self,x,y,color):
|
| 14 |
+
self.fig=px.bar(self.data,x=x,y=y,color=color)
|
| 15 |
+
|
| 16 |
+
def pie(self,x,y):
|
| 17 |
+
self.fig = px.pie(self.data,values=x,names=y)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def set_title(self,title):
|
| 21 |
+
|
| 22 |
+
self.fig.update_layout(
|
| 23 |
+
title=f"{title}",
|
| 24 |
+
yaxis=dict(tickmode="linear"),
|
| 25 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 26 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 27 |
+
font=dict(color='white',size=18))
|
| 28 |
+
|
| 29 |
+
def set_title_x(self,title):
|
| 30 |
+
|
| 31 |
+
self.fig.update_layout(
|
| 32 |
+
title=f"{title}",
|
| 33 |
+
xaxis=dict(tickmode="linear"),
|
| 34 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 35 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 36 |
+
font=dict(color='white',size=18))
|
| 37 |
+
|
| 38 |
+
def set_title_pie(self,title):
|
| 39 |
+
self.fig.update_layout(title=title,
|
| 40 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 41 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 42 |
+
font=dict(color='white',size=18))
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def plot(self):
|
| 47 |
+
st.write(self.fig)
|
| 48 |
+
|
| 49 |
+
class slide_bar:
|
| 50 |
+
value=4
|
| 51 |
+
def __init__(self,title,x,y):
|
| 52 |
+
self.title = title
|
| 53 |
+
self.x=x
|
| 54 |
+
self.y=y
|
| 55 |
+
self.slide_bar = None
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def set(self):
|
| 59 |
+
self.slide_bar = st.slider(self.title,self.x,self.y)
|
| 60 |
+
slide_bar.value=self.slide_bar
|
| 61 |
+
|
| 62 |
+
class select_box:
|
| 63 |
+
value="tyrion"
|
| 64 |
+
def __init__(self,data):
|
| 65 |
+
self.data=data
|
| 66 |
+
self.box=None
|
| 67 |
+
def place(self,title,key):
|
| 68 |
+
header(title)
|
| 69 |
+
self.box = st.selectbox(str(key),self.data)
|
| 70 |
+
select_box.value=self.box
|
| 71 |
+
|
| 72 |
+
def title(text,size,color):
|
| 73 |
+
st.markdown(f'<h1 style="font-weight:bolder;font-size:{size}px;color:{color};text-align:center;">{text}</h1>',unsafe_allow_html=True)
|
| 74 |
+
|
| 75 |
+
def header(text):
|
| 76 |
+
st.markdown(f"<p style='color:white;'>{text}</p>",unsafe_allow_html=True)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@st.cache(persist=True,suppress_st_warning=True)
|
| 82 |
+
def load_data():
|
| 83 |
+
df = pd.read_csv('final_data.csv')
|
| 84 |
+
df = df.iloc[:,1:]
|
| 85 |
+
return df
|
| 86 |
+
|
| 87 |
+
#loading the data
|
| 88 |
+
df = load_data()
|
| 89 |
+
|
| 90 |
+
#intializing the GotLib object
|
| 91 |
+
got = GotLib(df)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
with open("styles/style.css") as f:
|
| 96 |
+
st.markdown(f'<style>{f.read()}</style>',unsafe_allow_html=True)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
#-------------------------------Header-----------------------
|
| 100 |
+
|
| 101 |
+
st.markdown('<h1 style="text-align:center;color:white;font-weight:bolder;font-size:100px;">GAME<br>OF<br>THRONES</h1>',unsafe_allow_html=True)
|
| 102 |
+
# st.markdown('<h1 style="text-align:center;color:white;background-image:url("m1.png");">An analysis..</h1>',unsafe_allow_html=True)
|
| 103 |
+
st.markdown('<h2 style="text-align:center;color:white;">An analysis..</h2>',unsafe_allow_html=True)
|
| 104 |
+
st.image('images/got1.jpg',width=700)
|
| 105 |
+
st.markdown('### This is an analysis based project on the tv series game of thrones')
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
#------------------------Module 1--------------------------
|
| 110 |
+
title("Top characters based on number of words spoken in a season",60,"white")
|
| 111 |
+
|
| 112 |
+
header('season')
|
| 113 |
+
|
| 114 |
+
option_1_s = st.selectbox('',[1,2,3,4,5,6,7,8])
|
| 115 |
+
|
| 116 |
+
header("number of results")
|
| 117 |
+
num = st.slider("",4,50)
|
| 118 |
+
|
| 119 |
+
temp_data = got.show_top_by_season(option_1_s)
|
| 120 |
+
number=10
|
| 121 |
+
|
| 122 |
+
bar1 = plot_type(temp_data[-num:])
|
| 123 |
+
bar1.bar("spoken_words","character","spoken_words")
|
| 124 |
+
bar1.set_title(f"Season {option_1_s}")
|
| 125 |
+
bar1.plot()
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
#-----------------------Module 2------------------------------
|
| 129 |
+
|
| 130 |
+
title("Overall top characters based on number of spoken words",60,'white')
|
| 131 |
+
|
| 132 |
+
header("number of results")
|
| 133 |
+
|
| 134 |
+
num1 = st.slider("",5,60)
|
| 135 |
+
|
| 136 |
+
temp_data1 = got.get_overall_top()
|
| 137 |
+
|
| 138 |
+
bar2 = plot_type(temp_data1[-num1:])
|
| 139 |
+
bar2.bar("words","character","words")
|
| 140 |
+
bar2.set_title("Overall Top")
|
| 141 |
+
bar2.plot()
|
| 142 |
+
|
| 143 |
+
#------------------------Module 3-----------------------------
|
| 144 |
+
|
| 145 |
+
title("Character's number of dialogues over the seasons",60,"white")
|
| 146 |
+
st.markdown('### NOTE: displaying only top 100 characters in drop downs as there are more than 500+ it would be awkward to display it all :)')
|
| 147 |
+
|
| 148 |
+
characters = got.get_data_seasons()
|
| 149 |
+
stb1 = select_box(characters)
|
| 150 |
+
stb1.place("character",0)
|
| 151 |
+
@st.cache(persist=True)
|
| 152 |
+
def sbyc(df,stb1):
|
| 153 |
+
return got.show_bar_by_character_allSeason(stb1)
|
| 154 |
+
|
| 155 |
+
t_data = sbyc(df,stb1.value)
|
| 156 |
+
|
| 157 |
+
bar0 = plot_type(t_data)
|
| 158 |
+
bar0.bar("season","spoken_words","spoken_words")
|
| 159 |
+
# bar0.update_layout(title=f"{stbl.value}")
|
| 160 |
+
bar0.set_title_x(stb1.value)
|
| 161 |
+
|
| 162 |
+
bar0.plot()
|
| 163 |
+
|
| 164 |
+
#----------------------Module 4----------------------------------
|
| 165 |
+
|
| 166 |
+
title("Percentage of a character's performance in seasons",60,"white")
|
| 167 |
+
st.write("what is the character's distribution of his/her/(uhh. you know the rest) dialogue percentage over the seasons")
|
| 168 |
+
|
| 169 |
+
stb2 = select_box(characters)
|
| 170 |
+
stb2.place("character",9)
|
| 171 |
+
t_data1 = got.cal_importance(df,stb2.value)
|
| 172 |
+
|
| 173 |
+
pie2 = plot_type(t_data1)
|
| 174 |
+
pie2.pie("imp","season")
|
| 175 |
+
pie2.set_title_pie(stb2.value)
|
| 176 |
+
pie2.plot()
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
#-------------------------Module 5-----------------------------
|
| 180 |
+
|
| 181 |
+
title('Most number of words spoken by a character',60,'white')
|
| 182 |
+
st.markdown('#### removing all the stop words in the sense common words.')
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
select_box1 = select_box(characters)
|
| 187 |
+
select_box1.place('character',1)
|
| 188 |
+
header("range")
|
| 189 |
+
num2 = slide_bar("",5,55)
|
| 190 |
+
num2.set()
|
| 191 |
+
temp_data2,size = got.get_most_spokenwords_by_character(df,select_box1.value,num2.value)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
bar3 = plot_type(temp_data2)
|
| 195 |
+
bar3.bar("frequency","words","frequency")
|
| 196 |
+
bar3.set_title(f"{select_box1.value.capitalize()} total words spoken - {size}")
|
| 197 |
+
bar3.plot()
|
| 198 |
+
|
| 199 |
+
#--------------------------WORD_CLOUD---------------------------
|
| 200 |
+
|
| 201 |
+
title("WordCloud of a character",60,'white')
|
| 202 |
+
st.markdown('#### It may take a few seconds to load the result, so please hold on to your dragons.')
|
| 203 |
+
|
| 204 |
+
select_box2 = select_box(characters)
|
| 205 |
+
select_box2.place('character',2)
|
| 206 |
+
|
| 207 |
+
header('range')
|
| 208 |
+
sl = slide_bar('',50,200)
|
| 209 |
+
sl.set()
|
| 210 |
+
@st.cache(persist=True,suppress_st_warning=True)
|
| 211 |
+
def swc(df,v1,v2):
|
| 212 |
+
return got.show_word_cloud(df,v1,v2)
|
| 213 |
+
wc = swc(df,sl.value,select_box2.value)
|
| 214 |
+
fig = plt.figure(figsize=(8,8))
|
| 215 |
+
plt.imshow(wc,interpolation="bilinear")
|
| 216 |
+
plt.axis('off')
|
| 217 |
+
plt.title(select_box2.value,fontsize=18)
|
| 218 |
+
plt.tight_layout()
|
| 219 |
+
st.pyplot(fig)
|
| 220 |
+
|
| 221 |
+
#--------------------------Module 4------------------------------
|
| 222 |
+
|
| 223 |
+
title("Emotional characteristics",70,"white")
|
| 224 |
+
st.write("The below pie chart depicts the distribution of emotions of a character.")
|
| 225 |
+
st.write('Note: This is purely my calculations based on the text-corpus I created and also based on the words used by a character.')
|
| 226 |
+
|
| 227 |
+
select_box3 = select_box(characters)
|
| 228 |
+
select_box3.place('character',3)
|
| 229 |
+
|
| 230 |
+
temp_data3 = got.cal_character(select_box3.value)
|
| 231 |
+
pie1 = plot_type(temp_data3['data'])
|
| 232 |
+
pie1.pie(temp_data3['y'],temp_data3['x'])
|
| 233 |
+
pie1.set_title_pie(select_box3.value)
|
| 234 |
+
pie1.plot()
|
| 235 |
+
|
| 236 |
+
#---------------------------Module 5--------------------------
|
| 237 |
+
title("Most used name by a character",50,"white")
|
| 238 |
+
|
| 239 |
+
stb = select_box(characters[:50])
|
| 240 |
+
stb.place("character",4)
|
| 241 |
+
temp_df = got.most_name(stb.value)
|
| 242 |
+
num_range = temp_df.shape[0]
|
| 243 |
+
rangesl = slide_bar("",1,num_range)
|
| 244 |
+
rangesl.set()
|
| 245 |
+
|
| 246 |
+
bar5 = plot_type(temp_df.iloc[-rangesl.value:,:])
|
| 247 |
+
bar5.bar("number","name","number")
|
| 248 |
+
bar5.set_title(stb.value)
|
| 249 |
+
bar5.plot()
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
#----------------------Module 6----------------------------------
|
| 254 |
+
|
| 255 |
+
title('Similar Characters',60,'white')
|
| 256 |
+
st.write('The chart shows characters who are similar to a character with their similarity precentage, based on their similar usage of words, this same alogrithm is also used in movie recommender systems.')
|
| 257 |
+
st.write('Note: This is very much experimental and purely based on the scripts. And only depends on script text and nothing else.')
|
| 258 |
+
ch=characters[:]
|
| 259 |
+
ch1 = select_box(ch)
|
| 260 |
+
ch1.place('character',5)
|
| 261 |
+
val=ch1.value
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
results = got.get_similar_character(val)
|
| 265 |
+
|
| 266 |
+
colors = ['rgb(0,255,42)','rgb(221, 235, 30)','rgb(224, 91, 43)']
|
| 267 |
+
char = list(results['character'])
|
| 268 |
+
score = list(results['similarity'])
|
| 269 |
+
i=0
|
| 270 |
+
for name,sc in zip(char,score):
|
| 271 |
+
|
| 272 |
+
st.markdown(f"<h3 style='text-align:center;color:rgb(196, 196, 196);'><span style='font-weight:bolder;color:{colors[i]};font-size:50px;'>{name} </span> [{sc}%]</h3>",unsafe_allow_html=True)
|
| 273 |
+
i+=1
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
#----------------------------------------------------------------------THE END---------------------------------------------------------------------
|
| 277 |
+
|
| 278 |
+
st.write(' ')
|
| 279 |
+
|
| 280 |
+
st.markdown('#### The dataset here is created from the scripts, involved a lot of data cleaning,wrangling and pre-processing!. Took a lot of time to prepare it!. And is 85% accurate.')
|
| 281 |
+
|
| 282 |
+
st.write('check the box below to peak at the dataset')
|
| 283 |
+
if st.checkbox('',False):
|
| 284 |
+
st.subheader("Game_of_Thrones")
|
| 285 |
+
st.write(df)
|
| 286 |
+
|
| 287 |
+
st.write('')
|
| 288 |
+
st.write('')
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
st.markdown('<h3 style="text-align:center;">Made By <span style="color:#4f9bce;font-weight:bolder;font-size:40px;">Mario 😎</span></h3>',unsafe_allow_html=True)
|
| 292 |
+
st.markdown('<h2 style="text-align:center;text-decoration:none;font-weight:bolder;"><a style="text-decoration:none;color:rgb(96, 235, 133);" href="https://github.com/Mario-Vishal">-> GitHub <-</a></h2>',unsafe_allow_html=True)
|
| 293 |
+
st.markdown('<h2 style="text-align:center;text-decoration:none;font-weight:bolder;"><a style="text-decoration:none;color:rgb(20, 166, 219);" href="https://www.linkedin.com/in/mario-vishal">-> Linkedin <-</a></h2>',unsafe_allow_html=True)
|
| 294 |
+
st.markdown('<h2 style="text-align:center;text-decoration:none;font-weight:bolder;"><a style="text-decoration:none;color:red;" href="mailto:mariovishal12@gmail.com">-> Contact Me <-</a></h2>',unsafe_allow_html=True)
|