Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting error? ValueError: chunk structures must contain tagged tokens or trees

I've been tinkering with NLTK with the aim of extracting entities from some news articles, but I keep getting an error:

ValueError: chunk structures must contain tagged tokens or trees.

Here's my code:

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

Update

Here's the error message in full:

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
like image 279
elksie5000 Avatar asked Feb 18 '23 14:02

elksie5000


1 Answers

The parse() function can only handle one sentence at a time.

This works:

chunked = []
for s in output:
    chunked.append(cp.parse(s))

Result:

[Tree('S', [(u'POLICE', 'NN'), (u'are', 'VBP'), (u'hunting', 'VBG'), ... 
like image 185
Suzana Avatar answered Feb 21 '23 04:02

Suzana