Esempio n. 1
0
#ABM --> CliqueBM
from py2neo import Graph
from igraph import Graph as IGraph
import wikipedia
import re

graph = Graph()

#you can substitue any list of companies here, just make sure the first column is the name of the company
companyListURL = 'https://raw.githubusercontent.com/kvangundy/neo4j-knowledge-graph/master/data/global_2k_list.csv'

#cypher queries 

global2klist = 'LOAD CSV FROM "' + companyListURL + '" AS line WITH trim(line[0]) as line RETURN *'

emptyDB='''
MATCH (n)
DETACH DELETE n;
'''

createCompany = '''
MERGE (c:Company {id:{companyName}})
ON CREATE SET c.context = {wikiResp}, c.bioSplit = FALSE;
'''

parseBio = '''
CALL apoc.periodic.iterate(\'MATCH(p:Company) WHERE p.bioSplit = FALSE RETURN p\', 
	\'WITH p, split(p.context, \" \") as words UNWIND words as word MERGE (n:Tag {tag:word}) MERGE (p)-[r:HAS_TAG]->(n) ON MATCH SET r.count = r.count + 1 REMOVE p.bioSplit\'
	, {batchSize:100, parallel:true})
'''
Esempio n. 2
0
 def __init__(self, url, name, pwd):
     #pymysql.connect('rm-bp171b759ha99x5wfso.mysql.rds.aliyuncs.com',port=3306,user='******',passwd='HPGQEhutFBUCi8ZE8JYgWDwZVhAHXWJx',db='businessdata')
     self.graph = Graph(url, username=name, password=pwd)
     self.start = self.graph.begin()
Esempio n. 3
0
from py2neo import Graph
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="darkgrid")
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

graph = Graph('http://localhost:7474', password='******')

tracts = pd.DataFrame(
    graph.run("""

MATCH (t:Tract) WHERE t.date = 2019 
RETURN t.name, t.total_pop, t.median_age, t.white_perc, t.black_perc, t.asian_perc, 
       t.hdi_index, t.emp_unemployed_perc, t.edu_k_12_enrolled_perc, 
       t.edu_college_undergrad_perc, t.edu_stem_degree_perc, t.edu_no_HS_diploma_households, 
       t.edu_limited_english_households, t.hl_health_insured_perc, t.hl_life_expectancy, 
       t.hs_eviction_rate_perc, t.inc_income_median, t.inc_total_enrolled_SNAP
       
""").data())

cluster_df = tracts[[
    't.total_pop', 't.median_age', 't.emp_unemployed_perc',
    't.edu_k_12_enrolled_perc', 't.edu_college_undergrad_perc',
    't.hl_life_expectancy', 't.inc_income_median', 't.inc_total_enrolled_SNAP'
]]

