从Spacy文档对象创建依赖关系树图
visualise-spacy-tree的Python项目详细描述
可视化Spacy树
替代SpaCy'svisualizer,构建在GraphViz之上。
先决条件
- 您需要安装GraphViz。
安装
使用pip:
pip install visualise-spacy-tree
或来源:
git clone https://github.com/cyclecycle/visualise-spacy-tree.git visualise_spacy_tree
cd visualise_spacy_tree
python setup.py install
用法
# Parse a string to create SpaCy Doc objectimporten_core_web_smtext='Forging involves the shaping of metal using localized compressive forces.'nlp=en_core_web_sm.load()doc=nlp(text)# Create the plotimportvisualise_spacy_treepng=visualise_spacy_tree.create_png(doc)# Write it to a filewithopen('parse_tree.png','wb')asf:f.write(png)# If you're using Jupyter notebook, you can render it inlinefromIPython.displayimportImage,displaydisplay(Image(png))# Override node attributes to customise the plotfromspacy.tokensimportTokenToken.set_extension('plot',default={})# Create a token underscore extensionfortokenindoc:node_label='{0} [{1}])'.format(token.orth_,token.i)token._.plot['label']=node_labeliftoken.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'''
运行测试
运行
pytest
从根目录。
致谢
用途:
贡献
欢迎使用:)