def create_user_similarity(from_user_id: str, to_user_id: str, tdna_conf: float) -> bool: """ to create a relationship called "USER-SIMILAR" between two User nodes with confidence score as a property for that relationship. Args: from_user_id (string), from-node's user id to_user_id (string), to-node's user id tdna_conf (float), confidence score of similarity between users Returns: boolean, representing whether the relationship got inserted or not. """ matcher = py2neo.NodeMatcher(graph) from_node = matcher.match(USER_NODE_LABEL, user_id=from_user_id).first() to_node = matcher.match(USER_NODE_LABEL, user_id=to_user_id).first() user_similar = py2neo.Relationship(from_node, USER_SIMILAR_RELATIONSHIP, to_node, tdna_conf=tdna_conf) graph.create(user_similar) return graph.exists(user_similar)
def create_tweet_impostor_similarity(impostor_user_id: str, unverified_tweet_id: str, tdna_conf: float) -> bool: """ to create a relationship called "TWEET-SIMILAR" between a fraudulent not-verified Tweet with the impostor User among the network (thus Among Us) & confidence score as a property for that relationship. Args: impostor_user_id (string), impostor user id whose onboard pattern matched the tweet. unverified_tweet_id (string), the unverified tweet, which was posted to dupe or fake. tdna_conf (float), confidence score of similarity between impostor onboard & tweet patterns Returns: boolean, representing whether the relationship got inserted or not. """ matcher = py2neo.NodeMatcher(graph) from_node = matcher.match(USER_NODE_LABEL, user_id=impostor_user_id).first() to_node = matcher.match(TWEET_NODE_LABEL, tweet_id=unverified_tweet_id).first() tweet_similar = py2neo.Relationship(from_node, TWEET_SIMILAR_RELATIONSHIP, to_node, tdna_conf=tdna_conf) graph.create(tweet_similar) return graph.exists(tweet_similar)
def create_authentication_failed(from_user_id: str, unverified_tweet_id: str) -> bool: """ to create a relationship called "UNVERIFIED" between a legit User to an impostor tweet from their account. Args: from_user_id (string), person who tweeted, their user id unverified_tweet_id (string), the unauthenticated tweet id, from that user id Returns: boolean, representing whether the relationship got inserted or not. """ matcher = py2neo.NodeMatcher(graph) from_node = matcher.match(USER_NODE_LABEL, user_id=from_user_id).first() to_node = matcher.match(TWEET_NODE_LABEL, tweet_id=unverified_tweet_id).first() failed_auth = py2neo.Relationship( from_node, UNVERIFIED_RELATIONSHIP, to_node, ) graph.create(failed_auth) return graph.exists(failed_auth)
def create_verified_similarity(from_user_id: str, verified_tweet_id: str, tdna_conf: float) -> bool: """ to create a relationship called "VERIFIED" between a legit User to his typing DNA verified tweet & confidence score as a property for that relationship. Args: from_user_id (string), person who tweeted, their user id verified_tweet_id (string), the authenticated tweet id, from that user id tdna_conf (float), confidence score of similarity between onboard & tweet pattern Returns: boolean, representing whether the relationship got inserted or not. """ matcher = py2neo.NodeMatcher(graph) from_node = matcher.match(USER_NODE_LABEL, user_id=from_user_id).first() to_node = matcher.match(TWEET_NODE_LABEL, tweet_id=verified_tweet_id).first() verified_similar = py2neo.Relationship(from_node, VERIFIED_RELATIONSHIP, to_node, tdna_conf=tdna_conf) graph.create(verified_similar) return graph.exists(verified_similar)
def get_log_nodes(dataloader_name, image): return list( py2neo.NodeMatcher(get_graph()).match( "LoadingLog", dockerhub_image_name=image.tags[0].split(":")[0], dockerhub_image_tag=image.tags[0].split(":")[1], dockerhub_image_hash=image.id, ) )
def update_tweet( tweet_id: str, flagged: bool, ) -> bool: """ takes relevant tweet details and updates it Args: tweet_id (string), tweet's id for reference flagged (boolean), indicating whether the tweet got flagged or not. Returns: boolean, confirming whether Tweet got updated or not. """ matcher = py2neo.NodeMatcher(graph) tweet = matcher.match(TWEET_NODE_LABEL, tweet_id=tweet_id).first() tweet["flagged"] = flagged graph.push(tweet) return graph.exists(tweet)
def __init__(self, graph): self.graph = graph self.rship_matcher = pn.RelationshipMatcher(self.graph) self.node_matcher = pn.NodeMatcher(self.graph)
def NameMain(city,category,day,time,hourstr,minstr): # accessing graph uri = "bolt://*****:*****@localhost:8000" numelements = 0 graph = Graph(uri) matcher = py2neo.NodeMatcher(graph) busindex = 0 # adding concession for half hours if minstr[0] == '3': hourstr = hourstr + '.5' # fetching from graph namecom = "MATCH (n:Business)-[:IN_CATEGORY]->(c:Category) WHERE (c.id=" + category + ") AND (n.state='AZ') AND (n.city=" + city + ") RETURN (n.name) ORDER BY n.stars desc,n.review_count desc" daycom = "MATCH (n:Business)-[:IN_CATEGORY]->(c:Category) WHERE (c.id=" + category + ") AND (n.state='AZ') AND (n.city=" + city + ") RETURN (n." + day + ") ORDER BY n.stars desc,n.review_count desc" addresscom = "MATCH (n:Business)-[:IN_CATEGORY]->(c:Category) WHERE (c.id=" + category + ") AND (n.state='AZ') AND (n.city=" + city + ") RETURN (n.address) ORDER BY n.stars desc,n.review_count desc" idcom = "MATCH (n:Business)-[:IN_CATEGORY]->(c:Category) WHERE (c.id=" + category + ") AND (n.state='AZ') AND (n.city=" + city + ") RETURN (n.id) ORDER BY n.stars desc,n.review_count desc" starcom = "MATCH (n:Business)-[:IN_CATEGORY]->(c:Category) WHERE (c.id=" + category + ") AND (n.state='AZ') AND (n.city=" + city + ") RETURN (n.stars) ORDER BY n.stars desc,n.review_count desc" countcom = "MATCH (n:Business)-[:IN_CATEGORY]->(c:Category) WHERE (c.id=" + category + ") AND (n.state='AZ') AND (n.city=" + city + ") RETURN (n.review_count) ORDER BY n.stars desc,n.review_count desc" # converting from cursor object to a string object namestring = (graph.run(namecom)).data() daystring = (graph.run(daycom)).data() addressstring = (graph.run(addresscom)).data() idstring = (graph.run(idcom)).data() starstring = (graph.run(starcom)).data() countstring = (graph.run(countcom)).data() # to be filled with final sorted, relevant properties (final list) listname = {} listaddress = {} listday = {} listid = {} liststars = {} listcount = {} # temporary list used to update final lists tempname = {} tempid = {} tempday = {} tempaddress = {} tempstars = {} tempcount = {} # getting total number of elements for t in namestring: numelements += 1 if (numelements < 1): return ("No restaurants found that match your criteria", "", "", "", "") # filling list with necessary string elements for x in range(numelements): tempname[x] = str(namestring[x]) tempaddress[x] = str(addressstring[x]) tempday[x] = str(daystring[x]) tempid[x] = str(idstring[x]) tempstars[x] = str(starstring[x]) tempcount[x] = str(countstring[x]) for x in range(numelements): lengthname = len(tempname[x]) lengthaddress = len(tempaddress[x]) lengthopen = len(tempday[x]) lengthid = len(tempid[x]) lengthstars = len(tempstars[x]) lengthcount = len(tempcount[x]) wordname = '' wordaddress = '' wordday = '' wordid = '' wordstars = '' wordcount = '' # Handling inconsistincies in dataset for days if (day == "Monday") or (day == "Sunday") or (day == "Friday"): for k in range(16, lengthopen - 2): wordday = wordday + str(tempday[x][k]) listday[x] = wordday if (day == "Saturday") or (day == "Thursday"): for k in range(18, lengthopen - 2): wordday = wordday + str(tempday[x][k]) listday[x] = wordday if (day == "Tuesday"): for k in range(18, lengthopen - 2): wordday = wordday + str(tempday[x][k]) listday[x] = wordday if (day == "Wednesday"): for k in range(19, lengthopen - 2): wordday = wordday + str(tempday[x][k]) listday[x] = wordday # filling lists for k in range(14, lengthname - 2): wordname = wordname + str(tempname[x][k]) listname[x] = wordname for k in range(18, lengthaddress - 2): wordaddress = wordaddress + str(tempaddress[x][k]) listaddress[x] = wordaddress for k in range(12, lengthid - 2): wordid = wordid + str(tempid[x][k]) listid[x] = wordid for k in range(14, lengthstars - 3): wordstars = wordstars + str(tempstars[x][k]) liststars[x] = wordstars for k in range(21, lengthcount - 1): wordcount = wordcount + str(tempcount[x][k]) listcount[x] = wordcount busindex = getIndex(numelements, listday, hourstr) if (busindex == -10): return ("No restaurants found that match your criteria", "", "", "", "") # display of final relevant properties else: return (listname[busindex], listaddress[busindex], liststars[busindex], listid[busindex], listcount[busindex])
def find_node(driver, node_type, **kwargs): matcher = py2neo.NodeMatcher(driver) found = matcher.match(node_type, **kwargs).first() return found
import json import py2neo from py2neo import Graph import config as conf import csv import shutil, os from pathlib import Path uri = "{}://{}:{}@{}:{}".format(conf.PROTO, conf.USER, conf.PASSWORD, conf.HOSTNAME, conf.PORT) graph = Graph(uri) matcher = py2neo.NodeMatcher(graph) def select_business(state="AZ",cat='Restaurants'): bus_dict = graph.run("MATCH (business:Business)-[:IN_CATEGORY]->(c:Category) WHERE c.id in $categories RETURN business",parameters={'categories':cat}).data() #bu = result[0]['business']['name'] #Get a list of all bussiness using 'where' statement with the regex .* #bus_list= list(matcher.match("Business").where("_.name =~ '.*'")) bus_selected = [dict() for x in range(1)] for bus in bus_dict: if(bus['business']['state']==state): bus_selected.append(bus['business']) bus_selected.pop(0) return bus_selected def clean_data(): folder = Path("./") file = folder / "photo.json" with open(file) as f: # Import json data into an array of json objects data = [json.loads(line) for line in f] dictlist = [dict() for x in range(1)]
def write(self, filename="model.xml"): """Gets all nodes and relationships with p > 0.5 and writes them to an XML file which can be used to generate the final model in ArchiMate""" # Create XML file skeleton network = ET.Element("network") properties = ET.Element("properties") # Add a list of host properties that map force is going to use property_addr = ET.Element("property") property_addr_name = ET.SubElement(property_addr, "name") property_addr_type = ET.SubElement(property_addr, "type") property_addr_name.text = "address" property_addr_type.text = "string" property_ip = ET.Element("property") property_ip_name = ET.SubElement(property_ip, "name") property_ip_type = ET.SubElement(property_ip, "type") property_ip_name.text = "ip" property_ip_type.text = "string" property_port = ET.Element("property") property_port_name = ET.SubElement(property_port, "name") property_port_type = ET.SubElement(property_port, "type") property_port_name.text = "port" property_port_type.text = "number" property_likelihood = ET.Element("property") property_likelihood_name = ET.SubElement(property_likelihood, "name") property_likelihood_type = ET.SubElement(property_likelihood, "type") property_likelihood_name.text = "likelihood" property_likelihood_type.text = "number" # Append all properties to property node properties.append(property_addr) properties.append(property_ip) properties.append(property_port) properties.append(property_likelihood) # Append property node to network network.append(properties) # Get all hosts with p > 0.5 using cypher query, hosts is a list of dicts nodematcher = neo.NodeMatcher(self.graph) hosts = nodematcher.match("Host", p__gt=0.5) # Get all connections with p > 0.5 and both start_node.p > 0.5 and end_node.p > 0.5 relmatcher = neo.RelationshipMatcher(self.graph) rels = relmatcher.match(r_type="CONNECTED").where( "_.p > 0.5", "a.p > 0.5", "b.p > 0.5") # Iterate over all hosts and append XML nodes for each of them for h in hosts: host = ET.Element("host") name = ET.SubElement(host, "name") name.text = str(h["ip"]) address = ET.SubElement(host, "address") # For the ArchiMate model does not allow colons in properties, therefore use underscores instead address.text = h["addr"].replace(":", "_") property_ip = ET.SubElement(host, "property") property_ip_name = ET.SubElement(property_ip, "name") property_ip_value = ET.SubElement(property_ip, "value") property_ip_name.text = "ip" property_ip_value.text = str(h["ip"]) property_port = ET.SubElement(host, "property") property_port_name = ET.SubElement(property_port, "name") property_port_value = ET.SubElement(property_port, "value") property_port_name.text = "port" property_port_value.text = str(h["port"]) property_likelihood = ET.SubElement(host, "property") property_likelihood_name = ET.SubElement(property_likelihood, "name") property_likelihood_value = ET.SubElement(property_likelihood, "value") property_likelihood_name.text = "likelihood" property_likelihood_value.text = str(round(h["p"], 2)) # Append host node to network node network.append(host) # Now iterate over all "CONNECTED" relationships and add XML nodes for r in rels: rel = ET.Element("relationship") source = ET.SubElement(rel, "source") destination = ET.SubElement(rel, "destination") source.text = r.start_node["addr"].replace(":", "_") destination.text = r.end_node["addr"].replace(":", "_") property_likelihood = ET.SubElement(rel, "property") property_likelihood_name = ET.SubElement(property_likelihood, "name") property_likelihood_value = ET.SubElement(property_likelihood, "value") property_likelihood_name.text = "likelihood" property_likelihood_value.text = str(round(r["p"], 2)) network.append(rel) tree = ET.ElementTree(network) tree.write(filename)