繁体   English   中英

“NotImplementedError:使用 label() 访问节点标签”

[英]“NotImplementedError: Use label() to access a node label”

我需要从网站中提取所有城市名称。 我在以前的项目中使用过 beautifulSoup 和 RE,但在这个网站上,城市名称是常规文本的一部分,没有特定的格式。 我找到了满足我要求的 geograpy 包( 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.

任何帮助,将不胜感激

您可以通过将“.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':

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

完毕。 快乐编码。

看起来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)

包裹坏了。 您可以自己修复它或使用不同的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM