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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ab9d053e8edc0fbdf5b423d31f2dd8cdb6a47ce27c2edeac45f631d2207bfaa |
|
MD5 | 1c542af0dd2f1ed1c2a235446e7ec0fc |
|
BLAKE2b-256 | fad4774c4135f44965a149855eb535e3d1fbe7e7b3bbe3f017eb4f53bf168aa8 |
Close
Hashes for visualise_spacy_tree-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37250326f2f477eb2ae2d49669e81b9353581bf531defceebd7a242f1dc3cc0c |
|
MD5 | 883aec3b2f6400a6f070d2555e77ab4e |
|
BLAKE2b-256 | 783843a8b7020de58c85778d126fac1d8a1ba9a249442578f8de2ee2a0a8af2d |