From 9df9a87d03ae33150c0aabd3a441ffac44bbff17 Mon Sep 17 00:00:00 2001 From: ines Date: Wed, 17 May 2017 12:00:50 +0200 Subject: [PATCH] Add visualizer usage example --- website/docs/usage/visualizers.jade | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/website/docs/usage/visualizers.jade b/website/docs/usage/visualizers.jade index ba69923ab..0eff2e4d1 100644 --- a/website/docs/usage/visualizers.jade +++ b/website/docs/usage/visualizers.jade @@ -250,6 +250,25 @@ p +h(2, "examples") Usage examples ++h(3, "examples-export-svg") Export SVG graphics of dependency parses + ++code("Example"). + import spacy + from spacy import displacy + from pathlib import Path + + nlp = spacy.load('en') + sentences = ["This is an example.", "This is another one."] + for sent in sentences: + doc = nlp(sentence) + svg = displacy.render(doc, style='dep') + file_name = '-'.join([w.text for w in doc if not w.is_punct]) + '.svg' + output_path = Path('/images/' + file_name) + output_path.open('w', encoding='utf-8').write(svg) + +p + | The above code will generate the dependency visualizations and them to + | two files, #[code This-is-an-example.svg] and #[code This-is-another-one.svg]. +h(2, "manual-usage") Rendering data manually