Python &引用;NotImplementedError:使用label()访问节点标签;

Python &引用;NotImplementedError:使用label()访问节点标签;,python,nltk,geograpy,Python,Nltk,Geograpy,我需要从网站上提取所有的城市名称。我在以前的项目中使用过beautifulSoup和RE,但在这个网站上,城市名称是常规文本的一部分,没有特定的格式。我发现geograpy包()满足了我的需求 Geography使用nltk包。我安装了nltk的所有型号和软件包,但它不断抛出以下错误: >>> import geograpy >>> places = geograpy.get_place_context(url="http://www.state.gov/mi

我需要从网站上提取所有的城市名称。我在以前的项目中使用过beautifulSoup和RE,但在这个网站上,城市名称是常规文本的一部分,没有特定的格式。我发现geograpy包()满足了我的需求

Geography使用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.
>导入地理图
>>>地点=地理图。获取地点上下文(url=”http://www.state.gov/misc/list/")
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\site packages\geograpy\\uuuuu init\uuuuuu.py”,第6行,在get\u place\u上下文中
e、 查找实体()
文件“C:\Python27\lib\site packages\geograpy\extraction.py”,第31行,在find\u entities中
如果(ne.node=='GPE'或ne.node=='PERSON')和ne[0][1]=='NNP':
文件“C:\Python27\lib\site packages\nltk\tree.py”,第198行,在\u get\u节点中
raise NOTEImplementedError(“使用label()访问节点标签”)
NotImplementedError:使用label()访问节点标签。

任何帮助都将不胜感激

看起来
geography
正在调用
nltk
对象的
节点
方法:

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)

包裹坏了。您可以自己修复它,也可以使用另一个。

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

在您的问题中,您可以尝试替换

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


不要假设每个人都修改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”:


完成了。祝您编码愉快。

谢谢您的回答。这是我自己想出来的。我真的希望有人能帮我“修理”这个包裹。可能是有nltk经验的人。至于使用不同的软件包,我已经寻找类似的软件包好几天了,但还没有找到任何东西。也许你可以说为什么这是必要的,来扩展一下你的答案。例如,API中是否有更改?
if (ne.label() == 'GPE' or ne.label() == 'PERSON') and ne[0][1] == 'NNP':