def createRelationshipWithProperties():
    print("Start - Creating Relationships")
    # Authenticate the user using py2neo.authentication
    # Ensure that you change the password 'sumit' as per your database configuration.
    py2neo.authenticate("localhost:7474", "neo4j", "sumit")
    # Connect to Graph and get the instance of Graph
    graph = Graph("http://localhost:7474/db/data/")
    # Create Node with Properties
    amy = Node("FEMALE", name="Amy")
    # Create one more Node with Properties
    kristine = Node("FEMALE",name="Kristine")
    # Create one more Node with Properties
    sheryl = Node("FEMALE",name="Sheryl")
    
    #Define an Object of relationship which depicts the relationship between Amy and Kristine
    #We have also defined the properties of the relationship - "since=2005" 
    #By Default the direction of relationships is left to right, i.e. the -->
    kristine_amy = Relationship(kristine,"FRIEND",amy,since=2005)
    
    #This relationship is exactly same as the earlier one but here we are using "Rev"
    #"py2neo.Rev = It is used to define the reverse relationship (<--) between given nodes
    amy_sheryl=Relationship(amy,Rev("FRIEND"),sheryl,since=2001)
    
    #Finally use graph Object and Create Nodes and Relationship
    #When we create Relationship between, then Nodes are also created. 
    resultNodes = graph.create(kristine_amy,amy_sheryl)
    #Print the results (relationships)
    print("Relationship Created - ",resultNodes)
class Neo4jConnector:
    _graph_connection = None

    def __init__(self, connectionString=None):
        # set up authentication parameters
        authenticate("localhost:7474", "neo4j", "123")

        self.connectionString = "http://localhost:7474/db/data/"
        self._graph_connection = Graph(connectionString)
        self.error_file = open("Dump/log/dberror.txt", "a")
        return


    def createNodes(self, wikiPage):
        print ("creating node %s" %wikiPage.getTitle())
        try:
            if self._graph_connection is not None:
                alice = Node("article2",name = wikiPage.title,content= wikiPage.getContent())
                self._graph_connection.create(alice)
            else:
                self.error_file.write("create node failed: connection not avaialable".encode('utf-8'))
                print 'create node failed: connection not avaialable'
            return
        except Exception, e:
                self.error_file.write('Search failed: {%s} { %s } \n' % (wikiPage.getTitle(), e.message))
                print 'create node failed: %s %s' % (e.message,e.args)
                pass
Пример #3
0
def computeShortestPathCoherence(node1, node2, w):
	"""Connects to graph database, then creates and sends query to graph 
	database. Returns the shortest path between two nodes.
	Format: (67149)-[:'LINKS_TO']->(421)"""

	if node1.strip()==node2.strip():
		return w

	fromCache=rds.get("%s:%s" % (node1, node2))
	if fromCache:
		return float(fromCache)*w
	else:
		g = Graph()
		q="MATCH path=shortestPath((m:Page {name:\"%s\"})-[LINKS_TO*1..10]-(n:Page {name:\"%s\"})) RETURN LENGTH(path) AS length, path, m, n" % (node1, node2)

		cursor=g.run(q)
		path=None
		for c in cursor:
			path=c

	#
		if path:
			rds.set("%s:%s" % (node1, node2), 1/path["length"])
			rds.set("%s:%s" % (node2, node1), 1/path["length"])
			return w/path["length"]
		else:
			rds.set("%s:%s" % (node1, node2), 0.0)
			rds.set("%s:%s" % (node2, node1), 0.0)
			return 0.0
    def make_sequence(self):
        authenticate(settings.NeoHost, settings.NeoLog, settings.NeoPass)
        graph = Graph("{0}/db/data/".format(settings.NeoHost))
        query = """MATCH (start:Video)-[:Jaccard*5..10]->(sequence:Video) 
        WHERE start<>sequence MATCH p=shortestPath((start:Video)-[:Jaccard*]->(sequence:Video)) 
        WHERE NONE (n IN nodes(p) WHERE size(filter(x IN nodes(p) WHERE n = x))> 1)  
        RETURN EXTRACT(n IN NODES(p)|[n.id, n.rating]) LIMIT 100000"""
   
        r1 = graph.run(query).data()
        k = 0
        for i in r1:
            #print(i.values)
            for video in i['EXTRACT(n IN NODES(p)|[n.id, n.rating])']:
                 #print(video)
                 self.seq_ids.append(k)
                 self.video_ids.append(video[0])
                 self.ratings.append(video[1])
            k+=1
        data = {'sequence': self.seq_ids, 'video': self.video_ids, 'rating': self.ratings}
        df = pd.DataFrame(data)
        df = df[pd.notnull(df['video'])]
        print(df)
        dz = df.groupby('sequence')['rating'].std()
        print(dz)

        path = '{0}/{1}/'.format(settings.VideosDirPath, self.game)
        if not os.path.exists(path):
            os.makedirs(path)
        file_name = '{0}/sequences.csv'.format(path)
        df.to_csv(file_name, encoding='utf-8')
        summary_data = '{0}/summary.csv'.format(path)
        dz.to_csv(summary_data, encoding='utf-8')
        return
Пример #5
0
class Graph(object):

    def __init__(self, neo4j_uri):
        self.graph = NeoGraph(neo4j_uri)

    def find_node(self, label, node_id):
        args = dict(property_key="node_id", property_value=node_id)
        return self.graph.find_one(label, **args)

    def create_user(self, args):
        node = self.find_node("User", args["username"])
        if not node:
            properties = dict(
                node_id=args["username"],
                name=args["name"],
                city=args["city"]
            )
            node = Node("User", **properties)
            self.graph.create(node)
            return node, True
        return node, False

    def delete_user(self, user):
        node = self.find_node("User", user)
        if node:
            self.graph.delete(node)    
            return True
        return False
Пример #6
0
class PersonLoader(object):

    def __init__(self):
        authenticate("localhost:7474", "neo4j", "1111")
        self.graph_db = Graph(graph_DB_url)
        self.api_url = "http://127.0.0.1:8000/api/v1/person/?format=json"
        self.statement = "MERGE (n:Person {Name:{N}, Identification:{I}, id: {ID}, NonResidentForeigner:{NR}," \
                         "MoreInformation:{MI}}) RETURN n"

    def whipUp(self):
        objects = json.load(urlopen(self.api_url))["objects"]

        for object in objects:
            args = {}
            args["ID"]=object["id"]

            args["I"]=object["Identification"]
            args["NR"]=object["NonResidentForeigner"]
            args["N"]=object["Name"]
            args["MI"]=object["MoreInformation"]
            #args["AD"]=object["Address"]["id"]

            perCh = getPerson(Name=args["N"])
            if perCh!=None:
                continue

            db = self.graph_db.cypher.begin()
            db.append(self.statement, args)
            db.commit()
            address = getAddress(id=int(object["Address"]["id"]))
            person = getPerson(id=int(args["ID"]))

            self.graph_db.create(rel(person.one, "LIVED", address.one))
Пример #7
0
def get_tracks():
    track_metadata = {}
    graph = Graph()

    for track in graph.find("Track"):
        track_metadata[track.properties['id']] = inflate(track.properties)
    return track_metadata
Пример #8
0
class SocialFormationLoader(object):

    def __init__(self):
        authenticate("localhost:7474", "neo4j", "1111")
        self.graph_db = Graph(graph_DB_url)
        self.api_url = "http://127.0.0.1:9000/api/v1/socialformation/?format=json"
        self.statement = "MERGE (n:SocialFormation {id: {ID}, Name:{N}, DateReg:{DR}, RegNumb:{RN}}) RETURN n"

    def whipUp(self):
        objects = json.load(urlopen(self.api_url))["objects"]

        for object in objects:
            args = {}
            args["ID"]=object["id"]

            args["N"]=object["Name"]
            args["DR"]=object["DateReg"]
            args["RN"]=object["RegNumb"]

            #print args
            db = self.graph_db.cypher.begin()
            db.append(self.statement, args)
            db.commit()

            sf = getSocialFormation(args["ID"])
            adr = getAddress(id = int(object["Address"]["id"]), Street = object["Address"]["Street"])
            #per = getPerson(id = int(object["Person"]["id"]), Name = (object["Person"]["Surname"] + " " + object["Person"]["Name"]))
            per = getPerson(Name = (object["Person"]["Surname"] + " " + object["Person"]["Name"]))

            if adr != None:
                self.graph_db.create(rel(sf.one, "HAVE_ADDRESS", adr.one))
            if per != None:
                self.graph_db.create(rel(sf.one, "SF_HAVE_PERSON", per.one))
