Python 为什么我会出错?ValueError:块结构必须包含标记的标记或树
我一直在修补NLTK,目的是从一些新闻文章中提取实体,但我一直得到一个错误,ValueError:块结构必须包含标记的标记或树 有人能帮我吗Python 为什么我会出错?ValueError:块结构必须包含标记的标记或树,python,nltk,text-mining,Python,Nltk,Text Mining,我一直在修补NLTK,目的是从一些新闻文章中提取实体,但我一直得到一个错误,ValueError:块结构必须包含标记的标记或树 有人能帮我吗 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
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
import lxml.html
导入nltk、re、pprint
def ie_预处理(文档):
“”“此函数获取原始文本和切块,然后将进程连接到断开
它可以分解成句子,然后是单词,然后完成词性标注
句子=nltk.sent\u标记化(文档)
句子=[nltk.word_标记化(sent)表示已发送的句子]
句子=[nltk.pos_标记(已发送)表示已发送的句子]
复句
#重要故事
基本url=”http://www.thisisstaffordshire.co.uk/Yobs-pelt-999-crews-bottles-fireworks-Shelton/story-17256383-detail/story.html"
page=lxml.html.parse(基本url)
story=page.xpath('/*[@id=“story”]/div[2]/div[1]'))
原始文本=故事[0]。文本内容()
#标记化
输出=ie_预处理(原始文本)
打印输出
#大块
语法=r''
NP:
{*}
'''
cp=nltk.RegexpParser(语法)
chunked=cp.parse(输出)
打印块
更新:下面是完整的错误消息
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
回溯(最近一次呼叫最后一次):
文件“geo_locator.py”,第30行,在
chunked=cp.parse(输出)
解析中的文件“/Users/davidelks/pythontests/venv/lib/python2.7/site packages/nltk/chunk/regexp.py”,第1183行
chunk\u struct=parser.parse(chunk\u struct,trace=trace)
文件“/Users/davidelks/pythontests/venv/lib/python2.7/site packages/nltk/chunk/regexp.py”,第999行,在解析中
chunkstr=ChunkString(chunk\u struct)
文件“/Users/davidelks/pythontests/venv/lib/python2.7/site packages/nltk/chunk/regexp.py”,第93行,在__
tags=[自身中tok的自身标签(tok)]
文件“/Users/davidelks/pythontests/venv/lib/python2.7/site packages/nltk/chunk/regexp.py”,第103行,在_标记中
raise VALUERROR('块结构必须包含标记的'
ValueError:块结构必须包含标记的标记或树
函数parse()
一次只能处理一个句子
这项工作:
chunked = []
for s in output:
chunked.append(cp.parse(s))
结果:
[Tree('S', [(u'POLICE', 'NN'), (u'are', 'VBP'), (u'hunting', 'VBG'), ...
包括回溯-我们没有水晶球,没有人会在没有入口点的情况下查看您的代码抱歉。我会记住以供将来参考。如何获得与我定义为正则表达式的语法的精确匹配数?请发布一个新问题。