-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter_centrality_metrics.py
More file actions
58 lines (46 loc) · 2.24 KB
/
character_centrality_metrics.py
File metadata and controls
58 lines (46 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import networkx as nx
import pandas as pd
import os
# Directory path
output_dir = 'output'
output_metrics_dir = 'output_metrics'
files = [f for f in os.listdir(output_dir) if f.endswith('.csv')]
# Load the CSV with the play titles
plays_df = pd.read_csv('metadata/list_of_shakespeare_plays0.csv', header=None)
play_title_mapping = dict(zip(plays_df[0].str.strip(), plays_df[1].str.strip()))
# If the output_metrics directory does not exist, create it
if not os.path.exists(output_metrics_dir):
os.makedirs(output_metrics_dir)
# Dataframe to store results
results = pd.DataFrame(columns=['Play', 'Degree Centrality', 'Betweenness Centrality',
'Closeness Centrality', 'Eigenvector Centrality',
'Density', 'Cluster Coefficient'])
for f in files:
# Get the base of the filename (without extension)
base_filename = os.path.splitext(f)[0]
# Look up the full title in the play_title_mapping, default to the base filename if not found
full_play_title = play_title_mapping.get(base_filename, base_filename)
# Load dataframe from CSV
df = pd.read_csv(os.path.join(output_dir, f))
# Create the graph from the dataframe
G = nx.from_pandas_edgelist(df, 'speaker', 'listener', 'total_words')
# Calculate metrics
degree_centrality = max(nx.degree_centrality(G).values())
betweenness_centrality = max(nx.betweenness_centrality(G).values())
closeness_centrality = max(nx.closeness_centrality(G).values())
eigenvector_centrality = max(nx.eigenvector_centrality(G).values())
density = nx.density(G)
cluster_coefficient = nx.average_clustering(G)
# Append results to dataframe
new_row = pd.DataFrame([{
'Play': full_play_title,
'Degree Centrality': degree_centrality,
'Betweenness Centrality': betweenness_centrality,
'Closeness Centrality': closeness_centrality,
'Eigenvector Centrality': eigenvector_centrality,
'Density': density,
'Cluster Coefficient': cluster_coefficient
}])
results = pd.concat([results, new_row], ignore_index=True)
# Save results to a CSV in the 'output_metrics' folder
results.to_csv(os.path.join(output_metrics_dir, 'metrics_summary.csv'), index=False)