Last active
September 4, 2015 08:16
-
-
Save erichannell/701aabacc9f8bcf5e08d to your computer and use it in GitHub Desktop.
This file contains 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 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