# 基于neo4j,django,pytorch,py2neo的电影图谱及问答
功能主要包括实体识别、实体查询、关系查询以及问答几个模块。
项目中用到的数据来自网上公开数据集
前端页面参考:https://github.com/qq547276542/Agriculture_KnowledgeGraph,在此表示非常感谢。
## 准备数据及构建实体及关系
note:以下数据导入是在Neo4j控制台上完成,将数据data/node与data/relation放入neo4j安装目录下import文件夹下:
三类实体(节点):
实体类型 数据文件 数量 说明
Movie Movie.csv 4587 电影实体
Person Person.csv 22937 人员实体
Country Country.csv 84 国家实体
四类关系:
关系类型 主语实体类型 宾语实体类型 数据文件 数量 说明
ACTOR Movie Person actor.csv 35257 电影的主演
COMPOSER Movie Person composer.csv 8345 电影的编剧
DIRECTOR Movie Person director.csv 5015 电影的导演
DISTRICT Movie Country district.csv 6227 电影的制片国家/地区
### 一.为id创建唯一索引
1.create constraint on (c:Country) assert c.id is unique
2.create constraint on (m:Movie) assert m.id is unique
3.create constraint on (p:Person) assert p.id is unique
### 二.为name创建索引
1.create index on :Country(name)
2.create index on :Movie(name)
3.create index on :Person(name)
### 三.创建node
1.Country节点
call apoc.periodic.iterate(
'call apoc.load.csv("node/Country.csv",{header:true,sep:",",ignore:["label"]}) yield map as row return row',
'create(p:Country) set p=row',
{batchSize:1000,iterateList:true, parallel:true})
2.Movie节点
call apoc.periodic.iterate(
'call apoc.load.csv("node/Movie.csv",{header:true,sep:",",ignore:["label"]}) yield map as row return row',
'create(p:Movie) set p=row',
{batchSize:1000,iterateList:true, parallel:true})
3.Person节点
call apoc.periodic.iterate(
'call apoc.load.csv("node/Person.csv",{header:true,sep:",",ignore:["label"]}) yield map as row return row',
'create(p:Person) set p=row',
{batchSize:1000,iterateList:true, parallel:true})
### 四.创建relation
1.关系:Movie-[:ACTOR]->Person
call apoc.periodic.iterate(
'call apoc.load.csv( "relation/actor.csv",{header:true,sep:","}) yield map as row match (start:Movie{id:row.start_id}), (end:Person{id:row.end_id}) return start,end',
'create (start)-[:ACTOR]->(end)',
{batchSize:1000,iterateList:true, parallel:false})
2.关系:Movie-[:COMPOSER]->Person
call apoc.periodic.iterate(
'call apoc.load.csv( "relation/composer.csv",{header:true,sep:","}) yield map as row match (start:Movie{id:row.start_id}), (end:Person{id:row.end_id}) return start,end',
'create (start)-[:COMPOSER]->(end)',
{batchSize:1000,iterateList:true, parallel:false})
3.关系:Movie-[:DIRECTOR]->Person
call apoc.periodic.iterate(
'call apoc.load.csv( "relation/director.csv",{header:true,sep:","}) yield map as row match (start:Movie{id:row.start_id}), (end:Person{id:row.end_id}) return start,end',
'create (start)-[:DIRECTOR]->(end)',
{batchSize:1000,iterateList:true, parallel:false})
4.关系:Movie-[:DISTRICT]->Country
call apoc.periodic.iterate(
'call apoc.load.csv( "relation/district.csv",{header:true,sep:","}) yield map as row match (start:Movie{id:row.start_id}), (end:Country{id:row.end_id}) return start,end',
'create (start)-[:DISTRICT]->(end)',
{batchSize:1000,iterateList:true, parallel:false})
### 五.安装pyhanlp用作分词与实体识别
1.下载hanlp模型和hanlp的jar包放在目录Anaconda3\Lib\site-packages\pyhanlp\static
2.在路径Anaconda3\Lib\site-packages\pyhanlp\static\data\dictionary\custom下放以下文件(data/custom_dict/演员名.txt,data/custom_dict/电影名.txt,data/custom_dict/电影类型.txt,data/custom_dict/other.txt),当自定义词典
演员名.txt
电影名.txt
电影类型.txt
other.txt
3.修改自定义词典配制D:\Anaconda3\Lib\site-packages\pyhanlp\static\hanlp.properties
CustomDictionaryPath=data/dictionary/custom/CustomDictionary.txt; 现代汉语补充词库.txt; 全国地名大全.txt ns; 人名词典.txt; 机构名词典.txt; 上海地名.txt ns; 电影类型.txt ng; 电影名.txt nm; 演员名.txt nnt; other.txt;data/dictionary/person/nrf.txt nrf;
## 项目结构
```
.
├── data
│ ── custom_dict // 常规数据
│ ── node // 电影node数据
│ ── relation // 电影relation数据
│
├── intent_classification
│ ── classification_data //意图识别训练数据
│ ── pytorch // 神经网络分类模型
│ ── scikit_learn // 传统机器学习模型
│
├── movie_kg // django项目路径
│ ├── Model // 模型层,用于和neo4j交互,实现查询等核心功能
│ ├── movie_kg // 用于写页面的逻辑(View)
│ ├── static // 静态资源
│ ├── templates // html页面
│ └── util // 包括预加载一些数据和模型
.
```
## 功能模块
本地启动命令:python manage.py runserver (或 movie_kg\run.bat)
打开:http://127.0.0.1:8000/
### 一.实体实别
主要识别人物、地名、机构、电影名以及明星名
![image](https://raw.githubusercontent.com/jiangnanboy/movie_knowledge_graph_app/master/img/ner.png)
### 二.实体查询
输入电影名或演员名会图谱化展示直接关系
![image](https://raw.githubusercontent.com/jiangnanboy/movie_knowledge_graph_app/master/img/search_ner.png)
### 三.关系查询
4种主要实体关系:actor、composer、director、district
可以选择不同的关系类型,输入电影名或演员名,给个图谱展示
![image](https://raw.githubusercontent.com/jiangnanboy/movie_knowledge_graph_app/master/img/search_relation.png)
### 四.电影问答
1.利用分类模型对用户输入的问题进行意图识别
(1).训练数据在目录 intent_classification\classification_data\classification_segment_data.txt
(2).总共16个意图类别,见目录 intent_classification\classification_data\question_classification.txt
(3).意图识别
a.分类模型1,这里使用feedforward-network进行意图识别
训练代码:intent_classification\pytorch\feedforward_network\train.ipynb
预测代码:intent_classification\pytorch\feedforward_network\predict.ipynb
b.分类模型2,这里使用textcnn进行意图识别
其中[sgns.sogou.char]使用了sogou的预训练向量可从这里下载(https://github.com/Embedding/Chinese-Word-Vectors)
训练代码:intent_classification\pytorch\textcnn\train.ipynb
预测代码:intent_classification\pytorch\textcnn\predict.ipynb
c.分类模型3,这里使用textrnn进行意图识别
其中[sgns.sogou.char]使用了sogou的预训练向量可从这里下载(https://github.com/Embedding/Chinese-Word-Vectors)
训练代码:intent_classification\pytorch\textrnn\train.ipynb
预测代码:intent_classification\pytorch\textrnn\predict.ipynb
d.分类模型4,这
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
知识图谱是一种结构化的知识表达形式,它以图形的方式组织和存储了大量实体(如人、地点、事件等)及其相互关系。在知识图谱中,实体作为节点,实体之间的各种语义关联则通过边进行连接,形成了一个庞大的数据网络。 知识图谱的核心价值在于其能够精确、直观地表示复杂世界中的知识,并支持高效的知识查询与推理。例如,在搜索引擎中,知识图谱可以提升搜索结果的相关性和准确性,为用户提供直接的答案而非仅仅是网页链接。同时,知识图谱还能支撑高级的人工智能应用,比如问答系统、推荐系统、决策支持等领域。 构建知识图谱的过程通常包括数据抽取、知识融合、实体识别、关系抽取等多个步骤,涉及到自然语言处理、机器学习、数据库技术等多种技术手段。知识图谱的不断完善有助于实现从海量信息中挖掘深层次、有价值的知识,从而推动人工智能向着更加理解人类世界的智慧方向发展。 总之,知识图谱是一个大规模、多领域、多源异构知识集成的载体,是实现智能化信息系统的基础工具和关键基础设施,对于提升信息检索质量、推动智能应用研发具有重要作用。
资源推荐
资源详情
资源评论
收起资源包目录
电影知识图谱,主要包括实体识别、实体查询、关系查询以及智能问答等.zip (297个子文件)
events.out.tfevents.1608859808.DESKTOP-RO608.17208.1 1.28MB
events.out.tfevents.1610522638.DESKTOP-RO608.26548.1 13KB
events.out.tfevents.1610515742.DESKTOP-RO608.20116.11 25KB
events.out.tfevents.1609748495.DESKTOP-RO608.22780.13 13KB
run.bat 26B
sgns.sogou.char 119KB
jquery.easy-pie-chart.coffee 5KB
bootstrap-theme.css 116KB
bootstrap.min.css 95KB
style.css 91KB
jquery-ui-1.10.4.min.css 27KB
elegant-icons-style.css 25KB
font-awesome.css 25KB
font-awesome.min.css 20KB
theme.css 20KB
bootstrap-fullcalendar.css 12KB
fullcalendar.css 10KB
fullcalendar.css 10KB
footable.bootstrap.css 10KB
footable.bootstrap.min.css 8KB
style-responsive.css 7KB
widgets.css 7KB
xcharts.min.css 4KB
styles.css 4KB
style.css 4KB
styles.css 4KB
line-icons.css 2KB
prettify.css 2KB
owl.carousel.css 1KB
fullcalendar.print.css 1KB
tree_style.css 1KB
tagcloud.css 907B
jquery-jvectormap-1.2.2.css 744B
jquery.easy-pie-chart.css 141B
actor.csv 2.35MB
Person.csv 1.35MB
Movie.csv 1.18MB
composer.csv 570KB
district.csv 426KB
director.csv 343KB
classification_data.csv 6KB
Country.csv 5KB
fontawesome-webfont.eot 71KB
ElegantIcons.eot 58KB
glyphicons-halflings-regular.eot 14KB
.gitignore 176B
.gitignore 11B
model.h5 3.34MB
model.h5 3.32MB
model.h5 1.51MB
model.h5 84KB
model.h5 83KB
index.html 27KB
index.html 13KB
index.html 12KB
navigate.html 10KB
relation.html 9KB
entity_search.html 8KB
question_answering.html 7KB
external-dragging.html 4KB
index.html 4KB
sixup.html 3KB
index.html 3KB
selectable.html 2KB
theme.html 2KB
agenda-views.html 2KB
basic-views.html 2KB
default.html 2KB
gcal.html 2KB
json.html 1KB
radar.html 1KB
line.html 961B
bar.html 811B
polarArea.html 795B
doughnut.html 733B
pie.html 603B
404.html 227B
kgcar.iml 1KB
movie_knowledge_graph_app.iml 733B
train.ipynb 53KB
train.ipynb 11KB
train.ipynb 10KB
train.ipynb 10KB
predict.ipynb 7KB
predict-checkpoint.ipynb 7KB
predict.ipynb 6KB
predict.ipynb 6KB
predict.ipynb 6KB
train-checkpoint.ipynb 72B
chart-texture.jpg 8KB
echarts.js 2.32MB
echarts.common.min.js 435KB
footable.js 262KB
jquery-ui-1.9.2.custom.min.js 232KB
jquery-ui-1.10.4.min.js 223KB
jquery-jvectormap-world-mill-en.js 141KB
fullcalendar.js 122KB
jquery.sparkline.js 121KB
jquery.sparkline-11.js 118KB
jquery-1.8.3.min.js 91KB
共 297 条
- 1
- 2
- 3
资源评论
JJJ69
- 粉丝: 6236
- 资源: 5778
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功