Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from nltk.corpus import wordnet as wn
- def get_category_members(name):
- '''
- Use NLTK to get members of a category
- Huge thanks to http://stackoverflow.com/questions/15330725/how-to-get-all-the-hyponyms-of-a-word-synset-in-python-nltk-and-wordnet
- '''
- members = set()
- synsets = wn.synsets(name)
- for synset in synsets:
- members = members.union(set([w for s in synset.closure(lambda s:s.hyponyms(),depth=10) for w in s.lemma_names]))
- return members
- def w1_in_w2(w1,w2):
- '''
- Return leftover letters if all the letters from w1 are in w2
- Return False otherwise
- '''
- for c in w1:
- if c not in w2:
- return False
- else:
- pos = w2.find(c)
- w2 = w2[:pos] + w2[pos+1:]
- return w2
- # Set of trees
- trees = frozenset(get_category_members('tree'))
- # Set of herbs and spices
- herbs_spices = set()
- for x in ('herb','spice'):
- herbs_spices = herbs_spices.union(get_category_members(x))
- # Dictionary to help find anagrams
- sorted_to_word = dict()
- for word in herbs_spices:
- word = word.lower()
- s = ''.join(sorted(word))
- try:
- sorted_to_word[s].append(word)
- except KeyError:
- sorted_to_word[s] = [word]
- # Go through and give results
- for tree in trees:
- for herb in herbs_spices:
- leftover = w1_in_w2(herb.lower(),tree.lower())
- if leftover:
- l = leftover.replace('_','')
- s = ''.join(sorted(l))
- try:
- new_words = sorted_to_word[s]
- except KeyError:
- new_words = []
- for spice in new_words:
- print "%s -> %s %s" % (tree, herb, spice)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement