-
Notifications
You must be signed in to change notification settings - Fork 18
/
generateOntology.js
308 lines (285 loc) · 13.7 KB
/
generateOntology.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* --------------------------------------------------------------------------------------
* Diffbot Knowledge Graph Ontology Generator (Author: @jeromechoo)
* --------------------------------------------------------------------------------------
* - Sources published ontology from https://kg.diffbot.com/kg/ontology
* and builds markdown files (which are then rendered into HTML by Docusaurus).
* - Running this script is not necessary with each production build of docs.diffbot.com,
* but is highly recommended to ensure the latest ontology is available.
*
* ADDING A NEW ENTITY?
* - Note that not all entities are displayed. Supported entities in entityTypes var.
* - After adding a new supported entity, add a link to this entity in sidebars.json
*
*/
let fs = require("fs");
let { render } = require("mustache");
const axios = require("axios");
let args = process.argv.slice(2);
const DIFFBOTTOKEN = args[0] ? args[0] : "";
// Template helpers
const templateHelpers = {
lowercase: (text) => {
return (text, render) => {
return render(text).toLowerCase();
}
}
}
// Supported entity types and props
var entityTypes = {
DiffbotEntity: {
title: "All",
id: "kg-ont-diffbotentity",
description: "All Knowledge Graph entities will include the fields below, which are often generalized, linking, or metadata attributes common to all records. \n\nSee the left navigation panel for fields and attributes of specific entities.",
helpers: templateHelpers
},
Article: {
title: "Article",
id: "kg-ont-article",
description: "The article entity type encompasses news, blog posts, and article content known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:Article title:"The web as a database: The biggest knowledge graph ever"'
},
Organization: {
title: "Organization",
id: "kg-ont-organization",
description: "The organization entity type encompasses corporations, local businesses, non-profits, and other organizations known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:Organization name:"IBM"'
},
Person: {
title: "Person",
id: "kg-ont-person",
description: "The person entity type encompasses all people known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:Person name:"Tom Wheeler" revSortBy:importance'
},
Place: {
title: "Place",
id: "kg-ont-place",
description: "The place entity type encompasses cities, landmarks, countries, administrative areas, and other locations known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:Place revSortBy:importance'
},
CreativeWork: {
title: "CreativeWork",
id: "kg-ont-creativework",
description: "The Creative Work entity type encompasses movies, tv shows, musicals, scripts, and other creative works known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:CreativeWork name:"American Masters" revSortBy:importance'
},
Product: {
title: "Product",
id: "kg-ont-product",
description: "The product entity type encompasses retail products found throughout the web known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:Product name:"Gold Down Alternative Sleeping Pillows"'
},
Image: {
title: "Image",
id: "kg-ont-image",
description: "The Image entity type encompasses images found throughout the web and known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:Image title:"a visualization of the knowledge graph"'
},
Video: {
title: "Video",
id: "kg-ont-video",
description: "The Video entity type encompasses videos found throughout the web and known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:Video title:"Deconstructing Data Complexity and the Case for AI-Driven Smart Data Discovery"'
},
Event: {
title: "Event",
id: "kg-ont-event",
description: "The Event entity type encompasses webinars, meet ups, conferences, and other events known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:Event name:"Talent Connect"'
},
Discussion: {
title: "Discussion",
id: "kg-ont-discussion",
description: "The Discussion entity type encompasses forum and other similar discussions found throughout the web and known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers
},
Job: {
title: "Job",
id: "kg-ont-job",
description: "The Job entity type represents unique roles of employment found throughout the web and known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers
},
JobPost: {
title: "Job Post",
id: "kg-ont-JobPost",
description: "The Job Post entity type encompasses all job postings found throughout the web and known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers
},
AdministrativeArea: {
title: "AdministrativeArea",
id: "kg-ont-administrativearea",
description: "A sub-type of [Place](doc:place), the Administrative Area entity type encompasses all cities, regions, counties, sub-regions, provinces, and countries known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
helpers: templateHelpers,
exampleRecord: 'type:AdministrativeArea name:"California"'
},
// ROLE ONTOLOGY IS MISSING
// Role: {
// title: "Role",
// id: "kg-ont-role",
// description: "The Role entity type encompasses all roles known to the Knowledge Graph. \n\nNote that fields are not guaranteed to exist in every entity record.",
// helpers: templateHelpers
// },
};
// Translates technical content type definitions to layman descriptions & examples
const typeS = {
DDateTime: {
name: "DateTime",
example:
"{'str': 'd2021-01-12T20:14:38', 'precision': 4, 'timestamp': 1610482478000}",
},
};
const buildFieldExample = (fieldName, fieldEntity, ontDocs, exampleRecord) => {
// console.log("Field Name: ", fieldName);
// console.log(fieldEntity);
// console.log(ontDocs);
// console.log("Example Record: ", exampleRecord);
// Truncate example records
if (exampleRecord && exampleRecord[fieldName]) {
exampleRecord[fieldName] = Array.isArray(exampleRecord[fieldName]) ? exampleRecord[fieldName].slice(0, 3) : exampleRecord[fieldName];
}
// Build object/list examples for each field
let example = {};
// String & Intangible/Misc/DegreeEntity Types
if (
fieldEntity["fields"][fieldName]["type"] === "String" ||
(fieldEntity["fields"][fieldName]["leType"] && fieldEntity["fields"][fieldName]["leType"][0] === "Intangible") ||
(fieldEntity["fields"][fieldName]["leType"] && fieldEntity["fields"][fieldName]["leType"][0] === "Miscellaneous") ||
(fieldEntity["fields"][fieldName]["leType"] && fieldEntity["fields"][fieldName]["leType"][0] === "DegreeEntity")
) {
example = exampleRecord && exampleRecord[fieldName] ? exampleRecord[fieldName] : "";
}
// Numerical Type
else if (
fieldEntity["fields"][fieldName]["type"] === "Short" ||
fieldEntity["fields"][fieldName]["type"] === "Long" ||
fieldEntity["fields"][fieldName]["type"] === "Double" ||
fieldEntity["fields"][fieldName]["type"] === "Integer")
{
example = exampleRecord && exampleRecord[fieldName] ? exampleRecord[fieldName] : 0;
}
// Boolean Type
else if (fieldEntity["fields"][fieldName]["type"] === "Boolean") {
example = false;
}
// Composite Type
else if (fieldEntity["fields"][fieldName]["isComposite"] === true) {
let compositeField = ontDocs["composites"][fieldEntity["fields"][fieldName]["type"]];
if (compositeField) {
let compositeSubFieldNames = Object.keys(compositeField["fields"]);
if (exampleRecord && exampleRecord[fieldName]) {
example = exampleRecord[fieldName];
}
else {
if (fieldEntity["fields"][fieldName]["isList"] === true) {
example = [{}];
}
compositeSubFieldNames.forEach(subFieldName => {
if (fieldEntity["fields"][fieldName]["isList"] === true) {
example[0][subFieldName] = buildFieldExample(subFieldName, compositeField, ontDocs, exampleRecord && exampleRecord[fieldName] ? exampleRecord[fieldName] : false);
}
else {
example[subFieldName] = buildFieldExample(subFieldName, compositeField, ontDocs, exampleRecord && exampleRecord[fieldName] ? exampleRecord[fieldName] : false);
}
})
}
}
else {
console.error(`WARNING: The definition for composite field ${fieldEntity["fields"][fieldName]["type"]} does not exist in the ontology`)
}
}
// Linked Entity Type
else if (
fieldEntity["fields"][fieldName]["type"] === "LinkedEntity"
) {
// The call stack becomes insane here if we try to build this example with Linked Entities, so we'll just set string references if example doesn't exist
if (exampleRecord && exampleRecord[fieldName]) {
example = exampleRecord[fieldName];
}
else {
let linkedentityTypes = fieldEntity["fields"][fieldName]["leType"];
example = `[See ${linkedentityTypes.join(' or ')}]`;
}
}
// List Type
else if (fieldEntity["fields"][fieldName]["isList"] === true) {
if (exampleRecord && exampleRecord[fieldName] && exampleRecord[fieldName][0]) {
example = exampleRecord[fieldName].slice(0,3);
}
else {
example = [];
}
}
// Other/Non-Declared Type
else {
example = exampleRecord && exampleRecord[fieldName] ? exampleRecord[fieldName] : "";
}
return example;
};
// Transforms ont_docs to Mustache consumable JSON
const ontTransform = (ontDocsEntity, ontDocs) => {
// Grab starting model
let templateModel = entityTypes[ontDocsEntity.name];
// Add fields to the model (and transform to array)
let fieldNames = Object.keys(ontDocsEntity["fields"]).sort();
let fields = fieldNames.map(fieldName => {
if (!ontDocsEntity["fields"][fieldName]["example"]) {
ontDocsEntity["fields"][fieldName]["example"] = {}
}
ontDocsEntity["fields"][fieldName]["example"][fieldName] = buildFieldExample(fieldName, ontDocsEntity, ontDocs, templateModel['exampleRecord']);
ontDocsEntity["fields"][fieldName]["example"] = JSON.stringify(ontDocsEntity["fields"][fieldName]["example"], null, "\t");
return ontDocsEntity["fields"][fieldName]
});
templateModel["fields"] = fields;
return templateModel;
};
// Gets example records (if available) from the KG
const getExamples = (entityTypes) => {
let entities = Object.keys(entityTypes);
let promises = [];
for (entityIndex in entities) {
let entityTemplate = entityTypes[entities[entityIndex]];
if (entityTemplate['exampleRecord']) {
promises.push(axios.get(`https://kg.diffbot.com/kg/dql_endpoint?type=query&token=${DIFFBOTTOKEN}&size=1&query=${encodeURIComponent(entityTemplate['exampleRecord'])}`).then((response) => {
let example = {}
example[entityTemplate.title] = response.data.data[0];
return example;
}));
}
}
return Promise.all(promises);
};
getExamples(entityTypes)
.then((examples) => {
for (exampleIndex in examples) {
let entityType = Object.keys(examples[exampleIndex])[0];
entityTypes[entityType]['exampleRecord'] = examples[exampleIndex][entityType];
}
return axios.get('https://kg.diffbot.com/kg/ontology')
})
.then((response) => {
var ontology = response.data;
// Get all supported entity types and loop
var supportedEntities = Object.keys(entityTypes);
for (supportedEntityIndex in supportedEntities) {
// Get Template Model
let entityModel = ontTransform(ontology['types'][supportedEntities[supportedEntityIndex]], ontology);
// Get Template
let template = fs.readFileSync("./static/md/entity-ontology.md").toString();
// Render Template
let output = render(template, entityModel);
console.log(`Rendered ${entityModel.title} Ontology! Generating Markdown... /docs/${entityModel.id}.md`);
fs.writeFileSync(`../docs/${entityModel.id}.md`, output);
}
})
.catch((error) => {
console.log(error);
})