Coding a thesaurus ex36

I’m trying to complete ex36 (writing a text adventure game) and I am trying to use a thesaurus to allow a range of user inputs. I had a single function for this but I had to split this into functions for verbs, nouns and adjectives as the sets returned by the initial function were too vague and didn’t relate to the choice. Is there a way to cut these back down into one function? or at least shorten the code.

def Vthesaurus(word):
	synonyms = []
	for syn in wn.synsets(word, pos=wn.VERB):
		for l in syn.lemmas():
			synonyms.append(l.name())
	return(set(synonyms))

def Nthesaurus(word):
	synonyms = []
	for syn in wn.synsets(word, pos=wn.NOUN):
		for l in syn.lemmas():
			synonyms.append(l.name())
	return(set(synonyms))

def Athesaurus(word):
	synonyms = []
	for syn in wn.synsets(word, pos=wn.ADJ):
		for l in syn.lemmas():
			synonyms.append(l.name())
	return(set(synonyms))

I’m assuming the wn is related to NLTK.

It’s way easier to just give you the link to an experiment I did with what you are looking for.
If your synsets and wn are not from NLTK, it should be so similar as to not matter.

NLTK is a beast, and is really useful. Good luck!

1 Like

Nice blog @nellietobey. Tons of good stuff there and a really funny name too :rofl:

1 Like

First up, I did a quick edit. Seems you’re used to a different forum, so on this one use [code][/code] to wrap your code.

Now, take a minute and look at these three from the point of view of a pattern. In fact, imagine you wrote these on glass with a marker and then stacked the sheets of glass. Most everything would line up and be the same except just a few parts. Those parts that don’t match are places where you can introduce a variable.

I think if you turn this into a function that takes the pos= parameter you’d be done.

1 Like