def add_synset(self, synset: Synset): """ Add a synset to wordnet. Args: synset (Synset): The synset to be added. Raises: TypeError: If any argument has incorrect type. WordNetError: If a synset with the given id is already in the wordnet. """ if not isinstance(synset, Synset): raise TypeError( "Argument 'synset' has incorrect type, expected Synset, got {}" .format(type(synset).__name__)) if synset.id in self._synsets: raise WordNetError( "Synset with id '{}' is already in the wordnet".format( synset.id)) self._graph.add_node(synset.id) self._synsets[synset.id] = synset for literal in synset.literals: self._literal2synset[literal].append(synset.id)
def shortest_path(self, synset_id1: str, synset_id2: str, relations: set = None): """ Get the shortest path from the first synset to the second synset. Args: synset_id1 (str): Id of the first synset. synset_id2 (str): Id of the second synset. relations (list of str): The allowed relations in the shortest path algorithm. Returns: list of str: A list of synset ids representing the path from the first synset to the second synset. Raises: TypeError: If any argument has incorrect type. WordNerError: If there's no synset with the given ids in the wordnet or if any relation has an incorrect value. """ if not isinstance(synset_id1, str): raise TypeError( "Argument 'synset_id1' has incorrect type, expected str, got {}" .format(type(synset_id1).__name__)) if not isinstance(synset_id2, str): raise TypeError( "Argument 'synset_id2' has incorrect type, expected str, got {}" .format(type(synset_id2).__name__)) if synset_id1 not in self._synsets: raise WordNetError( "Synset with id '{}' is not in the wordnet".format(synset_id1)) if synset_id2 not in self._synsets: raise WordNetError( "Synset with id '{}' is not in the wordnet".format(synset_id2)) if relations is None: return nx.shortest_path(self._graph, synset_id1, synset_id2) else: if not isinstance(relations, set): raise TypeError( "Argument 'relations' has incorrect type, expected set, got {}" .format(type(relations).__name__)) for relation in relations: if not isinstance(relation, str): raise TypeError( "Argument 'relation - relations' has incorrect type, expected str, got {}" .format(type(relation).__name__)) if relation not in self._relation_types: raise WordNetError( "Relation '{}' is not a correct relation".format( relation)) queue = Queue() queue.put(synset_id1) from_synset_id = {} marked_synset_ids = [synset_id1] found = False while not queue.empty() and not found: cur_synset_id = queue.get() for adj_synset_id, data in self._graph.adj[ cur_synset_id].items(): if data['label'] in relations and adj_synset_id not in marked_synset_ids: from_synset_id[adj_synset_id] = cur_synset_id queue.put(adj_synset_id) marked_synset_ids.append(adj_synset_id) if adj_synset_id == synset_id2: found = True shortest_path_list = [synset_id2] cur_synset_id = synset_id2 while not cur_synset_id == synset_id1: cur_synset_id = from_synset_id[cur_synset_id] shortest_path_list.append(cur_synset_id) shortest_path_list.reverse() return shortest_path_list