いくつかのニュース記事からエンティティを抽出する目的で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 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
更新:これが完全なエラーメッセージです。
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