class Neo4j():
	graph = None
	def __init__(self):
		print("create neo4j class ...")
		
	def connectDB(self):
		self.graph = Graph("http://localhost:7474", username="******", password="******")
		print('connect successed')
		
	def matchItembyTitle(self,value):
		answer = self.graph.find_one(label="Item",property_key="title",property_value=value)
		return answer

	# 根据title值返回互动百科item
	def matchHudongItembyTitle(self,value):
		answer = self.graph.find_one(label="HudongItem",property_key="title",property_value=value)
		return answer
			
	# 返回限定个数的互动百科item
	def getAllHudongItem(self, limitnum):
		List = []
		ge = self.graph.find(label="HudongItem", limit=limitnum)
		for g in ge:
			List.append(HudongItem(g))
			
		print('load AllHudongItem over ...')
		return List
		
		
#test = Neo4j()
#test.connectDB()
#a = test.getLabeledHudongItem('labels.txt')
#print(a[10].openTypeList)
Пример #10
0
def get_track_comments():
    track_comments = {}
    graph = Graph()

    for comment in graph.find("Comment"):
        track_comments[comment.properties['id']] = inflate(comment.properties)
    return track_comments
Пример #11
0
def createRelationships():
    global relationships
    graph = Graph('http://localhost:7474/db/data')
    for r in relationships:
        NodeA = graph.find_one(r["start"]["collection"],property_key = "_id", property_value = str(r["start"]["_id"]))
        NodeB = graph.find_one(r["end"]["collection"],property_key = "_id", property_value = str(r["end"]["_id"]))
        graph.create(rel(NodeA,r["name"],NodeB))
def sync_meetup_data(group):
    graph = Graph(host=config['neo4j']['host'], user=config['neo4j']['user'],
                  password=config['neo4j']['password'])

    location = get_group_location(group)

    tx = graph.begin()
    location_node = Node('Location', city=location['city'], state=location['state'], country=location['country'])
    tx.create(location_node)
    tx.commit()

    meetup_groups = get_groups_in_location(location, category=34)

    logger.info('Finding upcoming meetup events at {} meetup groups'.format(len(meetup_groups)))

    for group in meetup_groups:
        time.sleep(2)
        group, events = get_group_events(group)
        tx = graph.begin()
        group_node = Node("Group", name=group)
        tx.create(group_node)
        location_relation = Relationship(location_node, 'HAS MEETUP', group_node)
        tx.create(location_relation)
        for event in events:
            event_node = Node('Event', name=event['name'], time=event['time'])
            tx.create(event_node)
            rel = Relationship(group_node, "HAS EVENT", event_node)
            tx.create(rel)
        tx.commit()
        logger.info('Transaction ({}) status: {}'.format(group, str(tx.finished())))
Пример #13
0
class Grapher:
    def __init__(self):
        self.graph = Graph()
    
    """Accepts a dict of scanning results and adds the server, its ports and vulerabilities in Neo4jDB"""
    def plot_scan_results(self, res):
        for host in res.keys():
            hostname = res[host][KEY_HOSTNAME]
            server  = Node(SERVER, id=host, address=host, hostname=hostname)
            for attr in res[host].keys():
                if attr not in top_keys:
                    for portno in res[host][attr]:
                        if res[host][attr][portno].get(KEY_STATE, "closed") == OPEN:
                            product = res[host][attr][portno][KEY_PRODUCT]
                            version = res[host][attr][portno][KEY_VERSION]
                            cpe     = res[host][attr][portno][KEY_CPE]
                            vulnerabilities = res[host][attr][portno][KEY_VULNERABILITIES]
                            port = Node(PORT, id=portno, number=portno, protocol=attr, product=product, version=version, cpe=cpe, state=OPEN)        
                            server_has_port = Relationship(server, HAS, port) 
                            self.graph.create(server_has_port)
                            for vulnerability in vulnerabilities:
                                published   = vulnerability[KEY_PUBLISHED]
                                cve         = vulnerability[KEY_CVE] 
                                summary     = vulnerability[KEY_SUMMARY] 
                                vuln        = Node(VULNERABILITY, id=cve, cve=cve, summary=summary, published=published)
                                port_has_vuln = Relationship(port, HAS, vuln)
                                self.graph.create(port_has_vuln)
Пример #14
0
    def handle(self, *args, **options):  # pylint: disable=unused-argument
        """
        Iterates through each course, serializes them into graphs, and saves
        those graphs to neo4j.
        """
        # first, make sure that there's a valid neo4j configuration
        if settings.NEO4J_CONFIG is None:
            raise CommandError(
                "No neo4j configuration (NEO4J_CONFIG) defined in lms.auth.json."
            )

        auth_params = ["{host}:{https_port}", "{user}", "{password}"]
        authenticate(*[param.format(**settings.NEO4J_CONFIG) for param in auth_params])

        graph = Graph(**settings.NEO4J_CONFIG)

        mss = ModuleStoreSerializer()

        total_number_of_courses = len(mss.all_courses)

        for index, course in enumerate(mss.all_courses):
            # first, clear the request cache to prevent memory leaks
            RequestCache.clear_request_cache()

            log.info(
                "Now exporting %s to neo4j: course %d of %d total courses",
                course.id,
                index + 1,
                total_number_of_courses
            )
            nodes, relationships = mss.serialize_course(course.id)
            log.info(
                "%d nodes and %d relationships in %s",
                len(nodes),
                len(relationships),
                course.id
            )

            transaction = graph.begin()
            try:
                # first, delete existing course
                transaction.run(
                    "MATCH (n:item) WHERE n.course_key='{}' DETACH DELETE n".format(
                        six.text_type(course.id)
                    )
                )

                # now, re-add it
                self.add_to_transaction(nodes, transaction)
                self.add_to_transaction(relationships, transaction)
                transaction.commit()

            except Exception:  # pylint: disable=broad-except
                log.exception(
                    "Error trying to dump course %s to neo4j, rolling back",
                    six.text_type(course.id)
                )
                transaction.rollback()
Пример #15
0
class GraphImporter(object):
  def __init__(self, graphurl, commitEvery=100):
    self.graph = Graph(graphurl)
    self.commitEvery = commitEvery
    self._act = None
    self._actC = commitEvery

  def delete_all(self):
    self.graph.delete_all()
    self.graph.cypher.run('CREATE INDEX ON :_Network_Node(id)')
    self.graph.cypher.run('CREATE INDEX ON :_Set_Node(id)')

  def _tx(self):
    if self._act is not None:
      return self._act
    self._act = self.graph.cypher.begin()
    self._actC = self.commitEvery
    return self._act

  def _done(self):
    self._actC -= 1
    if self._actC == 0:  # commit
      self._act.process()
      self._act.commit()
      sys.stdout.write('.')
      self._act = None

  def _close(self):
    if self._act is not None:  # commit last tx
      self._actC = 1
      self._done()

  def add_node(self, labels, node_id, properties):
    tx = self._tx()
    add_node(tx, labels, node_id, properties)
    self._done()

  def done_nodes(self):
    self._done()

  def append(self, query):
    tx = self._tx()
    tx.append(query)
    self._done()


  def add_edge(self, label, source_node_id, target_node_id, properties, source_type=u'_Network_Node', update_only=False):
    tx = self._tx()
    add_edge(tx, label, source_node_id, target_node_id, properties, source_type, update_only)
    self._done()

  def __call__(self, query):
    tx = self._tx()
    tx.append(query)
    self._done()

  def finish(self):
    self._close()
