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

Fai*_*sal 7 python nltk

我需要从网站中提取所有城市名称.我在以前的项目中使用过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.
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激

小智 19

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

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

if (ne.node == 'GPE' or ne.node == 'PERSON') and ne[0][1] == 'NNP':
Run Code Online (Sandbox Code Playgroud)

if (ne.label() == 'GPE' or ne.label() == 'PERSON') and ne[0][1] == 'NNP':
Run Code Online (Sandbox Code Playgroud)


Pat*_*ins 2

看起来像是geograpy在调用node一个nltk Tree对象的方法:

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':
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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