Create dependency tree plots from SpaCy Doc objects
Project description
visualise-spacy-tree
An alternative to SpaCy's visualizer, built on GraphViz.
Prerequisites
- You need GraphViz installed.
Installation
With pip:
pip install visualise-spacy-tree
Or from source:
git clone https://github.com/cyclecycle/visualise-spacy-tree.git visualise_spacy_tree
cd visualise_spacy_tree
python setup.py install
Usage
# Parse a string to create SpaCy Doc object
import en_core_web_sm
text = 'Forging involves the shaping of metal using localized compressive forces.'
nlp = en_core_web_sm.load()
doc = nlp(text)
# Create the plot
import visualise_spacy_tree
png = visualise_spacy_tree.create_png(doc)
# Write it to a file
with open('parse_tree.png', 'wb') as f:
f.write(png)
# If you're using Jupyter notebook, you can render it inline
from IPython.display import Image, display
display(Image(png))
# Override node attributes to customise the plot
from spacy.tokens import Token
Token.set_extension('plot', default={}) # Create a token underscore extension
for token in doc:
node_text = '{0} [{1}])'.format(token.orth_, token.i)
token._.plot['text'] = node_text
if token.dep_ == 'ROOT':
token._.plot['color'] = 'green'
'''
You can set any valid GraphViz dot attribute in 'plot'.
See GraphViz docs for reference of possible node attributes:
https://graphviz.gitlab.io/_pages/doc/info/attrs.html
'''
Running the tests
Run
pytest
from the root directory.
Acknowledgements
Uses:
Contributions
Are welcome :)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for visualise-spacy-tree-0.0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b0a9ca53b33cd235c88eb2643099d3cfa826b8c528a71e493553453561afd38 |
|
MD5 | 460036691f653e7a4c3648e6d2ae09cb |
|
BLAKE2b-256 | 4889f1bba50b42647890b0f5666b01c045753cec9975c7e3f0e83d9f4f487698 |
Close
Hashes for visualise_spacy_tree-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe6edddc0de336b47de5a4a8b4b4989877af5829f4f6db1204bc40f095f2ceb5 |
|
MD5 | 85b45c84918350722e3a8d65c85d3d16 |
|
BLAKE2b-256 | d058eca41cdfb75bf879bbade7d630b3f12a8ff7d9ab3543c742f121af466d8f |