Пример #16
0
def get_graph():
    global NEO4J_URL,NEO4J_HOST,NEO4J_PORT,NEO4J_AUTH

    # Connect to graph
    creds = NEO4J_AUTH.split('/')
    graph = Graph(user=creds[0], password=creds[1], host=NEO4J_HOST)

    graph.run('match (t:Tweet) return COUNT(t)')
    return graph
Пример #17
0
class Person(object):

    def __init__(self):
        self.graph = Graph()
        #do nothing

    def createPerson(self, name, fullname, occupation, homepage, wikipedia_link):
        person = Node('Person', name=name, fullname=fullname, occupation=occupation, homepage=homepage, wikipedia_link=wikipedia_link)
        self.graph.create(person)
        return person
Пример #18
0
def add2neo(courses):
	authenticate("localhost:7474", 'neo4j', 'admin')
	graph = Graph()
	for course in courses:
		name = course.prefix+course.number

		for pre in course.prereqs:
			print 'adding rel', name, pre
			try:
				graph.create(Relationship(courseNodes[name]['node'], 'REQUIRES', courseNodes[pre]['node']))
			except:
				print 'could not add', name, pre
def expot_data(cid, data):
    """
    将数据导入到neo4j,给每个导入的实体添加一个标签cid.
    :param cid: 
    :param data: 
    :return: 
    """
    title = data[0]
    host, http_port, bolt_port, user, password = '******', 7474, 7687, 'neo4j', 'gswewf'
    graph = Graph(host=host, http_port=http_port, bolt_port=bolt_port, user=user, password=password)
    # title = ["_id", "_labels", "tagline", "title", "released", "name", "born", "_start", "_end", "_type", "roles"]
    _start_index = title.index('_start')
    node_property = title[2:_start_index]
    relation_property = title[_start_index + 3:]
    nodes = {}
    relationships = []
    tx = graph.begin()
    for line in data[1:]:
        _id, _labels = line[:2]
        node_property_value = line[2:_start_index]
        _start, _end, _type = line[_start_index:_start_index + 3]
        relation_property_value = line[_start_index + 3:]
        _labels = [label for label in _labels.strip().split(':') if label]
        _labels.append(cid.capitalize())
        # print(line)
        # nodes = {"a": Node("person", name="weiyudang", age=13), "b": Node("person", name="wangjiaqi")}
        if _id and not _start and not _end:
            property_dict = {k: v for k, v in zip(node_property, node_property_value) if v}
            _cid = "{}_{}".format(cid.lower(), _id)
            updatetime = int(time.time() * 1000)  # 与 neo4j的timestamp()一致
            node = Node(*_labels, _cid=_cid, updatetime=updatetime, **property_dict)
            # graph.merge(node)
            nodes.setdefault(_cid, node)
            tx.create(node)
        elif not _id and _start and _end:
            property_dict = {k: v for k, v in zip(relation_property, relation_property_value) if v}
            start_cid = "{}_{}".format(cid.lower(), _start)
            end_cid = "{}_{}".format(cid.lower(), _end)
            # a = Node(_cid=start_cid)
            # b = Node(_cid=end_cid)
            a = nodes.get(start_cid)
            b = nodes.get(end_cid)
            a_knows_b = Relationship(a, _type, b, **property_dict)
            # graph.merge(a_knows_b)
            relationships.append(a_knows_b)
            tx.create(a_knows_b)
        else:
            raise ValueError("数据有误: {}".format(line))
    print(len(nodes), len(relationships))
    # sub_graph = Subgraph(nodes=nodes, relationships=relationships)
    # graph.create(sub_graph)
    tx.commit()
Пример #20
0
def import_api_data():
    """
    imports data from my register (method and all adjacent) into graph DB
    """

    graph = Graph()
    # Uncomment on the first run!
    # graph.schema.create_uniqueness_constraint("Method", "id")
    # graph.schema.create_uniqueness_constraint("Author", "id")
    # graph.schema.create_uniqueness_constraint("Category", "id")

    methods = get_objects('method')

    for api_method in methods:

        node_method = graph.merge_one("Method", "id", api_method["id"])
        node_method["name"] = api_method["name"]
        node_method["creation_date"] = api_method["creation_date"]
        node_method["approval_date"] = api_method["approval_date"]

        for api_author in api_method["authors"]:
            node_author = graph.merge_one("Author", "id", api_author["id"])
            node_author["name"] = api_author["name"]
            node_author.push()
            graph.create_unique(Relationship(node_author, "WROTE", node_method))

        api_category = api_method["category"]
        node_category = graph.merge_one("Category", "id", api_category["id"])
        node_category["name"] = api_category["name"]
        node_category.push()
        graph.create_unique(Relationship(node_category, "CONTAINS", node_method))
        node_method.push()
class Neo4j():
	graph = None
	def __init__(self):
		print("create neo4j class ...")
		
	def connectDB(self):
		self.graph = Graph("http://localhost:7474", username="******", password="******")
		
	def matchItembyTitle(self,value):
		answer = self.graph.find_one(label="Item",property_key="title",property_value=value)
		return answer

	# 根据title值返回互动百科item
	def matchHudongItembyTitle(self,value):
		answer = self.graph.find_one(label="HudongItem",property_key="title",property_value=value)
		return answer
		
	# 返回所有已经标注过的互动百科item   filename为labels.txt
	def getLabeledHudongItem(self, filename):
		labels = readCSV2(filename)
		List = []
		i = 0
		for line in labels:
			ctx = self.graph.find_one(label="HudongItem",property_key="title",property_value=line[0])
			if ctx == None:
				continue;
			cur = HudongItem(ctx)
			cur.label = line[1]
			List.append(cur)
		
		print('load LabeledHudongItem over ...')
		return List
	
	# 返回限定个数的互动百科item
	def getAllHudongItem(self, limitnum):
		List = []
		ge = self.graph.find(label="HudongItem", limit=limitnum)
		for g in ge:
			List.append(HudongItem(g))
			
		print('load AllHudongItem over ...')
		return List
		
		
#test = Neo4j()
#test.connectDB()
#answer = test.graph.find_one(label="HudongItem",property_key="title",property_value='火龙果')
#print(answer)
#a = test.getLabeledHudongItem('labels.txt')
#print(a[10].openTypeList)
Пример #22
0
def get_graph():
    graph = Graph()
    nodes = set()
    links = []
    for rel_type in [REL_FOLLOWS, REL_UPLOADED, REL_FAVORITED]:
        for rel in graph.match(rel_type=rel_type):
            start = node_to_id(rel.start_node)
            end = node_to_id(rel.end_node)
            if rel_type == REL_FAVORITED and end not in nodes:
                continue
            nodes.add(start)
            nodes.add(end)
            links.append([start, end])
    return {'nodes': list(nodes), 'links': links}
Пример #23
0
def import_api_data2():
    authenticate("localhost:7474", "neo4j", "1111")
    graph = Graph()
    #graph.delete_all()

    # Uncomment on the first run!
    #graph.schema.create_uniqueness_constraint("Borjnuk", "id")
    #graph.schema.create_uniqueness_constraint("Obtaj", "id")
    #graph.schema.create_uniqueness_constraint("Property", "id")

    obtajenna = get_objects_art('obtaj')

    for api_obtaj in obtajenna:

        node_obtaj= graph.merge_one("Obtaj", "id", api_obtaj["id"])
        node_obtaj["reason_doc"] = api_obtaj["reason_doc"]
        node_obtaj["cost_size"] = api_obtaj["cost_size"]

        for api_author in api_obtaj["borjnuku"]:
            node_borjnuk = graph.merge_one("Borjnuk", "id", api_author["id"])
            node_borjnuk["name"] = api_author["name"]
            node_borjnuk["tel_number"] = api_author["tel_number"]
            node_borjnuk.push()
            graph.create_unique(Relationship(node_borjnuk, "obtajuetsa", node_obtaj))

        for api_property in api_obtaj["properties"]:
            node_property = graph.merge_one("Property", "id", api_property["id"])
            node_property["name"] = api_property["name_property"]
            node_property["ser_number"] = api_property["ser_number"]
            node_property.push()
            graph.create_unique(Relationship(node_property, "zakladena", node_obtaj))
        node_obtaj.push()
