2017-07-03 32 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