2014-12-07 53 views
7

我需要从网站中提取所有城市名称。我在以前的项目中使用了RE的beautifulSoup,但在本网站上,城市名称是常规文本的一部分,没有特定的格式。我找到符合我要求的地理包裹(https://pypi.python.org/pypi/geograpy/0.3.7)。“NotImplementedError:使用标签()访问节点标签”

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. 

任何帮助,将不胜感激

回答

2

看起来geograpy呼吁的nltkTree对象的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) 

包装坏了。你可以自己修复或使用其他的。

+0

感谢您的回答。我自己想到了这一点。我实际上希望有人能帮助我“修复”这个软件包。可能是有nltk经验的人。至于使用不同的包装,我一直在寻找类似的包装好几天,但还没有发现任何东西。 – Faisal 2014-12-09 13:54:32

13

您可以通过更换 “.node” “.label()” 解决这个问题。

在你的问题,你可以尝试用

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

也许你可以通过说出为什么这是必要的来扩展你的答案。例如,API有没有改变? – 2015-06-01 12:51:21

4

更换

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

不要以为每个人修改LIB文件。对于需要帮助的人或者任何人,您需要访问安装包的位置。你想修改extract.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()== '人')和ne [0] [1] == 'NNP':

完成。快乐编码。