Skip to main content

Create dependency tree plots from SpaCy Doc objects

Project description

visualise-spacy-tree

An alternative to SpaCy's visualizer, built on GraphViz.

Custom plot image

Prerequisites

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

pytest

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

visualise_spacy_tree_0.0.1-0.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file visualise_spacy_tree_0.0.1-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: visualise_spacy_tree_0.0.1-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for visualise_spacy_tree_0.0.1-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d4a3a7ccf0327211d4dfe6db671403369aeda168fbca4b19f841426c1d5a8d8
MD5 3ad308f1f61fde40c7030ffcfbc0ca48
BLAKE2b-256 a0cd2e94f6bf4b4db74c240350d618fac98a240948ff2c56c8dad6cef490178d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page