【问题标题】:NLTK was unable to find the gs fileNLTK 找不到 gs 文件
【发布时间】:2016-04-29 15:30:01
【问题描述】:

我正在尝试使用 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 文件是什么。

【问题讨论】:

    标签: python nlp nltk


    【解决方案1】:

    我也遇到了这个错误。

    gs 代表幽灵脚本。您收到错误是因为您的分块器正在尝试使用 ghostscript 来绘制句子的解析树,如下所示:

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

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

    在谷歌上搜索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.
    ===========================================================================
    

    【讨论】:

    • mac用户可以通过brewbrew install ghostscript安装ghostscript。对于其他操作系统,可以在此处找到说明:wiki.scribus.net/canvas/…
    • 我安装了 ghostscript,但仍然遇到同样的错误,即使 Windows 搜索显示“gswin64c.exe”文件。
    • Windows:安装 Ghostscript 并手动将 Ghostscript bin 文件夹添加到我的路径后,我仍然需要重新启动我的机器,以便 NLTK 获取 Ghostcript 可执行文件。
    【解决方案2】:

    只是添加到以前的答案,如果您将“实体”替换为“打印(实体)”,您将不会收到错误。

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

    【讨论】:

    • 是的!只需将命令存储在变量中并使用 print。有用!谢谢,Axle Max
    • @Pranzell 不客气。我还在学习代码,所以我很高兴能帮助别人做出改变。 :-)
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      如果由于某种原因 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) 
      

      【讨论】:

        【解决方案5】:

        而不是entitiesentities.draw() 它应该可以工作。

        【讨论】:

          【解决方案6】:

          除了 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'), ('.', '.')]) 
          

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              【解决方案8】:

              对我来说,它在 conda 提示符下与“conda install --channel=conda-forge ghostscript”一起使用。我的操作系统是 Windows,我在 jupyter notebook 上运行代码。

              【讨论】:

                猜你喜欢
                • 2015-02-28
                • 2011-11-16
                • 1970-01-01
                • 2014-11-08
                • 2015-11-11
                • 2015-03-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多