Last active
February 24, 2020 06:25
-
-
Save philschmid/53ca4cb29b0f9fa5b19bdab979409980 to your computer and use it in GitHub Desktop.
cypher python api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def entity_parser(entity_array): | |
res_ent=[] | |
for ent in inpust_list: | |
if ent['sailence'] > 0 and ent['type] != 'OTHER': | |
res_ent.append({"name":ent['name'], "type":ent['type'].capitalize(), | |
"relation": f"is{ent['type'].capitalize()}", "sailence": ent['sailence']}) | |
return res_ent | |
def create_article_script(input_article=None): | |
statement=[] | |
#parset entities for correct labeling | |
input_article['entities'] = entity_parser(input_article['entities']) | |
#creates initial news-article node | |
main_node, main_label = create_node({"title":input_article['origion]['title]},'News-Article') | |
# adds main_node to cypher sql | |
statement.append(main_node) | |
#iterates through article dictonary | |
for key,value in input_article.items(): | |
# creates node url (spiegel.de) | |
# example for string attribute which is its own entity with all relations | |
if key == 'url': | |
url_node, url_label = create_node({"name":value},'Url') | |
url_rel = create_relation(_from=main_label.lower(),to=url_label.lower(),relation='publishedOn') | |
# adds cypher statements to big statement | |
statement.append(url_node) | |
statement.append(url_rel) | |
# creates nodes for categories | |
# example for list attribute where each one its is own entity with all relations | |
elif key == 'categories': | |
for idx, category in enumerate(value): | |
category_node, category_label = create_node({"name":category['name]},'Category',idx) | |
category_rel = create_relation(_from=main_label.lower(),to=f"{url_label.lower()}-{idx}", | |
relation='hasCategory',rel_property={'score':category['score']},idx) | |
statement.append(category_node) | |
statement.append(category_rel) | |
return " ".join(statement) | |
# This only an Example the script doesn provide the full cypher for the news.json yet. You have to extend the script | |
# for each attribute which should be a node. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .cypher_helper import has_cypher_idx, create_cypher_properties | |
def create_node(input_node='',label='',idx=None): | |
cypher_create= f"""MERGE ({label.lower()}{has_cypher_idx(idx)}:{label} {{ {create_cypher_properties(input_node)} }})""" | |
return cypher_create, label |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .cypher_helper import has_cypher_idx, rel_has_prop | |
def create_relation(_from='',to='',relation='',rel_property=None,idx=None): | |
cypher_create= f"""MERGE ({_from}-[{relation}{has_cypher_idx(idx)} {rel_has_prop(rel_property)}]->({to})""" | |
return cypher_create |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# checks if input is str and extends ' | |
def is_cypher_int(input_value): | |
if isinstance(value,str): | |
return "'" | |
else: | |
return "" | |
# if relation/node has en index return substring for creating nodde | |
def has_cypher_idx(idx): | |
if isintance(idx,int): | |
return f"-{idx}" | |
else: | |
return "" | |
# checks if relation has properties and creates string | |
def rel_has_prop(prop): | |
if isintance(idx,dict): | |
return f"{{ {create_cypher_properties(prop)} }}" | |
else: | |
return "" | |
# parses dictonary to cypher properties -> each node needs atleast 1 property | |
def create_cypher_properties(in_dict): | |
cypher_properties = [] | |
for key,value in in_dict.items(): | |
cypher_properties.append(f"{key}:{is_cypher_int(value)}{value}{is_cypher_int(value)}") | |
return ", ".join(cypher_properties) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment