Skip to content

Instantly share code, notes, and snippets.

@terbo
Last active December 28, 2015 09:41
Show Gist options
  • Save terbo/fb3bf7c98ab03be038f0 to your computer and use it in GitHub Desktop.
Save terbo/fb3bf7c98ab03be038f0 to your computer and use it in GitHub Desktop.
TextBlob Parts of Speech Tagger with Penn Treebank Tag Explanations - CLI
#!/usr/bin/python
# simple cli pos tagger for short sentences
# usage: pos.py some sort of sentance.
# or: echo some sentance | pos.py
#
# takes some time to load, but may
# move on to use opennlp in a server role.
import sys, fileinput
from textblob import TextBlob as tb;
tags = {
'CC': 'Coordinating conjunction',
'CD': 'Cardinal number',
'DT': 'Determiner',
'EX': 'Existential there',
'FW': 'Foreign word',
'IN': 'Preposition or subordinating conjunction',
'JJ': 'Adjective',
'JJR': 'Adjective, comparative',
'JJS': 'Adjective, superlative',
'LS': 'List item marker',
'MD': 'Modal',
'NN': 'Noun, singular or mass',
'NNS': 'Noun, plural',
'NNP': 'Proper noun, singular',
'NNPS': 'Proper noun, plural',
'PDT': 'Predeterminer',
'POS': 'Possessive ending',
'PRP': 'Personal pronoun',
'PRP$': 'Possessive pronoun',
'RB': 'Adverb',
'RBR': 'Adverb, comparative',
'RBS': 'Adverb, superlative',
'RP': 'Particle',
'SYM': 'Symbol',
'TO': 'to',
'UH': 'Interjection',
'VB': 'Verb, base form',
'VBD': 'Verb, past tense',
'VBG': 'Verb, gerund or present participle',
'VBN': 'Verb, past participle',
'VBP': 'Verb, non-3rd person singular present',
'VBZ': 'Verb, 3rd person singular present',
'WDT': 'Wh-determiner',
'WP': 'Wh-pronoun',
'WP$': 'Possessive wh-pronoun',
'WRB': 'Wh-adverb'
}
if(len(sys.argv)>1):
str = tb(' '.join(sys.argv[1:]))
else:
str = tb(' '.join(fileinput.input()));
pos = str.tags
for x in range(len(pos)):
print '%s - %s - %s' % (pos[x][0], pos[x][1], tags[pos[x][1]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment