Skip to content

Instantly share code, notes, and snippets.

@amnrzv
Last active November 1, 2017 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amnrzv/4aee7cf8ce827314d0e9562a35633cf5 to your computer and use it in GitHub Desktop.
Save amnrzv/4aee7cf8ce827314d0e9562a35633cf5 to your computer and use it in GitHub Desktop.
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