Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python NLTK POS tagger workaround example.
import nltk
import re
from nltk.tokenize import word_tokenize, sent_tokenize
text = "I'm not going to the party."
words = word_tokenize(text)
pos_tags = nltk.pos_tag(words)
print (pos_tags)
for index, tag in enumerate(pos_tags):
match = re.search('\w.*', tag[1])
if match:
if tag[0] == "n't" or tag[0] == "not":
pos_tags[index] = (pos_tags[index][0], "NEG")
print (pos_tags)
[('I', 'PRP'), ("'m", 'VBP'), ('not', 'RB'), ('going', 'VBG'), ('to', 'TO'), ('the', 'DT'), ('party', 'NN'), ('.', '.')]
[('I', 'PRP'), ("'m", 'VBP'), ('not', 'NEG'), ('going', 'VBG'), ('to', 'TO'), ('the', 'DT'), ('party', 'NN'), ('.', '.')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.