9

我需要从网站中提取所有城市名称。我在以前的项目中使用了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.

任何帮助,将不胜感激

4

3 回答 3

26

您可以通过将“.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':
于 2015-06-01T12:31:16.523 回答
4

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

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

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

完毕。快乐编码。

于 2016-01-19T03:43:41.463 回答
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)

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

于 2014-12-07T10:29:20.890 回答