[docs]@deprecated(since="0.3.8",removal="1.0",alternative_import="langchain_neo4j.chains.graph_qa.cypher_utils.CypherQueryCorrector",)classCypherQueryCorrector:""" Used to correct relationship direction in generated Cypher statements. This code is copied from the winner's submission to the Cypher competition: https://github.com/sakusaku-rich/cypher-direction-competition """property_pattern=re.compile(r"\{.+?\}")node_pattern=re.compile(r"\(.+?\)")path_pattern=re.compile(r"(\([^\,\(\)]*?(\{.+\})?[^\,\(\)]*?\))(<?-)(\[.*?\])?(->?)(\([^\,\(\)]*?(\{.+\})?[^\,\(\)]*?\))")node_relation_node_pattern=re.compile(r"(\()+(?P<left_node>[^()]*?)\)(?P<relation>.*?)\((?P<right_node>[^()]*?)(\))+")relation_type_pattern=re.compile(r":(?P<relation_type>.+?)?(\{.+\})?]")
[docs]def__init__(self,schemas:List[Schema]):""" Args: schemas: list of schemas """self.schemas=schemas
[docs]defclean_node(self,node:str)->str:""" Args: node: node in string format """node=re.sub(self.property_pattern,"",node)node=node.replace("(","")node=node.replace(")","")node=node.strip()returnnode
[docs]defjudge_direction(self,relation:str)->str:""" Args: relation: relation in string format """direction="BIDIRECTIONAL"ifrelation[0]=="<":direction="INCOMING"ifrelation[-1]==">":direction="OUTGOING"returndirection
[docs]defextract_node_variable(self,part:str)->Optional[str]:""" Args: part: node in string format """part=part.lstrip("(").rstrip(")")idx=part.find(":")ifidx!=-1:part=part[:idx]returnNoneifpart==""elsepart
[docs]defdetect_labels(self,str_node:str,node_variable_dict:Dict[str,Any])->List[str]:""" Args: str_node: node in string format node_variable_dict: dictionary of node variables """splitted_node=str_node.split(":")variable=splitted_node[0]labels=[]ifvariableinnode_variable_dict:labels=node_variable_dict[variable]elifvariable==""andlen(splitted_node)>1:labels=splitted_node[1:]returnlabels
[docs]defverify_schema(self,from_node_labels:List[str],relation_types:List[str],to_node_labels:List[str],)->bool:""" Args: from_node_labels: labels of the from node relation_type: type of the relation to_node_labels: labels of the to node """valid_schemas=self.schemasiffrom_node_labels!=[]:from_node_labels=[label.strip("`")forlabelinfrom_node_labels]valid_schemas=[schemaforschemainvalid_schemasifschema[0]infrom_node_labels]ifto_node_labels!=[]:to_node_labels=[label.strip("`")forlabelinto_node_labels]valid_schemas=[schemaforschemainvalid_schemasifschema[2]into_node_labels]ifrelation_types!=[]:relation_types=[type.strip("`")fortypeinrelation_types]valid_schemas=[schemaforschemainvalid_schemasifschema[1]inrelation_types]returnvalid_schemas!=[]
[docs]defdetect_relation_types(self,str_relation:str)->Tuple[str,List[str]]:""" Args: str_relation: relation in string format """relation_direction=self.judge_direction(str_relation)relation_type=self.relation_type_pattern.search(str_relation)ifrelation_typeisNoneorrelation_type.group("relation_type")isNone:returnrelation_direction,[]relation_types=[t.strip().strip("!")fortinrelation_type.group("relation_type").split("|")]returnrelation_direction,relation_types