-
Notifications
You must be signed in to change notification settings - Fork 0
/
pplot3.py
130 lines (117 loc) · 4.04 KB
/
pplot3.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from typing import Dict, Tuple, Optional, Callable
import argparse, math
import pathlib as pl
import igraph as ig
import networkx as nx
import matplotlib.pyplot as plt
from eng import eng_name_style
from extract import extract_from_tei, group_minor_characters_
from renard.graph_utils import cumulative_graph, graph_with_names
from renard.pipeline.character_unification import Character
LANG_TO_NAMESTYLE = {"fra": None, "eng": eng_name_style}
def pplot_graph(
G: nx.Graph,
layout: Dict[Character, Tuple[float, float]],
ax=None,
):
nx.draw_networkx_nodes(
G,
layout,
ax=ax,
node_color=[degree for _, degree in G.degree],
cmap="YlOrRd",
node_size=[1 + degree * 10 for _, degree in G.degree],
edgecolors="black",
linewidths=0.5,
)
nx.draw_networkx_edges(
G,
layout,
ax=ax,
edge_color=["lightblue" if e[0] != e[1] else "red" for e in G.edges],
connectionstyle="arc3,rad=0.2" if isinstance(G, nx.DiGraph) else "arc3",
width=[1 + math.log(d["weight"]) for _, _, d in G.edges.data()],
alpha=0.7,
)
nx.draw_networkx_labels(
G,
pos=layout,
ax=ax,
verticalalignment="center",
font_size=10,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output-dir", type=pl.Path)
parser.add_argument("-r", "--group", action="store_true")
parser.add_argument(
"-l", "--lang", type=str, help="one of 'fra', 'eng'", default="fra"
)
args = parser.parse_args()
# get the layout from the co-occurrence network
co_occurrence_out = extract_from_tei(
"./lorenzaccio.tei.xml",
"co-occurrence",
dynamic=True,
dynamic_window=1,
dynamic_overlap=0,
)
if args.group:
group_minor_characters_(co_occurrence_out, "co-occurrence")
G_co_occurrence = cumulative_graph(co_occurrence_out.character_network)[-1]
G_co_occurrence = graph_with_names(
G_co_occurrence, name_style=LANG_TO_NAMESTYLE[args.lang]
)
g_c = ig.Graph.from_networkx(graph_with_names(G_co_occurrence))
layout = {
char: coords
for char, coords in zip(g_c.vs["_nx_name"], g_c.layout_kamada_kawai())
}
mention_out = extract_from_tei(
"./lorenzaccio.tei.xml",
"mention",
dynamic=True,
dynamic_window=1,
dynamic_overlap=0,
)
if args.group:
group_minor_characters_(mention_out, "mention")
G_mention = cumulative_graph(mention_out.character_network)[-1]
G_mention = graph_with_names(G_mention, name_style=LANG_TO_NAMESTYLE[args.lang])
conversation_out = extract_from_tei(
"./lorenzaccio.tei.xml",
"conversation",
dynamic=True,
dynamic_window=1,
dynamic_overlap=0,
)
if args.group:
group_minor_characters_(conversation_out, "conversation")
G_conversation = cumulative_graph(conversation_out.character_network)[-1]
G_conversation = graph_with_names(
G_conversation, name_style=LANG_TO_NAMESTYLE[args.lang]
)
fig = plt.figure(layout="constrained", figsize=(14, 12))
gs = fig.add_gridspec(2, 4)
ax_co_occurrence = fig.add_subplot(gs[0, 1:3])
if args.lang == "eng":
ax_co_occurrence.set_title("Co-occurrence network (a)")
else:
ax_co_occurrence.set_title("Graphe de co-occurrence (a)")
ax_mention = fig.add_subplot(gs[1, 0:2])
if args.lang == "eng":
ax_mention.set_title("Mention network (b)")
else:
ax_mention.set_title("Graphe de mention (b)")
ax_conversation = fig.add_subplot(gs[1, 2:4])
if args.lang == "eng":
ax_conversation.set_title("Conversational network (c)")
else:
ax_conversation.set_title("Graphe conversationnel (c)")
pplot_graph(G_co_occurrence, layout, ax=ax_co_occurrence)
pplot_graph(G_mention, layout, ax=ax_mention)
pplot_graph(G_conversation, layout, ax=ax_conversation)
if args.output_dir:
plt.savefig(args.output_dir)
else:
plt.show()