string index out of range in POS tagging

Viewed 1337

I am doing POS tagging using nltk package in python. Now it's showing error string index out of range even though my string not much big.

import nltk

sample_list = ['', 'emma', 'jane', 'austen', '1816', '', 'volume', 'chapter', 'emma', 'woodhouse', ' ','handsome', ' ', 'clever', ' ', 'rich', ' ', 'comfortable', 'home', 'happy', 'disposition', ' ','seemed', 'unite', 'best','blessings', 'existence', '', 'lived','nearly', 'twenty-one', 'years','world', 'little', 'distress', 'vex', '', 'youngest','two']

tagged = nltk.pos_tag(sample_list)

screenshot of the error

2 Answers

Your problem is with empty strings, namely '' so you can use:

tagged = nltk.pos_tag([i for i in sample_list if i])

Your input contains empty 'words', such as your first items in the list. Try filtering is out like this:

clean_sample_list = [word for word in sample_list if 
word]
tagged = nltk.pos_tag(clean_sample_list)
Related