class Word:
    word_dict = {}

def __init__(self, name, synonyms):
    self.name = name
self.synonyms = synonyms
Word.word_dict[name] = self
for s in synonyms:
    if not s in Word.word_dict:
        Word.word_dict[s] = Word(s, [])

def isAncestor(self, other):
    if other in self.synonyms:
        return True
for s in self.synonyms:
    if Word.word_dict[s].isAncestor(other):
        return True
return False

def areWordsRelated(word1, word2):
    if not word1 in Word.word_dict or not word2 in Word.word_dict:
        return False
return Word.word_dict[word1].isAncestor(word2) or Word.word_dict[word2
    ].isAncestor(word1)
words = []
words.append(Word('cat', ['feline', 'kitten', 'mouser']))
words.append(Word('kitten', ['kitty', 'kit']))
words.append(Word('kit', ['patck', 'bag', 'gear']))
words.append(Word('computer', ['electronics', 'PC', 'abacus']))
print(areWordsRelated('cat', 'kit'))
print(areWordsRelated('kit', 'cat'))
print(areWordsRelated('cat', 'computer'))
print(areWordsRelated('dog', 'computer'))
How to find Relationships between Objects