python operation neo4j creating knowledge map template

Data:

Name of purchaser

Name of seller

amount of money

Electronic toll center of Shandong Expressway Group Co., Ltd

Harbin told the company

2000W level transaction

Hunan daoyue expressway industry Co., Ltd

Hubei a Shen Nan Expressway Development Co., Ltd

2000W level transaction

Hunan daoyue expressway industry Co., Ltd

Hubei a Shen Nan Expressway Development Co., Ltd

100W level transaction

Hunan daoyue expressway industry Co., Ltd

Hubei a Shen Nan Expressway Development Co., Ltd

2000W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hubei a Shen Nan Expressway Development Co., Ltd

2000W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hubei a Shen Nan Expressway Development Co., Ltd

2000W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hubei a Shen Nan Expressway Development Co., Ltd

500W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hubei a Shen Nan Expressway Development Co., Ltd

500W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hubei a Shen Nan Expressway Development Co., Ltd

500W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hubei CCCC Jiatong Expressway Development Co., Ltd

2000W level transaction

Hubei a Shen Nan Expressway Development Co., Ltd

Hunan daoyue expressway industry Co., Ltd

100W level transaction

Hubei a Shen Nan Expressway Development Co., Ltd

Hunan daoyue expressway industry Co., Ltd

500W level transaction

Hubei a Shen Nan Expressway Development Co., Ltd

Hunan daoyue expressway industry Co., Ltd

100W level transaction

Hubei a Shen Nan Expressway Development Co., Ltd

Hunan daoyue expressway industry Co., Ltd

500W level transaction

Hubei CCCC Jiatong Expressway Development Co., Ltd

Hunan daoyue expressway industry Co., Ltd

2000W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hunan daoyue expressway industry Co., Ltd

500W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hunan daoyue expressway industry Co., Ltd

500W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hunan daoyue expressway industry Co., Ltd

500W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Hunan daoyue expressway industry Co., Ltd

2000W level transaction

Hunan daoyue expressway industry Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

8000W level transaction

Hunan daoyue expressway industry Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

8000W level transaction

Hunan daoyue expressway industry Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

2000W level transaction

Hunan daoyue expressway industry Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

2000W level transaction

Modern Investment Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

8000W level transaction

Modern Investment Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

8000W level transaction

Modern Investment Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

2000W level transaction

Modern Investment Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

2000W level transaction

Modern Investment Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

2000W level transaction

Modern Investment Co., Ltd

Electronic toll center of Shandong Expressway Group Co., Ltd

2000W level transaction

Hunan daoyue expressway industry Co., Ltd

Modern Investment Co., Ltd

8000W level transaction

Hunan daoyue expressway industry Co., Ltd

Modern Investment Co., Ltd

8000W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Modern Investment Co., Ltd

500W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Modern Investment Co., Ltd

500W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Modern Investment Co., Ltd

2000W level transaction

Electronic toll center of Shandong Expressway Group Co., Ltd

Modern Investment Co., Ltd

100W level transaction

from py2neo import Node, Graph, Relationship, NodeMatcher
import pandas as pd


class DataToNeo4j:
    def __init__(self):
        # Connect to database
        link = Graph("http:localhost:7474", username="neo4j", password="rhino1qaz@wsx")
        self.graph = link
        # Establish node
        self.buy = "buy"
        self.sell = "sell"
        self.graph.delete_all()  # Empty the database first
        self.matcher = NodeMatcher(link)  # Then define a matcher

    def create_node(self, node_buy_key, node_sell_key):
        """Establish node"""
        for name in node_buy_key:
            buy_node = Node(self.buy, name=name)
            self.graph.create(buy_node)
        for name in node_sell_key:
            sell_node = Node(self.sell, name=name)
            self.graph.create(sell_node)

    def create_relation(self, df_data):
        """Establish contact"""
        for m in range(0, len(df_data)):
            # print(df_data['buy'][m], df_data['money'][m], df_data['sell'][m])
            rel = Relationship(self.matcher.match(self.buy).where("_.name=" + "'" + df_data['buy'][m] + "'").first(),
                               df_data['money'][m],
                               self.matcher.match(self.sell).where("_.name=" + "'" + df_data['sell'][m] + "'").first())
            self.graph.create(rel)


class Processor:
    def __init__(self, path):
        self.invoice_data = pd.read_excel(path, header=0)

    def node_extraction(self):
        # Take the buyer's name to the list
        node_buy_key = self.invoice_data['Name of purchaser'].tolist()

        node_sell_key = self.invoice_data['Name of seller'].tolist()

        # Remove duplicate invoice names
        node_buy_key = list(set(node_buy_key))
        node_sell_key = list(set(node_sell_key))

        return node_buy_key, node_sell_key

    def relation_extraction(self):
        """Contact data extraction"""

        links_dict = {}
        sell_list = []
        money_list = []
        buy_list = []

        for i in range(0, len(self.invoice_data)):
            money_list.append(self.invoice_data[self.invoice_data.columns[19]][i])  # amount of money
            sell_list.append(self.invoice_data[self.invoice_data.columns[10]][i])  # Name of seller
            buy_list.append(self.invoice_data[self.invoice_data.columns[6]][i])  # Name of purchaser

        # Convert all int types in the data to string
        sell_list = [str(i) for i in sell_list]
        buy_list = [str(i) for i in buy_list]
        money_list = [str(i) for i in money_list]

        # Integrate the data and integrate the three list s into a dict
        links_dict['buy'] = buy_list
        links_dict['money'] = money_list
        links_dict['sell'] = sell_list
        # Convert data to DataFrame
        df_data = pd.DataFrame(links_dict)
        return df_data


if __name__ == '__main__':
    # dataToNeo4j = DataToNeo4j()
    path = './Invoice_data_Demo.xls'
    processor = Processor(path)
    node_buy_key, node_sell_key = processor.node_extraction()
    print(node_buy_key)
    print(node_sell_key)
    df_data = processor.relation_extraction()
    dataToNeo4j = DataToNeo4j()
    dataToNeo4j.create_node(node_buy_key, node_sell_key)
    dataToNeo4j.create_relation(df_data)

result:

Posted on Wed, 24 Nov 2021 01:17:18 -0500 by suvarthi