Visualizing a graph
udsyntax includes Python functions and utility scripts to create diagrams of SyntaxGraphs in the dot syntax used by the freely available graphviz software, and in Mermaid syntax. Mermaid diagrams can be rendered in many Markdown viewers, on mermaid.live, etc. dot graphs require you to install graphviz to generate a PNG or SVG image.
Prerequisites
- install
udsyntaxand the spaCy pipeline for whichever you want to use
To use dot files, install graphviz:
on Mac OS:
brew install graphviz
on Windows: download from https://graphviz.org/download/ or
winget install --id Graphviz.Graphviz
To verify it worked: print a version string.
dot -V
Utility scripts
text_to_dot.py
text_to_dot.py: analyzes a string of text and composes a string with the dot format of the graph. The --lang (or -l) paramater is required.
python scripts/text_to_dot.py "Gallia est omnis divisa in partes tres." --lang la If you have installed graphviz, then you can pipe the result into dot. -T sets the output type, -o names the output file.
python scripts/text_to_dot.py "Gallia est omnis divisa in partes tres." --lang la | dot -Tpng -o bg_1_1.pngcorpus_to_dot.py
corpus_to_dot.py: takes a text file with a corpus in CEX format and a URN, analyzes the text of the cited passage, and generates its dot representation, just like text_to_dot.py. The --urn and --lang (-l) parameters are required .
python3 scripts/corpus_to_dot.py scratch/data/compnov.cex --urn "urn:cts:compnov:bible.genesis.vulgate:1.1" --lang la Pipe it to dot:
python3 scripts/corpus_to_dot.py scratch/data/compnov.cex --urn "urn:cts:compnov:bible.genesis.vulgate:1.1" --lang la | dot -Tpng -o genesis_1_1.pngComposing diagrams from Python
udsyntax includes two functions, to_mermaid() and to_dot(), that create the Mermaid graph or dot graph of a SyntaxGraph object.
graph = SyntaxGraph.from_doc(doc)
mmdgraph = graph.to_mermaid() # default BT orientation
mmdgraph.to_mermaid(orientation="LR") # left-to-right instead
gviz = graph.to_dot() # a Graphviz digraph
gviz = graph.to_dot(orientation="LR") # left-to-right instead You could save these results to a file, or in an interactive environment