forked from ownthink/KnowledgeGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knowledgegraph.py
60 lines (54 loc) · 1.89 KB
/
knowledgegraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
'''
* Copyright (C) 2017 OwnThink Technologies Inc.
*
* Name : knowledgegraph.py - 知识图谱
* Author : Yener <[email protected]>
* Version : 0.01
* Description : 知识图谱api请求示范
'''
import requests
def mention2entity(mention):
'''
* mention2entity - 提及->实体
* @mention: [in]提及
* 根据提及获取歧义关系
'''
url = 'https://api.ownthink.com/kg/ambiguous?mention={mention}'.format(mention=mention) # 知识图谱API,歧义关系
sess = requests.get(url) # 请求
text = sess.text # 获取返回的数据
entitys = eval(text) # 转为字典类型
return entitys
def entity2knowledge(entity):
'''
* entity2knowledge - 实体->知识
* @entity: [in]实体名
* 根据实体获取实体知识
'''
url = 'https://api.ownthink.com/kg/knowledge?entity={entity}'.format(entity=entity) # 知识图谱API,实体知识
sess = requests.get(url) # 请求
text = sess.text # 获取返回的数据
knowledge = eval(text) # 转为字典类型
return knowledge
def entity_attribute2value(entity, attribute):
'''
* entity_attribute2value - 实体&属性->属性值
* @entity: [in]实体名
* @attribute: [in]属性名
* 根据实体、属性获取属性值
'''
url = 'https://api.ownthink.com/kg/eav?entity={entity}&attribute={attribute}'.format(entity=entity, attribute=attribute) # 知识图谱API,属性值
sess = requests.get(url) # 请求
text = sess.text # 获取返回的数据
values = eval(text) # 转为字典类型
return values
if __name__=='__main__':
mention = '红楼梦'
entitys = mention2entity(mention) # 根据提及获取歧义关系
print(entitys)
entity = '刘德华'
knowledge = entity2knowledge(entity) # 根据实体获取知识
print(knowledge)
entity = '苹果'
attribute = '颜色'
values = entity_attribute2value(entity, attribute) # 根据实体、属性获取属性值
print(values)