• 1119飞花令句子,好友关系导入


    飞花令句子

    数据来源

    之前通过分析后,将句子中包含飞花令“字”的诗句抽取出来

    格式如下

     导入代码

    import pandas as pd
    import numpy as np
    import re
    from py2neo import Node,Relationship,Graph,NodeMatcher,RelationshipMatcher
    
    # 创建节点
    def CreateNode(m_graph,m_label,m_attrs):
        #根绝节点name属性,查找节点
        m_n="_.name="+"\'"+m_attrs['name']+"\'"
        matcher = NodeMatcher(m_graph)
        re_value = matcher.match(m_label).where(m_n).first()
        #print(re_value)
        if re_value is None:
            m_mode = Node(m_label,**m_attrs)
            n = graph.create(m_mode)
            return n
        return None
    # 查询节点
    def MatchNode(m_graph,m_label,m_attrs):
        m_n="_.name="+"\'"+m_attrs['name']+"\'"
        matcher = NodeMatcher(m_graph)
        re_value = matcher.match(m_label).where(m_n).first()
        return re_value
    # 创建关系
    def CreateRelationship(m_graph,m_label1,m_attrs1,m_label2,m_attrs2,m_r_name):
        reValue1 = MatchNode(m_graph,m_label1,m_attrs1)
        reValue2 = MatchNode(m_graph,m_label2,m_attrs2)
        if reValue1 is None or reValue2 is None:
            return False
        m_r = Relationship(reValue1,m_r_name,reValue2)
        n = graph.create(m_r)
        return n
    
    #查找关系
    def findRelationship(m_graph,m_label1,m_attrs1,m_label2,m_attrs2,m_r_name):
        reValue1 = MatchNode(m_graph, m_label1, m_attrs1)
        reValue2 = MatchNode(m_graph, m_label2, m_attrs2)
        if reValue1 is None or reValue2 is None:
            return False
        m_r = Relationship(reValue1, m_r_name['name'], reValue2)
        return m_r
    
    def updateRelation(m_graph,m_label1,m_attrs1,m_label2,m_attrs2,m_r_name):
        reValue1 = MatchNode(m_graph, m_label1, m_attrs1)
        reValue2 = MatchNode(m_graph, m_label2, m_attrs2)
        if reValue1 is None or reValue2 is None:
            return False
        print(m_r_name)
        propertyes={'value': m_r_name['value'], 'danwei': m_r_name['danwei']}
        m_r = Relationship(reValue1, m_r_name['name'], reValue2,**propertyes)
        graph.merge(m_r)
    
    #修改节点属性
    def updateNode(m_graph,m_label1,m_attrs1,new_attrs):
        reValue1 = MatchNode(m_graph, m_label1, m_attrs1)
        if reValue1 is None:
            return False
        reValue1.update(new_attrs)
        graph.push(reValue1)
    
    
    
    graph = Graph('http://localhost:7474',username='neo4j',password='fengge666')
    
    #获取指定文件夹下的excel
    import os
    def get_filename(path,filetype):  # 输入路径、文件类型例如'.xlsx'
        name = []
        for root,dirs,files in os.walk(path):
            for i in files:
                if os.path.splitext(i)[1]==filetype:
                    name.append(i)
        return name            # 输出由有后缀的文件名组成的列表
    
    
    def create_sentence():
        file = 'sentences/'
        lists = get_filename(file, '.xlsx')
        for it in lists:
            newfile = file + it
            print(newfile)
    
            # 获取诗词内容
            data = pd.read_excel(newfile).fillna("")
    
            sentens = list(data.sentens)
            author = list(data.author)
            title = list(data.title)
            keys = list(data.word)
    
            sentence_label='sentence'
            word_label='word'
    
            for i in range(len(sentens)):
                print("" + str(i) + "")
                attr1 = {"name": sentens[i], "author": author[i], "title": title[i]}
                CreateNode(graph, sentence_label, attr1)
                print("创建诗句:" + sentens[i] + "成功!!")
                word_list=keys[i].split(',')
                for it in word_list:
                    attr2 = {"name": it}
                    # 创建关系
                    m_r_name1 = "关键字"
                    reValue1 = CreateRelationship(graph, sentence_label, attr1, word_label, attr2, m_r_name1)
                    print("创建关系:" + sentens[i] + "-关键字-" + it + "成功")
                    m_r_name2 = "诗句"
                    reValue2 = CreateRelationship(graph, word_label, attr2, sentence_label, attr1, m_r_name2)
                    print("创建关系:" + it + "-诗句-" + sentens[i] + "成功")
    
    
    
    if __name__ == '__main__':
        create_sentence()

    展示效果

     好友关系导入

    数据来源

    之前通过对诗人的个人简介与生平经历进行人名抽取,输出为excel保存

     关系导入

    import pandas as pd
    import numpy as np
    import re
    from py2neo import Node,Relationship,Graph,NodeMatcher,RelationshipMatcher
    
    # 创建节点
    def CreateNode(m_graph,m_label,m_attrs):
        #根绝节点name属性,查找节点
        m_n="_.name="+"\'"+m_attrs['name']+"\'"
        matcher = NodeMatcher(m_graph)
        re_value = matcher.match(m_label).where(m_n).first()
        #print(re_value)
        if re_value is None:
            m_mode = Node(m_label,**m_attrs)
            n = graph.create(m_mode)
            return n
        return None
    # 查询节点
    def MatchNode(m_graph,m_label,m_attrs):
        m_n="_.name="+"\'"+m_attrs['name']+"\'"
        matcher = NodeMatcher(m_graph)
        re_value = matcher.match(m_label).where(m_n).first()
        return re_value
    # 创建关系
    def CreateRelationship(m_graph,m_label1,m_attrs1,m_label2,m_attrs2,m_r_name):
        reValue1 = MatchNode(m_graph,m_label1,m_attrs1)
        reValue2 = MatchNode(m_graph,m_label2,m_attrs2)
        if reValue1 is None or reValue2 is None:
            return False
        m_r = Relationship(reValue1,m_r_name,reValue2)
        n = graph.create(m_r)
        return n
    
    #查找关系
    def findRelationship(m_graph,m_label1,m_attrs1,m_label2,m_attrs2,m_r_name):
        reValue1 = MatchNode(m_graph, m_label1, m_attrs1)
        reValue2 = MatchNode(m_graph, m_label2, m_attrs2)
        if reValue1 is None or reValue2 is None:
            return False
        m_r = Relationship(reValue1, m_r_name['name'], reValue2)
        return m_r
    
    def updateRelation(m_graph,m_label1,m_attrs1,m_label2,m_attrs2,m_r_name):
        reValue1 = MatchNode(m_graph, m_label1, m_attrs1)
        reValue2 = MatchNode(m_graph, m_label2, m_attrs2)
        if reValue1 is None or reValue2 is None:
            return False
        print(m_r_name)
        propertyes={'value': m_r_name['value'], 'danwei': m_r_name['danwei']}
        m_r = Relationship(reValue1, m_r_name['name'], reValue2,**propertyes)
        graph.merge(m_r)
    
    #修改节点属性
    def updateNode(m_graph,m_label1,m_attrs1,new_attrs):
        reValue1 = MatchNode(m_graph, m_label1, m_attrs1)
        if reValue1 is None:
            return False
        reValue1.update(new_attrs)
        graph.push(reValue1)
    
    
    
    graph = Graph('http://localhost:7474',username='neo4j',password='fengge666')
    
    
    def create_friend():
        file = 'data2/friend.xlsx'
    
        # 获取诗词内容
        data = pd.read_excel(file).fillna("")
    
        author=list(data.author)
        friend=list(data.friend)
    
    
        author_label='author'
    
        for i in range(len(author)):
            print("" + str(i) + "")
            attr1 = {"name": author[i]}
            if MatchNode(graph, author_label, attr1) != None:
                friend_list=friend[i].split(',')
                for it in friend_list:
                    attr2 = {"name": it}
                    if MatchNode(graph, author_label, attr2) != None and it!=author[i]:
                        # 创建关系
                        m_r_name1 = "好友"
                        reValue1 = CreateRelationship(graph, author_label, attr1, author_label, attr2, m_r_name1)
                        print("创建关系:" + author[i] + "-好友-" + it + "成功")
                        m_r_name2 = "好友"
                        reValue2 = CreateRelationship(graph, author_label, attr2, author_label, attr1, m_r_name2)
                        print("创建关系:" + it + "-好友-" + author[i] + "成功")
    
    if __name__ == '__main__':
        create_friend()

    效果展示

     

     

  • 相关阅读:
    Asp.net2.0 中自定义过滤器对Response内容进行处理 dodo
    自动化测试工具 dodo
    TestDriven.NET 2.0——单元测试的好助手(转) dodo
    JS弹出窗口的运用与技巧 dodo
    ElasticSearch 简介 规格严格
    修改PostgreSQL字段长度导致cached plan must not change result type错误 规格严格
    Linux系统更改时区(转) 规格严格
    mvn编译“Cannot find matching toolchain definitions for the following toolchain types“报错解决方法 规格严格
    ElasticSearch 集群 & 数据备份 & 优化 规格严格
    Elasticsearch黑鸟教程22:索引模板的详细介绍 规格严格
  • 原文地址:https://www.cnblogs.com/xiaofengzai/p/15582334.html
Copyright © 2020-2023  润新知