Skip to content

Commit

Permalink
fixed the node-link format (workaround NetworkX errors dropping some …
Browse files Browse the repository at this point in the history
…properties)
  • Loading branch information
ceteri committed Jan 9, 2024
1 parent 2fa8bd2 commit e540503
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 5 additions & 5 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,26 @@
## transform graph data to a _graph of relations_
start_time = time.time()

graph: textgraphs.GraphOfRelations = textgraphs.GraphOfRelations(
gor: textgraphs.GraphOfRelations = textgraphs.GraphOfRelations(
tg,
)

graph.seeds(
gor.seeds(
debug = False, # True
)

graph.construct_gor(
gor.construct_gor(
debug = False, # True
)

_scores: typing.Dict[ tuple, float ] = graph.get_affinity_scores(
_scores: typing.Dict[ tuple, float ] = gor.get_affinity_scores(
debug = False, # True
)

duration = round(time.time() - start_time, 3)
print(f"{duration:7.3f} sec: graph of relations")

graph.render_gor_plt(_scores)
gor.render_gor_plt(_scores)
plt.show()

#sys.exit(0)
Expand Down
1 change: 1 addition & 0 deletions textgraphs/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ def construct_lemma_graph (
{
"kind": str(edge.kind),
"title": edge.rel,
"key": edge_key,
"weight": float(edge.count),
"prob": edge.prob,
"count": edge.count,
Expand Down
22 changes: 21 additions & 1 deletion textgraphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,28 @@ def dump_lemma_graph (
nx_node["length"] = node.length
nx_node["annotated"] = node.annotated

# emulate a node-link format serialization, since the call to
# `NetworkX.node_link_data()` drop some properties, such as `key`
edge_list: typing.List[ dict ] = []

for src, dst, props in self.lemma_graph.edges.data():
props["source"] = src
props["target"] = dst
edge_list.append(props)

node_link: dict = {
"directed": True,
"multigraph": True,
"nodes": [
props
for node_id, props in self.lemma_graph.nodes.data()
],
"edges": edge_list,
"graph": {}
}

return json.dumps(
nx.node_link_data(self.lemma_graph),
node_link,
sort_keys = True,
indent = 2,
separators = ( ",", ":" ),
Expand Down

0 comments on commit e540503

Please sign in to comment.