├── Protege使用
├── KGexample.owl
├── Protege-5.0.0-beta-16.zip
├── catalog-v001.xml
├── readme.txt
├── 效果.PNG
└── 第二讲 知识表示和知识建模 (1).pdf
├── README.md
├── 常用开放知识图谱API调用
├── CN_DBpedia.py
├── __init__.py
├── readme.txt
└── wikipedia.py
├── 汽车行业知识图谱第一次验收
├── conf.py
├── data
│ ├── __init__.py
│ ├── data.py
│ ├── mysql.py
│ └── three_tuples.txt
├── readme.txt
├── visualization
│ ├── .DS_Store
│ ├── __init__.py
│ ├── run_server.py
│ └── server
│ │ ├── .DS_Store
│ │ ├── __init__.py
│ │ ├── app.py
│ │ ├── models.py
│ │ ├── mysql.py
│ │ ├── templates
│ │ ├── .DS_Store
│ │ ├── d3.zip
│ │ ├── data.json
│ │ └── index.html
│ │ └── views.py
└── 效果.PNG
├── 汽车行业知识图谱第二次验收
├── __init__.py
├── conf.py
├── data
│ ├── http接口说明.txt
│ ├── statistics.txt
│ └── three_tuples.txt
├── kg
│ ├── kg.py
│ └── kg_test.py
├── readme.txt
├── server
│ ├── .DS_Store
│ ├── __init__.py
│ ├── app.py
│ ├── http_test.py
│ ├── module.py
│ ├── run_server.py
│ └── views.py
├── test.py
└── 效果.png
└── 知识图谱学习笔记
└── 笔记.docx
/Protege使用/KGexample.owl:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 |
8 |
9 |
10 | ]>
11 |
12 |
13 |
20 |
21 |
22 |
23 |
24 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 | <曾住 rdf:resource="&untitled-ontology-6;镇江金山寺"/>
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 | <别名>苏东坡、东坡别名>
196 | <相识 rdf:resource="&untitled-ontology-6;佛印禅师"/>
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 | <法号>法海法号>
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
--------------------------------------------------------------------------------
/Protege使用/Protege-5.0.0-beta-16.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/Protege使用/Protege-5.0.0-beta-16.zip
--------------------------------------------------------------------------------
/Protege使用/catalog-v001.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Protege使用/readme.txt:
--------------------------------------------------------------------------------
1 | 1.使用介绍:第二讲 知识表示和知识建模 (1).pdf
2 |
3 | 2.安装程序:Protege-5.0.0-beta-16.zip
4 |
5 | 3.示例程序:KGexample.owl
6 |
7 | 4.效果:效果.PNG
8 |
9 | 5.作用:本体建模
--------------------------------------------------------------------------------
/Protege使用/效果.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/Protege使用/效果.PNG
--------------------------------------------------------------------------------
/Protege使用/第二讲 知识表示和知识建模 (1).pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/Protege使用/第二讲 知识表示和知识建模 (1).pdf
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # KnowledgeGraph
2 | 知识图谱车音工作项目
3 |
--------------------------------------------------------------------------------
/常用开放知识图谱API调用/CN_DBpedia.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2018年1月12日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 |
11 |
12 |
13 | import json
14 | import requests
15 |
16 | #查询实体
17 | url='http://shuyantech.com/api/cndbpedia/avpair'
18 | quest={'q':'复旦大学'}
19 | print(requests.post(url,quest).text)
20 |
21 | #查询概念
22 | url='http://shuyantech.com/api/cnprobase/concept'
23 | quest={'q':'刘德华'}
24 | print(requests.post(url,quest).text)
--------------------------------------------------------------------------------
/常用开放知识图谱API调用/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/常用开放知识图谱API调用/__init__.py
--------------------------------------------------------------------------------
/常用开放知识图谱API调用/readme.txt:
--------------------------------------------------------------------------------
1 | 1.google的api可以返回不同实体的得分,只能读,需要翻墙
2 |
3 | 2.wikipedia可以返回网页信息,例如标题,概要,其它不可,可编辑,需要翻墙
4 |
5 | 3.CN_DBpedia可以中文查询概念的知识图谱,并返回相应概念的权重,不需要翻墙
--------------------------------------------------------------------------------
/常用开放知识图谱API调用/wikipedia.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2018年1月12日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | 介绍:https://github.com/goldsmith/Wikipedia
10 | '''
11 | import wikipedia
12 |
13 | def show(data):
14 | try:
15 | print('title',data.title)#标题
16 | print('title',data.url)#页面url
17 | print('title',data.content)#页面内容
18 | print('title',data.links[0])#页面连接
19 | except:
20 | print(data)
21 |
22 | wikipedia.set_lang('zh')#设置语言
23 | print(wikipedia.search('中山大小'))#搜索
24 | my=wikipedia.page('中山大小')#获取页面
25 | show(my)
26 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/conf.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2017年12月11日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | #数据路径
11 | DATA_PATH='data/web'
12 |
13 | #MYSQL信息
14 | MYSQL_HOST="192.168.1.245"
15 | MYSQL_PORT=3306
16 | MYSQL_USER="knowInfo"
17 | MYSQL_PASSWORD="knowInfo"
18 | MYSQL_db='KnowGrah_car_info'
19 |
20 | #web
21 | http_host='0.0.0.0'
22 | http_port=8090
23 | #json_path="/templates/data.json"
24 | json_path="/server/templates/data.json"
25 |
26 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/data/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第一次验收/data/__init__.py
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/data/data.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2017年11月27日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | import os
11 | import re
12 | import sys
13 | sys.path.append("..")
14 | from conf import *
15 | from bs4 import BeautifulSoup
16 |
17 | class data(object):
18 | def __init__(self):
19 | #self.database=database()
20 | self.process_cars()
21 | def process_cars(self):
22 | #连接数据库
23 | f= open(os.path.join(DATA_PATH,'..','three_tuples.txt'),'w',encoding='utf-8')
24 | for _,_,files in os.walk(DATA_PATH):
25 | for file in files:
26 | print('进度:%s:%d/%d'%(file,files.index(file)+1,len(files)))
27 | my_data=self.process_html(file)
28 | if(my_data==None):
29 | continue
30 | for data in my_data:
31 | f.write('%s:%s:%s\n'%(data[0],data[1],data[2]))
32 |
33 | #self.database.execute(car_info)
34 | #解析每一个html
35 | def process_html(self,file):
36 | file = open(os.path.join(DATA_PATH,file),'r',encoding='utf-8')
37 | my_data=set()
38 | soup=BeautifulSoup(file,'html.parser', from_encoding='utf-8')
39 |
40 | main_key=soup.find('title')
41 | if main_key==None:
42 | return None
43 | main_key=self.clean_string(main_key.string)
44 | names=[]
45 | for value in soup.find_all('dt',class_='basicInfo-item name'):
46 | names.append(self.clean_string(value.string))
47 | values=[]
48 | for value in soup.find_all('dd',class_='basicInfo-item value'):
49 | if value.string==None:
50 | values.append(self.clean_string(value.get_text()))
51 | else:
52 | values.append(self.clean_string(value.string))
53 | if(len(values)!=len(names)):
54 | my_data=[]
55 | return
56 | for name in names:
57 | value=values[names.index(name)]
58 | my_data.add((main_key,name,value))
59 | #print(my_data[0])
60 | return my_data
61 |
62 | def clean_string(self,value):
63 | if(value=='' or value==None):
64 | return
65 | value=value.strip().replace(' ','').upper()
66 | value = re.compile(r"_百度百科").sub('',value)
67 | value = re.compile(r"\[.*\]").sub('',value)
68 | value = re.compile(r"\(.*\)").sub('',value)
69 | value = re.compile(r"\(.*\)").sub('',value)
70 | value = re.compile(r" ").sub('',value)
71 | return value
72 |
73 | if __name__ == "__main__":
74 | data=data()
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/data/mysql.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2017年12月11日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | import pymysql
11 | import os
12 | import sys
13 | sys.path.append("..")
14 | from conf import *
15 |
16 | car_node='''
17 | create table car_node(
18 | id int primary key NOT NULL AUTO_INCREMENT,
19 | name varchar(100),
20 | ext varchar(500),
21 | create_time DATETIME,
22 | create_id INT(32),
23 | update_time DATETIME,
24 | update_id INT(32),
25 | is_delete INT DEFAULT '0' NOT NULL
26 | );
27 | '''
28 | car_node_attribute='''
29 | create table car_node_attribute(
30 | id int primary key NOT NULL AUTO_INCREMENT,
31 | node_id int not null,
32 | attribute_name varchar(200),
33 | attribute_value varchar(200),
34 | is_delete INT DEFAULT '0' NOT NULL
35 | );
36 | '''
37 | class database(object):
38 | def __init__(self):
39 | self.conn = pymysql.connect(
40 | host = MYSQL_HOST,
41 | port = MYSQL_PORT,
42 | user = MYSQL_USER,
43 | password = MYSQL_PASSWORD,
44 | charset ='utf8',
45 | db = MYSQL_db)
46 | self.cursor = self.conn.cursor()
47 | def __del__(self):
48 | self.conn.close()
49 | def data_process(self):
50 | with open(os.path.join(DATA_PATH,'..','three_tuples.txt'),'r',encoding='utf-8') as f:
51 | for line in f.readlines():
52 | line=self.clean_string(line).split(':')
53 | if len(line)!=3:
54 | continue
55 | mysql_cmd="select id from car_node where name='%s';"%(line[0])
56 | count=self.cursor.execute(mysql_cmd)
57 | if(count==0):
58 | mysql_cmd="insert into car_node(name) values('%s');"%(line[0])
59 | self.cursor.execute(mysql_cmd)
60 | node_id=self.conn.insert_id()
61 | #print(node_id,line[1],line[2])
62 | mysql_cmd='''insert into car_node_attribute(node_id,attribute_name,attribute_value) values(%d,'%s','%s');'''%(node_id,line[1],line[2])
63 | self.cursor.execute(mysql_cmd)
64 | else:
65 | result=self.cursor.fetchone()
66 | node_id=result[0]
67 | mysql_cmd='''insert into car_node_attribute
68 | (node_id,attribute_name,attribute_value) values(%d,'%s','%s');'''%(node_id,line[1],line[2])
69 | print(mysql_cmd)
70 | self.cursor.execute(mysql_cmd)
71 | self.conn.commit()
72 |
73 | def clean_string(self,value):
74 | if(value=='' or value==None):
75 | return
76 | value=value.strip().replace('\'','')
77 | return value
78 |
79 | if __name__=='__main__':
80 | database=database()
81 | database.data_process()
82 |
83 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/readme.txt:
--------------------------------------------------------------------------------
1 | 环境:python3+flask+mysql+D3
2 |
3 | 1.data:数据处理
4 | (1)data.py将爬虫趴下来的html数据处理成三元组,存在文件:three_tuples.txt
5 | (2)mysql.py将three_tuples.txt里数据存入Mysql数据
6 |
7 | 2.visualization:可视化
8 | (1)执行run_server.py,启动web服务,然后在浏览器中输入:
9 | http://192.168.1.245:8090/api/v1?car_name=日产天籁
10 | (2)原理:flask框架下查询mysql,存入json文件,index.html导入json文件,采用用D3的js框架
11 | (3)效果见:效果.PNG
12 | (4)数据量:11200个
13 | 3.conf.py:配置文件
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第一次验收/visualization/.DS_Store
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/__init__.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 |
4 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/run_server.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # @Author: ioriiod0
3 | # @Date: 2017-07-12 11:47:28
4 | # @Last Modified by: ioriiod0
5 | # @Last Modified time: 2017-07-14 19:21:37
6 | import sys
7 | sys.path.append("..")
8 |
9 | import gevent
10 | from gevent.monkey import patch_all
11 | from conf import *
12 | '''
13 | gevent是第三方库,通过greenlet实现协程,其基本思想是:
14 | 当一个greenlet遇到IO操作时,比如访问网络,就自动切换到其他的greenlet,等到IO操作完成,再在适当的时候切换回来继续执行。
15 | 由于IO操作非常耗时,经常使程序处于等待状态,有了gevent为我们自动切换协程,就保证总有greenlet在运行,而不是等待IO。
16 | 由于切换是在IO操作时自动完成,所以gevent需要修改Python自带的一些标准库,这一过程在启动时通过monkey patch完成:
17 | '''
18 | patch_all()
19 | from server.app import app
20 |
21 | if __name__ == '__main__':
22 | app.run(debug=True, host=http_host, port=http_port)
23 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第一次验收/visualization/server/.DS_Store
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/__init__.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 |
4 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 |
3 | app = Flask(__name__)
4 |
5 | from server.views import *
6 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/models.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # @Author: Shuang0420
3 | # @Date: 2017-08-29 17:08:03
4 | # @Last Modified by: Shuang0420
5 | # @Last Modified time: 2017-08-29 18:58:13
6 |
7 |
8 | import json
9 | import json
10 | import pymysql
11 | import sys
12 | import os
13 | from conf import *
14 |
15 | fname = os.getcwd() + json_path
16 |
17 | edge_sql_car = """select a.attribute_name,a.attribute_value from car_node n,car_node_attribute a where n.id = a.node_id and n.name = '%s';"""
18 | edge_sql_relation = """SELECT subj, obj, pred, company.company_name, company.code, person.name, type FROM spo JOIN company JOIN person WHERE spo.subj=company.id AND spo.obj=person.id AND person.name="%s";"""
19 | secondary_edge_sql = 'SELECT * FROM spo WHERE subj="%s"'
20 |
21 | def execute(database, attr):
22 | js = {}
23 | edges=[]
24 | try:
25 | if attr[0]=='car_name':
26 | sql = edge_sql_car%(attr[1])
27 | count=database.cursor.execute(sql)
28 | if count != 0:
29 | for result in database.cursor.fetchall():
30 | print(result)
31 | edges.append({"source": attr[1], "target": result[1], "relation": result[0], "label": 'relation'})
32 | else:
33 | print('car_name不存在')
34 | except:
35 | print("ERROR: " + sql)
36 | database.conn.rollback()
37 | # js["nodes"] = nodes
38 | js["edges"] = edges
39 | mydata = json.dumps(js, ensure_ascii=False).encode("utf8")
40 | print('Json路径是:%s'%(fname))
41 | with open(fname, 'w',encoding='utf-8') as f:
42 | f.write(str(mydata))
43 | return mydata
44 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/mysql.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2017年12月11日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | import pymysql
11 | import os
12 | from conf import *
13 |
14 | class database(object):
15 | def __init__(self):
16 | self.conn = pymysql.connect(
17 | host = MYSQL_HOST,
18 | port = MYSQL_PORT,
19 | user = MYSQL_USER,
20 | password = MYSQL_PASSWORD,
21 | charset ='utf8',
22 | db = MYSQL_db)
23 | self.cursor = self.conn.cursor()
24 | def __del__(self):
25 | self.conn.close()
26 |
27 |
28 | if __name__=='__main__':
29 | database=database()
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/templates/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第一次验收/visualization/server/templates/.DS_Store
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/templates/d3.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第一次验收/visualization/server/templates/d3.zip
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/templates/data.json:
--------------------------------------------------------------------------------
1 | b'{"edges": [{"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe7\x94\x9f\xe4\xba\xa7\xe5\x8e\x82\xe5\x95\x86", "target": "\xe6\x97\xa5\xe4\xba\xa7\xe6\xb1\xbd\xe8\xbd\xa6", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe5\x8a\xa0\xe9\x80\x9f\xe6\x97\xb6\xe9\x97\xb4", "target": "9.7S", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe9\xa9\xb1\xe5\x8a\xa8\xe6\x96\xb9\xe5\xbc\x8f", "target": "\xe5\x89\x8d\xe7\xbd\xae\xe5\x89\x8d\xe9\xa9\xb1", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe6\xa0\x87\xe5\x87\x86\xe5\xba\xa7\xe4\xbd\x8d\xe6\x95\xb0", "target": "5\xe5\xba\xa7", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe5\x93\x81\xe7\x89\x8c", "target": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe5\xba\xa7\xe4\xbd\x8d\xe6\x9d\x90\xe8\xb4\xa8", "target": "\xe7\x9c\x9f\xe7\x9a\xae", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe8\xbd\xb4\xe8\xb7\x9d", "target": "2775\xe6\xaf\xab\xe7\xb1\xb3", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe8\xbd\xa6\xe5\x9e\x8b\xe5\xb0\xba\xe5\xaf\xb8", "target": "4.868\xe7\xb1\xb31.830\xe7\xb1\xb31.490\xe7\xb1\xb3", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe6\xb2\xb9\xe8\x80\x97", "target": "7.3\xe5\x8d\x87/\xe7\x99\xbe\xe5\x85\xac\xe9\x87\x8c", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe6\xb2\xb9\xe7\xae\xb1\xe5\xae\xb9\xe7\xa7\xaf", "target": "70L", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe5\x88\xb6\xe5\x8a\xa8\xe6\x96\xb9\xe5\xbc\x8f", "target": "\xe9\x80\x9a\xe9\xa3\x8e\xe7\x9b\x98\xe5\xbc\x8f", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe5\xbc\x95\xe6\x93\x8e\xe7\xb1\xbb\xe5\x9e\x8b", "target": "\xe7\x9b\xb4\xe5\x88\x974\xe7\xbc\xb8/\xe5\x8f\x8c\xe9\xa1\xb6\xe7\xbd\xae\xe5\x87\xb8\xe8\xbd\xae\xe8\xbd\xb4", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe6\x9c\x80\xe9\xab\x98\xe6\x97\xb6\xe9\x80\x9f", "target": "210KM/H", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe8\xa1\x8c\xe6\x9d\x8e\xe8\x88\xb1\xe5\xae\xb9\xe7\xa7\xaf", "target": "516L", "label": "relation"}, {"source": "\xe6\x97\xa5\xe4\xba\xa7\xe5\xa4\xa9\xe7\xb1\x81", "relation": "\xe6\x9c\x80\xe5\xa4\xa7\xe5\x8a\x9f\xe7\x8e\x87", "target": "137KW/RPM", "label": "relation"}]}'
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Force
5 |
75 |
76 |
77 |
298 |
299 |
300 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/visualization/server/views.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # @Author: ioriiod0
3 | # @Date: 2017-07-12 13:24:54
4 | # @Last Modified by: ioriiod0
5 | # @Last Modified time: 2017-07-13 23:57:12
6 |
7 | import os
8 | import time
9 |
10 | from flask import request,render_template
11 | from server.app import app
12 | from server import models
13 | from server import mysql
14 |
15 | database=mysql.database()
16 | mydata = ""
17 |
18 | @app.route("/api/v1", methods=["GET"])
19 | def parse():
20 | global mydata
21 | req = request.args
22 | # .args.get('username')
23 | if "car_name" in req:
24 | name = req["car_name"]
25 | mydata = models.execute(database,("car_name", name))
26 | '''
27 | elif "relation" in req:
28 | name = req["relation"]
29 | mydata = models.execute(database,("relation", name))
30 | '''
31 | # return {"resultmsg":"OK","resultno":ERROR_OK},200
32 | # 模板的位置放在templates文件夹下面
33 | return render_template('index.html')
34 |
35 | @app.route("/api/data")
36 | def data():
37 | return mydata
38 |
39 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第一次验收/效果.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第一次验收/效果.PNG
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第二次验收/__init__.py
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/conf.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2017年12月11日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | #数据路径
11 |
12 | import argparse
13 |
14 | def get_args():
15 | parser = argparse.ArgumentParser()
16 | parser.add_argument('-nu','--neo4j_user', help='neo4j的用户名',type=str,default='neo4j')
17 | parser.add_argument('-np','--neo4j_password', help='neo4j的密码',type=str,default='123456')
18 | parser.add_argument('-p','--http_port', help='http端口',type=int,default='8080')
19 | args = parser.parse_args()
20 | return args
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/data/http接口说明.txt:
--------------------------------------------------------------------------------
1 | 1.查询单个实体:
2 | 说明:
3 | 1.deep代表查找的深度,一般不要超过3,最小为1,建议默认为3
4 | 2.edges中的每一列,代表一条边,第一个为起始点,第二个为关系,第三个为终点
5 | 3.当success为false的时候代表没有查找到数据
6 | 举例:
7 | 输入:{"method":'entry','id':1,'jsonrpc':2.0,"params":{'name':'英朗','deep':3}}
8 | 输出:{'method': 'entry', 'id': 1, 'jsonrpc': 2.0, 'result': {'edges': [['前轮驱动', '中文名', '前轮驱动'], ['捷达', '驱动方式', '前轮驱动'], ['别克君越', '品牌', '别克'], ['红旗旗舰', '驱动方式', '前轮驱动'], ['英朗', '驱动方式', '前轮驱动'], ['英朗', '油耗', '7.4L/100KM'], ['一汽', '所属行业', '汽车制造'], ['重锤', '制片地区', '美国'], ['日产天籁', '标准座位数', '5座'], ['雪佛兰景程', '驱动方式', '前轮驱动'], ['斯柯达晶锐', '驱动方式', '前轮驱动'], ['前轮驱动', '性质', '动力分配方式'], ['骐达', '驱动方式', '前轮驱动'], ['威利斯', '国家', '美国'], ['英菲尼迪', '经营范围', '汽车制造'], ['新君威', '驱动方式', '前轮驱动'], ['吉利帝豪', '驱动方式', '前轮驱动'], ['双环汽车', '经营范围', '汽车制造'], ['尚酷', '驱动方式', '前轮驱动'], ['山地自行车', '起源地', '美国'], ['斯柯达速派', '标准座位数', '5座'], ['火鸟', '每集长度', '60'], ['长城', '制片地区', '美国'], ['现代途胜', '驱动方式', '前轮驱动'], ['高尔', '驱动方式', '前轮驱动'], ['福莱尔', '标准座位数', '5座'], ['本田思域', '驱动方式', '前轮驱动'], ['索诺马', '总部', '美国'], ['帕萨特领驭', '驱动方式', '前轮驱动'], ['别克', '总部地点', '美国'], ['陆尊', '品牌', '别克'], ['富康', '驱动方式', '前轮驱动'], ['爱丽丝', '国籍', '美国'], ['菲亚特', '经营范围', '汽车制造'], ['英菲尼迪', '创立地点', '美国'], ['威志', '驱动方式', '前轮驱动'], ['指挥官', '制片地区', '美国'], ['别克', '经营范围', '中高端汽车生产'], ['驱动齿轮', '地区:', '美国'], ['英朗', '引擎类型', '自然吸气涡轮增压'], ['速腾', '驱动方式', '前轮驱动'], ['雷诺', '经营范围', '中高端汽车生产'], ['朗逸', '驱动方式', '前轮驱动'], ['道奇', '国别', '美国'], ['大捷龙', '驱动方式', '前轮驱动'], ['双龙爱腾', '标准座位数', '5座'], ['尊驰', '驱动方式', '前轮驱动'], ['昂克雷', '生产厂商', '别克'], ['大众迈腾', '驱动方式', '前轮驱动'], ['英朗', '行李舱容积', '375L'], ['蒙迪欧致胜', '驱动型式', '前轮驱动'], ['帕诺兹', '经营范围', '汽车制造'], ['盾牌', '制片地区', '美国'], ['雪佛兰', '国别', '美国'], ['安全气囊', '发明国家', '美国'], ['亚伯拉罕·林肯', '国籍', '美国'], ['前轮驱动', '外文名', 'FRONTWHEELDRIVE'], ['赛欧', '驱动方式', '前轮驱动'], ['福特', '经营范围', '中高端汽车生产'], ['福特S-MAX', '驱动方式', '前轮驱动'], ['前轮驱动', '特点', '发动机只驱动一对前轮'], ['蓝瑟·翼神', '驱动方式', '前轮驱动'], ['超人前传', '制片地区', '美国'], ['悦动', '驱动方式', '前轮驱动'], ['颐达', '驱动方式', '前轮驱动'], ['北京吉普', '所属行业', '汽车制造'], ['前轮驱动', '属于', '汽车设计'], ['斯柯达昊锐', '驱动方式', '前轮驱动'], ['雪佛兰科帕奇', '生产厂商', '上海通用'], ['江淮和悦', '驱动方式', '前轮驱动'], ['路虎', '成立时间', '1904年'], ['别克昂科威', '品牌', '别克'], ['吉利', '经营范围', '汽车制造'], ['英朗', '车身重量', '1430'], ['日产骏逸', '驱动方式', '前轮驱动'], ['宝骏乐驰', '驱动方式', '前轮驱动'], ['英朗', '轴距', '2685'], ['贵士', '驱动方式', '前轮驱动'], ['摩根·弗里曼', '国籍', '美国'], ['福特蒙迪欧', '标准座位数', '5座'], ['力矩扳手', '起源于', '美国'], ['别克', '品牌定位', '豪华品牌'], ['宝来', '驱动方式', '前轮驱动'], ['富康', '标准座位数', '5座'], ['猎豹飞腾', '驱动方式', '前轮驱动'], ['英朗', '生产厂商', '上海通用'], ['别克', '公司名称', '别克汽车公司'], ['优派', '国别', '美国'], ['开瑞', '驱动方式', '前轮驱动'], ['英朗', '车型尺寸', '4419*1814*1487'], ['雪佛兰科鲁兹', '驱动方式', '前轮驱动'], ['别克', '成立时间', '1904年'], ['别克', '创始人', '大卫·邓巴·别克'], ['新君威', '生产厂商', '上海通用'], ['别克君威', '品牌', '别克'], ['英朗', '油箱容积', '60'], ['自由舰', '驱动方式', '前轮驱动'], ['海福星', '驱动方式', '前轮驱动'], ['途安', '驱动方式', '前轮驱动'], ['赛影', '标准座位数', '5座'], ['长安之星', '驱动方式', '前轮驱动'], ['英朗', '参考价格', '12.99-18.73'], ['英朗', '标准座位数', '5座'], ['别克君越', '生产厂商', '上海通用'], ['斯巴鲁傲虎', '标准座位数', '5座'], ['别克昂科雷', '所属品牌', '别克'], ['一汽大众', '经营范围', '汽车制造'], ['别克', '公司性质', '汽车制造'], ['福美来', '标准座位数', '5座'], ['守卫者', '国籍', '美国'], ['菱悦', '驱动方式', '前轮驱动'], ['雅阁', '驱动方式', '前轮驱动'], ['威利斯', '马力', '60'], ['英朗', '加速时间', '12.3'], ['长安杰勋', '驱动方式', '前轮驱动'], ['别克', '外文名称', 'BUICK'], ['霍顿', '经营范围', '汽车制造'], ['新佳乐', '驱动方式', '前轮驱动'], ['泰肖恩·普林斯', '国籍', '美国'], ['别克', '公司口号', '心静,思远,志行千里'], ['英朗', '最高时速', '210'], ['英朗', '品牌', '别克'], ['锐欧', '标准座位数', '5座']], 'success': 'true'}}
9 |
10 | 2.查询统计信息:
11 | 说明:total_nums代表当前数据库里有多少个实体,api_nums代表图数据库被调用的次数
12 | 举例:
13 | 输入:{"method":'statistics','id':1,'jsonrpc':2.0}
14 | 输出:{'method': 'statistics', 'id': 1, 'jsonrpc': 2.0, 'result': {'total_nums': 11039, 'success': 'true', 'api_nums': 45}}
15 |
16 | 3.查询实体与实体间的关系:
17 | 说明:
18 | 1.当success为false的时候代表没有查找到数据
19 | 2.edges中的每一列,代表一条边,第一个为起始点,第二个为关系,第三个为终点
20 | 举例:
21 | 输入:{"method":'entry_to_entry','id':1,'jsonrpc':2.0,"params":{'entry1':'英朗','entry2':'前轮驱动'}}
22 | 输出:{'method': 'entry_to_entry', 'id': 1, 'jsonrpc': 2.0, 'result': {'edges': [['英朗', '驱动方式', '前轮驱动']], 'success': 'true'}}
23 |
24 | 4.查询实体与属性的关系:
25 | 说明:
26 | 1.当success为false的时候代表没有查找到数据
27 | 2.edges中的每一列,代表一条边,第一个为起始点,第二个为关系,第三个为终点
28 | 举例:
29 | 输入:{"method":'entry_to_property','id':1,'jsonrpc':2.0,"params":{'entry':'英朗','property':'驱动方式'}}
30 | 输出:{'method': 'entry_to_property', 'id': 1, 'jsonrpc': 2.0, 'result': {'edges': [['英朗', '驱动方式', '前轮驱动']], 'success': 'true'}}
31 | 5.查询当前接口是否可用:
32 | 输入:{"method":'live','id':1,'jsonrpc':2.0}
33 | 输出:{'result': {'success': 'true'}, 'jsonrpc': 2.0, 'method': 'live', 'id': 1}
34 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/data/statistics.txt:
--------------------------------------------------------------------------------
1 | 0
2 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/kg/kg.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2018年1月26日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | import sys
11 | import re
12 | sys.path.append('..')
13 | from conf import get_args
14 | from py2neo import Node,Relationship,size,order,Graph,NodeSelector
15 |
16 | class data(object):
17 | def __init__(self):
18 | self.args=get_args()
19 | self.data_process()
20 |
21 | def data_process(self):
22 | #初始化操作
23 | self.data_init()
24 |
25 | #插入数据
26 | self.insert_datas()
27 |
28 |
29 | def data_init(self):
30 | #连接图数据库
31 | print('开始数据预处理')
32 | self.graph = Graph('127.0.0.1:7474',user=self.args.neo4j_user,password=self.args.neo4j_password)
33 | self.selector=NodeSelector(self.graph)
34 | self.graph.delete_all()
35 |
36 | def insert_datas(self):
37 | print('开始插入数据')
38 | with open('../data/three_tuples.txt','r',encoding='utf-8') as f:
39 | lines,num=f.readlines(),-1
40 | for line in lines:
41 | num+=1
42 | if num%500==0:
43 | print('当前处理进度:{}/{}'.format(lines.index(line),len(lines)))
44 | line=line.strip().split('\t')
45 | if len(line)!=3:
46 | print('insert_datas错误:',line)
47 | continue
48 | self.insert_one_data(line)
49 |
50 |
51 | def insert_one_data(self,line):
52 | if '' in line:
53 | print('insert_one_data错误',line)
54 | return
55 |
56 | start=self.look_and_create(line[0])
57 | for name in self.get_items(line[2]):
58 | end=self.look_and_create(name)
59 | r=Relationship(start,line[1],end,name=line[1])
60 | self.graph.create(r)#当存在时不会创建新的
61 |
62 | #查找节点是否不存,不存在就创建一个
63 | def look_and_create(self,name):
64 | end=self.graph.find_one(label="car_industry",property_key="name",property_value=name)
65 | if end==None:
66 | end=Node('car_industry',name=name)
67 | return end
68 |
69 | def get_items(self,line):
70 | if '{' not in line and '}' not in line:
71 | return [line]
72 | #检查
73 | if '{' not in line or '}' not in line:
74 | print('get_items Error',line)
75 | lines= [w[1:-1] for w in re.findall('{.*?}',line)]
76 | return lines
77 |
78 | if __name__=='__main__':
79 | data=data()
80 |
81 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/kg/kg_test.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2018年1月25日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | from py2neo import Node,Relationship,size,order,Graph,NodeSelector
11 |
12 | #graph = Graph('127.0.0.1:7474',user='neo4j',password='123456')
13 | '''
14 | 常用API
15 | graph.delete_all() 删除所有的节点和关系
16 | graph.data("MATCH (n) RETURN n") 调用Cypher查询语言
17 |
18 | a=Node('Person',name='Mike',age=21,location='广州')
19 | b=Node('Person',name='Bob',age=22,location='上海')
20 | c=Node('Person',name='Alice',age=21,location='北京')
21 | r1=Relationship(a,'KNOWS',b)
22 | r2=Relationship(b,'KNOWS',c)
23 | graph.create(a|b|c|r1|r2)
24 |
25 | selector=NodeSelector(graph)#会找出所有的关系
26 | #persons=selector.select('Person',age=21)
27 | #print(list(persons))
28 | #使用正则表达式进行查找
29 | persons=selector.select('Person').where("_.name =~ 'A.*'")
30 | print(list(persons))
31 | #查找单个节点
32 | persons=selector.select('Person').where("_.name =~ 'A.*'").first()
33 | print(list(persons))
34 |
35 | #查找一个
36 | relations=graph.match_one(rel_type='KNOWS')
37 | print(relations)
38 |
39 | #排序
40 | selector=NodeSelector(graph)#会找出所有的关系
41 | persons=selector.select('Person').order_by('_.age')
42 | print(list(persons))
43 |
44 | #运行Cyper语句
45 | data=graph.run('MATCH (p:Person) RETURN p LIMIT 5')
46 | print(list(data))
47 |
48 | '''
49 | '''
50 | #OGM可以实现一个对象和Node的关联
51 | from py2neo.ogm import GraphObject,Property,RelatedTo
52 |
53 | class Person(GraphObject):
54 | __primarykey__='name'#默认是id,设置主键的好处是push数据的时候不会重复添加
55 | name=Property()
56 | age=Property()
57 | location=Property()
58 | knows=RelatedTo('Person','KNOWS')
59 |
60 | person=Person.select(graph).where(age=20).first()
61 | print(person)
62 | print(person.name)
63 | print(person.location)
64 | person.age=20
65 | print(person.__ogm__.node)
66 | graph.push(person)
67 | print(list(person.knows))
68 |
69 | new_person=Person()
70 | new_person.name='Durant'
71 | new_person.age=28
72 | person.knows.add(new_person)
73 | print(list(person.knows))
74 | graph.push(person)#添加到数据库
75 |
76 | target=Person.select(graph).where(name='Durant').first()
77 | print(list(person.knows))
78 | person.knows.remove(target)#删除节点间的关系
79 | print(list(person.knows))
80 |
81 | graph.push(person)
82 | graph.delete(target)#先删除节点上的关系,才能删除节点
83 |
84 | graph.delete_all()
85 | b=Node('Person',name='Bob',age=22,location='上海')
86 | a=Node('Person',name='Bob2',age=22,location='上海')
87 | c=Node('Person',name='Bob2',age=22,location='上海')
88 | r1=Relationship(a,'KNOWS',b)
89 | graph.create(r1)
90 | r2=Relationship(a,'KNOWS',c)
91 | graph.create(r2)
92 |
93 | c=Node('Person',name='Bob3',age=22,location='上海')
94 | r1=Relationship(a,'KNOWS',c)
95 | graph.create(r1)
96 | r1=Relationship(c,'KNOWS',b)
97 | graph.create(r1)
98 | #graph.push(b)
99 | '''
100 | '''
101 | with open('../data/three_tuples.txt','r',encoding='utf-8') as f:
102 | with open('three_tuples.txt','w',encoding='utf-8') as f2:
103 | for line in f.readlines():
104 | line=line.strip()
105 | line=line.split(':')
106 | try:
107 | f2.write('{}\t{}\t{}\n'.format(line[0],line[1],''.join(line[2:])))
108 | except:
109 | f2.write(''.join(line))
110 |
111 | '''
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/readme.txt:
--------------------------------------------------------------------------------
1 | 1.基于知识图谱的问答系统:
2 | 目前通过前端限定用户通过指定的格式来输入问题,然后通过查询知识图谱的形式来回答
3 |
4 |
5 | 2.本次跟新介绍:
6 | 1.考虑到后续数据量大,将数据库从Mysql替换为Neo4j
7 | 2.新增5个http接口
8 | 3.支持多层查询与存储,考虑到查询的时间消耗,建议不超过3层
9 |
10 | 3.http接口说明:
11 | 详细说明:data/http接口说明.txt
12 | 1.查询单个实体,支持查询深度限制,深度为3时,查询时间为3.5s,深度为4时,查询时间为85s。
13 | 2.查询两个实体间的关系
14 | 3.查询单个实体的某个属性值
15 | 4.统计当前数据库里有多少个实体以及当前数据库被访问多少次
16 | 5.当前接口是否可用
17 |
18 | 4.环境:
19 | python3+Neo4j+flask
20 |
21 | 5.运行:
22 | 1.启动数据库:neo4j console
23 | 2.插入数据:kg/kg.py,数据格式:data/three_tuples.txt
24 | 3.提供web服务:python3 server/run_server.py
25 |
26 | 6.效果:效果.PNG
27 |
28 |
29 | 7.后续改进可能:
30 | 1.通过知识图谱三元组与原始输入的向量之间利用深度学习求相似度,找到最相近的三元组,作为答案,达到准确率的要求后再给出答案
31 | 2.但考虑到在实际使用时,本方案会与所有三元组计算一下相似度,时间消耗较大,因此需要命名实体识别,先找出相关的一部分三元组,再求相似
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/server/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第二次验收/server/.DS_Store
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/server/__init__.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 |
4 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/server/app.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2018年1月9日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | from flask import Flask
11 | app = Flask(__name__)
12 | from server.views import *
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/server/http_test.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2017年10月9日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 | import requests
11 | import json
12 | s = requests
13 | lookup_entry={"method":'entry','id':1,'jsonrpc':2.0,"params":{'name':'英朗','deep':3}}
14 | lookup_statistics={"method":'statistics','id':1,'jsonrpc':2.0}
15 | lookup_entry2entry={"method":'entry_to_entry','id':1,'jsonrpc':2.0,"params":{'entry1':'英朗','entry2':'前轮驱动'}}
16 | lookup_entry2property={"method":'entry_to_property','id':1,'jsonrpc':2.0,"params":{'entry':'英朗','property':'驱动方式'}}
17 |
18 |
19 | while True:
20 | choice=input('你想测试的方法是:\n1.entry\n2.statistics\n3.entry2entry\n4.entry2property\n:')
21 | if choice=='1':
22 | data=lookup_entry
23 | elif choice=='2':
24 | data=lookup_statistics
25 | elif choice=='3':
26 | data=lookup_entry2entry
27 | elif choice=='4':
28 | data=lookup_entry2property
29 | try:
30 | r = s.post('http://192.168.1.245:1111/KnowGraph/v2', json.dumps(data))
31 | print (r.status_code)
32 | print (r.headers['content-type'])
33 | r.encoding = 'utf-8'
34 | print (eval(r.text))
35 | except Exception as e:
36 | print(e)
37 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/server/module.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | import tensorflow as tf
3 | import numpy as np
4 | import os
5 | import time
6 | from threading import Thread
7 | from py2neo import Node,Relationship,size,order,Graph,NodeSelector
8 |
9 | class KnowGraph(object):
10 | def __init__(self,args):
11 | self.args=args
12 | self.graph = Graph('127.0.0.1:7474',user='neo4j',password='123456')
13 | self.write_to_statistics()
14 | #查找单个实体
15 | def lookup_entry(self,client_params,server_param):
16 | #支持设定网络查找的深度
17 | start_time = time.time()
18 | params=client_params["params"]
19 | edges=set()
20 | self.lookup_entry_deep(edges,params,0)
21 | if len(edges)==0:
22 | server_param['result']={"success":'false'}
23 | else:
24 | server_param['result']={'edges':[list(i) for i in edges],"success":'true'}
25 | print('本次查找三元组的数量为:{},耗时:{}s'.format(len(edges),time.time()-start_time))
26 |
27 | #查找两个实体间的关系
28 | def lookup_entry2entry(self,client_params,server_param):
29 | params=client_params["params"]
30 | edges=set()
31 |
32 | #考虑到顺序的问题,所以查了四次
33 | result1=self.graph.data("MATCH (s)-[r]->(e) where s.name='{}' and e.name='{}' RETURN s.name,r.name,e.name".format(params['entry1'],params['entry2']))
34 | result2=self.graph.data("MATCH (s)-[r]->(e) where s.name='{}' and e.name='{}' RETURN s.name,r.name,e.name".format(params['entry2'],params['entry1']))
35 |
36 | if len(result1)==0 and len(result2)==0:
37 | server_param["result"]={"success":'false'}
38 | return
39 | for item in result1:
40 | edges.add((item['s.name'],item['r.name'],item['e.name']))
41 | for item in result2:
42 | edges.add((item['s.name'],item['r.name'],item['e.name']))
43 |
44 | #result=self.graph.data("match (a),(b) where a.name='{}' and b.name='{}' match p = shortestPath((a)-[*..15]-(b))return p".format(params['entry1'],params['entry2']))
45 | server_param["result"]={'edges':[list(i) for i in edges],"success":'true'}
46 |
47 | #查找指定实体的指定属性
48 | def lookup_entry2property(self,client_params,server_param):
49 | params=client_params["params"]
50 | edges=set()
51 | result1=self.graph.data("MATCH (s)-[r]->(e) where s.name='{}' and r.name='{}' RETURN s.name,r.name,e.name".format(params['entry'],params['property']))
52 | result2=self.graph.data("MATCH (e)<-[r]-(s) where e.name='{}' and r.name='{}' RETURN s.name,r.name,e.name".format(params['entry'],params['property']))
53 | if len(result1)==0 and len(result2)==0:
54 | server_param["result"]=[{"success":'false'}]
55 | return
56 | for item in result1:
57 | edges.add((item['s.name'],item['r.name'],item['e.name']))
58 | for item in result2:
59 | edges.add((item['s.name'],item['r.name'],item['e.name']))
60 | server_param["result"]={'edges':[list(i) for i in edges],"success":'true'}
61 |
62 | #查询统计信息
63 | def lookup_statistics(self,client_params,server_param):
64 | result=self.graph.data("MATCH (n) RETURN n")
65 | with open('../data/statistics.txt','r',encoding='utf-8') as f:
66 | api_nums=f.readline().strip()
67 | server_param['result']={'total_nums':len(result),'api_nums':api_nums,"success":'true'}
68 |
69 | #统计API访问次数
70 | def write_to_statistics(self):
71 | with open('../data/statistics.txt','r',encoding='utf-8') as f:
72 | api_nums=int(f.readline().strip())+1
73 | with open('../data/statistics.txt','w',encoding='utf-8') as f:
74 | f.write(str(api_nums)+'\n')
75 |
76 | #限制深度的查找
77 | def lookup_entry_deep(self,edges,params,deep):
78 | #当前查找深度不得等于要求的深度
79 | if deep >= params['deep']:
80 | return
81 | #正向查找
82 | result1=self.graph.data("match (s)-[r]->(e) where s.name='{}' return s.name,r.name,e.name".format(params['name']))
83 | result2=self.graph.data("match (e)<-[r]-(s) where e.name='{}' return s.name,r.name,e.name".format(params['name']))
84 | if len(result1)==0 and len(result2)==0:
85 | return
86 | for item in result1:
87 | edges.add((item['s.name'],item['r.name'],item['e.name']))
88 | if item['s.name'] != item['e.name']:#避免出现:双面胶:中文名:双面胶的死循环
89 | params['name']=item['e.name']
90 | self.lookup_entry_deep(edges,params.copy(),deep+1)
91 |
92 | for item in result2:
93 | edges.add((item['s.name'],item['r.name'],item['e.name']))
94 | if item['s.name'] != item['e.name']:#避免出现:双面胶:中文名:双面胶的死循环
95 | params['name']=item['e.name']
96 | self.lookup_entry_deep(edges,params.copy(),deep+1)
97 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/server/run_server.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | import sys
3 | sys.path.append("..")
4 | from app import app
5 | from conf import *
6 |
7 | if __name__ == '__main__':
8 | args=get_args()
9 | print('\nhttp_host:{},http_port:{}'.format('0.0.0.0',args.http_port))
10 | app.run(debug=True, host='0.0.0.0', port=args.http_port)
11 |
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/server/views.py:
--------------------------------------------------------------------------------
1 | #coding:utf-8
2 | '''
3 | Created on 2018年1月9日
4 |
5 | @author: qiujiahao
6 |
7 | @email:997018209@qq.com
8 |
9 | '''
10 |
11 | from flask import jsonify
12 | from conf import *
13 | from flask import Flask
14 | from flask import request
15 | from server.app import app
16 | import tensorflow as tf
17 | from server.module import KnowGraph
18 | import json
19 |
20 | @app.route('/KnowGraph/v2',methods=["POST"])
21 | def look_up():
22 | kg=KnowGraph(get_args())
23 | client_params=request.get_json(force=True)
24 | server_param={}
25 | if client_params['method'] == 'entry_to_entry':
26 | kg.lookup_entry2entry(client_params,server_param)
27 | elif client_params['method'] == 'entry_to_property':
28 | kg.lookup_entry2property(client_params,server_param)
29 | elif client_params['method'] == 'entry':
30 | kg.lookup_entry(client_params,server_param)
31 | elif client_params['method'] == 'statistics':
32 | kg.lookup_statistics(client_params,server_param)
33 | elif client_params['method'] == 'live':
34 | params={'success':'true'}
35 | server_param['result']=params
36 | server_param['id']=client_params['id']
37 | server_param['jsonrpc']=client_params['jsonrpc']
38 | server_param['method']=client_params['method']
39 | print(server_param)
40 | return json.dumps(server_param, ensure_ascii=False).encode("utf-8")
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/test.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第二次验收/test.py
--------------------------------------------------------------------------------
/汽车行业知识图谱第二次验收/效果.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/汽车行业知识图谱第二次验收/效果.png
--------------------------------------------------------------------------------
/知识图谱学习笔记/笔记.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qiu997018209/KnowledgeGraph/5d91826c2346b9f9f301f99278e98464d79fbb4b/知识图谱学习笔记/笔记.docx
--------------------------------------------------------------------------------