0
votes

I have sentences and I want to replace only its Pronoun, nouns and adjective with its corresponding POS Tags. For example my sentence is :

"I am going to the most beautiful city, Islamabad"

and want the result

"PRP am VBG to the most JJ NN, NNP".

1
So what is the question? Where do you have difficulties? - Tomasz Plaskota
how can I do it efficiently in Python NLTK? thanks @TomaszPlaskota - Irfanullah

1 Answers

0
votes

TL;DR

>>> from nltk import pos_tag, word_tokenize
>>> sent = "I am going to the most beautiful city, Islamabad"
>>> [pos if any(p for p in wanted_tags if pos.startswith(p)) else word for word, pos in pos_tag(word_tokenize(sent))]
['PRP', 'VBP', 'VBG', 'to', 'the', 'RBS', 'JJ', 'NN', ',', 'NNP']

>>> from nltk.corpus import stopwords
>>> stoplist = stopwords.words('english')
>>> [pos if any(p for p in wanted_tags if pos.startswith(p)) and word not in stoplist  else word for word, pos in pos_tag(word_tokenize(sent))]
['PRP', 'am', 'VBG', 'to', 'the', 'most', 'JJ', 'NN', ',', 'NNP']