Press "Enter" to skip to content

在 Jupyter Notebook 中显示 nltk tree

本站内容均来自兴趣收集,如不慎侵害的您的相关权益,请留言告知,我们将尽快删除.谢谢.

我们在做 Constituency Parsing 的时候,得到的结果往往是一个用 bracket string 表示的nltk.tree.Tree
对象,例如:

 

(S (NP (PRP She)) (VP (VBZ enjoys) (S (VP (VBG playing) (NP (NN tennis))))) (. .))

 

现在我们就以这个树作为例子,简单说下在 notebook 中绘制树时出现的问题。

 

先将这个字符串转成nltk.tree.Tree

 

from nltk.tree import Tree
tree = Tree.fromstring('(S (NP (PRP She)) (VP (VBZ enjoys) (S (VP (VBG playing) (NP (NN tennis))))) (. .))')

 

然后在本地 Jupyter Notebook 开发的时候,如果直接在 notebook 里输出这个树,会报以下错误:

 

>>> tree
The Ghostscript executable isn't found.
See http://web.mit.edu/ghostscript/www/Install.htm
If you're using a Mac, you can try installing
https://docs.brew.sh/Installation then `brew install ghostscript`
---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
~\Anaconda3\lib\site-packages
ltk\tree.py in _repr_png_(self)
    797                     [
--> 798                         find_binary(
    799                             "gs",
~\Anaconda3\lib\site-packages
ltk\internals.py in find_binary(name, path_to_bin, env_vars, searchpath, binary_names, url, verbose)
    687 ):
--> 688     return next(
    689         find_binary_iter(
~\Anaconda3\lib\site-packages
ltk\internals.py in find_binary_iter(name, path_to_bin, env_vars, searchpath, binary_names, url, verbose)
    672     """
--> 673     for file in find_file_iter(
    674         path_to_bin or name, env_vars, searchpath, binary_names, url, verbose
~\Anaconda3\lib\site-packages
ltk\internals.py in find_file_iter(filename, env_vars, searchpath, file_names, url, verbose, finding_dir)
    631         div = "=" * 75
--> 632         raise LookupError("
%s
%s
%s" % (div, msg, div))
    633 
LookupError: 
===========================================================================
NLTK was unable to find the gs file!
Use software specific configuration paramaters or set the PATH environment variable.
===========================================================================
During handling of the above exception, another exception occurred:
LookupError                               Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:
~\Anaconda3\lib\site-packages
ltk\tree.py in _repr_png_(self)
    815                 )
    816                 print(pre_error_message, file=sys.stderr)
--> 817                 raise LookupError
    818 
    819             with open(out_path, "rb") as sr:
LookupError: 
Tree('S', [Tree('NP', [Tree('PRP', ['She'])]), Tree('VP', [Tree('VBZ', ['enjoys']), Tree('S', [Tree('VP', [Tree('VBG', ['playing']), Tree('NP', [Tree('NN', ['tennis'])])])])]), Tree('.', ['.'])])

 

不过我们可以调用tree.draw()
来画出这个树:

 

 

但如果我们是远程开发,notebook 是在远程 Linux 服务器上运行的,那tree.draw()
可就也不管用了:

 

---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
/tmp/ipykernel_12094/455345907.py in <module>
----> 1 output.draw()
/data/anaconda3/envs/envname/lib/python3.8/site-packages/nltk/tree.py in draw(self)
    763         from nltk.draw.tree import draw_trees
    764 
--> 765         draw_trees(self)
    766 
    767     def pretty_print(self, sentence=None, highlight=(), stream=None, **kwargs):
/data/anaconda3/envs/envname/lib/python3.8/site-packages/nltk/draw/tree.py in draw_trees(*trees)
   1006     :rtype: None
   1007     """
-> 1008     TreeView(*trees).mainloop()
   1009     return
   1010 
/data/anaconda3/envs/envname/lib/python3.8/site-packages/nltk/draw/tree.py in __init__(self, *trees)
    857         self._trees = trees
    858 
--> 859         self._top = Tk()
    860         self._top.title("NLTK")
    861         self._top.bind("<Control-x>", self.destroy)
/data/anaconda3/envs/envname/lib/python3.8/tkinter/__init__.py in __init__(self, screenName, baseName, className, useTk, sync, use)
   2268                 baseName = baseName + ext
   2269         interactive = 0
-> 2270         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   2271         if useTk:
   2272             self._loadtk()
TclError: no display name and no $DISPLAY environment variable

 

解决

 

解决方案很简单,只需两行代码

 

import svgling
svgling.disable_nltk_png()

 

在 notebook 开头引入以上两行代码即可,禁用 nltk 的绘制功能,转而使用svgling
来绘制树。

 

结果如下:

 

 

缺点就是这就变成静态图了,不能收缩节点了。

 

是不是很简单,之前总是遇到这个问题,现在终于有个简单方便的解决方法了!

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。