【问题标题】:"NotImplementedError: Use label() to access a node label"“NotImplementedError:使用 label() 访问节点标签”
【发布时间】:2015-02-05 02:58:14
【问题描述】:

我需要从网站中提取所有城市名称。我在以前的项目中使用了beautifulSoup 和RE,但在这个网站上,城市名称是常规文本的一部分,没有特定的格式。我找到了满足我要求的地理包 (https://pypi.python.org/pypi/geograpy/0.3.7)。

Geograpy 使用 nltk 包。我为 nltk 安装了所有模型和包,但它一直抛出这个错误:

>>> import geograpy
>>> places = geograpy.get_place_context(url="http://www.state.gov/misc/list/")

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\geograpy\__init__.py", line 6, in get_place_context
e.find_entities()
File "C:\Python27\lib\site-packages\geograpy\extraction.py", line 31, in find_entities
if (ne.node == 'GPE' or ne.node == 'PERSON') and ne[0][1] == 'NNP':
File "C:\Python27\lib\site-packages\nltk\tree.py", line 198, in _get_node
raise NotImplementedError("Use label() to access a nod label.")
NotImplementedError: Use label() to access a node label.

任何帮助将不胜感激

【问题讨论】:

    标签: python nltk geograpy


    【解决方案1】:

    您可以通过将“.node”替换为“.label()”来解决此问题。

    在你的问题,你可以尝试更换

    if (ne.node == 'GPE' or ne.node == 'PERSON') and ne[0][1] == 'NNP':
    

    if (ne.label() == 'GPE' or ne.label() == 'PERSON') and ne[0][1] == 'NNP':
    

    【讨论】:

    • 也许您可以通过说明为什么有必要来扩展您的答案。例如,API 是否发生了变化?
    【解决方案2】:

    不要假设每个人都修改 lib 文件。对于需要帮助的人或任何人,您需要访问软件包的安装位置。你想修改extraction.py。如果您使用的是 Windows 10 或类似系统,该文件可以位于 C:\Python27\Lib\site-packages\geograpy\extraction.py 中。它通常与 python 在同一个安装目录中。正如其他人之前提到的,更改(第 31 行)

    如果(ne.node == 'GPE' 或 ne.node == 'PERSON')和 ne[0][1] == 'NNP':

    如果 (ne.label() == 'GPE' 或 ne.label() == 'PERSON') 和 ne[0][1] == 'NNP':

    完成。快乐编码。

    【讨论】:

      【解决方案3】:

      看起来geograpy 正在调用nltk Tree 对象的node 方法:

      nes = nltk.ne_chunk(nltk.pos_tag(text))
      for ne in nes:
          if len(ne) == 1:
              if (ne.node == 'GPE' or ne.node == 'PERSON') and ne[0][1] == 'NNP':
      

      nltk 包已标记为已弃用:

      def _get_node(self):
          """Outdated method to access the node value; use the label() method instead."""
          raise NotImplementedError("Use label() to access a node label.")
      def _set_node(self, value):
          """Outdated method to set the node value; use the set_label() method instead."""
          raise NotImplementedError("Use set_label() method to set a node label.")
      node = property(_get_node, _set_node)
      

      包裹坏了。您可以自行修复或使用其他的。

      【讨论】:

      • 感谢您的回答。我自己已经想通了。我实际上希望有人能帮助我“修复”这个包。可能是有nltk经验的人。至于使用不同的包,我一直在寻找类似的包,但还没有找到任何东西。
      猜你喜欢
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 2018-12-31
      • 2020-06-24
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      相关资源
      最近更新 更多