zoukankan      html  css  js  c++  java
  • 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()

    效果展示

     

     

  • 相关阅读:
    BF算法和KMP算法
    Python课程笔记 (五)
    0268. Missing Number (E)
    0009. Palindrome Number (E)
    0008. String to Integer (atoi) (M)
    0213. House Robber II (M)
    0198. House Robber (E)
    0187. Repeated DNA Sequences (M)
    0007. Reverse Integer (E)
    0006. ZigZag Conversion (M)
  • 原文地址:https://www.cnblogs.com/xiaofengzai/p/15582334.html
Copyright © 2011-2022 走看看