class AgoraOrganization(object):
    def __init__(self):
        self.name = None
        self.unique_id = None
        self.mission_statement = None
        self.email = None
        self.is_open = False
        self.is_invite_only = False
        self.website = None
        self.graph_db = Graph()

    @property
    def org_node(self):
        return self.graph_db.find_one(AgoraLabel.ORGANIZATION,
                                      property_key='name',
                                      property_value=self.name)

    @property
    def org_members(self):
        """
        list of the members of the organization
        :return: list of tuple of member name, email
        """
        org_members_nodes = self.graph_db.match(start_node=self.org_node,
                                                rel_type=AgoraRelationship.MEMBER_OF,
                                                end_node=None)
        org_members_list = []
        for item in org_members_nodes:
            org_members_list.append((item.end_node["name"], item.end_node["email"]))
        return org_members_list

    def create_organization(self):
        """
        create a new organization
        :return: py2neo Node
        """
        self.unique_id = str(uuid.uuid4())
        new_org_properties = {
            "name": self.name,
            "mission_statement": self.mission_statement,
            "unique_id": self.unique_id,
            "email": self.email,
            "is_open": self.is_open,
            "is_invite_only": self.is_invite_only,
            "website": self.website}

        new_org_node = Node.cast(AgoraLabel.ORGANIZATION, new_org_properties)
        self.graph_db.create(new_org_node)

        return new_org_node
Пример #25
0
def main():
    if not has_py2neo:
        sys.exit("[!] py2neo must be installed for this script.")

    if not has_evtx:
        sys.exit("[!] python-evtx must be installed for this script.")

    if not has_lxml:
        sys.exit("[!] lxml must be installed for this script.")

    if not has_lxml:
        sys.exit("[!] numpy must be installed for this script.")

    if not has_changefinder:
        sys.exit("[!] changefinder must be installed for this script.")

    try:
        graph_http = "http://" + NEO4J_USER + ":" + NEO4J_PASSWORD +"@" + NEO4J_SERVER + ":" + NEO4J_PORT + "/db/data/"
        GRAPH = Graph(graph_http)
    except:
        sys.exit("[!] Can't connect Neo4j Database.")

    print("[*] Script start. %s" % datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))

    if args.run:
        try:
            app.run(threaded=True, host="0.0.0.0", port=WEB_PORT)
        except:
            sys.exit("[!] Can't runnning web application.")

    # Delete database data
    if args.delete:
        GRAPH.delete_all()
        print("[*] Delete all nodes and relationships from this Neo4j database.")

    if args.evtx:
        for evtx_file in args.evtx:
            if not os.path.isfile(evtx_file):
                sys.exit("[!] Can't open file {0}.".format(evtx_file))
        parse_evtx(args.evtx, GRAPH)

    if args.xmls:
        for xml_file in args.xmls:
            if not os.path.isfile(xml_file):
                sys.exit("[!] Can't open file {0}.".format(xml_file))
        parse_evtx(args.xmls, GRAPH)

    print("[*] Script end. %s" % datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
Пример #26
0
def test_can_cast_3_tuple():
    casted = Graph.cast(("Alice", "KNOWS", "Bob"))
    assert isinstance(casted, Relationship)
    assert not casted.bound
    assert casted.start_node == Node("Alice")
    assert casted.type == "KNOWS"
    assert casted.end_node == Node("Bob")
class Neo4j():
	graph = None
	def __init__(self):
		print("create neo4j class ...")
		
	def connectDB(self):
		self.graph = Graph("http://localhost:7474", username="******", password="******")
		
	def matchItembyTitle(self,value):
		answer = self.graph.find_one(label="Item",property_key="title",property_value=value)
		return answer

	# 根据title值返回互动百科item
	def matchHudongItembyTitle(self,value):
		answer = self.graph.find_one(label="HudongItem",property_key="title",property_value=value)
		return answer
Пример #28
0
 def save_publications_list(publications):
     """
     Method to save all the publications to neo4j database
     """
     
     graph = Graph()
     
     for publication in publications:
         publication_node = Node("Publication")
         
         for key in publication:
             if key not in ['_id', 'authorsSearched', 'identifiers', 'rank', 'work-citation-type']:
                 publication_node.properties[key] = publication[key]
         
         graph.create(publication_node)
         print 'Inserted Publication: ' + publication['doi']
Пример #29
0
class Achievement(object):
    def __init__(self, graph_db):
        self.name = None
        self.id = None
        self.description = None
        self.title = None
        self.is_visible = True
        self.date = None
        self._graph_db = Graph(settings.DATABASE_URL)

    @property
    def achievement_node(self):
        return self._graph_db.find_one(GraphLabel.ACHIEVEMENT,
                                      property_key='id',
                                      property_value=self.id)

    @property
    def achievement_interests(self):
        """
        get list of interests linked to this achievement
        :return:
        """
        # ach_interests = self.graph_db.match(start_node=self.achievement_node,
        #                                     rel_type=Relationship.)
        return None
Пример #30
0
	def connect(self):
		conf = self.get_config()
		authenticate(conf['host'] + ":" + conf['port'],conf['username'],conf["password"])
		try:
			self.neo = Graph("http://" + conf['host'] + ":" + conf["port"] + "/db/data")
		except:
			print "Failed to connect!"
Пример #31
0
 def __init__(self):
     self.g = Graph("http://localhost:7474", username="******", password="******")
     self.num_limit = 20
class MedicalGraph:
    def __init__(self):
        cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])
        self.data_path = os.path.join(cur_dir, 'data/disease.csv')
        self.graph = Graph("http://localhost:7474",
                           username="******",
                           password="******")

    def read_file(self):
        """
        读取文件,获得实体,实体关系
        :return:
        """
        # cols = ["name", "alias", "part", "age", "infection", "insurance", "department", "checklist", "symptom",
        #         "complication", "treatment", "drug", "period", "rate", "money"]
        # 实体
        diseases = []  # 疾病
        aliases = []  # 别名
        symptoms = []  # 症状
        parts = []  # 部位
        departments = []  # 科室
        complications = []  # 并发症
        drugs = []  # 药品

        # 疾病的属性:age, infection, insurance, checklist, treatment, period, rate, money
        diseases_infos = []
        # 关系
        disease_to_symptom = []  # 疾病与症状关系
        disease_to_alias = []  # 疾病与别名关系
        disease_to_part = []  # 疾病与部位关系
        disease_to_department = []  # 疾病与科室关系
        disease_to_complication = []  # 疾病与并发症关系
        disease_to_drug = []  # 疾病与药品关系

        all_data = pd.read_csv(self.data_path,
                               encoding='gb18030').loc[:, :].values
        for data in all_data:
            disease_dict = {}  # 疾病信息
            # 疾病
            disease = str(data[0]).replace("...", " ").strip()
            disease_dict["name"] = disease
            # 别名
            line = re.sub("[,、;,.;]", " ", str(data[1])) if str(
                data[1]) else "未知"
            for alias in line.strip().split():
                aliases.append(alias)
                if [disease, alias] not in disease_to_alias:
                    disease_to_alias.append([disease, alias])
            # 部位
            part_list = str(data[2]).strip().split() if str(data[2]) else "未知"
            for part in part_list:
                parts.append(part)
                if [disease, part] not in disease_to_part:
                    disease_to_part.append([disease, part])
            # 年龄
            age = str(data[3]).strip()
            disease_dict["age"] = age
            # 传染性
            infect = str(data[4]).strip()
            disease_dict["infection"] = infect
            # 医保
            insurance = str(data[5]).strip()
            disease_dict["insurance"] = insurance
            # 科室
            department_list = str(data[6]).strip().split()
            for department in department_list:
                departments.append(department)
                if [disease, department] not in disease_to_department:
                    disease_to_department.append([disease, department])
            # 检查项
            check = str(data[7]).strip()
            disease_dict["checklist"] = check
            # 症状
            symptom_list = str(data[8]).replace("...",
                                                " ").strip().split()[:-1]
            for symptom in symptom_list:
                symptoms.append(symptom)
                if [disease, symptom] not in disease_to_symptom:
                    disease_to_symptom.append([disease, symptom])
            # 并发症
            complication_list = str(data[9]).strip().split()[:-1] if str(
                data[9]) else "未知"
            for complication in complication_list:
                complications.append(complication)
                if [disease, complication] not in disease_to_complication:
                    disease_to_complication.append([disease, complication])
            # 治疗方法
            treat = str(data[10]).strip()[:-4]
            disease_dict["treatment"] = treat
            # 药品
            drug_string = str(data[11]).replace("...", " ").strip()
            for drug in drug_string.split()[:-1]:
                drugs.append(drug)
                if [disease, drug] not in disease_to_drug:
                    disease_to_drug.append([disease, drug])
            # 治愈周期
            period = str(data[12]).strip()
            disease_dict["period"] = period
            # 治愈率
            rate = str(data[13]).strip()
            disease_dict["rate"] = rate
            # 费用
            money = str(data[14]).strip() if str(data[14]) else "未知"
            disease_dict["money"] = money

            diseases_infos.append(disease_dict)

        return set(diseases), set(symptoms), set(aliases), set(parts), set(departments), set(complications), \
               set(drugs), disease_to_alias, disease_to_symptom, disease_to_part, disease_to_department, \
               disease_to_complication, disease_to_drug, diseases_infos

    def create_node(self, label, nodes):
        """
        创建节点
        :param label: 标签
        :param nodes: 节点
        :return:
        """
        count = 0
        for node_name in nodes:
            node = Node(label, name=node_name)
            self.graph.create(node)
            count += 1
            print(count, len(nodes))
        return

    # def create_node(self, label, nodes):
    #     count = 0
    #     for node_name in nodes:
    #         # print(node_name)
    #         node = Node(label, name=node_name)
    #         matcher = NodeMatcher(self.graph)
    #         txt = matcher.match(label, name=node_name).first()
    #         if txt is None:
    #             self.graph.create(node)
    #
    #         count += 1
    #         # print(count, len(nodes))
    #     return

    def create_diseases_nodes(self, disease_info):
        """
        创建疾病节点的属性
        :param disease_info: list(Dict)
        :return:
        """
        count = 0
        for disease_dict in disease_info:
            node = Node("Disease",
                        name=disease_dict['name'],
                        age=disease_dict['age'],
                        infection=disease_dict['infection'],
                        insurance=disease_dict['insurance'],
                        treatment=disease_dict['treatment'],
                        checklist=disease_dict['checklist'],
                        period=disease_dict['period'],
                        rate=disease_dict['rate'],
                        money=disease_dict['money'])
            self.graph.create(node)
            count += 1
            print(count)
        return

    def create_graphNodes(self):
        """
        创建知识图谱实体
        :return:
        """
        disease, symptom, alias, part, department, complication, drug, rel_alias, rel_symptom, rel_part, \
        rel_department, rel_complication, rel_drug, rel_infos = self.read_file()
        self.create_diseases_nodes(rel_infos)
        self.create_node("Symptom", symptom)
        self.create_node("Alias", alias)
        self.create_node("Part", part)
        self.create_node("Department", department)
        self.create_node("Complication", complication)
        self.create_node("Drug", drug)

        return

    def create_graphRels(self):
        disease, symptom, alias, part, department, complication, drug, \
        rel_alias, rel_symptom, rel_part, rel_department, rel_complication, rel_drug, rel_infos = self.read_file()

        self.create_relationship("Disease", "Alias", rel_alias, "ALIAS_IS",
                                 "别名")
        self.create_relationship("Disease", "Symptom", rel_symptom,
                                 "HAS_SYMPTOM", "症状")
        self.create_relationship("Disease", "Part", rel_part, "PART_IS",
                                 "发病部位")
        self.create_relationship("Disease", "Department", rel_department,
                                 "DEPARTMENT_IS", "所属科室")
        self.create_relationship("Disease", "Complication", rel_complication,
                                 "HAS_COMPLICATION", "并发症")
        self.create_relationship("Disease", "Drug", rel_drug, "HAS_DRUG", "药品")

    def create_relationship(self, start_node, end_node, edges, rel_type,
                            rel_name):
        """
        创建实体关系边
        :param start_node:
        :param end_node:
        :param edges:
        :param rel_type:
        :param rel_name:
        :return:
        """
        count = 0
        # 去重处理
        set_edges = []
        for edge in edges:
            set_edges.append('###'.join(edge))
        all = len(set(set_edges))
        for edge in set(set_edges):
            edge = edge.split('###')
            p = edge[0]
            q = edge[1]
            query = "match(p:%s),(q:%s) where p.name='%s'and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)" % (
                start_node, end_node, p, q, rel_type, rel_name)
            try:
                self.graph.run(query)
                count += 1
                print(rel_type, count, all)
            except Exception as e:
                print(e)
        return

    '''导出数据'''

    def export_data(self):
        disease, symptom, alias, part, department, complication, drug, \
        rel_alias, rel_symptom, rel_part, rel_department, rel_complication, rel_drug, rel_infos = self.read_file()
        f_disease = open('data1/disease.txt', 'w+', encoding='utf-8')
        f_alias = open('data1/alias.txt', 'w+', encoding='utf-8')
        f_symptom = open('data1/symptom.txt', 'w+', encoding='utf-8')
        f_part = open('data1/part.txt', 'w+', encoding='utf-8')
        f_department = open('data1/department.txt', 'w+', encoding='utf-8')
        f_complication = open('data1/complication.txt', 'w+', encoding='utf-8')
        f_drug = open('data1/drug.txt', 'w+', encoding='utf-8')

        f_disease.write('\n'.join(list(disease)))
        f_alias.write('\n'.join(list(alias)))
        f_symptom.write('\n'.join(list(symptom)))
        f_part.write('\n'.join(list(part)))
        f_department.write('\n'.join(list(department)))
        f_complication.write('\n'.join(list(complication)))
        f_drug.write('\n'.join(list(drug)))

        f_disease.close()
        f_alias.close()
        f_symptom.close()
        f_part.close()
        f_department.close()
        f_complication.close()
        f_drug.close()
Пример #33
0
# coding:utf-8
from py2neo import Graph, Node, Relationship
import time
import xlrd
import xlwt
from datetime import date, datetime

# 打开图数据库
graph = Graph('http://localhost:7474', name='neo4j', password='******')
# graph.delete_all()

# 打开 Excel:zzjg
file = "zzjg.xls"
wb = xlrd.open_workbook(filename=file)
sheetn = wb.sheet_by_index(0)
print(sheetn.name, sheetn.nrows, sheetn.ncols)
rowsacount = sheetn.nrows

# rows = sheet1.row_values(0) #获取行内容
# print(rows)

# cols = sheet1.col_values(0) #获取列内容
# print(cols)

rootnode = Node('v组织机构', name='中国石化')
graph.create(rootnode)
amax = 1
for i in range(0, rowsacount):
    # print(i)
    rn = sheetn.row_values(i)
    # print(rn[3].split("/")[0])
Пример #34
0
import datetime
import calendar
import time
startTime = time.time()


def getUTCStamp(dt):
    return calendar.timegm(dt.utctimetuple())


