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".
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".
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']