2017-07-03 132 views
0

我想绘制图(内联)的nltkjupyter-notebook。但得到的错误:使用tkinter为nltk绘制jupyter笔记本内

TclError: no display name and no $DISPLAY environment variable 

我已经尝试设置$DISPLAY为不同的值:

$env DISPLAY=0.0 
# or 
$env DISPLAY=inline 
# or 
$env DISPLAY= 

,但得到的错误(或类似):

TclError: couldn't connect to display "0.0" 

这里是我的代码https://github.com/hyzhak/nltk-experiments/blob/master/main.ipynb最后细胞。

环境:官方anaconda3码头 - continuumio/anaconda3:4.4.0https://github.com/ContinuumIO/docker-images。里面有nltk==3.2.3

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. 

我怎么能解决这个错误,在线nltk图形jupyter notebook里面?

更新1

http://www.nltk.org/_modules/nltk/draw/tree.html#draw_trees根据NLTK树的来源绘制它使用tkinter

更新2

我也问过官方NLTK的github仓库同样的问题https://github.com/nltk/nltk/issues/1765

更新3

我认为错误的原因可能是无头的主机(搬运工)。我已经安装了xvfb,但似乎已经足够了。

RUN apt-get install -y xvfb 

解决方案

我认为xvfb默认情况下推出的,但应当明确的运行。所以在我启动之后,我可以制作nltk树形图的截图。

+0

嘿,你怎么跑'xvfb'明确?我运行了'Xvfb:1 -screen 0 800x600x16'命令,但仍然收到相同的错误。 –

+0

我已经将它作为守护进程启动。 –

回答

0

尝试结合下面的代码片段在代码

import os 
import matplotlib as mpl 
if os.environ.get('DISPLAY','') == '': 
    print('no display found. Using non-interactive Agg backend') 
    mpl.use('Agg') 
import matplotlib.pyplot as plt 

不管。你的问题是Matplotlib对x-use后端的默认调用显然会导致问题。这个答案绝不是唯一可能的解决方案,但我认为这将解决您的问题。 请记住,在导入pyplot之前切换后端之前是很重要的。

或者,你可以尝试 IPython.core.display.display(分块)

确保您的笔记本电脑服务器使用-x标志设置..

def jupyter_draw_nltk_tree(tree): 
    from IPython.display import Image, display 
    from nltk.draw import TreeWidget 
    from nltk.draw.util import CanvasFrame 
    import subprocess 
    cf = CanvasFrame() 
    tc = TreeWidget(cf.canvas(), tree) 
    tc['node_font'] = 'arial 13 bold' 
    tc['leaf_font'] = 'arial 14' 
    tc['node_color'] = '#005990' 
    tc['leaf_color'] = '#3F8F57' 
    tc['line_color'] = '#175252' 
    cf.add_widget(tc, 10, 10) 
    cf.print_to_file('tmp_tree_output.ps') 
    cf.destroy() 

    args = (['convert', 'tmp_tree_output.ps', 'tmp_tree_output.png']) 
    subprocess.call(args) 
    display(Image(filename='tmp_tree_output.png')) 
    os.system('rm tmp_tree_output.ps tmp_tree_output.png') 

jupyter_draw_nltk_tree(chunked) 
+0

我有同样的错误。据我所知,'nltk'直接使用'tkinter',并忽略'matplotlib'。也许还有其他方法可以在不使用inner(nltk)图形方法的情况下绘制'nltk'的图形? –

+0

http://www.nltk.org/_modules/nltk/draw/tree.html#draw_trees我刚刚看过图形绘制的源代码,发现它确实不使用matplotlib绘制图形。非常奇怪:( –

+0

编辑答案.. – Uvar

1
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') 

[出]:

enter image description here

+0

因为'nltk.draw .tree.TreeView'创建'tkinter'实例'self._top = Tk()'(http://www.nltk.org/_modules/nltk/draw/tree.html#TreeView) –

+0

好的,有趣的,你运行jupyter在你的本地主机系统上,或者在docker或远程主机里面?难道是Anaconda docker的问题吗? –

+0

我认为这是conda docker。我在本地运行jupyter。 – alvas