首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我会得到错误?ValueError:区块结构必须包含标记的标记或树

为什么我会得到错误?ValueError:区块结构必须包含标记的标记或树
EN

Stack Overflow用户
提问于 2012-11-07 20:11:23
回答 1查看 2.4K关注 0票数 4

我一直在修补NLTK,目的是从一些新闻文章中提取实体,但我一直收到一个错误,ValueError:块结构必须包含标记的标记或树。

有谁可以帮我?

代码语言:javascript
运行
复制
import lxml.html
import nltk, re, pprint 



def ie_preprocess(document):
    """This function takes raw text and chops and then connects the process to break     
       it down into sentences, then words and then complete part-of-speech tagging"""
    sentences = nltk.sent_tokenize(document)
    sentences = [nltk.word_tokenize(sent) for sent in sentences]
    sentences = [nltk.pos_tag(sent) for sent in sentences]
    return sentences


    #import story
    base_url = "http://www.thisisstaffordshire.co.uk/Yobs-pelt-999-crews-bottles-fireworks-Shelton/story-17256383-detail/story.html"
    page = lxml.html.parse(base_url)
    story = page.xpath('//*[@id="story"]/div[2]/div[1]')
    raw_text = story[0].text_content()
    #tokenize
    output = ie_preprocess(raw_text)
    print output
    #chunk
    grammar = r'''
       NP: 
       {<DT><NN.*><.*>*<NN.*>} 
       '''
    cp = nltk.RegexpParser(grammar)

    chunked = cp.parse(output)

    print chunked

更新:以下是完整的错误消息。

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "geo_locator.py", line 30, in <module>
    chunked = cp.parse(output)
  File "/Users/davidelks/pythontests/venv/lib/python2.7/site-packages/nltk/chunk/regexp.py", line 1183, in parse
    chunk_struct = parser.parse(chunk_struct, trace=trace)
  File "/Users/davidelks/pythontests/venv/lib/python2.7/site-packages/nltk/chunk/regexp.py", line 999, in parse
     chunkstr = ChunkString(chunk_struct)
  File "/Users/davidelks/pythontests/venv/lib/python2.7/site-packages/nltk/chunk/regexp.py", line 93, in __init__
tags = [self._tag(tok) for tok in self._pieces]
  File "/Users/davidelks/pythontests/venv/lib/python2.7/site-packages/nltk/chunk/regexp.py", line 103, in _tag
    raise ValueError('chunk structures must contain tagged '
ValueError: chunk structures must contain tagged tokens or trees
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-08 21:28:36

parse()函数一次只能处理一个句子。

这是可行的:

代码语言:javascript
运行
复制
chunked = []
for s in output:
    chunked.append(cp.parse(s))

结果:

代码语言:javascript
运行
复制
[Tree('S', [(u'POLICE', 'NN'), (u'are', 'VBP'), (u'hunting', 'VBG'), ... 
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13269543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档