从Spacy文档对象创建依赖关系树图
visualise-spacy-tree-0.0.1的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_treeplot=visualise_spacy_tree.plot(doc)# Write it to a file (it's png format)withopen('parse_tree.png','wb')asf:f.write(plot)# If you're using Jupyter notebook, you can render it inlinefromIPython.displayimportImage,displaydisplay(Image(plot))# Override node attributes like sofromspacy.tokensimportTokenToken.set_extension('plot',default={})# Create a token underscore extensionfortokenindoc:node_text='{0} [{1}])'.format(token.orth_,token.i)token._.plot['text']=node_textiftoken.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
致谢
用途:
贡献
欢迎使用:)