@@ -0,0 +1,2651 @@
+from collections import defaultdict%0Afrom dataclasses import dataclass%0A%0A@dataclass%0Aclass Node:%0A%0A    language: str%0A    word: str%0A%0A    def __hash__(self) -%3E int:%0A        # dummy hashing...%0A        # not needed if you use unsafe_hash = True in dataclass decorator.%0A        return hash(self.language + self.word)%0A%0A%0Aclass GenericTranslator:%0A    def __init__(self) -%3E None:%0A        self.langage_map = defaultdict(list)%0A%0A    def add(self, from_lang: str, from_word: str, to_lang: str, to_word: str) -%3E None:%0A        # add the translation from one language to another to the graph%0A        self._add(Node(from_lang, from_word), Node(to_lang, to_word))%0A%0A    def _add(self, from_node: Node, to_node: Node) -%3E None:%0A        # bi-directional mapping added to the grpah%0A        self.langage_map%5Bfrom_node%5D.append(to_node)%0A        self.langage_map%5Bto_node%5D.append(from_node)%0A%0A    def translate(self, from_lang: str, to_lang: str, word: str):%0A        # translate from one language to another if we are expected to find that conversion%0A        visited = set()%0A        result = self.dfs(Node(from_lang, word), to_lang, visited)%0A        if result == None:%0A            return f%22Sorry cannot translate %7Bword%7D to %7Bto_lang%7D language....%22%0A        return f%22%7Bword%7D in %7Bfrom_lang%7D, when succesfully translated to %7Bto_lang%7D becomes %7Bresult%7D !!!%22%0A%0A    def dfs(self, node: Node, to_lang: str, seen: set) -%3E str:%0A        if node == None:%0A            return None%0A        if node.language == to_lang:%0A            return node.word%0A        if self.langage_map.get(node) == None:%0A            return None%0A        if node in seen:%0A            return None%0A%0A        # mark as visited%0A        seen.add(node)%0A%0A        # scan the graph%0A        for neighbour_node in self.langage_map.get(node):%0A            result = self.dfs(neighbour_node, to_lang, seen)%0A            if result != None:%0A                return result%0A%0A        # backtrack%0A        seen.remove(node)%0A        return None%0A%0A%0Adef main():%0A    # simple helper method to dry run...%0A    translator = GenericTranslator()%0A    translator.add(%22English%22, %22Hello%22, %22Spanish%22, %22Hola%22)%0A    translator.add(%22Spanish%22, %22Hola%22, %22Hindi%22, %22Namaste%22)%0A%0A    print(translator.translate(%22Hindi%22, %22English%22, %22Namaste%22))%0A    print(translator.translate(%22Hindi%22, %22Spanish%22, %22Namaste%22))%0A    print(translator.translate(%22English%22, %22Hindi%22, %22Hello%22))%0A    print(translator.translate(%22English%22, %22Spanish%22, %22Hello%22))%0A    print(translator.translate(%22Spanish%22, %22English%22, %22Hola%22))%0A    print(translator.translate(%22Spanish%22, %22Hindi%22, %22Hola%22))%0A    print(translator.translate(%22Spanish%22, %22French%22, %22Hola%22))%0A    print(translator.translate(%22Spanish%22, %22Hindi%22, %22Holaa%22))%0A%0A%0Amain()%0A

Create generic_language_translator_via_dfs.py