cluster_df = cluster_df.rename(
 def __init__(self):
     self._graph = Graph(
         f"http://{ApplicationConfig.get_neo4j_ip()}:{ApplicationConfig.get_neo4j_port()}/browser",
         user=ApplicationConfig.get_neo4j_user(),
         password=ApplicationConfig.get_neo4j_password())
def delete_tree():
	graph=Graph("bolt://localhost:7687",auth=("neo4j","1234"))
	graph.delete_all()
	print("Graph deleted successfully")
Esempio n. 6
0
 def __init__(self):
     # 1: username 2: password 3: port
     #                          |1 | 2 |          | 3 |
     self.graph = Graph("http://*****:*****@127.0.0.1:7474/db/data")
Esempio n. 7
0
# coding=utf-8
from py2neo import Graph
from flask import Flask, render_template, request, redirect

app = Flask(__name__)
graph = Graph("http://*****:*****@app.route('/', methods=['GET', 'POST'])
def index():
    return render_template('获取关联内容.html')


@app.route('/show', methods=['POST'])
def do_show():
    keyword = request.values.get("text")
    Get_associated_content(keyword)
Esempio n. 8
0
# -*- coding: utf-8; -*-

from py2neo import Graph

__author__ = '[email protected] (Pedro Puertas)'

graph = Graph('bolt://localhost:7687')

load_data = graph.run("""\

LOAD CSV WITH HEADERS FROM 'file:///dataset_keywords.csv' AS line
WITH line where not line.user_id is null
MERGE (user:User { id: line.user_id})
MERGE (product:Product { id:line.tag_id, product_name:line.product_name, keyword:line.keyword })
MERGE (user)-[:VIEW]->(product)
""")
Esempio n. 9
0
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6swf*p1h--m@k)4t^vrg1heonaca1u9ntw$w8vtdrehs8936#e'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Temporary solution due to integration via ip address instead of domain name and https
SERVER_URL = get_env_value('SERVER_URL')
CORS_ALLOW_ALL_ORIGINS = True

# Neo4J
host = get_env_value('NEO4J_HOST')
# host = "bolt://*****:*****@neo4j:7687"
graph = Graph(host)

# Moodle
# token = 'aaea8dd196a2d639e1b1283a4dcfa2a4'
# moodle_api_url = 'http://localhost:81/webservice/rest/server.php?moodlewsrestformat=json&wstoken={0}&wsfunction={1}'

# Informatics
# token = 'aaea8dd196a2d639e1b1283a4dcfa2a4'
# moodle_api_url = 'http://informatics.computermath.ru/webservice/rest/server.php?moodlewsrestformat=json&wstoken={0}&wsfunction={1}'

# X_FRAME_OPTIONS = "ALLOW-FROM localhost"

# SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

ALLOWED_HOSTS = ['*']
                    sum += alpha * (1 - CarsMutual[x][car])
                    CarsMutual[x][car] += alpha * (1 - CarsMutual[x][car])
                    updated.append((x, car, CarsMutual[x][car], '↑'))
                elif TimesCarsCameras[camera, x] < time - 2 * deltaT:
                    if car in CarsMutual and x in CarsMutual[car]:
                        sum -= alpha * (CarsMutual[car][x])
                        CarsMutual[car][x] -= alpha * (CarsMutual[car][x])
                        updated.append((x, car, CarsMutual[car][x], '↓'))
                    del TimesCarsCameras[camera, x]
                if car in CarsMutual and x in CarsMutual[
                        car] and CarsMutual[car][x] < eps:
                    sum -= CarsMutual[car][x]
                    n -= 1
                    del CarsMutual[car][x]

        if len(updated) > 0 and n > 0:
            AVG = sum / n
            for x, car, value, increase in updated:
                if value - AVG >= suspicious_level2:
                    print(x, car, value, increase)
    return flag


host = '192.168.1.111'
password = '******'
gr = Graph(host=host, password=password, bolt=True)
hash = '2786849839b0aa78fcfef8754fdbe877'
time = 1545033444680
deltaT = 120000

print(SimpleAlert(gr, deltaT, 0.9))
Esempio n. 11
0
# curs.execute(query, param)
# # curs.execute("SELECT * FROM enterprise where id = %s", ("00000029-019c-4c91-86dd-3c20c946d09d",))
# # #curs.execute("SELECT * FROM enterprise where id = '00000029-019c-4c91-86dd-3c20c946d09d'")
# rv = curs.fetchall()
# ids = None
# for t in rv:
#     if t:
#         ids = t[1]
#     else:
#         ids = None
# print(ids)
# name = rv[0][0]
# name = [name]
# print(name)
#driver = GraphDatabase.driver("bolt://10.168.7.245:7687", auth=("neo4j", "123456"))
db = Graph("bolt://localhost:7687", password="******")
# def get_db():
#     if not hasattr(g, 'neo4j_db'):
#         g.neo4j_db = driver.session()
#     return g.neo4j_db
#
# @app.teardown_appcontext
# def close_db(error):
#     if hasattr(g, 'neo4j_db'):
#         g.neo4j_db.close()


@app.route("/")
def get_index():
    #return app.send_static_file('index.html')
    return "he"
Esempio n. 12
0
from py2neo import Graph
graph = graph = Graph("http://localhost:7474/", password="******")

def deleteNode(nodeName):
    strCmd = 'match (n:%s) detach delete n' %(nodeName)
    graph.run(strCmd)

deleteNode("Organization")
deleteNode("Organization")
deleteNode("Department")
deleteNode("Supplier")
deleteNode("Customer")
deleteNode("Warehouse")
deleteNode("WarehouseLocation")
deleteNode("Product")
deleteNode("PurchaseOrder")
deleteNode("SalesOrder")
deleteNode("PurchaseOrderDetail")
deleteNode("SalesOrderDetail")
Esempio n. 13
0
#该例程正确,无重复节点
import csv
from py2neo import Graph, Node, Relationship, NodeMatcher
graph = Graph("http://localhost:7474", username="******", password='******')
#csv 读取
csv_file1 = csv.reader(
    open(
        'D:\\常艺茹的文档\\研究生期间的资料\\知识图谱\\Vulnerability-Knowledge-Graph-master\\Vulnerability-Knowledge-Graph-master\\漏洞demo\\作者.csv',
        'r',
        encoding='utf-8'))
print(csv_file1)  #打印出来的csv_file1只是一个对象的模型
csv_file2 = csv.reader(
    open(
        'D:\\常艺茹的文档\\研究生期间的资料\\知识图谱\\Vulnerability-Knowledge-Graph-master\\Vulnerability-Knowledge-Graph-master\\漏洞demo\\关键词.csv',
        'r',
        encoding='utf-8'))
print(csv_file2)  #打印出来的csv_file2只是一个对象的模型
csv_file3 = csv.reader(
    open(
        'D:\\常艺茹的文档\\研究生期间的资料\\知识图谱\\Vulnerability-Knowledge-Graph-master\\Vulnerability-Knowledge-Graph-master\\漏洞demo\\Bug.csv',
        'r',
        encoding='utf-8'))
print(csv_file3)  #打印出来的csv_file3只是一个对象的模型

#读取第一个文件
csv_list1 = list(csv_file1)
for i in range(1, len(csv_list1)):  #len(csv_list)
    if len(csv_list1[i]) != 0:
        print(csv_list1[i])
        # 题目实体节点已经存在,无需创建
        Title = Node('题目', Title_name=csv_list1[i][0])
Esempio n. 14
0
from py2neo import Graph, Node, Relationship
from passlib.hash import bcrypt
import os

graph = Graph('http://localhost:7474/db/data/',
              username="******",
              password="******")


class User:
    def __init__(self, username):
        self.username = username

    def find(self):
        user = graph.find_one('User', 'username', self.username)
        return user

    def register(self, password):
        if not self.find():
            user = Node('User',
                        username=self.username,
                        password=bcrypt.encrypt(password))
            graph.create(user)
            return True
        else:
            return False

    def verify_password(self, password):
        user = self.find()
        if user:
            return bcrypt.verify(password, user['password'])
# encoding=utf8
import csv
from py2neo import Graph,Node,Relationship

#把协议实体写入neo4j

f = csv.reader(open('./file/agreement.csv','r'))
graph = Graph("http://localhost:7474",auth=("neo4j","12345"))
root_node = graph.nodes.match("协议",name="协议").first()
ser_node = graph.nodes.match("服务",name="服务").first()

try:
	for i in f:
		name = i[0]
		detail = i[1]
		node=Node("协议", name=name, detail=detail)
		ab = Relationship(root_node, "包含协议", node)
		graph.create(ab)
		if i[2]:
			# print(i[2])
			service = graph.nodes.match("服务",name=i[2]).first()
			if service:
				ab2 = Relationship(service, "服务使用协议", node)
			else:
				print(i[2])
				ser = Node("服务", name=i[2])
				ab3 = Relationship(ser_node, "服务使用协议", ser)
				graph.create(ab3)
				ab2 = Relationship(ser, "服务使用协议", node)
			graph.create(ab2)
 def __init__(self):
     self.g = Graph()
     self.num_limit = 20
Esempio n. 17
0
 def __init__(self, ip, username, password):
     self.neo_graph = Graph(ip, username=username, password=password)
Esempio n. 18
0
from py2neo import Graph

graph = Graph('http://172.22.67.25:7474',
              username='******',
              password='******')
search_pid = "match (n) where n.PID ='{0}' return n.name".format(
    'f356f990-90d6-11ea-883f-f4d108568a73_f38ac9b6-90d6-11ea-873c-f4d108568a73'
)
# print("search_pid:",search_pid)
# 以上一轮对话意图为PID的意图
pid_res = list(graph.run(search_pid))
pid_res = [res.values()[0] for res in pid_res]
print('以上一轮对话意图为PID的意图:')
print(pid_res)
Esempio n. 19
0
 def __init__(self):
     authenticate("localhost:7474", "neo4j", Conf().getNeo4jPassword())
     self.graph = Graph()
Esempio n. 20
0
 def __init__(self):
     self.g = Graph(host="127.0.0.1",
                    http_port=7474,
                    username="******",
                    password="******")
     self.num_limit = 20
from pyspark.ml import Pipeline
from pyspark.ml.classification import RandomForestClassifier
from pyspark.ml.feature import StringIndexer, VectorAssembler
from pyspark.ml.evaluation import BinaryClassificationEvaluator
from pyspark.sql.types import *
from pyspark.sql import functions as F
from sklearn.metrics import roc_curve, auc
from collections import Counter
from cycler import cycler
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt



graph = Graph("bolt://localhost", auth=("neo4j", "123"))
def down_sample(df):
    copy = df.copy()
    zero = Counter(copy.label.values)[0]
    un = Counter(copy.label.values)[1]
    n = zero - un
    copy = copy.drop(copy[copy.label == 0].sample(n=n, random_state=1).index)
    return copy.sample(frac=1)

# Find positive examples
train_existing_links = graph.run("""
MATCH (a:Character)-[:Interacts_early]->(b:Character)
RETURN id(a) AS node1, id(b) AS node2, 1 AS label
""").to_data_frame()# Find negative examples
train_missing_links = graph.run("""
MATCH (a:Character)
Esempio n. 22
0
    def __init__(self, stopwords_thr=0.8):
        authenticate("localhost:7474", "neo4j", "neo")
        self.graph = Graph("http://localhost:7474/db/data/")

        self.stopwords_docFrequencyThreshold = stopwords_thr
        self.bowSize = 2000

        self.doSentences = True
        self.doDocuments = False
        self.localVocabularies = False

        self.stopwords = []
        self.BOW_global = None

        ### Global BOW: documents
        self.query_BOW_documents = """
            match (:{label})
            with count(*) as nDocs
            match (:{label})-[:HAS_ANNOTATED_TEXT]->(a:AnnotatedText)-[:CONTAINS_SENTENCE]->(:Sentence)-[:HAS_TAG]->(t:Tag)
            with t.id as tagId, count(distinct a) as docCount, 1.0f * count(distinct a) / nDocs as docFreq
            where docFreq < {freqThr}
            with tagId, docCount
            order by docCount desc
            return collect(tagId)[..{dim}] as BOW
        """
        ### Global BOW: sentences
        self.query_BOW_sentences = """
            match (s:Sentence)
            with count(*) as nSent
            match (s:Sentence)-[:HAS_TAG]->(t:Tag)
            with t.id as tagId, count(distinct s) as sentCount, 1.0f * count(distinct s) / nSent as sentFreq
            where sentFreq < {freqThr}
            with tagId, sentCount
            order by sentCount desc
            return collect(tagId)[..{dim}] as BOW
        """

        ### Generic queries (BOW is provided from the outside)
        self.query_document_vectors = """
            match (n:{label})-[:HAS_ANNOTATED_TEXT]->(a:AnnotatedText)-[:CONTAINS_SENTENCE]->(s:Sentence)-[:HAS_TAG]->(t:Tag)
            with a, collect(distinct t.id) as tags
            return id(a) as docId, extract(t in {bow} | (case when t in tags then 1 else 0 end)) as vector
        """
        self.query_single_document_vector = """
            match (n:{label})-[:HAS_ANNOTATED_TEXT]->(a:AnnotatedText)-[:CONTAINS_SENTENCE]->(s:Sentence)-[:HAS_TAG]->(t:Tag)
            where id(a) = {id}
            with a, collect(distinct t.id) as tags
            return id(a) as docId, extract(t in {bow} | (case when t in tags then 1 else 0 end)) as vector
        """
        self.query_sentence_vectors = """
            match (s:Sentence)-[:HAS_TAG]->(t:Tag)
            with s, collect(distinct t.id) as tags
            return id(s) as docId, extract(t in {bow} | (case when t in tags then 1 else 0 end)) as vector
        """

        ### Queries for sentence-wise vectors: _local_ document vocabularies (BOWs)
        self.query_localBOW_and_sentence_vectors = """
            match (a:AnnotatedText)-[:CONTAINS_SENTENCE]->(s:Sentence)-[r:HAS_TAG]->(t:Tag)
            where id(a) = {idSpec} and not (id(t) in {stopwords}) and (ANY(pos IN t.pos WHERE pos IN ["NN", "NNS", "NNP", "NNPS", "JJ", "JJR", "JJS", "VB", "VBD", "VBG", "VBN", "VBP", "VBZ"]) OR size(t.pos) = 0)
            with a, t, count(distinct s) as nSentences, sum(r.tf) as sumTf
            order by id(a), nSentences desc, sumTf desc
            with a, collect(id(t)) as tags
            with a, tags[..{dim}] as BOW
            where size(BOW) = {dim}

            match (a)-[:CONTAINS_SENTENCE]->(s:Sentence)-[r:HAS_TAG]->(t:Tag)
            where not (id(t) in {stopwords}) and (ANY(pos IN t.pos WHERE pos IN ["NN", "NNS", "NNP", "NNPS", "JJ", "JJR", "JJS", "VB", "VBD", "VBG", "VBN", "VBP", "VBZ"]) OR size(t.pos) = 0)
            with BOW, a, s, collect(distinct id(t)) as tags
            return id(s) as docId, extract(t in BOW | (case when t in tags then 1 else 0 end)) as vector
        """
        self.query_sentence_vectors_local = """
            // create batches
            match (n:{label})-[:HAS_ANNOTATED_TEXT]->(a:AnnotatedText)
            //where not n.processed = true
            //with n, a
            //limit 50 // batch size!
            //set n.processed = true
            with n, id(a) as id
            
            // get BOW per document and sentence vectors
            %s
        """ % self.query_localBOW_and_sentence_vectors
def create_tree(_subject,_classes):
	graph=Graph("bolt://localhost:7687",auth=("neo4j","1234"))
	sub=_subject.upper()
	cl=_classes.upper()
	cg.generate_graph(graph,sub.split(","),cl.split(","))
Esempio n. 24
0
 def _connect(self):
     return Graph(self.current_app.config["NEO4J_URI"],
                  username=self.current_app.config["NEO4J_USERNAME"],
                  password=self.current_app.config["NEO4J_PASSWORD"])
Esempio n. 25
0
from py2neo import Node, Relationship, Graph, NodeMatcher
import time

from App.utils.config_helper import get_config_map

config_map = get_config_map()
url = 'http://{}:{}'.format(config_map['neo4j']['host'],
                            config_map['neo4j']['port'])
graph = Graph(url,
              auth=(config_map['neo4j']['username'],
                    config_map['neo4j']['password']))


# 查询neo4j数据库中的所有三元组
# returns:
#     [
#         {
#             'source': '头节点',
#             'target': '尾节点',
#             'rela': '关系类型'
#         }
#     ]
def get_all_triplets():
    cyber = 'MATCH (a)-[b]->(c) RETURN a.name as source, c.name as target, type(b) as rela, ' \
            '"s"+id(a)+1 as source_id, c.code as target_id, id(b)+1 as triplet_id'
    relationships = graph.run(cyber)

    rs = []
    start_time = time.time()
    for relation in relationships:
        # print(type(relation), relation.items(), dict(relation.items()))
Esempio n. 26
0
def get_graph():
    if config.NEO4J_PWD is not None:
        graph = Graph(uri=config.NEO4J_URI, password=config.NEO4J_PWD)
    else:
        graph = Graph(uri=config.NEO4J_URI)
    return graph
Esempio n. 27
0
from py2neo import NodeMatcher, RelationshipMatcher
from django.test import TestCase
from EventExploreServer.utils.entity_list import all_entity_dict
from config import ENTMT_ES_INDEX
from EventExploreServer.component import search_all_do
from EventExploreServer.component import extract_article


host = '10.176.24.53'
port = 7687
user = '******'
password = '******'

debug_logger = logging.getLogger('debug')
info_logger = logging.getLogger('info')
graph = Graph(host=host, port=port, user=user, password=password)
# print(graph.run('match (n) return n'))
# print(graph.name)


def build():
    pass


def exist(person):
    nodes = NodeMatcher(graph)
    res = nodes.match("Person", name=person['name'])
    # print(len(res))
    return True if len(res)>0 else False

def add_persons(persons, bulk_size=1000):
def assert_max_size(old_size, new_size):
    assert new_size > old_size

def is_subset(set_a, set_b):
    assert set_a.issubset(set_b) == True

if __name__ == '__main__':
    start_time = '[START extract empresas]: ' + get_time()

    user = sys.argv[1] if len(sys.argv) > 1 else config(
        'NEO4J_USER', default='neo4j')
    password = sys.argv[2] if len(sys.argv) > 2 else config(
        'NEO4J_PASSWORD', default='password')

    neo4j = Graph("localhost", user=user, password=password)

    query_participantes = "MATCH (p:Participante) WHERE size(p.cpf_cnpj) = 14 RETURN p.cpf_cnpj;"
    query_doadores = "MATCH (d:Doador) WHERE size(d.cpf_cnpj) = 14 RETURN d.cpf_cnpj;"

    participantes = get_set_cpf_cnpj_participantes(neo4j, query_participantes)
    doadores = get_set_cpf_cnpj_doadores(neo4j, query_doadores)

    empresas = participantes
    old_size = len(empresas)

    empresas.update(doadores)
    new_size = len(empresas)

    assert_max_size(old_size, new_size)
    is_subset(doadores, empresas)
Esempio n. 29
0
 def __init__(self):
     self.g = Graph(
         "bolt://localhost:7687",  # neo4j 搭载服务器的ip地址,ifconfig可获取到
         user="******",  # 数据库user name,如果没有更改过,应该是neo4j
         password="******")
     self.num_limit = 100
Esempio n. 30
0
 def __init__(self, ip_addr, username, password):
     self.ip_addr = ip_addr
     self.username = username
     self.password = password
     self.graph = Graph(self.ip_addr, username=self.username, password=self.password)