I created a generator as follow
from textacy.extract.kwic import keyword_in_context
test = keyword_in_context('this is a test. another test to see how', keyword='test', window_width=5)
print(test)
# Out: <generator object keyword_in_context at 0x000001C21D033F20>
But when I tried to iterate the generator test, it didn't work as expected: it only printed out the last item in test:
for i in test:
print(next(test))
# Out: ('ther ', 'test', ' to s')
However, this raw generator worked properly:
for i in keyword_in_context('this is a test. another test see how', keyword='test', window_width=5):
print(i)
# Out:
# ('is a ', 'test', '. ano')
# ('ther ', 'test', ' see ')
My questions are:
- Why the first for loop didn't work?
- Why couldn't I use
items()oriteritems()(which resulted inAttributeError: 'generator' object has no attribute 'items'/'iteritems) - What is the best way to extract all items from this generator?