NLTK无法找到gs文件

时间:2016-04-29 15:30:01

标签: python nlp nltk

我正在尝试使用stanford自然语言工具包NLTK。 安装完所需的文件后,我开始执行演示代码: http://www.nltk.org/index.html

>>> import nltk

>>> sentence = """At eight o'clock on Thursday morning
... Arthur didn't feel very good."""

>>> tokens = nltk.word_tokenize(sentence)

>>> tokens

['At', 'eight', "o'clock", 'on', 'Thursday', 'morning',
<'>'亚瑟','做',“不是”,'感觉','非常','好','。']

>>> tagged = nltk.pos_tag(tokens)

>>> tagged[0:6]

[('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'),

('周四','NNP'),('早上','NN')]

>>> entities = nltk.chunk.ne_chunk(tagged)

>>> entities

然后我收到消息:

LookupError: 

===========================================================================
NLTK was unable to find the gs file!
Use software specific configuration paramaters or set the PATH environment variable.

我试过谷歌,但是没有人告诉你遗漏的gs文件是什么。

6 个答案:

答案 0 :(得分:12)

我也遇到了这个错误。

gs代表ghostscript。你得到错误是因为你的chunker试图使用ghostscript绘制句子的解析树,如下所示:

enter image description here

我在使用IPython;调试问题我使用命令verbose将追溯详细程度设置为%xmode verbose,该命令打印每个堆栈帧的局部变量。 (参见下面的完整追溯)文件名是:

file_names=['gs', 'gswin32c.exe', 'gswin64c.exe']

Google搜索gswin32c.exe的一点点告诉我这是ghostscript。

/Users/jasonwirth/anaconda/lib/python3.4/site-packages/nltk/__init__.py in find_file_iter(filename='gs', env_vars=['PATH'], searchpath=(), file_names=['gs', 'gswin32c.exe', 'gswin64c.exe'], url=None, verbose=False)
    517                         (filename, url))
    518         div = '='*75
--> 519         raise LookupError('\n\n%s\n%s\n%s' % (div, msg, div))
    520 
    521 def find_file(filename, env_vars=(), searchpath=(),

LookupError: 

===========================================================================
NLTK was unable to find the gs file!
Use software specific configuration paramaters or set the PATH environment variable.
===========================================================================

答案 1 :(得分:3)

Jason Wirth的回答有点补充。在Windows下,这行代码将在环境变量PATH中搜索“gswin64c.exe”,但是,ghostscript安装程序不会将二进制文件添加到PATH,因此要使其工作,您需要找到ghostscript的安装位置并将/ bin子文件夹添加到PATH。

例如,在我的情况下,我将 C:\ Program Files \ gs \ gs9.19 \ bin 添加到PATH。

答案 2 :(得分:2)

只需添加到先前的答案中,如果将“实体”替换为“打印(实体)”,则不会收到错误消息。

没有print(),控制台/笔记本不知道如何“绘制”树对象。

答案 3 :(得分:0)

除了Alex Kinman之外,即使安装了ghostscript并将其添加到nltk路径后,我仍然会遇到相同的错误。使用print()可以打印实体,即使出现此错误,我似乎也可以在下面获得输出,但不幸的是还没有树。

Tree('S', [('At', 'IN'), ('eight', 'CD'), ("o'clock", 'NN'), ('on', 'IN'), ('Thursday', 'NNP'), ('morning', 'NN'), Tree('PERSON', [('Arthur', 'NNP')]), ('did', 'VBD'), ("n't", 'RB'), ('feel', 'VB'), ('very', 'RB'), ('good', 'JJ'), ('.', '.')]) 

答案 4 :(得分:0)

如果由于某种原因无法在您的平台上使用ghostscript或无法安装,则还可以使用精美的networkx软件包来可视化此类树:

import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout
import matplotlib.pyplot as plt

def drawNodes(G,nodeLabels,parent,lvl=0):
    def addNode(G,nodeLabels,label):
        n = G.number_of_nodes()
        G.add_node(n)
        nodeLabels[n] = label
        return n
    def findNode(nodeLabels,label):
        # Travel backwards from end to find right parent
        for i in reversed(range(len(nodeLabels))):
            if nodeLabels[i] == label:
                return i

    indent = " "*lvl
    if lvl == 0:
        addNode(G,nodeLabels,parent.label())
    for node in parent:
        if type(node) == nltk.Tree:
            n = addNode(G,nodeLabels,node.label())
            G.add_edge(findNode(nodeLabels,parent.label()),n)
            drawNodes(G,nodeLabels,node,lvl+1)
        else:
            print node
            n1 = addNode(G,nodeLabels,node[1])
            n0 = addNode(G,nodeLabels,node[0])
            G.add_edge(findNode(nodeLabels,parent.label()),n1)
            G.add_edge(n0,n1)

G = nx.Graph()
nodeLabels = {}
drawNodes(G,nodeLabels,entities)
options = {
    'node_color': 'white',
    'node_size': 100
 }
plt.figure(1,figsize=(12,6))
pos=graphviz_layout(G, prog='dot')
nx.draw(G, pos, font_weight='bold', arrows=False, **options)
l = nx.draw_networkx_labels(G,pos,nodeLabels) 

NLTK Token Tree plotted with NetworkX

答案 5 :(得分:0)

就我而言, 运行可执行文件 gs9.53.3.exe 并将 C:\Program Files\gs\gs9.53.3\bin 设置为我的 PATH 后,我不得不重新启动系统