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

效果展示

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章