def toMin(timeDiff):
    seconds = timeDiff
    return str("%.3f" % seconds)


graph = Graph()

# Create the correct UTC stamps for each month.
months = []
for i in range(1, 13):
    months.append(getUTCStamp(datetime.datetime(2008, i, 1)))
'''
###
### Check Total number of Obama menntions in 2008
###
query = '\
	MATCH (c:Comment) \
	WHERE c.body =~ ".*[Oo]bama.*" \
	RETURN count(c) as obamas\
	'
tick = time.time()
 def __init__(self):
     cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])
     self.data_path = os.path.join(cur_dir, 'data/disease.csv')
     self.graph = Graph("http://localhost:7474",
                        username="******",
                        password="******")
Пример #36
0
#coding: utf-8
from py2neo import Graph, Node, Relationship
from py2neo.matching import NodeMatcher, RelationshipMatcher
from py2neo.ogm import Label
import re
import xlrd
import time
from com.wdk.stock.knowledgegraph.py2neo_advanced_tools import copy_all_incoming_relationship,\
    copy_all_outgoing_relationship

# 连接neo4j数据库
graph = Graph("http://127.0.0.1:7474", username="******", password="******")

file_name_resume = '上市公司高管简历.xlsx'
file_name_manager_profile = '上市公司高管.xls'


# 把管理层的基本信息输入到图数据库中
def add_manager_profile_to_graph(file_name_manager_profile):
    workbook = xlrd.open_workbook(file_name_manager_profile)
    sheet_names = workbook.sheet_names()
    for sheet_name in sheet_names:
        sheet = workbook.sheet_by_name(sheet_name)

    row_count = 0
    current_year = int(time.strftime('%Y', time.localtime()))
    for rowx in range(1, sheet.nrows):
        row_count += 1
        manager_name = sheet.row_values(rowx)[0]
        stock_name = sheet.row_values(rowx)[1]
        manager_sex = sheet.row_values(rowx)[2]
Пример #37
0
def connect_graph():
    graph = Graph("http://localhost:7474/db/data/", user='******', password='******')
    return graph
Пример #38
0
 def __init__(self):
     self.graph = Graph("http://localhost:7474",
                        username="******",
                        password="******")
Пример #39
0
__author__ = 'Andrei'

from py2neo import Graph
from py2neo import Node, Relationship

graph = Graph('http://*****:*****@localhost:7474/db/data')
alice = Node("Person", name="Alice")
bob = Node("Person", name="Bob")
alice_knows_bob = Relationship(alice, "KNOWS", "bob")
graph.create(alice_knows_bob)

alice.properties["age"] = 33
bob.properties["age"] = 44

graph.push(alice, bob)
Пример #40
0
from py2neo.cypher import cypher_escape
import re
# s = os.path.dirname(os.getcwd())
# print(s)
# with open(s+r"\GetData\orginfoFormat.pkl","rb") as fp:
# 	ngoData = pickle.load(fp)

orgType = ["全部","国内机构","境外机构","基金会","企业","政府部门"]
provinceNameMap = {"西藏省":"西藏自治区","内蒙省":"内蒙古自治区","广西省":"广西壮族自治区",
                    "新疆省":"新疆维吾尔自治区","宁夏省":"宁夏回族自治区"}
fp = open("orginfoFinalNew.pkl", "rb")
ngo = pickle.load(fp)
print(len(ngo))

authenticate("localhost:7474", "neo4j", "liyi193328")
graph = Graph()
# graph.delete_all()

def getAllNgoName():
    fp = open("orginfoFinalNew.pkl", "rb")
    ngo = pickle.load(fp)
    names = list()
    for per in ngo:
        names.append(per.name)
    fp = open("AllNgoName.pkl","wb")
    pickle.dump(names,fp)
    fp.close()
    return names

def findNode(pat,names):
    if len(pat) < 2:
Пример #41
0
from py2neo import Graph, Path
graph = Graph()

#a.	Add movie = "John Wick"
#b.	Add directors by Chad Stahelski, David Leitch
#c.	Add actors William Dafoe and Michael Nyquist
#d.	Add DIRECTED_BY and ACTS_IN relationships for all
#   - include role property on ACTS_IN

wick = graph.cypher.execute(
    'CREATE (m:Movie {title: {title}, year: {year}}) RETURN m',
    title='John Wick',
    year='2014-10-24')[0][0]

tx2 = graph.cypher.begin()
for actor_name in ['William Dafoe', 'Michael Nyquist']:
    tx2.append('CREATE (a:Actor {name: {name}}) RETURN a', name=actor_name)
will, mike = [result.one for result in tx2.commit()]

will_path = Path(will, ('ACTS_IN', {'role': 'Marcus'}), wick)
graph.create(will_path)

mike_path = Path(mike, ('ACTS_IN', {'role': 'Viggo Tarasov'}), wick)
graph.create(mike_path)

tx3 = graph.cypher.begin()
for director_name in ['Chad Stahelski', 'David Leitch']:
    tx3.append('CREATE (d:Director {name: {name}}) RETURN d',
               name=director_name)
chad, david = [result.one for result in tx3.commit()]
from py2neo import Graph, Node, Relationship, watch
from py2neo.ogm import GraphObject, Property
from uuid import uuid4
from datetime import datetime
from passlib.hash import bcrypt
from flask_login import UserMixin, current_user

#URL = 'http://hobby-ehpkpbgfojekgbkepcgcceol.dbs.graphenedb.com:24789'
URL = os.environ.get('NEO4J_URL') or 'http://localhost:7474'

username = os.environ.get('NEO4J_USER') or 'neo4j'
password = os.environ.get('NEO4J_PASS') or 'arfat78692'
#username = '******'
#password = '******'

graph = Graph(URL + '/db/data/', username=username, password=password, bolt=False)

# OGM of User Label
class User(GraphObject, UserMixin):
    __primarykey__ = 'email'

    email = Property()
    username = Property()
    password = Property()

    def __init__(self, email=''):
        return User.select(graph, email).first()

    @classmethod
    def get_user(cls, email):
        return cls.select(graph, email).first()
from py2neo import Graph, Database, Node, Relationship
import os
graph=Graph("http://*****:*****@123"))

def scope(data_file):
    import json
    json = json.load(data_file)
    query = """
        WITH {json} AS data
        UNWIND KEYS($json) AS k
        CREATE(label:author{name:$json[k].author,hashtag:$json[k].hashtags,tweetdate:$json[k].date,date_time:$json[k].datetime,tid:$json[k].tid,tweettext:$json[k].tweet_text,authorid:$json[k].author_id,location:$json[k].location,language:$json[k].lang,like_count:$json[k].like_count})
    """
    graph.run(query,json=json)

def problem1():
    for record in graph.run("MATCH (n) WHERE n.name='selfresqingprncess' RETURN n ORDER BY n.date_time DESC"):
        print(record["n"]["tid"])
        print(record["n"]["tweettext"])
        print(record["n"]["authorid"])
        print(record["n"]["location"])
        print(record["n"]["language"])

def problem2():
    for record in graph.run("MATCH (n) WHERE n.tweettext CONTAINS 'Senate' RETURN n ORDER BY n.like_count DESC"):
        print(record["n"]["tweettext"])

def problem3():
    for record in graph.run("MATCH (n) WHERE n.hashtag='pqr' RETURN n ORDER BY n.date_time DESC"):
        print(record["n"]["tweettext"])

def problem4():
Пример #44
0
import re
from datetime import datetime, timedelta

import requests
from py2neo import Graph

import constants
from helpers import post_message

graph = Graph(constants.neo_url)

# pat = re.compile(r'(?i)\b((?:https?://|www\d{0,3}[]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
pat = re.compile(r'(https?.\/\/+)([^ ]+)')
r = re.compile(pat)

def onMessage(message):

	m = r.search(message['text'])
	if m is None:
		return

	url = m.group()
	print(url)

	# check if it exists

	check_statement = """
	MATCH (u:User)--(m:Message)--(l:Link {id: {url}})
	return u.name as name, m.date as date
	"""
Пример #45
0
    good = False
    # This simply checks to see if you have a connection string in your repo.
    # I use `strip` to remove whitespace/newlines.
    for line in gi:
        if line.strip() == "connect_remote.json":
            good = True
            break

if good is False:
    print("The connect_remote.json file is not in your .gitignore file. \
           Please add it!")

with open('../connect_remote.json') as f:
    data = json.load(f)

graph = Graph(**data[1])

tx = graph.begin()

re3api_short = "https://www.re3data.org/api/beta/repository/"
kws = set()

for repo in repositories:
    print("Working on " + str(repo['name']))
    uri = "https://www.re3data.org/api/beta/repository/" + repo['id']
    repodata = parsere3(uri)['r3d:re3data']['r3d:repository']
    node = {'id': repo.get('id'),
            'name': repodata.get('r3d:repositoryName').get('#text'),
            'url': repodata.get('r3d:repositoryURL'),
            'keywords': repodata.get('r3d:keyword'),
            'description': repodata.get('r3d:description').get('#text'),
Пример #46
0
import json
import uuid
import os
import googlemaps


# === DB Connections ===

"""
	For Connecting to NEO4j PROD DB on Heroku
"""
graphenedb_url = os.environ.get("GRAPHENEDB_BOLT_URL", 'blah')
graphenedb_user = os.environ.get("GRAPHENEDB_BOLT_USER", 'blah')
graphenedb_pass = os.environ.get("GRAPHENEDB_BOLT_PASSWORD", 'blah')
authenticate(graphenedb_url.strip("bolt://").replace("24787", "24780"), graphenedb_user, graphenedb_pass)
graph = Graph(graphenedb_url, user=graphenedb_user, password=graphenedb_pass, bolt = True, secure = True, http_port = 24789, https_port = 24780)


"""
	For Connecting to local host instance
"""
# graph = Graph(bolt=True, host=os.environ.get("NEO4J_PYTHON_HOST", 'n/a'), user=os.environ.get("NEO4J_PYTHON_USER", 'n/a'), password=os.environ.get("NEO4J_PYTHON_PASS", 'n/a'))

"""
	Google Maps Object for querying New Restaurants
"""
gmaps = googlemaps.Client(key=os.environ["GOOGLE_MAPS_API_KEY"])

# === User Model for UserService API ===

class User:
Пример #47
0
from py2neo import Graph, Database

db = Database()
graph = Graph(user="******")


def confCommuntity():
    query = """
    MATCH (c:conference)-[r:edition]->(p:paper)<--(a:author)
    WITH c.name as confName, a.authorName as authorName, count(distinct r.year) as nYear
    WHERE nYear>=4
    RETURN confName, collect(authorName) as authorNames
    """
    return graph.run(query).data()


if __name__ == '__main__':
    list_confComm = confCommuntity()
    print(
        "List of conference name and their community authors with papers in 4 different editions."
    )
    for val in list_confComm:
        print(val)
Пример #48
0
from py2neo import Graph, Node, Relationship, authenticate
from passlib.hash import bcrypt
from datetime import datetime
import queryService
import loadOntologyEntities, readOntology
import os
import uuid

#url = os.environ.get('http://*****:*****@emborectest.sb04.stations.graphenedb.com:24789/db/data/")
class User:
    def __init__(self, username):
        self.username = username

    def find(self):
        user = graph.find_one("OnlineMusicAccount", "username", self.username)
        return user

    def register(self, password, group, age):
        if not self.find():
            user = Node("OnlineMusicAccount",
                        username=self.username,
Пример #49
0
    parser.add_option('--user',
                      dest='user',
                      default='neo4j',
                      help='neo4j user',
                      metavar='str')
    parser.add_option('--password',
                      dest='password',
                      default='neo4j',
                      help='neo4j user password',
                      metavar='str')

    (options, args) = parser.parse_args()

    if options.input is None:
        print('input is required')
        parser.print_help()
        exit(1)

    graph = Graph(host=options.host,
                  http_port=options.port,
                  user=options.user,
                  password=options.password)
    delete_all_data(graph)

    prepare_graph(graph)

    with open(options.input) as f:
        dump = json.load(f)

    create_nodes_and_rels(graph, dump)
Пример #50
0
 def __init__(self):
     self.g = Graph('bolt://localhost:7687', username='******', password='******')
Пример #51
0
# -*- coding: utf-8 -*-
'''
将csv文件导入neo4j数据库
import文件夹是neo4j默认的数据导入文件夹
所以首先要将data文件夹下所有csv文件拷贝到neo4j数据库的根目录import文件夹下,没有则先创建import文件夹
然后运行此程序
'''

from py2neo import Graph

graph = Graph("http://localhost:7474", username="******", password="******")
"""
#测试
cql='''
MATCH (p:Person)
where p.name="张柏芝"
return p
'''
#清空数据库
#data = graph.run('MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r')
data = graph.run(cql)
print(list(data)[0]['p']["biography"])
"""

# 导入节点 电影类型  == 注意类型转换
cql = '''
LOAD CSV WITH HEADERS  FROM "file:///genre.csv" AS line
MERGE (p:Genre{gid:toInteger(line.gid),name:line.gname})
'''
result = graph.run(cql)
print(result, "电影类型 存储成功")
Пример #52
0
import time
from py2neo.ogm import *
from py2neo import Node, Relationship, Graph
import os, json

# NODES Author Hashtag tweet
#  RELATIONSHIPS: Reply(user , tweet) Retweet(user , tweet) Post(user, tweet) Mention(tweet,user Hashtag(tweet,hashtag)

graph = ''
try:
    graph = Graph(host="localhost", password="******")
except:
    print("coudn't connect to neo4j")
    exit()

count = 0
count2 = 0
count3 = 0
count4 = 0
flag = 0
a = graph.run("MATCH (n) DETACH DELETE n")
pres = []
newpres = []
file_data = open('ds.json').read()
read_data = json.loads(file_data)
tx = graph.begin(autocommit=False)

for key in read_data.keys():
    read = read_data[key]
    hashtags = read['hashtags']
    mentions = read['mentions']
Пример #53
0
from py2neo import Graph, Schema

graph = Graph(auth=("neo4j", "137137137"))
graph.delete_all()

# Create Indexes for primary keys of entites
tx = graph.begin()
tx.run('''CREATE INDEX ON :Member(ID)''')
tx.run('''CREATE INDEX ON :Organization(ID)''')
tx.run('''CREATE INDEX ON :User(ID)''')
tx.commit()

# Create Entities
tx = graph.begin()
tx.run('''
    LOAD CSV WITH HEADERS FROM "file:///data/organization.csv" AS row
    CREATE (o:Organization)
    SET o = row,
        o.ID = toInteger(row.ID)
    RETURN o
