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
plot = visualise_spacy_tree.plot(doc)
# Write it to a file (it's png format)
with open('parse_tree.png', 'wb') as f:
f.write(plot)
# If you're using Jupyter notebook, you can render it inline
from IPython.display import Image, display
display(Image(plot))
# Override node attributes like so
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.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b198aa660f049f08605e4340ee2a1c14b4b644d8e578808e743f4245a2cbc3e8 |
|
MD5 | 73971d2331c70c5628426dd5362c7d0a |
|
BLAKE2b-256 | 22c6e439ffce36b6c964b5f9e7ef3ec73f0a7df2d5eb704e0cf624b78c6d1dac |
Close
Hashes for visualise_spacy_tree-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 290f7963614e0207dd156e71e9735dd010221346f53d1b0573afdd4902d48910 |
|
MD5 | 821c99ea95e706b9d3dd89c880b76347 |
|
BLAKE2b-256 | 18fe8854e4281963785979076e282d4a2abf15852808177b2bd03e673a59df72 |