-1

I am getting a parsing error when I use the following code in Jupyter using python 3.4

import nltk
from nltk.corpus import state_union
from nltk.tokenize import PunktSentenceTokenizer

train_text = state_union.raw("2005-GWBush.txt")
sample_text = state_union.raw("2006-GWBush.txt")

custom_sent_tokenizer = PunktSentenceTokenizer(train_text)

tokenized = custom_sent_tokenizer.tokenize(sample_text)

def process_content():
    try:
        for i in tokenized:
            words = nltk.word_tokenize(i)
            tagged = nltk.pos_tag(words)

            chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP><NN>?}"""

            chunkParser = nltk.RegexpParser(chunkGram)
            chunked = chunkParser.parse(tagged)

            print(chunked)

I am getting the following error:

 File "<ipython-input-12-a049462ffecb>", line 26

    ^
SyntaxError: unexpected EOF while parsing

Please advise on how I can solve this parsing error

MayoLikeOJ
  • 15
  • 1
  • 6
  • Take a look at http://stackoverflow.com/questions/1835756/using-try-vs-if-in-python and http://stackoverflow.com/questions/7604636/better-to-try-something-and-catch-the-exception-or-test-if-its-possible-first and https://github.com/usaarhat/pywarmups/blob/master/session4.md or – alvas Mar 14 '16 at 18:02

1 Answers1

2

Python is looking for the rest of the compound try: statement, e.g. a finally: or except: block.

As you didn't provide one, Python complains about this. With no other blocks at a lower indentation level, it only knew for certain the rest was missing when the parser reached the end of the file. Because the parser was expecting to find another part of the statement, finding an EOF (end of file) instead is unexpected.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343