wiki-entity 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/lib/index.js +129 -126
- package/lib/request.js +37 -19
- package/lib/simpleEntity/convertToSimpleEntity.js +53 -53
- package/lib/simpleEntity/getEntityCountry.js +25 -25
- package/lib/simpleEntity/getEntityData.js +27 -27
- package/lib/simpleEntity/getEntityInstanceType.js +32 -32
- package/lib/simpleEntity/getEntityType.js +62 -62
- package/lib/simpleEntity/getEntityTypeByExtract.js +28 -28
- package/lib/simpleEntity/index.js +7 -7
- package/lib/simpleEntity/simpleEntity.js +20 -20
- package/lib/types.js +13 -13
- package/lib/utils.js +15 -15
- package/lib/wikidata/api.js +106 -106
- package/lib/wikidata/get_entity_types.js +60 -60
- package/lib/wikidata/index.js +112 -112
- package/lib/wikidata/simplify_claims.js +100 -100
- package/lib/wikidata/simplify_entity.js +76 -76
- package/lib/wikipedia/api.js +146 -146
- package/package.json +1 -1
- package/types/index.d.ts +9 -8
- package/types/request.d.ts +5 -3
- package/types/simpleEntity/convertToSimpleEntity.d.ts +6 -6
- package/types/simpleEntity/getEntityCountry.d.ts +2 -2
- package/types/simpleEntity/getEntityData.d.ts +3 -3
- package/types/simpleEntity/getEntityInstanceType.d.ts +3 -3
- package/types/simpleEntity/getEntityType.d.ts +3 -3
- package/types/simpleEntity/getEntityTypeByExtract.d.ts +2 -2
- package/types/simpleEntity/index.d.ts +2 -2
- package/types/simpleEntity/simpleEntity.d.ts +29 -29
- package/types/types.d.ts +68 -68
- package/types/utils.d.ts +8 -8
- package/types/wikidata/api.d.ts +4 -4
- package/types/wikidata/get_entity_types.d.ts +1 -1
- package/types/wikidata/index.d.ts +3 -3
- package/types/wikidata/simplify_claims.d.ts +4 -4
- package/types/wikidata/simplify_entity.d.ts +21 -21
- package/types/wikipedia/api.d.ts +31 -31
package/README.md
CHANGED
|
@@ -14,6 +14,24 @@ getEntities({ language: 'en', titles: ['Europe'] }).then(entities => {});
|
|
|
14
14
|
getEntities({ language: 'en', ids: ['Q46'] }).then(entities => {});
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
+
## User-Agent
|
|
18
|
+
|
|
19
|
+
[Wikimedia's User-Agent policy](https://meta.wikimedia.org/wiki/User-Agent_policy) requires every client to send an identifying `User-Agent` header. Generic UAs are rate-limited or blocked (HTTP `429`).
|
|
20
|
+
|
|
21
|
+
Since `0.7.0`, `wiki-entity` sends a default UA built from `package.json`, but you should override it with your app's name and a contact URL/email:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { setUserAgent } from "wiki-entity";
|
|
25
|
+
|
|
26
|
+
setUserAgent("MyApp/1.0 (https://myapp.example; contact@myapp.example)");
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Or set it via environment variable:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
WIKI_ENTITY_USER_AGENT="MyApp/1.0 (https://myapp.example; contact@myapp.example)"
|
|
33
|
+
```
|
|
34
|
+
|
|
17
35
|
## API
|
|
18
36
|
|
|
19
37
|
### getEntities(params): Promise<WikiEntity[]>
|
package/lib/index.js
CHANGED
|
@@ -1,126 +1,129 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.mapRedirects = exports.getEntities = exports.WikipediaApi = exports.simplifyEntity = void 0;
|
|
23
|
-
const wikidata_1 = require("./wikidata");
|
|
24
|
-
const api_1 = require("./wikipedia/api");
|
|
25
|
-
Object.defineProperty(exports, "WikipediaApi", { enumerable: true, get: function () { return api_1.Api; } });
|
|
26
|
-
const utils_1 = require("./utils");
|
|
27
|
-
const get_entity_types_1 = require("./wikidata/get_entity_types");
|
|
28
|
-
var simplify_entity_1 = require("./wikidata/simplify_entity");
|
|
29
|
-
Object.defineProperty(exports, "simplifyEntity", { enumerable: true, get: function () { return simplify_entity_1.simplifyEntity; } });
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
function
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
if (
|
|
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
|
-
redirects
|
|
111
|
-
|
|
112
|
-
.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.mapRedirects = exports.getEntities = exports.WikipediaApi = exports.getUserAgent = exports.setUserAgent = exports.simplifyEntity = void 0;
|
|
23
|
+
const wikidata_1 = require("./wikidata");
|
|
24
|
+
const api_1 = require("./wikipedia/api");
|
|
25
|
+
Object.defineProperty(exports, "WikipediaApi", { enumerable: true, get: function () { return api_1.Api; } });
|
|
26
|
+
const utils_1 = require("./utils");
|
|
27
|
+
const get_entity_types_1 = require("./wikidata/get_entity_types");
|
|
28
|
+
var simplify_entity_1 = require("./wikidata/simplify_entity");
|
|
29
|
+
Object.defineProperty(exports, "simplifyEntity", { enumerable: true, get: function () { return simplify_entity_1.simplifyEntity; } });
|
|
30
|
+
var request_1 = require("./request");
|
|
31
|
+
Object.defineProperty(exports, "setUserAgent", { enumerable: true, get: function () { return request_1.setUserAgent; } });
|
|
32
|
+
Object.defineProperty(exports, "getUserAgent", { enumerable: true, get: function () { return request_1.getUserAgent; } });
|
|
33
|
+
__exportStar(require("./simpleEntity"), exports);
|
|
34
|
+
__exportStar(require("./types"), exports);
|
|
35
|
+
function getEntities(params) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
const lang = params.language || "en";
|
|
38
|
+
const entities = yield (0, wikidata_1.getEntities)(params);
|
|
39
|
+
const ids = Object.keys(entities).filter((id) => (0, utils_1.isValidWikiId)(id));
|
|
40
|
+
if (ids.length === 0)
|
|
41
|
+
return [];
|
|
42
|
+
const wikiApi = new api_1.Api({}, params.httpTimeout);
|
|
43
|
+
if (params.extract)
|
|
44
|
+
wikiApi.extract(params.extract);
|
|
45
|
+
if (params.redirects)
|
|
46
|
+
wikiApi.redirects();
|
|
47
|
+
if (params.categories)
|
|
48
|
+
wikiApi.categories();
|
|
49
|
+
const useWikiApi = params.extract ||
|
|
50
|
+
params.redirects ||
|
|
51
|
+
params.categories ||
|
|
52
|
+
params.wikiPageId !== false;
|
|
53
|
+
const tasks = [];
|
|
54
|
+
if (useWikiApi && entities[ids[0]] && entities[ids[0]].sitelinks) {
|
|
55
|
+
const titleIds = ids.reduce((prev, id) => {
|
|
56
|
+
if (entities[id] &&
|
|
57
|
+
entities[id].sitelinks &&
|
|
58
|
+
entities[id].sitelinks[lang]) {
|
|
59
|
+
prev[entities[id].sitelinks[lang]] = id;
|
|
60
|
+
}
|
|
61
|
+
return prev;
|
|
62
|
+
}, {});
|
|
63
|
+
tasks.push(wikiApi
|
|
64
|
+
.query(lang, {
|
|
65
|
+
titles: ids
|
|
66
|
+
.map((id) => (entities[id] &&
|
|
67
|
+
entities[id].sitelinks &&
|
|
68
|
+
entities[id].sitelinks[lang]) ||
|
|
69
|
+
null)
|
|
70
|
+
.filter((item) => !!item)
|
|
71
|
+
.join("|")
|
|
72
|
+
})
|
|
73
|
+
.then((apiResults) => apiResults.forEach((result) => {
|
|
74
|
+
const entity = entities[titleIds[result.title]];
|
|
75
|
+
entity.pageid = result.pageid;
|
|
76
|
+
if (params.extract)
|
|
77
|
+
entity.extract = result.extract;
|
|
78
|
+
if (params.redirects)
|
|
79
|
+
entity.redirects = result.redirects;
|
|
80
|
+
if (params.categories)
|
|
81
|
+
entity.categories = result.categories;
|
|
82
|
+
})));
|
|
83
|
+
}
|
|
84
|
+
if (params.types === true || Array.isArray(params.types)) {
|
|
85
|
+
const prefixes = Array.isArray(params.types)
|
|
86
|
+
? params.types
|
|
87
|
+
: null;
|
|
88
|
+
ids.forEach((id) => {
|
|
89
|
+
if (entities[id] && entities[id].sitelinks) {
|
|
90
|
+
const enName = entities[id].sitelinks["en"];
|
|
91
|
+
if (enName) {
|
|
92
|
+
tasks.push((0, get_entity_types_1.getEntityTypesByName)(enName, prefixes).then((types) => {
|
|
93
|
+
entities[id].types = types;
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
yield Promise.all(tasks);
|
|
100
|
+
return Object.keys(entities)
|
|
101
|
+
.map((id) => entities[id])
|
|
102
|
+
.filter((it) => !!it);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
exports.getEntities = getEntities;
|
|
106
|
+
function mapRedirects(titles, lang) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const wikiApi = new api_1.Api();
|
|
109
|
+
return wikiApi
|
|
110
|
+
.redirects()
|
|
111
|
+
.query(lang, {
|
|
112
|
+
titles: titles.map((it) => it.replace(/\s+/g, "_")).join("|"),
|
|
113
|
+
redirects: "yes"
|
|
114
|
+
})
|
|
115
|
+
.then((apiResults) => {
|
|
116
|
+
const result = {};
|
|
117
|
+
apiResults.forEach((r) => {
|
|
118
|
+
var _a;
|
|
119
|
+
if ((_a = r.redirects) === null || _a === void 0 ? void 0 : _a.length) {
|
|
120
|
+
const it = r.redirects.find((it) => titles.includes(it));
|
|
121
|
+
if (it && it !== r.title)
|
|
122
|
+
result[it] = r.title;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return result;
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
exports.mapRedirects = mapRedirects;
|
package/lib/request.js
CHANGED
|
@@ -1,19 +1,37 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getUserAgent = exports.setUserAgent = void 0;
|
|
13
|
+
const axios_1 = require("axios");
|
|
14
|
+
const pkg = require("../package.json");
|
|
15
|
+
const DEFAULT_USER_AGENT = process.env.WIKI_ENTITY_USER_AGENT ||
|
|
16
|
+
`${pkg.name}/${pkg.version} (${pkg.homepage ||
|
|
17
|
+
(pkg.bugs && pkg.bugs.url) ||
|
|
18
|
+
"https://github.com/entitizer/wiki-entity"})`;
|
|
19
|
+
let userAgent = DEFAULT_USER_AGENT;
|
|
20
|
+
function setUserAgent(ua) {
|
|
21
|
+
if (!ua || typeof ua !== "string") {
|
|
22
|
+
throw new Error("User-Agent must be a non-empty string");
|
|
23
|
+
}
|
|
24
|
+
userAgent = ua;
|
|
25
|
+
}
|
|
26
|
+
exports.setUserAgent = setUserAgent;
|
|
27
|
+
function getUserAgent() {
|
|
28
|
+
return userAgent;
|
|
29
|
+
}
|
|
30
|
+
exports.getUserAgent = getUserAgent;
|
|
31
|
+
function request(url, options) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
const response = yield (0, axios_1.default)(url, Object.assign(Object.assign({ method: "GET", responseType: "json", timeout: 15 * 1000 }, options), { headers: Object.assign({ "User-Agent": userAgent, "Accept-Encoding": "gzip" }, (options.headers || {})) }));
|
|
34
|
+
return response.data;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
exports.default = request;
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertToSimpleEntity = void 0;
|
|
4
|
-
const getEntityType_1 = require("./getEntityType");
|
|
5
|
-
const getEntityInstanceType_1 = require("./getEntityInstanceType");
|
|
6
|
-
const getEntityTypeByExtract_1 = require("./getEntityTypeByExtract");
|
|
7
|
-
const getEntityData_1 = require("./getEntityData");
|
|
8
|
-
const getEntityCountry_1 = require("./getEntityCountry");
|
|
9
|
-
const utils_1 = require("../utils");
|
|
10
|
-
function convertToSimpleEntity(wikiEntity, lang, options) {
|
|
11
|
-
options = options || {};
|
|
12
|
-
const entity = {};
|
|
13
|
-
entity.lang = lang.toLowerCase();
|
|
14
|
-
entity.wikiDataId = wikiEntity.id;
|
|
15
|
-
entity.name = wikiEntity.label;
|
|
16
|
-
entity.description = wikiEntity.description;
|
|
17
|
-
entity.wikiPageId = wikiEntity.pageid;
|
|
18
|
-
entity.about = wikiEntity.extract;
|
|
19
|
-
if (wikiEntity.redirectsToId)
|
|
20
|
-
entity.redirectsToId = wikiEntity.redirectsToId;
|
|
21
|
-
if (wikiEntity.redirectsFromId)
|
|
22
|
-
entity.redirectsFromId = wikiEntity.redirectsFromId;
|
|
23
|
-
if (wikiEntity.types) {
|
|
24
|
-
entity.types = (0, utils_1.uniq)(wikiEntity.types.filter((item) => !/:(Thing|Agent)$/.test(item)));
|
|
25
|
-
}
|
|
26
|
-
entity.type = (0, getEntityType_1.getEntityType)(wikiEntity);
|
|
27
|
-
if (!entity.type) {
|
|
28
|
-
entity.type = (0, getEntityInstanceType_1.getEntityType)(wikiEntity);
|
|
29
|
-
if (!entity.type && entity.about && !entity.types) {
|
|
30
|
-
entity.type = (0, getEntityTypeByExtract_1.getEntityTypeByExtract)(entity.about, lang);
|
|
31
|
-
}
|
|
32
|
-
if (!entity.type && options.defaultType) {
|
|
33
|
-
entity.type = options.defaultType;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (wikiEntity.sitelinks) {
|
|
37
|
-
entity.wikiPageTitle = wikiEntity.sitelinks[lang];
|
|
38
|
-
}
|
|
39
|
-
entity.categories = createCategories(wikiEntity);
|
|
40
|
-
if (wikiEntity.claims) {
|
|
41
|
-
const ids = Object.keys(wikiEntity.claims);
|
|
42
|
-
if (ids.length) {
|
|
43
|
-
entity.data = (0, getEntityData_1.getEntityData)(wikiEntity);
|
|
44
|
-
}
|
|
45
|
-
entity.countryCodes = (0, getEntityCountry_1.getEntityCountryCode)(wikiEntity);
|
|
46
|
-
}
|
|
47
|
-
return entity;
|
|
48
|
-
}
|
|
49
|
-
exports.convertToSimpleEntity = convertToSimpleEntity;
|
|
50
|
-
function createCategories(entity) {
|
|
51
|
-
let categories = entity.categories || [];
|
|
52
|
-
return (0, utils_1.uniq)(categories);
|
|
53
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertToSimpleEntity = void 0;
|
|
4
|
+
const getEntityType_1 = require("./getEntityType");
|
|
5
|
+
const getEntityInstanceType_1 = require("./getEntityInstanceType");
|
|
6
|
+
const getEntityTypeByExtract_1 = require("./getEntityTypeByExtract");
|
|
7
|
+
const getEntityData_1 = require("./getEntityData");
|
|
8
|
+
const getEntityCountry_1 = require("./getEntityCountry");
|
|
9
|
+
const utils_1 = require("../utils");
|
|
10
|
+
function convertToSimpleEntity(wikiEntity, lang, options) {
|
|
11
|
+
options = options || {};
|
|
12
|
+
const entity = {};
|
|
13
|
+
entity.lang = lang.toLowerCase();
|
|
14
|
+
entity.wikiDataId = wikiEntity.id;
|
|
15
|
+
entity.name = wikiEntity.label;
|
|
16
|
+
entity.description = wikiEntity.description;
|
|
17
|
+
entity.wikiPageId = wikiEntity.pageid;
|
|
18
|
+
entity.about = wikiEntity.extract;
|
|
19
|
+
if (wikiEntity.redirectsToId)
|
|
20
|
+
entity.redirectsToId = wikiEntity.redirectsToId;
|
|
21
|
+
if (wikiEntity.redirectsFromId)
|
|
22
|
+
entity.redirectsFromId = wikiEntity.redirectsFromId;
|
|
23
|
+
if (wikiEntity.types) {
|
|
24
|
+
entity.types = (0, utils_1.uniq)(wikiEntity.types.filter((item) => !/:(Thing|Agent)$/.test(item)));
|
|
25
|
+
}
|
|
26
|
+
entity.type = (0, getEntityType_1.getEntityType)(wikiEntity);
|
|
27
|
+
if (!entity.type) {
|
|
28
|
+
entity.type = (0, getEntityInstanceType_1.getEntityType)(wikiEntity);
|
|
29
|
+
if (!entity.type && entity.about && !entity.types) {
|
|
30
|
+
entity.type = (0, getEntityTypeByExtract_1.getEntityTypeByExtract)(entity.about, lang);
|
|
31
|
+
}
|
|
32
|
+
if (!entity.type && options.defaultType) {
|
|
33
|
+
entity.type = options.defaultType;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (wikiEntity.sitelinks) {
|
|
37
|
+
entity.wikiPageTitle = wikiEntity.sitelinks[lang];
|
|
38
|
+
}
|
|
39
|
+
entity.categories = createCategories(wikiEntity);
|
|
40
|
+
if (wikiEntity.claims) {
|
|
41
|
+
const ids = Object.keys(wikiEntity.claims);
|
|
42
|
+
if (ids.length) {
|
|
43
|
+
entity.data = (0, getEntityData_1.getEntityData)(wikiEntity);
|
|
44
|
+
}
|
|
45
|
+
entity.countryCodes = (0, getEntityCountry_1.getEntityCountryCode)(wikiEntity);
|
|
46
|
+
}
|
|
47
|
+
return entity;
|
|
48
|
+
}
|
|
49
|
+
exports.convertToSimpleEntity = convertToSimpleEntity;
|
|
50
|
+
function createCategories(entity) {
|
|
51
|
+
let categories = entity.categories || [];
|
|
52
|
+
return (0, utils_1.uniq)(categories);
|
|
53
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getEntityCountryCode = void 0;
|
|
4
|
-
const countries = require("../../data/countries.json");
|
|
5
|
-
function getEntityCountryCode(wikiEntity) {
|
|
6
|
-
if (!(wikiEntity === null || wikiEntity === void 0 ? void 0 : wikiEntity.claims)) {
|
|
7
|
-
return null;
|
|
8
|
-
}
|
|
9
|
-
const countryIds = getEntityCountryIds(wikiEntity);
|
|
10
|
-
const list = [];
|
|
11
|
-
for (let id of countryIds) {
|
|
12
|
-
if (countries[id]) {
|
|
13
|
-
list.push(countries[id].cc2.toLowerCase());
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return list.length ? list : null;
|
|
17
|
-
}
|
|
18
|
-
exports.getEntityCountryCode = getEntityCountryCode;
|
|
19
|
-
function getEntityCountryIds(wikiEntity) {
|
|
20
|
-
let prop = wikiEntity.claims.P17 ||
|
|
21
|
-
wikiEntity.claims.P27 ||
|
|
22
|
-
wikiEntity.claims.P495 ||
|
|
23
|
-
wikiEntity.claims.P1532;
|
|
24
|
-
return (prop && prop.values.map((item) => item.value)) || [];
|
|
25
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEntityCountryCode = void 0;
|
|
4
|
+
const countries = require("../../data/countries.json");
|
|
5
|
+
function getEntityCountryCode(wikiEntity) {
|
|
6
|
+
if (!(wikiEntity === null || wikiEntity === void 0 ? void 0 : wikiEntity.claims)) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const countryIds = getEntityCountryIds(wikiEntity);
|
|
10
|
+
const list = [];
|
|
11
|
+
for (let id of countryIds) {
|
|
12
|
+
if (countries[id]) {
|
|
13
|
+
list.push(countries[id].cc2.toLowerCase());
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return list.length ? list : null;
|
|
17
|
+
}
|
|
18
|
+
exports.getEntityCountryCode = getEntityCountryCode;
|
|
19
|
+
function getEntityCountryIds(wikiEntity) {
|
|
20
|
+
let prop = wikiEntity.claims.P17 ||
|
|
21
|
+
wikiEntity.claims.P27 ||
|
|
22
|
+
wikiEntity.claims.P495 ||
|
|
23
|
+
wikiEntity.claims.P1532;
|
|
24
|
+
return (prop && prop.values.map((item) => item.value)) || [];
|
|
25
|
+
}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getEntityData = void 0;
|
|
4
|
-
function getEntityData(wikiEntity) {
|
|
5
|
-
if (!wikiEntity.claims) {
|
|
6
|
-
return null;
|
|
7
|
-
}
|
|
8
|
-
const data = {};
|
|
9
|
-
for (let key in wikiEntity.claims) {
|
|
10
|
-
const values = wikiEntity.claims[key].values;
|
|
11
|
-
data[key] = [];
|
|
12
|
-
values.forEach((value) => {
|
|
13
|
-
if (!value.value_string && value.value === null) {
|
|
14
|
-
throw new Error(`WikiEntity claim value cannot be null: ${wikiEntity.id}:${key}`);
|
|
15
|
-
}
|
|
16
|
-
const v = {
|
|
17
|
-
value: value.value_string || value.value.toString()
|
|
18
|
-
};
|
|
19
|
-
if (value.label && v.value !== value.label) {
|
|
20
|
-
v.label = value.label;
|
|
21
|
-
}
|
|
22
|
-
data[key].push(v.value);
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
return data;
|
|
26
|
-
}
|
|
27
|
-
exports.getEntityData = getEntityData;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEntityData = void 0;
|
|
4
|
+
function getEntityData(wikiEntity) {
|
|
5
|
+
if (!wikiEntity.claims) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const data = {};
|
|
9
|
+
for (let key in wikiEntity.claims) {
|
|
10
|
+
const values = wikiEntity.claims[key].values;
|
|
11
|
+
data[key] = [];
|
|
12
|
+
values.forEach((value) => {
|
|
13
|
+
if (!value.value_string && value.value === null) {
|
|
14
|
+
throw new Error(`WikiEntity claim value cannot be null: ${wikiEntity.id}:${key}`);
|
|
15
|
+
}
|
|
16
|
+
const v = {
|
|
17
|
+
value: value.value_string || value.value.toString()
|
|
18
|
+
};
|
|
19
|
+
if (value.label && v.value !== value.label) {
|
|
20
|
+
v.label = value.label;
|
|
21
|
+
}
|
|
22
|
+
data[key].push(v.value);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
exports.getEntityData = getEntityData;
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getEntityType = void 0;
|
|
4
|
-
const simpleEntity_1 = require("./simpleEntity");
|
|
5
|
-
function getEntityType(wikiEntity) {
|
|
6
|
-
if (!wikiEntity.claims) {
|
|
7
|
-
return null;
|
|
8
|
-
}
|
|
9
|
-
const type = getTypeByProp(wikiEntity, 'P31');
|
|
10
|
-
if (type) {
|
|
11
|
-
return type;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
exports.getEntityType = getEntityType;
|
|
15
|
-
function getTypeByProp(wikiEntity, prop) {
|
|
16
|
-
const instanceOf = wikiEntity.claims[prop];
|
|
17
|
-
if (!instanceOf) {
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
const ENTITY_TYPES = require('../../data/entity_types.json');
|
|
21
|
-
const types = simpleEntity_1.SIMPLE_ENTITY_TYPES;
|
|
22
|
-
for (let i = 0; i < types.length; i++) {
|
|
23
|
-
const type = types[i];
|
|
24
|
-
for (let j = 0; j < instanceOf.values.length; j++) {
|
|
25
|
-
const value = instanceOf.values[j].value;
|
|
26
|
-
if (~ENTITY_TYPES[type].indexOf(value)) {
|
|
27
|
-
return type;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEntityType = void 0;
|
|
4
|
+
const simpleEntity_1 = require("./simpleEntity");
|
|
5
|
+
function getEntityType(wikiEntity) {
|
|
6
|
+
if (!wikiEntity.claims) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const type = getTypeByProp(wikiEntity, 'P31');
|
|
10
|
+
if (type) {
|
|
11
|
+
return type;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.getEntityType = getEntityType;
|
|
15
|
+
function getTypeByProp(wikiEntity, prop) {
|
|
16
|
+
const instanceOf = wikiEntity.claims[prop];
|
|
17
|
+
if (!instanceOf) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
const ENTITY_TYPES = require('../../data/entity_types.json');
|
|
21
|
+
const types = simpleEntity_1.SIMPLE_ENTITY_TYPES;
|
|
22
|
+
for (let i = 0; i < types.length; i++) {
|
|
23
|
+
const type = types[i];
|
|
24
|
+
for (let j = 0; j < instanceOf.values.length; j++) {
|
|
25
|
+
const value = instanceOf.values[j].value;
|
|
26
|
+
if (~ENTITY_TYPES[type].indexOf(value)) {
|
|
27
|
+
return type;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|