Skip to content

Instantly share code, notes, and snippets.

@erichannell
Last active September 4, 2015 08:16
Show Gist options
  • Save erichannell/701aabacc9f8bcf5e08d to your computer and use it in GitHub Desktop.
Save erichannell/701aabacc9f8bcf5e08d to your computer and use it in GitHub Desktop.
from textblob import Word
import re
def parse(text):
pattern = '''Synset\(\'(.*)\.n'''
return re.findall(pattern,text)[0]
def classify(word):
try:
word = Word(word)
except:
return 'no category'
try:
type_of_word = word.synsets[0].hypernyms()[0]
return parse(str(type_of_word))
except:
return 'no category'
words = ["Cat","Castle","King","Lion","Swan","Tavern"]
for word in words:
print word, "->", classify(word)
# Cat -> feline
# Castle -> mansion
# King -> sovereign
# Lion -> big_cat
# Swan -> aquatic_bird
# Tavern -> building
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment