# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# + [markdown] tags=[]
# # "Assignment 08 - Network Analysis"
#
# - toc:true
# - badges:true
# - comments: true
# - author: <NAME>
# - categories: [network analysis, Shakespeare]
#

# + [markdown] tags=[]
# ### Network Visualization: All's Well That Ends Well by Shakespeare

# +
#hide

# Reading the Play
with open('CompleteShakespeare.txt') as f:
    x = f.read()

# + tags=[]
# Splitting by Acts - Only All's Well That Ends Well PLay

acts = x.split('ACT')[6:11]


# -

# Total Number of Acts
len(acts)

characters = ['KING',
              'DUKE',
              'BERTRAM',
              'LAFEW',
              'PAROLLES',
              'CLOWN',
              'COUNTESS',
              'HELENA',
              'DIANA',
              'MARIANA'
             ]

# +
#collapse_show
charnum = {}
connections = {}

for i in range(len(characters)-1):
    for j in range(i + 1, len(characters)):
        connections[(characters[i], characters[j])] = 0

for k in characters:
    charnum[k] = 0
    
for i in acts:
    for j in i.split('SCENE')[1:]:
        scenechars = []
        for k in characters:
            if j.find(k) != -1:
                scenechars.append(k)
                charnum[k] += 1
        for a in range(len(scenechars)-1):
            for b in range(a+1, len(scenechars)):
                connections[(scenechars[a], scenechars[b])] += 1
# -

# Number of Character Appearances 
for k,v in charnum.items():
    print(k,v)

# Character Connections/Interactions w/ other Characters 
connections

# Imports
import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms.community.centrality import girvan_newman

# +
nxgraphitems = []

for k,v in connections.items():
    if v != 0:
        dashedge = (k[0],k[1])
        nxgraphitems.append(dashedge)
    
nxgraphitems
# -

G = nx.Graph()

G.add_edges_from(nxgraphitems)

G.nodes

communities = girvan_newman(G)

# +
#collapse_show
node_groups = []
for com in next(communities):
    node_groups.append(list(com))
 
print(node_groups)
 
color_map = []
for node in G:
    if node in node_groups[0]:
        color_map.append('yellow')
    else:
        color_map.append('green')
nx.draw(G, node_color=color_map, with_labels=True)
plt.show()
# -

nx.density(G)

nx.degree_centrality(G)

nx.betweenness_centrality(G)

# #### Character Network Analysis from All's Well That Ends Well

# This network visualizations depicts all the speaking characters and their interactions with each other within Shakespeare's play All's Well That Ends Well. 

# According to our network visualization, the most important nodes are Bertram, Helena, and Parolles because they have the highest degree centrality of 0.888. In other words, these are the characters that seem to have the most interactions with the other characters and are a vital part of the social network within the play. 

StarcoderdataJupyterScriptsDedupFiltered