Somewhat a python/programming newbie here.
I am trying to access a specified range of tuples from a list of tuples, but I only want to access the first element from the range of tuples. The specified range is based on a pattern I am looking for in a string of text that has been tokenized and tagged by nltk. My code:
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
text = "It is pretty good as far as driveway size is concerned, otherwise I would skip it"
tokenized = word_tokenize(text)
tagged = pos_tag(tokenized)
def find_phrase():
counter = -1
for tag in tagged:
counter += 1
if tag[0] == "as" and tagged[counter+6][0] == "concerned":
print tagged[counter:counter+7]
find_phrase()
Printed output:
[('as', 'IN'), ('far', 'RB'), ('as', 'IN'), ('driveway', 'NN'), ('size', 'NN'), ('is', 'VBZ'), ('concerned', 'VBN')]
What I actually want:
['as', 'far', 'as', 'driveway', 'size', 'is', 'concerned']
Is it possible to modify the my line of code print tagged[counter:counter+7]
to get my desired printed output?