def find_substring_edge(self, substring, suffix_tree_id): """Returns an edge that matches the given substring. """ suffix_tree = self.suffix_tree_repo[suffix_tree_id] started = datetime.datetime.now() edge, ln = find_substring_edge(substring=substring, suffix_tree=suffix_tree, edge_repo=self.edge_repo) # if edge is not None: # print("Got edge for substring '{}': {}".format(substring, edge)) # else: # print("No edge for substring '{}'".format(substring)) print(" - searched for edge in {} for substring: '{}'".format(datetime.datetime.now() - started, substring)) return edge, ln
def remove_string(self, string, string_id): # Todo: Maybe this is an application service? assert isinstance(string_id, six.string_types) if self._case_insensitive: string = string.lower() assert STRING_ID_END not in string_id assert STRING_ID_END not in string # string += u"{}{}".format(string_id, STRING_ID_END) string += u"{}".format(STRING_ID_END) # Disassociate the string ID from its leaf nodes. # - but don't discard the node entity, because we might # need to add this suffix back into the tree, # and a hard prune would be relatively complicated. # - although, it might be useful to have something do garbage collection from eventsourcing.contrib.suffixtrees.domain.services.generalizedsuffixtree import find_substring_edge for i in range(len(string)): # Find the suffix in the tree. suffix = string[i:] edge, ln = find_substring_edge(suffix, self, self._edge_repo) if edge is None: continue # Get the leaf node. leaf_node = self.get_node(edge.dest_node_id) assert isinstance(leaf_node, SuffixTreeNode) if leaf_node.string_id == string_id: leaf_node.string_id = None try: stringid_collection = self._stringid_collection_repo[ leaf_node.id] except KeyError: pass else: assert isinstance(stringid_collection, StringidCollection) if string_id in stringid_collection.items: stringid_collection.remove_item(string_id)
def remove_string(self, string, string_id): # Todo: Maybe this is an application service? assert isinstance(string_id, six.string_types) if self._case_insensitive: string = string.lower() assert STRING_ID_END not in string_id assert STRING_ID_END not in string # string += u"{}{}".format(string_id, STRING_ID_END) string += u"{}".format(STRING_ID_END) # Disassociate the string ID from its leaf nodes. # - but don't discard the node entity, because we might # need to add this suffix back into the tree, # and a hard prune would be relatively complicated. # - although, it might be useful to have something do garbage collection from eventsourcing.contrib.suffixtrees.domain.services.generalizedsuffixtree import find_substring_edge for i in range(len(string)): # Find the suffix in the tree. suffix = string[i:] edge, ln = find_substring_edge(suffix, self, self._edge_repo) if edge is None: continue # Get the leaf node. leaf_node = self.get_node(edge.dest_node_id) assert isinstance(leaf_node, SuffixTreeNode) if leaf_node.string_id == string_id: leaf_node.string_id = None try: stringid_collection = self._stringid_collection_repo[leaf_node.id] except KeyError: pass else: assert isinstance(stringid_collection, StringidCollection) if string_id in stringid_collection.items: stringid_collection.remove_item(string_id)