• 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()

    效果展示

     

     

  • 相关阅读:
    day 71
    day 70
    day 69 Django 视图层(views)and 模板层(templates)
    Python基础语法 第1节课(标识符、注释、缩进、输入输出、变量、数据类型)
    第019讲:函数 我的地盘听我的--函数变量的作用域(课后测试题及答案 )
    第018讲:函数 灵活即强大(课后测试题及答案 )
    第017讲:函数 python的乐高积木(课后测试题及答案 )
    第012课:列表:一个打了激素的数组3(课后习题及解答)
    第011讲:列表:一个打了激素的数组2 (课后习题及答案)
    第016讲:序列!序列! (课后测试题及答案 )
  • 原文地址:https://www.cnblogs.com/xiaofengzai/p/15582334.html
Copyright © 2020-2023  润新知