wsj
is of type nltk.corpus.reader.util.ConcatenatedCorpusView
that behaves like a list (this is why you can use functions like index()
), but "behind the scenes" NLTK never reads the whole list into memory, it will only read those parts from a file object that it needs. It seems that if you iterate over a CorpusView object and use index()
(which requires iterating again) at the same time, the file object will return None
.
This way it works, though it is less elegant than a list comprehension:
for i in range(len(wsj)):
if wsj[i][0] in cfd2['VN'].keys():
print wsj[(i-1):(i+1)]