Created
December 19, 2017 18:26
-
-
Save abehmiel/04d53ca14f851b595ab05b07f1ae4aeb to your computer and use it in GitHub Desktop.
Part-of-speech clarifier from nltk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from nltk import pos_tag | |
from nltk.tag import str2tuple | |
""" | |
Usage: | |
dictionary_df['Pos'] = dictionary_df['Word'].apply(pos_maker) | |
dictionary_df['Help Definition'] = dictionary_df['Pos'].apply(clarify_pos) | |
""" | |
def clarify_pos(pos): | |
""" | |
From https://stackoverflow.com/questions/15388831/what-are-all-possible-pos-tags-of-nltk | |
Aim is to aid in analysis, can be easily applied to pandas dataframes to create | |
a new column so that pos signifiers can be read in plain English. | |
Input: String (part of speech) | |
Output: String (help definition from upenn corpus), possibly None | |
""" | |
if pos == 'CC': | |
return 'conjunction, coordinating' | |
"""& 'n and both but either et for less minus neither nor or plus so | |
therefore times v. versus vs. whether yet""" | |
if pos == 'CD': | |
return 'numeral, cardinal' | |
"""mid-1890 nine-thirty forty-two one-tenth ten million 0.5 one forty- | |
seven 1987 twenty '79 zero two 78-degrees eighty-four IX '60s .025 | |
fifteen 271,124 dozen quintillion DM2,000 ... """ | |
if pos == 'DT': | |
return 'determiner' | |
"""all an another any both del each either every half la many much nary | |
neither no some such that the them these this those""" | |
if pos == 'EX': | |
return 'existential there' | |
"""there""" | |
if pos == 'IN': | |
return 'preposition or conjunction, subordinating' | |
"""astride among uppon whether out inside pro despite on by throughout | |
below within for towards near behind atop around if like until below | |
next into if beside ...""" | |
if pos == 'JJ': | |
return 'adjective or numeral, ordinal' | |
"""third ill-mannered pre-war regrettable oiled calamitous first separable | |
ectoplasmic battery-powered participatory fourth still-to-be-named | |
multilingual multi-disciplinary ...""" | |
if pos == 'JJR': | |
return 'adjective, comparative' | |
"""bleaker braver breezier briefer brighter brisker broader bumper busier | |
calmer cheaper choosier cleaner clearer closer colder commoner costlier | |
cozier creamier crunchier cuter ...""" | |
if pos == 'JJS': | |
return 'adjective, superlative' | |
"""calmest cheapest choicest classiest cleanest clearest closest commonest | |
corniest costliest crassest creepiest crudest cutest darkest deadliest | |
dearest deepest densest dinkiest ...""" | |
if pos == 'LS': | |
return 'list item marker' | |
"""A A. B B. C C. D E F First G H I J K One SP-44001 SP-44002 SP-44005 | |
SP-44007 Second Third Three Two * a b c d first five four one six three | |
two""" | |
if pos == 'MD': | |
return 'modal auxiliary' | |
"""can cannot could couldn't dare may might must need ought shall should | |
shouldn't will would""" | |
if pos == 'NN': | |
return 'noun, common, singular or mass' | |
"""common-carrier cabbage knuckle-duster Casino afghan shed thermostat | |
investment slide humour falloff slick wind hyena override subhumanity | |
machinist ...""" | |
if pos == 'NNP': | |
return 'noun, proper, singular' | |
"""Motown Venneboerger Czestochwa Ranzer Conchita Trumplane Christos | |
Oceanside Escobar Kreisler Sawyer Cougar Yvette Ervin ODI Darryl CTCA | |
Shannon A.K.C. Meltex Liverpool ...""" | |
if pos == 'NNS': | |
return 'noun, common, plural' | |
"""undergraduates scotches bric-a-brac products bodyguards facets coasts | |
divestitures storehouses designs clubs fragrances averages | |
subjectivists apprehensions muses factory-jobs ...""" | |
if pos == 'PDT': | |
return 'pre-determiner' | |
"""all both half many quite such sure this""" | |
if pos == 'POS': | |
return 'genitive marker' | |
"""' 's """ | |
if pos == 'PRP': | |
return 'pronoun, personal' | |
"""hers herself him himself hisself it itself me myself one oneself ours | |
ourselves ownself self she thee theirs them themselves they thou thy us""" | |
if pos == 'PRP$': | |
return 'pronoun, possessive' | |
"""her his mine my our ours their thy your""" | |
if pos == 'RB': | |
return 'adverb' | |
"""occasionally unabatingly maddeningly adventurously professedly | |
stirringly prominently technologically magisterially predominately | |
swiftly fiscally pitilessly ...""" | |
if pos == 'RBR': | |
return 'adverb, comparative' | |
"""further gloomier grander graver greater grimmer harder harsher | |
healthier heavier higher however larger later leaner lengthier less- | |
perfectly lesser lonelier longer louder lower more ...""" | |
if pos == 'RBS': | |
return 'adverb, superlative' | |
"""best biggest bluntest earliest farthest first furthest hardest | |
heartiest highest largest least less most nearest second tightest worst""" | |
if pos == 'RP': | |
return 'particle' | |
"""aboard about across along apart around aside at away back before behind | |
by crop down ever fast for forth from go high i.e. in into just later | |
low more off on open out over per pie raising start teeth that through | |
under unto up up-pp upon whole with you""" | |
if pos == 'TO': | |
return '"to" as preposition or infinitive marker'' | |
"""to""" | |
if pos == 'UH': | |
return 'interjection' | |
"""Goodbye Goody Gosh Wow Jeepers Jee-sus Hubba Hey Kee-reist Oops amen | |
huh howdy uh dammit whammo shucks heck anyways whodunnit honey golly | |
man baby diddle hush sonuvabitch ...""" | |
if pos == 'VB': | |
return 'verb, base form' | |
"""ask assemble assess assign assume atone attention avoid bake balkanize | |
bank begin behold believe bend benefit bevel beware bless boil bomb | |
boost brace break bring broil brush build ...""" | |
if pos == 'VBD': | |
return 'verb, past tense' | |
"""dipped pleaded swiped regummed soaked tidied convened halted registered | |
cushioned exacted snubbed strode aimed adopted belied figgered | |
speculated wore appreciated contemplated ...""" | |
if pos == 'VBG': | |
return 'verb, present participle or gerund' | |
"""telegraphing stirring focusing angering judging stalling lactating | |
hankerin' alleging veering capping approaching traveling besieging | |
encrypting interrupting erasing wincing ...""" | |
if pos == 'VBN': | |
return 'verb, past participle' | |
"""multihulled dilapidated aerosolized chaired languished panelized used | |
experimented flourished imitated reunifed factored condensed sheared | |
unsettled primed dubbed desired ...""" | |
if pos == 'VBP': | |
return 'verb, present tense, not 3rd person singular' | |
"""predominate wrap resort sue twist spill cure lengthen brush terminate | |
appear tend stray glisten obtain comprise detest tease attract | |
emphasize mold postpone sever return wag ...""" | |
if pos == 'VBZ': | |
return 'verb, present tense, 3rd person singular' | |
"""bases reconstructs marks mixes displeases seals carps weaves snatches | |
slumps stretches authorizes smolders pictures emerges stockpiles | |
seduces fizzes uses bolsters slaps speaks pleads ...""" | |
if pos == 'WDT': | |
return 'WH-determiner' | |
"""that what whatever which whichever""" | |
if pos == 'WP': | |
return 'WH-pronoun' | |
"""that what whatever whatsoever which who whom whosoever""" | |
if pos == 'WRB': | |
return 'Wh-adverb' | |
"""how however whence whenever where whereby whereever wherein whereof why""" | |
def pos_maker(x): | |
""" | |
Uses pos_tag on a single word to return its part of speech | |
""" | |
try: | |
pos = pos_tag([x.lower()])[0][1] | |
except: | |
if x == 'NULL': | |
#throws a 'cannot use .lower() method on float' error | |
pos = 'NN' | |
return pos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment