I want to save the visualisation of spaCy with the code that spaCy offers here : https://spacy.io/usage/visualizers
Here is my code :
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"Autonomous cars shift insurance liability toward manufacturers")
svg = spacy.displacy.render(doc, style="dep")
output_path = Path(os.path.join("./", "sentence.svg"))
output_path.open('w', encoding="utf-8").write(svg)
But when I execute this code, there is an error : TypeError: write() argument must be str, not None
So how can I save the output of spacy.displacy.render ? How can I fix this error ?
The code provided in the question works fine, when executed in an IDE like PyCharm.
However, displacy.render()
works slightly differently in a Jupyter notebook, cf the documentation here and here:
If you’re running a Jupyter notebook, displaCy will detect this and return the markup in a format ready to be rendered and exported.
You can overwrite this automated detection by explicitly setting jupyter=False
in the render()
call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With