''')
tx.run('''
    LOAD CSV WITH HEADERS FROM "file:///data/user.csv" AS row
    CREATE (u:User)
    SET u = row,
        u.ID = toInteger(row.ID)
    RETURN u
''')
tx.commit()

# Create Relationships
Пример #54
0
def ad(request):
    user_id = None
    if request.user.is_authenticated():
        user_id = request.user.id

    if request.method == 'POST':
        form = AdForm(request.POST)

        if form.is_valid():
            ad = Anuncio.objects.create(
                categoria=form.cleaned_data['categoria'],
                produto=form.cleaned_data['produto'],
                marca=form.cleaned_data['marca'],
                modelo=form.cleaned_data['modelo'],
                descricao=form.cleaned_data['descricao'],
                numserie=form.cleaned_data['numserie'],
                peso=form.cleaned_data['peso'],
                c_prof=form.cleaned_data['c_prof'],
                c_altura=form.cleaned_data['c_altura'],
                c_largura=form.cleaned_data['c_largura'],
                diaria=form.cleaned_data['diaria'],
                usuario_id=user_id)

            categoria = form.cleaned_data['categoria']
            produto = form.cleaned_data['produto']
            categorias = categoria.split('/')

            # Autentica no BD neo4j
            authenticate("localhost:7474", "neo4j", "secret")
            # Recupera o BD para uma variável
            stuffgraph = Graph()
            # Cria um node para o produto
            produto_node = Node("Produto", id=ad.id, nome=produto)
            # Monta a query Cypher para buscar a categoria
            cypher_str = 'MATCH '

            for i in range(len(categorias) - 1):
                cypher_str = cypher_str + '''(%s: Categoria {nome: "%s"})
                                            -[:TEM_SUBCATEGORIA]->''' % (
                    "a" + str(i), categorias[i])
            cypher_str = cypher_str + '''(%s: Categoria {nome: "%s"})
            RETURN %s''' % ("a" + str(len(categorias)), categorias[
                len(categorias) - 1], "a" + str(len(categorias)))
            # Captura em uma variável o node de categoria retornado pela query
            categoria_node = stuffgraph.cypher.execute_one(cypher_str)

            # Cria a relação ESTA_EM entre produto e categoria
            rel_produto_categoria = Relationship(produto_node, "ESTA_EM",
                                                 categoria_node)

            # Persiste no BD neo4j
            stuffgraph.create(rel_produto_categoria)

            return HttpResponseRedirect('/')
    else:
        form = AdForm()
    variables = RequestContext(request, {'form': form})

    return render_to_response(
        'rentstuff/ad.html',
        variables,
    )
Пример #55
0
from py2neo import authenticate, Graph
import pandas as pd
from pandas.util.testing import assert_frame_equal
from final_main_test import *

# IN CASE YOU HAVEN'T USED PYTEST BEFORE
# install pytest - "pip install pytest" should work
# from the command line, navigate to the folder
# run "py.test neo4j_test.py" from command line
# once all 3 tests pass, you're good

authenticate("54.85.112.231:7474", "neo4j", "LEbKqX3q")
graph = Graph("bolt://54.85.112.231/db/data/")


def test_one():
    """Return all of the associations of Ariel Sharon """

    # this is the query that your wrapper should parse and return from
    datalog_query = """
    q(organization) :-
    actor(id, _, pname, _),
    affiliation(id, organization, _, _),
    pname = 'Ariel Sharon'
    """

    # i am using your execute function to run the query above
    datalog_result = final_query(datalog_query)

    # this is what i would expect your parser to ask the database
    neo_query = """
Пример #56
0
class AnswerSearcher:
    def __init__(self):
        self.g = Graph("http://localhost:7474", username="******", password="******")
        self.num_limit = 20

    '''执行cypher查询,并返回相应结果'''
    def search_main(self, sqls):
        final_answers = []
        for sql_ in sqls:
            question_type = sql_['question_type']
            queries = sql_['sql']
            answers = []
            for query in queries:
                ress = self.g.run(query).data()
                answers += ress
            final_answer = self.answer_prettify(question_type, answers)
            if final_answer:
                final_answers.append(final_answer)
        return final_answers

    '''根据对应的qustion_type,调用相应的回复模板'''
    def answer_prettify(self, question_type, answers):
        final_answer = []
        if not answers:
            return ''
        if question_type == 'disease_symptom':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}的症状包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'symptom_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '症状{0}可能染上的疾病有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_cause':
            desc = [i['m.cause'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}可能的成因有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_prevent':
            desc = [i['m.prevent'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}的预防措施包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_lasttime':
            desc = [i['m.cure_lasttime'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}治疗可能持续的周期为:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_cureway':
            desc = [';'.join(i['m.cure_way']) for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}可以尝试如下治疗:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_cureprob':
            desc = [i['m.cured_prob'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}治愈的概率为(仅供参考):{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_easyget':
            desc = [i['m.easy_get'] for i in answers]
            subject = answers[0]['m.name']

            final_answer = '{0}的易感人群包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_desc':
            desc = [i['m.desc'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0},熟悉一下:{1}'.format(subject,  ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_acompany':
            desc1 = [i['n.name'] for i in answers]
            desc2 = [i['m.name'] for i in answers]
            subject = answers[0]['m.name']
            desc = [i for i in desc1 + desc2 if i != subject]
            final_answer = '{0}的症状包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_not_food':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}忌食的食物包括有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_do_food':
            do_desc = [i['n.name'] for i in answers if i['r.name'] == '宜吃']
            recommand_desc = [i['n.name'] for i in answers if i['r.name'] == '推荐食谱']
            subject = answers[0]['m.name']
            final_answer = '{0}宜食的食物包括有:{1}\n推荐食谱包括有:{2}'.format(subject, ';'.join(list(set(do_desc))[:self.num_limit]), ';'.join(list(set(recommand_desc))[:self.num_limit]))

        elif question_type == 'food_not_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '患有{0}的人最好不要吃{1}'.format(';'.join(list(set(desc))[:self.num_limit]), subject)

        elif question_type == 'food_do_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '患有{0}的人建议多试试{1}'.format(';'.join(list(set(desc))[:self.num_limit]), subject)

        elif question_type == 'disease_drug':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}通常的使用的药品包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'drug_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '{0}主治的疾病有{1},可以试试'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_check':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}通常可以通过以下方式检查出来:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'check_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '通常可以通过{0}检查出来的疾病有{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        return final_answer
Пример #57
0
def graph(uri):
    return Graph(uri)
Пример #58
0
from py2neo import Graph
graph = Graph("http://localhost:7474/db/data/")
# remove self loop
graph.data("match (n)-[r]->(n) delete r")
# return graph
graph.data("MATCH p = ()-[r:UnderProgram]->(n {name: program_name}) RETURN p")
# match (n)-[r:HasDuplicate]->(n) delete r
# MATCH (n {name: 'Alice'})->(m)
# "Accounting (BS)"
MATCH p = ()-[r: UnderProgram]->(n {name: "Accounting (BS)"}) RETURN p

MATCH (m)-[r:UnderProgram]->(n {name: 'Accounting (BS)'})
where (m)-[r:HasPreparation]->()


graph_3.data("MATCH (m)-[r:UnderProgram]->(n {name: 'Accounting (BS)'})\
where not (m)-[r:HasPreparation]->() and not (m)-[r:HasPrerequisite]->()\
return m")



MATCH (k)-[r*]->(n:ABC)
with k, r, n, count(k)

import pandas as pd
courses = list()
courselist = graph_3.data("MATCH p = (m)-[r:UnderProgram]->(n {name: 'Accounting (BS)'}) return p")
for course in courselist:
    graph_3.data("Match ")
def create_connection_with_neo4j_mysql():
    # create connection with neo4j
    global graph_database
    graph_database = Graph("http://localhost:7474/db/data/",
                           auth=("neo4j", "test"))
Пример #60
0
from pandas import DataFrame
from newBookInfor import *
'''
host:服务器ip地址,默认为'localhost'
http_port:http协议——服务器监听端口,默认为7474
https_port:https协议——服务器监听端口,默认为7473
bolt_port:bolt协议——服务器监听端口,默认为7687
user:登录用户名,默认为'neo4j'
password:登录密码,无默认值,故若数据库其他参数都为默认值,则可直接通过密码登录
'''
result = readBookField().fetchall()
#cow_result = readBookField().description()

#graph1 = Graph(host='localhost', http_port=7978, user='******', password='******')
graph1 = Graph(host='http://112.74.160.185',
               http_port=7474,
               user='******',
               password='******')
graph2 = Graph('http://112.74.160.185:7474/browser/',
               user='******',
               password='******')
graph3 = Graph('https://localhost:7473/browser/',
               user='******',
               password='******')
graph4 = Graph(password='******')

graph = graph2.begin()  # 打开图数据库,未打开时不能进行操作
matcher = NodeMatcher(graph)  #使用NodeMatcher来查找数据


def createNode(label_name):
    node = Node('base_node', name=label_name