欢迎来到我的主页~【蜡笔小新..】
本篇收录于专栏【Python】
如果对你有帮助,希望点赞收藏加关注啦~
目录
前言
neo4j基础知识
Pycharm及代码实现
py2neo
数据集获取
代码介绍
前言
Python实验课时,老师提到用知识图谱构建红楼梦的人物关系图,所以课后根据视频和博客的学习,初尝试用neo4j进行构建,也在学习如何用Python直接进行手撕关系图并可视化,但是目前效果不佳,后续构建好继续分享在专栏。
neo4j基础知识
neo4j.com进行数据库下载安装
使用命令行输入
neo4j.bat console
找到localhost:7474,复制到浏览器进行登录,初始用户名和密码均为neo4j
其实在最顶端的窗口可以直接进行图谱的搭建,但由于本文是实现 Python进行构建,所以不在此进行赘述。
Pycharm及代码实现
py2neo
neo4j提供python接口,可以使用py2neo库通过python操作neo4j数据库,py2neo安装较为简单,仅需要使用pip install方式进行安装。使用如下命令:
pip install py2neo
安装完后进行测试:
from py2neo import Graph, Node, Relationship
def connect_database(username, password, dbname):
graph_db = Graph("http://localhost:7474", name=dbname, auth=(username, password))
return graph_db
if __name__ == '__main__':
graph = connect_database("neo4j", "password", "neo4j")
数据集获取
将以下内容复制到txt文件中,再改为csv文件
文章来源地址https://www.toymoban.com/news/detail-851376.html
"head","tail","relation","label"
"贾代善","贾源","son","子"
"娄氏","贾源","daughter_in_law_of_grandson","重孙媳妇"
"贾母","贾代善","wife","妻"
"老姨奶奶","贾代善","concubine","妾"
"贾敏","贾代善","daughter","女"
"嫣红","贾赦","concubine","妾"
"翠云","贾赦","concubine","妾"
"娇红","贾赦","concubine","妾"
"贾迎春","贾赦","daughter","女"
"赵姨娘","贾政","concubine","妾"
"周姨娘","贾政","concubine","妾"
"贾珠","贾政","son","子"
"尤二姐","贾琏","concubine","妾"
"秋桐","贾琏","concubine","妾"
"平儿","贾琏","concubine","妾"
"薛宝衩","贾宝玉","wife","妻"
"花袭人","贾宝玉","concubine","妾"
"贾桂","贾宝玉","son","子"
"贾菌","娄氏","son","子"
"周秀才","贾巧姐","brother_in_law","姐夫"
"周财主","周秀才","father","父亲"
"周妈妈","周秀才","mother","母亲"
"孙亲太太","孙绍祖","mother","母亲"
"刑大舅二姐","邢夫人","younger_sister","妹"
"邢德全","邢夫人","younger_brother","弟"
"张大老爷","邢夫人","old_relatives","老亲"
"邢忠","邢夫人","elder_brother","兄"
"张大老爷之女","张大老爷","daughter","女"
"刑秞烟","邢忠","daughter","女"
"李婶","李守中","sister_in_law","弟媳"
"李婶之弟","李婶","younger_brother","弟"
"李纹","李婶","daughter","女"
"甄宝玉","李绮","husband","夫"
"甄应嘉","甄宝玉祖母","son","子"
"甄夫人","甄应嘉","wife","妻"
"大姑娘","甄应嘉","daughter","女"
"二姑娘","甄应嘉","daughter","女"
"三姑娘","甄应嘉","daughter","女"
"袭人之母","袭人","mother","母亲"
"花自芳","袭人","elder_brother","兄"
"周琼之子","贾探春","husband","夫"
"林如海之父","林如海","father","父亲"
"林如海之祖","林如海","grandfather","祖父"
"林如海之子","林如海","son","子"
"李妈","贾巧姐","nurser","乳母"
"昭儿","贾琏","servant","奴仆"
"兴儿","贾琏","servant","奴仆"
"隆儿","贾琏","servant","奴仆"
"庆儿","贾琏","servant","奴仆"
"赵嬷嬷","贾琏","nurser","乳母"
"赵天梁","赵嬷嬷","son","子"
"赵天栋","赵嬷嬷","son","子"
"王信","贾琏","servant","奴仆"
"王信媳妇","贾琏","servant","奴仆"
"鲍二","贾琏","servant","奴仆"
"善姐","尤二姐","servant_girl","丫鬟"
"迎春乳母","贾迎春","nurser","乳母"
"王住儿媳妇","迎春乳母","daughter_in_law","子媳"
"绣桔","贾迎春","servant_girl","丫鬟"
"莲花儿","贾迎春","servant_girl","丫鬟"
"秦思祺","贾迎春","servant_girl","丫鬟"
"潘又安","秦思祺","boy_friend","男友"
"贾琮奶妈","贾琮","nurser","乳母"
"篆儿","刑秞烟","servant_girl","丫鬟"
"新进来的奶子","贾兰","nurser","奶妈"
"素云","李纨","servant_girl","丫鬟"
"碧月","李纨","servant_girl","丫鬟"
"抱琴","贾元春","servant_girl","丫鬟"
"李嬷嬷","贾宝玉","nurser","乳母"
"赵嬷嬷","贾宝玉","nurser","乳母"
"张嬷嬷","贾宝玉","nurser","乳母"
"宋嬷嬷","贾宝玉","servant","奴仆"
"叶茗烟","贾宝玉","servant","奴仆"
"锄药","贾宝玉","servant","奴仆"
"扫红","贾宝玉","servant","奴仆"
"墨雨","贾宝玉","servant","奴仆"
"引泉","贾宝玉","servant","奴仆"
"扫花","贾宝玉","servant","奴仆"
"挑云","贾宝玉","servant","奴仆"
"伴鹤","贾宝玉","servant","奴仆"
"双瑞","贾宝玉","servant","奴仆"
"双寿","贾宝玉","servant","奴仆"
"老叶妈","叶茗烟","mother","母亲"
"李贵","李嬷嬷","son","子"
"李嬷嬷孙子","贾宝玉","servant","奴仆"
"王荣","贾宝玉","servant","奴仆"
"张若锦","贾宝玉","servant","奴仆"
"赵亦华","贾宝玉","servant","奴仆"
"周瑞","王夫人","servant","奴仆"
"周瑞女儿","周瑞","daughter","女"
"冷子兴","周瑞","son_in_law","女婿"
"周嫂子的儿子","周瑞","son","子"
"何三","周瑞","adopted_son","干儿子"
"白金钏","王夫人","servant_girl","丫鬟"
"白玉钏","王夫人","servant_girl","丫鬟"
"彩云","王夫人","servant_girl","丫鬟"
"彩鸾","王夫人","servant_girl","丫鬟"
"绣鸾","王夫人","servant_girl","丫鬟"
"绣凤","王夫人","servant_girl","丫鬟"
"白老媳妇","白金钏","mother","母亲"
"白老媳妇","白玉钏","mother","母亲"
"彩霞之母","彩霞","mother","母亲"
"小霞","彩霞","younger_sister","妹"
"小鹊","赵姨娘","servant_girl","丫鬟"
"小吉祥","赵姨娘","servant_girl","丫鬟"
"赵国基","赵姨娘","elder_brother","兄"
"钱槐","赵姨娘","elder_male_cousin","表兄"
"钱槐","贾环","servant","奴仆"
"雪雁","林黛玉","servant_girl","丫鬟"
"王嬷嬷","林黛玉","nurser","乳母"
"紫鹊","林黛玉","servant_girl","丫鬟"
"春纤","林黛玉","servant_girl","丫鬟"
"藕官","林黛玉","servant_girl","丫鬟"
"赖嬷嬷","赖大","mother","母亲"
"赖尚荣","赖大","son","子"
"赖大的女儿","赖大","daughter","女"
"来旺儿","王熙凤","servant","奴仆"
"旺儿媳妇","王熙凤","servant_girl","丫鬟"
"来喜家的","王熙凤","servant","奴仆"
"丰儿","王熙凤","servant_girl","丫鬟"
"彩明","王熙凤","servant_girl","丫鬟"
"林红玉","王熙凤","servant_girl","丫鬟"
"林之孝","林红玉","father","父亲"
"王善保","邢夫人","servant","奴仆"
"费婆子","邢夫人","servant_girl","丫鬟"
"金鸳鸯","贾母","servant_girl","丫鬟"
"琥珀","贾母","servant_girl","丫鬟"
"鹦鹉","贾母","servant_girl","丫鬟"
"珍珠","贾母","servant_girl","丫鬟"
"翡翠","贾母","servant_girl","丫鬟"
"玻璃","贾母","servant_girl","丫鬟"
"文官","贾母","servant_girl","丫鬟"
"傻大姐的娘","傻大姐","mother","母亲"
"金彩","鸳鸯","father","父亲"
"金文翔","鸳鸯","elder_brother","兄"
"多官","晴雯","elder_male_cousin","表哥"
"多浑虫父亲","晴雯","mothers_brother","舅"
"多姑娘儿","多官","wife","妻"
"吴贵","晴雯","elder_male_cousin","表兄"
"花芳官","贾宝玉","servant_girl","丫鬟"
"媚人","贾宝玉","servant_girl","丫鬟"
"麝月","贾宝玉","servant_girl","丫鬟"
"茜雪","贾宝玉","servant_girl","丫鬟"
"秋纹","贾宝玉","servant_girl","丫鬟"
"绮霞","贾宝玉","servant_girl","丫鬟"
"碧痕","贾宝玉","servant_girl","丫鬟"
"檀云","贾宝玉","servant_girl","丫鬟"
"四儿","贾宝玉","servant_girl","丫鬟"
"佳蕙","贾宝玉","servant_girl","丫鬟"
"坠儿","贾宝玉","servant_girl","丫鬟"
"紫绡","贾宝玉","servant_girl","丫鬟"
"良儿","贾宝玉","servant_girl","丫鬟"
"何春燕","贾宝玉","servant_girl","丫鬟"
"厨房中的柳家媳妇","柳五儿","mother","母亲"
"柳二媳妇的妹子","柳五儿","younger_sister","妹"
"哥嫂侄儿","柳五儿","nephew","侄"
"侍书","贾探春","servant_girl","丫鬟"
"翠墨","贾探春","servant_girl","丫鬟"
"艾官","贾探春","servant_girl","丫鬟"
"小蝉","贾探春","servant_girl","丫鬟"
"何婆","何春燕","mother","母亲"
"何婆","芳官","adopted_mother","干娘"
"小鸠儿","何春燕","younger_sister","妹"
"夏婆子","何春燕","maternal_aunt","姨妈"
"夏婆子","藕官","adopted_mother","干娘"
"夏婆子","小蝉","grandmother","外祖母"
"蒋玉菡","贾宝玉","friend","朋友"
"通判傅试","贾政","pupil","门生"
"傅秋芳","傅试","younger_sister","妹"
"太祖皇帝","先皇","father","父亲"
"太上皇","先皇","son","子"
"皇太后","太上皇","wife","妻"
"太妃","太上皇","kings_concubine","嫔妃"
"皇帝","太上皇","son","子"
"贾元春","皇帝","imperial_concubine","妃"
"吴贵妃","皇帝","imperial_concubine","妃"
"周贵人","皇帝","imperial_concubine","妃"
"周贵人父亲","周贵人","father","父亲"
"吴天佑","吴贵妃","father","父亲"
"戴权","皇帝","minister","臣"
"夏守忠","皇帝","minister","臣"
"贾代化","贾演","son","子"
"焦大","贾演","servant","奴仆"
"贾敷","贾代化","son","子"
"贾敬","贾代化","son","子"
"贾惜春","贾敬","daughter","女"
"史湘云爷爷","史侯","son","子"
"史鼐","史湘云爷爷","son","子"
"史鼎","史湘云爷爷","son","子"
"湘云母","史湘云爷爷","daughter_in_law","儿媳"
"史鼎的夫人","史鼎","wife","妻"
"史湘云","湘云母","daughter","女"
"卫若兰","史湘云","husband","夫"
"翠缕","史湘云","servant_girl","丫鬟"
"葵官","史湘云","servant_girl","丫鬟"
"周奶妈","史湘云","nurser","乳母"
"凤姐之祖王夫人之父","王公","son","子"
"王夫人之大兄凤姐之父","凤姐之祖王夫人之父","son","子"
"王子腾","凤姐之祖王夫人之父","son","子"
"王子胜","凤姐之祖王夫人之父","son","子"
"王夫人","凤姐之祖王夫人之父","daughter","女"
"薛姨妈","凤姐之祖王夫人之父","daughter","女"
"王成父","凤姐之祖王夫人之父","nephew","侄"
"老舅太太","王夫人之大兄凤姐之父","wife","妻"
"王仁","王夫人之大兄凤姐之父","son","子"
"王熙凤","王夫人之大兄凤姐之父","daughter","女"
"王子腾夫人","王子腾","wife","妻"
"王子腾之女","王子腾","daughter","女"
"保宁侯之子","王子腾之女","husband","夫"
"王成","王成父","son","子"
"王狗儿","王成","son","子"
"刘氏","王狗儿","wife","妻"
"王青儿","王狗儿","daughter","女"
"王板儿","王狗儿","son","子"
"刘姥姥","刘氏","mother","母亲"
"宝钗祖父","薛公","son","子"
"薛公之孙","宝钗祖父","son","子"
"薛宝琴父","宝钗祖父","son","子"
"薛姨妈","薛公之孙","wife","妻"
"薛宝钗","薛公之孙","daughter","女"
"薛蟠","薛公之孙","son","子"
"同喜","薛姨妈","servant_girl","丫鬟"
"同贵","薛姨妈","servant_girl","丫鬟"
"莺儿","薛宝钗","servant_girl","丫鬟"
"文杏","薛宝钗","servant_girl","丫鬟"
"喜儿","薛宝钗","servant_girl","丫鬟"
"蕊官","薛宝钗","servant_girl","丫鬟"
"夏金桂","薛蟠","wife","妻"
"老苍头","薛蟠","meme_ama","乳父"
"宝蟾","夏金桂","servant_girl","丫鬟"
"小舍儿","夏金桂","servant_girl","丫鬟"
"夏奶奶","夏金桂","mother","母亲"
"夏三","夏奶奶","son","子"
"臻儿","香菱","servant_girl","丫鬟"
"薛宝琴母","薛宝琴父","wife","妻"
"薛蝌","薛宝琴父","son","子"
"薛宝琴","薛宝琴父","daughter","女"
"邢岫烟","薛蝌","wife","妻"
"梅翰林之子","薛宝琴","husband","夫"
"小螺","薛宝琴","servant_girl","丫鬟"
"荳官","薛宝琴","servant_girl","丫鬟"
"梅翰林","梅翰林之子","father","父亲"
"贾瑞之父","贾代儒","son","子"
"贾瑞之母","贾瑞之父","wife","妻"
"贾瑞","贾瑞之父","son","子"
"贾琼","贾琼之母","son","子"
"贾四姐","贾琼之母","daughter","女"
"贾王扁","贾王扁之母","son","子"
"贾喜鸾","贾王扁之母","daughter","女"
"贾芸","五嫂子卜氏","son","子"
"卜世仁","五嫂子卜氏","brother","兄弟"
"小丫头子","贾芸","servant_girl","丫鬟"
"倪儿娘子","倪二","wife","妻"
"倪二女儿","倪二","daughter","女"
"马贩子王短腿","倪二","friend","友人"
"卜世仁娘子","卜世仁","wife","妻"
"卜银姐","卜世仁","daughter","女"
"王奶奶","卜世仁","neighbour","邻居"
"卜世仁店里伙计","卜世仁","partner","伙计"
"贾芹","周氏","son","子"
"金氏","贾璜","wife","妻"
"胡氏","金氏","elder_sister_in_law","嫂"
"金荣","胡氏","son","子"
"娇杏","贾化","wife","妻"
"贾雨村子","贾化","son","子"
"应天府门子","贾化","son","子"
"张如圭","贾化","friend","友人"
"王老爷","贾化","friend","友人"
"天子之妻","应天府门子","wife","妻"
"东安郡王穆莳拜","东平郡王","son","子"
"南安王太妃","南安郡王","wife","妻"
"南安郡王之孙","南安郡王","grandson","孙子"
"西宁郡王之孙","西宁郡王","grandson","孙子"
"西宁郡王妃","西宁郡王","wife","妻"
"北静王太妃","北静郡王","daughter_in_law","儿媳"
"北静王少妃","水溶","wife","妻"
"北静郡王长府官","水溶","minister","臣"
"镇国公诰命","牛清","wife","妻"
"镇国公诰命长男","牛清","son","子"
"牛继宗","牛清","grandson","孙子"
"柳芳","柳彪","grandson","孙子"
"陈瑞文","陈翼","grandson","孙子"
"马尚","马魁","grandson","孙子"
"侯孝康","侯明","grandson","孙子"
"缮国公诰命","缮国公","wife","妻"
"石光珠","缮国公","grandson","孙子"
"蒋子宁","平原侯","grandson","孙子"
"谢鲸","定城侯","grandson","孙子"
"戚建辉","襄阳侯","grandson","孙子"
"裘良","景田侯","grandson","孙子"
"锦乡侯诰命","锦乡侯","wife","妻"
"韩奇","锦乡侯","son","子"
"冯紫英","神武将军冯唐","son","子"
"锦田侯诰命","马道婆","donor","施主"
"蕊官","地藏庵的圆心","pupil","徒弟"
"藕官","圆心","pupil","徒弟"
"智能儿","净虚","pupil","徒弟"
"智善","净虚","pupil","徒弟"
"智通","净虚","pupil","徒弟"
"于老爷","净虚","donor","施主"
"胡老爷","净虚","donor","施主"
"胡老爷太太","净虚","donor","施主"
"胡老爷公子","净虚","donor","施主"
"张大财主","净虚","donor","施主"
"花芳官","智能","pupil","徒弟"
"张金哥","张大财主","daughter","女"
"李公子","张金哥","employer","雇主"
"长安原任守备","李公子","father","父亲"
"李衙内","长安府府太爷","mothers_brother","小舅子"
"贾代化","贾演","son","子"
"焦大","贾演","servant","奴仆"
"贾敷","贾代化","son","子"
"贾敬","贾代化","son","子"
"尤氏","尤老娘","daughter","女"
"贾蔷","贾演","great_great_grandson","玄孙"
"龄官","贾蔷","girl_friend","女友"
"佩凤","贾珍","concubine","妾"
"偕鸾","贾珍","concubine","妾"
"文花","贾珍","concubine","妾"
"茄官","尤氏","servant_girl","丫鬟"
"银蝶","尤氏","servant_girl","丫鬟"
"炒豆儿","尤氏","servant_girl","丫鬟"
"秦可卿","贾蓉","wife","妻"
"胡氏","贾蓉","step_wife","续弦"
"许氏","贾蓉","step_wife","续弦"
"入画","贾惜春","servant_girl","丫鬟"
"彩屏","贾惜春","servant_girl","丫鬟"
"彩儿","贾惜春","servant_girl","丫鬟"
"瑞珠","秦可卿","servant_girl","丫鬟"
"宝珠","秦可卿","servant_girl","丫鬟"
"秦钟","秦业","son","子"
"智能儿","秦钟","girl_friend","女友"
"净虚","智能儿","master","师父"
"智善","净虚","pupil","徒弟"
"智通","净虚","pupil","徒弟"
"喜儿","贾珍","servant","奴仆"
"寿儿","贾珍","servant","奴仆"
"尤三姐","柳湘莲","girl_friend","女友"
"杏奴","柳湘莲","servant","奴仆"
"尤二姐","贾琏","concubine","妾"
"善姐","尤二姐","servant_girl","丫鬟"
"甄宝玉","李绮","husband","夫"
"贾演","贾源","elder_brother","兄"
"贾赦","贾代善","son","子"
"贾政","贾代善","son","子"
"贾敏","贾政","younger_sister","妹"
"邢夫人","贾赦","wife","妻"
"贾琏","贾赦","son","子"
"贾琮","贾赦","son","子"
"王夫人","贾政","wife","妻"
"贾元春","贾政","daughter","女"
"贾宝玉","贾政","son","子"
"贾探春","贾政","daughter","女"
"贾环","贾政","son","子"
"王熙凤","贾琏","wife","妻"
"贾巧姐","贾琏","daughter","女"
"贾兰","贾珠","son","子"
"李纨","贾珠","wife","妻"
"孙绍祖","贾迎春","husband","夫"
"李守中","李纨","father","父亲"
"李绮","李婶","daughter","女"
"甄宝玉","甄应嘉","son","子"
"林如海","贾敏","husband","夫"
"林黛玉","林如海","daughter","女"
"王嬷嬷","贾宝玉","nurser","乳母"
"彩霞","王夫人","servant_girl","丫鬟"
"傻大姐","贾母","servant_girl","丫鬟"
"晴雯","贾宝玉","servant_girl","丫鬟"
"篆儿","贾宝玉","servant_girl","丫鬟"
"柳五儿","贾宝玉","servant_girl","丫鬟"
"贾珍","贾敬","son","子"
"贾母","史侯","daughter","女"
"香菱","薛蟠","concubine","妾"
"倪二","贾芸","friend","友人"
"水溶","北静郡王","grandson","孙子"
"尤氏","贾珍","wife","妻"
"尤二姐","尤老娘","daughter","女"
"尤三姐","尤老娘","daughter","女"
"贾蓉","贾珍","son","子"
"秦可卿","秦业","daughter","女"
代码介绍
由于大部分代码都进行了注释,所以大家可以直接拷贝代码进行学习,在此不赘述代码作用
import csv
from py2neo import Graph, Node, Relationship
# 创建与 Neo4j 数据库的连接
g = Graph('http://localhost:7474', user='neo4j', password='neo4j123..', name="neo4j")
# 打开 CSV 文件
with open('HongLou.csv', 'r', encoding='utf-8') as f:
# 创建一个 CSV 读取器
reader = csv.reader(f)
# 遍历 CSV 文件中的每一行
for item in reader:
# 跳过标题行,因为从第二行开始才是人物信息
if reader.line_num == 1:
continue
# 打印当前行号和内容
print("当前行数:", reader.line_num, "当前内容:", item)
# 使用 CSV 中的值创建起始节点和结束节点,节点标签为 'Person',名称属性来自 CSV
start_node = Node("Person", name=item[0])
end_node = Node("Person", name=item[1])
# 使用 CSV 中的值创建起始节点和结束节点之间的关系,关系类型为 CSV 中的值
relation = Relationship(start_node, item[3], end_node)
# 将起始节点和结束节点合并到图中,使用节点标签 'Person' 和名称属性进行匹配
g.merge(start_node, "Person", "name")
g.merge(end_node, "Person", "name")
# 将关系合并到图中,使用节点标签 'Person' 和名称属性进行匹配
g.merge(relation, "Person", "name")
最后回到我们的neo4j中就能看到关系图谱啦
文章来源:https://www.toymoban.com/news/detail-851376.html
到了这里,关于Pycharm+Neo4j红楼梦人物关系图谱的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!