{"id":18746405,"url":"https://github.com/ownthink/kg-view","last_synced_at":"2025-11-23T20:30:16.627Z","repository":{"id":60344676,"uuid":"142519457","full_name":"ownthink/KG-View","owner":"ownthink","description":"知识图谱可视化展示","archived":false,"fork":false,"pushed_at":"2022-04-19T09:28:08.000Z","size":10,"stargazers_count":332,"open_issues_count":5,"forks_count":111,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-12-28T20:29:14.159Z","etag":null,"topics":["knowledgegraph"],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ownthink.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-27T02:47:52.000Z","updated_at":"2024-12-26T09:25:22.000Z","dependencies_parsed_at":"2022-09-28T09:50:17.872Z","dependency_job_id":null,"html_url":"https://github.com/ownthink/KG-View","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ownthink%2FKG-View","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ownthink%2FKG-View/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ownthink%2FKG-View/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ownthink%2FKG-View/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ownthink","download_url":"https://codeload.github.com/ownthink/KG-View/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239628781,"owners_count":19671157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["knowledgegraph"],"created_at":"2024-11-07T16:25:23.592Z","updated_at":"2025-11-23T20:30:16.585Z","avatar_url":"https://github.com/ownthink.png","language":"HTML","readme":"# 知识图谱可视化\n\n知识图谱是将复杂的信息通过计算处理成能够结构化表示的知识，所表示的知识可以通过图形绘制而展现出来，为人们的学习提供有价值的参考，为信息的检索提供便利。本文将利用思知知识图谱API接口对知识图谱进行可视化展示。（API请求说明见：https://www.ownthink.com/  ）\n\n![head](https://pic1.zhimg.com/80/v2-681d10fdf19e3f249b24bdd7805144e6_hd.jpg)\n\n### 环境准备\n\nPython3安装requests库：pip3 install requests\n\n\n\n### 数据获取方式\n\n目前知识图谱有许多开放的API接口，为知识图谱可视化提供了大大的便利，这里以思知API接口对知识图谱可视化进行简单示范。\n\n思知请求API：\n\n```shell\nhttps://api.ownthink.com/kg/knowledge?entity=刘德华\n```\n\n正确返回的数据格式为：\n\n```shell\n{\n    \"entity\": \"entity_name\",            // 实体名称\n    \"desc\": \"entity_desc\",              // 实体描述\n    \"avp\": [                            // AVP列表\n        [\n            \"entity_attribute1\",        // 属性1\n            \"entity_value1\"             // 值\n        ],\n        [\n            \"entity_attribute2\",        // 属性2\n            \"entity_value2\"             // 值\n        ]\n    ],\n    \"tag\": [                            // 标签列表\n        \"tag1\",                         // 标签1\n        \"tag2\"                          // 标签2\n    ]\n}\n```\n\n利用Python请求api接口，获取可视化数据节点：\n\n```python\nimport os\nimport sys\nimport requests\n\ndef KG_View(entity):\n\turl = 'https://api.ownthink.com/kg/knowledge?entity=%s'%entity      # 知识图谱API\n\t\n\tsess = requests.get(url) # 请求\n\ttext = sess.text # 获取返回的数据\n\n\tresponse = eval(text) # 转为字典类型\n\tknowledge = response['data']\n\t\n\tnodes = []\n\tfor avp in knowledge['avp']:\n\t\tif avp[1] == knowledge['entity']:\n\t\t\tcontinue\n\t\tnode = {'source': knowledge['entity'], 'target': avp[1], 'type': \"resolved\", 'rela':avp[0]}\n\t\tnodes.append(node)\n\t\t\n\tfor node in nodes:\n\t\tnode = str(node)\n\t\tnode = node.replace(\"'type'\", 'type').replace(\"'source'\", 'source').replace(\"'target'\", 'target')\n\t\tprint(node+',')\n\t\nif __name__=='__main__':\n\tKG_View('图灵')\n```\n\n这里将其保存为kgview.py 并运行\n\n```shell\n[Yener@localhost ~]$ python3 kgview.py \n```\n\n打印出来的节点数据：\n\n```python\n{type: 'resolved', target: 'Alan Mathison Turing', source: '艾伦·麦席森·图灵', 'rela': '外文名'},\n{type: 'resolved', target: '英国', source: '艾伦·麦席森·图灵', 'rela': '国籍'},\n{type: 'resolved', target: '英国伦敦', source: '艾伦·麦席森·图灵', 'rela': '出生地'},\n{type: 'resolved', target: '1912年6月23日', source: '艾伦·麦席森·图灵', 'rela': '出生日期'},\n{type: 'resolved', target: '1954年6月7日', source: '艾伦·麦席森·图灵', 'rela': '逝世日期'},\n{type: 'resolved', target: '数学家，逻辑学家，密码学家', source: '艾伦·麦席森·图灵', 'rela': '职业'},\n{type: 'resolved', target: '剑桥大学国王学院，普林斯顿大学', source: '艾伦·麦席森·图灵', 'rela': '毕业院校'},\n{type: 'resolved', target: '“计算机科学之父”', source: '艾伦·麦席森·图灵', 'rela': '主要成就'},\n{type: 'resolved', target: '提出“图灵测试”概念', source: '艾伦·麦席森·图灵', 'rela': '主要成就'},\n{type: 'resolved', target: '人工智能', source: '艾伦·麦席森·图灵', 'rela': '主要成就'},\n{type: 'resolved', target: '破解德国的著名密码系统Enigma', source: '艾伦·麦席森·图灵', 'rela': '主要成就'},\n{type: 'resolved', target: '《论数字计算在决断难题中的应用》', source: '艾伦·麦席森·图灵', 'rela': '代表作品'},\n{type: 'resolved', target: '《机器能思考吗？》', source: '艾伦·麦席森·图灵', 'rela': '代表作品'},\n```\n\n\n\n### 数据可视化\n\n利用d3js实现可视化展示：\n\n将前面打印出来的数据复制到以下的html代码的links中，如下所示。html全部代码见文末GitHub，保存后直接用浏览器打开即可对数据进行可视化绘制。\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003cmeta charset=\"utf-8\"\u003e\n\u003cstyle\u003e.link {  fill: none;  stroke: #666;  stroke-width: 1.5px;}#licensing {  fill: green;}.link.licensing {  stroke: green;}.link.resolved {  stroke-dasharray: 0,2 1;}circle {  fill: #ccc;  stroke: #333;  stroke-width: 1.5px;}text {  font: 12px Microsoft YaHei;  pointer-events: none;  text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;}.linetext {    font-size: 12px Microsoft YaHei;}\u003c/style\u003e\n\u003cbody\u003e\n\u003cscript src=\"https://d3js.org/d3.v3.min.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n\nvar links = \n[\n{source: '艾伦·麦席森·图灵', target: 'Alan Mathison Turing', 'rela': '外文名', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '英国', 'rela': '国籍', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '英国伦敦', 'rela': '出生地', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '1912年6月23日', 'rela': '出生日期', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '1954年6月7日', 'rela': '逝世日期', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '数学家，逻辑学家，密码学家', 'rela': '职业', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '剑桥大学国王学院，普林斯顿大学', 'rela': '毕业院校', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '“计算机科学之父”', 'rela': '主要成就', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '提出“图灵测试”概念', 'rela': '主要成就', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '人工智能', 'rela': '主要成就', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '破解德国的著名密码系统Enigma', 'rela': '主要成就', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '《论数字计算在决断难题中的应用》', 'rela': '代表作品', type: 'resolved'},\n{source: '艾伦·麦席森·图灵', target: '《机器能思考吗？》', 'rela': '代表作品', type: 'resolved'},\n];\n\nvar nodes = {};\n\nlinks.forEach(function(link)\n{\n  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});\n  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});\n});\n\n```\n\n\n\n\n\nGitHub：https://github.com/ownthink/KG-View\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fownthink%2Fkg-view","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fownthink%2Fkg-view","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fownthink%2Fkg-view/lists"}