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

时间:2014-12-07 09:27:54

标签: 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.

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:19)

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

答案 1 :(得分:4)

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

if(ne.node ==&#39; GPE&#39;或ne.node ==&#39; PERSON&#39;)和ne [0] [1] ==&#39; NNP&#39 ;:

if(ne.label()==&#39; GPE&#39;或ne.label()==&#39; PERSON&#39;)和ne [0] [1] ==&#39 ; NNP&#39;:

完成。快乐的编码。

答案 2 :(得分: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':

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)

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