问题描述
我正在尝试在jupyter-notebook
内绘制nltk
的图形(内嵌).但是出现了错误:
I'm trying to draw graph (inline) of nltk
inside of jupyter-notebook
. But got error:
TclError: no display name and no $DISPLAY environment variable
我尝试将$DISPLAY
设置为不同的值:
I have tried to set $DISPLAY
to different values:
$env DISPLAY=0.0
# or
$env DISPLAY=inline
# or
$env DISPLAY=
但出现错误(或类似错误):
but got error (or similar):
TclError: couldn't connect to display "0.0"
这是我的代码 https://github.com/hyzhak /nltk-experiments/blob/master/main.ipynb 最后一个单元格.
here is my code https://github.com/hyzhak/nltk-experiments/blob/master/main.ipynb the last cell.
环境:正式的anaconda3泊坞窗-continuumio/anaconda3:4.4.0
https://github.com/ContinuumIO/docker -图片.里面是nltk==3.2.3
.
Environment: official anaconda3 docker -- continuumio/anaconda3:4.4.0
https://github.com/ContinuumIO/docker-images. With nltk==3.2.3
inside.
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:09:58)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
如何解决此错误和jupyter notebook
中的内联 nltk
图?
How could I solve this error and inline nltk
graph inside of jupyter notebook
?
http://www.nltk.org/_modules/nltk /draw/tree.html#draw_trees 根据它使用tkinter
的nltk树绘制的来源.
http://www.nltk.org/_modules/nltk/draw/tree.html#draw_trees according to the sources of nltk tree draw it uses tkinter
.
我在官方的nltk github存储库中问了同样的问题 https://github.com/nltk/nltk/issues/1765
I have asked the same question in official nltk github repository https://github.com/nltk/nltk/issues/1765
我认为错误的原因可能是无头主机(码头工人).我已经安装了xvfb
,但似乎已经足够注意了.
I think the reason of error could be the headless host (docker). I have installed xvfb
but seems it is note enough.
RUN apt-get install -y xvfb
解决方案
我认为xvfb
是默认启动的,但应明确运行.因此,启动后,我可以制作nltk树图的屏幕截图.
Solution
I thought that xvfb
is launched by default but It should be run explicitly. So after I have launched it I could make screenshots of nltk tree graph.
推荐答案
import os
import nltk
from IPython.display import Image
chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP>+<NN>?}"""
chunkParser = nltk.RegexpParser(chunkGram)
tagged = [('Tonight', 'NN'), ('we', 'PRP'), ('are', 'VBP'), ('comforted', 'VBN'), ('by', 'IN'), ('the', 'DT'), ('hope', 'NN'), ('of', 'IN'), ('a', 'DT'), ('glad', 'JJ'), ('reunion', 'NN'), ('with', 'IN'), ('the', 'DT'), ('husband', 'NN'), ('who', 'WP'), ('was', 'VBD'), ('taken', 'VBN'), ('so', 'RB'), ('long', 'RB'), ('ago', 'RB'), (',', ','), ('and', 'CC'), ('we', 'PRP'), ('are', 'VBP'), ('grateful', 'JJ'), ('for', 'IN'), ('the', 'DT'), ('good', 'JJ'), ('life', 'NN'), ('of', 'IN'), ('Coretta', 'NNP'), ('Scott', 'NNP'), ('King', 'NNP'), ('.', '.')]
chunked = chunkParser.parse(tagged)
nltk.draw.tree.TreeView(chunked)._cframe.print_to_file('output.ps')
os.system('convert output.ps output.png')
Image(filename='output.png')
[输出]:
这篇关于使用tkinter在jupyter笔记本中进行nltk绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!