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.
Files changed (38) hide show
  1. package/README.md +18 -0
  2. package/lib/index.js +129 -126
  3. package/lib/request.js +37 -19
  4. package/lib/simpleEntity/convertToSimpleEntity.js +53 -53
  5. package/lib/simpleEntity/getEntityCountry.js +25 -25
  6. package/lib/simpleEntity/getEntityData.js +27 -27
  7. package/lib/simpleEntity/getEntityInstanceType.js +32 -32
  8. package/lib/simpleEntity/getEntityType.js +62 -62
  9. package/lib/simpleEntity/getEntityTypeByExtract.js +28 -28
  10. package/lib/simpleEntity/index.js +7 -7
  11. package/lib/simpleEntity/simpleEntity.js +20 -20
  12. package/lib/types.js +13 -13
  13. package/lib/utils.js +15 -15
  14. package/lib/wikidata/api.js +106 -106
  15. package/lib/wikidata/get_entity_types.js +60 -60
  16. package/lib/wikidata/index.js +112 -112
  17. package/lib/wikidata/simplify_claims.js +100 -100
  18. package/lib/wikidata/simplify_entity.js +76 -76
  19. package/lib/wikipedia/api.js +146 -146
  20. package/package.json +1 -1
  21. package/types/index.d.ts +9 -8
  22. package/types/request.d.ts +5 -3
  23. package/types/simpleEntity/convertToSimpleEntity.d.ts +6 -6
  24. package/types/simpleEntity/getEntityCountry.d.ts +2 -2
  25. package/types/simpleEntity/getEntityData.d.ts +3 -3
  26. package/types/simpleEntity/getEntityInstanceType.d.ts +3 -3
  27. package/types/simpleEntity/getEntityType.d.ts +3 -3
  28. package/types/simpleEntity/getEntityTypeByExtract.d.ts +2 -2
  29. package/types/simpleEntity/index.d.ts +2 -2
  30. package/types/simpleEntity/simpleEntity.d.ts +29 -29
  31. package/types/types.d.ts +68 -68
  32. package/types/utils.d.ts +8 -8
  33. package/types/wikidata/api.d.ts +4 -4
  34. package/types/wikidata/get_entity_types.d.ts +1 -1
  35. package/types/wikidata/index.d.ts +3 -3
  36. package/types/wikidata/simplify_claims.d.ts +4 -4
  37. package/types/wikidata/simplify_entity.d.ts +21 -21
  38. package/types/wikipedia/api.d.ts +31 -31
@@ -1,62 +1,62 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEntityType = void 0;
4
- const simpleEntity_1 = require("./simpleEntity");
5
- const TYPES_MAP = {
6
- 'dbo:FictionalCharacter': simpleEntity_1.SimpleEntityType.WORK,
7
- 'wikidata:Q95074': simpleEntity_1.SimpleEntityType.WORK,
8
- 'dbo:Book': simpleEntity_1.SimpleEntityType.WORK,
9
- 'wikidata:Q571': simpleEntity_1.SimpleEntityType.WORK,
10
- 'dbo:PopulatedPlace': simpleEntity_1.SimpleEntityType.PLACE,
11
- 'dbo:Place': simpleEntity_1.SimpleEntityType.PLACE,
12
- 'schema:Place': simpleEntity_1.SimpleEntityType.PLACE,
13
- 'schema:City': simpleEntity_1.SimpleEntityType.PLACE,
14
- 'dbo:Location': simpleEntity_1.SimpleEntityType.PLACE,
15
- 'wikidata:Q515': simpleEntity_1.SimpleEntityType.PLACE,
16
- 'wikidata:Q486972': simpleEntity_1.SimpleEntityType.PLACE,
17
- 'dbo:Company': simpleEntity_1.SimpleEntityType.ORG,
18
- 'schema:Organization': simpleEntity_1.SimpleEntityType.ORG,
19
- 'dbo:Organisation': simpleEntity_1.SimpleEntityType.ORG,
20
- 'wikidata:Q43229': simpleEntity_1.SimpleEntityType.ORG,
21
- 'schema:Person': simpleEntity_1.SimpleEntityType.PERSON,
22
- 'wikidata:Q215627': simpleEntity_1.SimpleEntityType.PERSON,
23
- 'dul:NaturalPerson': simpleEntity_1.SimpleEntityType.PERSON,
24
- 'wikidata:Q5': simpleEntity_1.SimpleEntityType.PERSON,
25
- 'foaf:Person': simpleEntity_1.SimpleEntityType.PERSON,
26
- 'dbo:Person': simpleEntity_1.SimpleEntityType.PERSON,
27
- 'wikidata:Q1656682': simpleEntity_1.SimpleEntityType.EVENT,
28
- 'dul:Event': simpleEntity_1.SimpleEntityType.EVENT,
29
- 'schema:Event': simpleEntity_1.SimpleEntityType.EVENT,
30
- 'dbo:Event': simpleEntity_1.SimpleEntityType.EVENT,
31
- 'dbo:Software': simpleEntity_1.SimpleEntityType.PRODUCT,
32
- 'wikidata:Q7397': simpleEntity_1.SimpleEntityType.PRODUCT,
33
- };
34
- const TYPES_NAME_MAP = Object.keys(TYPES_MAP)
35
- .reduce((map, name) => {
36
- const type = TYPES_MAP[name];
37
- map[type] = map[type] || [];
38
- map[type].push(name);
39
- return map;
40
- }, {});
41
- function getEntityType(wikiEntity) {
42
- if (!wikiEntity.types) {
43
- return null;
44
- }
45
- if (containsType(wikiEntity.types, simpleEntity_1.SimpleEntityType.ORG) && containsType(wikiEntity.types, simpleEntity_1.SimpleEntityType.PERSON)) {
46
- return simpleEntity_1.SimpleEntityType.ORG;
47
- }
48
- for (var i = 0; i < wikiEntity.types.length; i++) {
49
- if (TYPES_MAP[wikiEntity.types[i]]) {
50
- return TYPES_MAP[wikiEntity.types[i]];
51
- }
52
- }
53
- }
54
- exports.getEntityType = getEntityType;
55
- function containsType(types, type) {
56
- for (const typeName of types) {
57
- if (~TYPES_NAME_MAP[type].indexOf(typeName)) {
58
- return true;
59
- }
60
- }
61
- return false;
62
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEntityType = void 0;
4
+ const simpleEntity_1 = require("./simpleEntity");
5
+ const TYPES_MAP = {
6
+ 'dbo:FictionalCharacter': simpleEntity_1.SimpleEntityType.WORK,
7
+ 'wikidata:Q95074': simpleEntity_1.SimpleEntityType.WORK,
8
+ 'dbo:Book': simpleEntity_1.SimpleEntityType.WORK,
9
+ 'wikidata:Q571': simpleEntity_1.SimpleEntityType.WORK,
10
+ 'dbo:PopulatedPlace': simpleEntity_1.SimpleEntityType.PLACE,
11
+ 'dbo:Place': simpleEntity_1.SimpleEntityType.PLACE,
12
+ 'schema:Place': simpleEntity_1.SimpleEntityType.PLACE,
13
+ 'schema:City': simpleEntity_1.SimpleEntityType.PLACE,
14
+ 'dbo:Location': simpleEntity_1.SimpleEntityType.PLACE,
15
+ 'wikidata:Q515': simpleEntity_1.SimpleEntityType.PLACE,
16
+ 'wikidata:Q486972': simpleEntity_1.SimpleEntityType.PLACE,
17
+ 'dbo:Company': simpleEntity_1.SimpleEntityType.ORG,
18
+ 'schema:Organization': simpleEntity_1.SimpleEntityType.ORG,
19
+ 'dbo:Organisation': simpleEntity_1.SimpleEntityType.ORG,
20
+ 'wikidata:Q43229': simpleEntity_1.SimpleEntityType.ORG,
21
+ 'schema:Person': simpleEntity_1.SimpleEntityType.PERSON,
22
+ 'wikidata:Q215627': simpleEntity_1.SimpleEntityType.PERSON,
23
+ 'dul:NaturalPerson': simpleEntity_1.SimpleEntityType.PERSON,
24
+ 'wikidata:Q5': simpleEntity_1.SimpleEntityType.PERSON,
25
+ 'foaf:Person': simpleEntity_1.SimpleEntityType.PERSON,
26
+ 'dbo:Person': simpleEntity_1.SimpleEntityType.PERSON,
27
+ 'wikidata:Q1656682': simpleEntity_1.SimpleEntityType.EVENT,
28
+ 'dul:Event': simpleEntity_1.SimpleEntityType.EVENT,
29
+ 'schema:Event': simpleEntity_1.SimpleEntityType.EVENT,
30
+ 'dbo:Event': simpleEntity_1.SimpleEntityType.EVENT,
31
+ 'dbo:Software': simpleEntity_1.SimpleEntityType.PRODUCT,
32
+ 'wikidata:Q7397': simpleEntity_1.SimpleEntityType.PRODUCT,
33
+ };
34
+ const TYPES_NAME_MAP = Object.keys(TYPES_MAP)
35
+ .reduce((map, name) => {
36
+ const type = TYPES_MAP[name];
37
+ map[type] = map[type] || [];
38
+ map[type].push(name);
39
+ return map;
40
+ }, {});
41
+ function getEntityType(wikiEntity) {
42
+ if (!wikiEntity.types) {
43
+ return null;
44
+ }
45
+ if (containsType(wikiEntity.types, simpleEntity_1.SimpleEntityType.ORG) && containsType(wikiEntity.types, simpleEntity_1.SimpleEntityType.PERSON)) {
46
+ return simpleEntity_1.SimpleEntityType.ORG;
47
+ }
48
+ for (var i = 0; i < wikiEntity.types.length; i++) {
49
+ if (TYPES_MAP[wikiEntity.types[i]]) {
50
+ return TYPES_MAP[wikiEntity.types[i]];
51
+ }
52
+ }
53
+ }
54
+ exports.getEntityType = getEntityType;
55
+ function containsType(types, type) {
56
+ for (const typeName of types) {
57
+ if (~TYPES_NAME_MAP[type].indexOf(typeName)) {
58
+ return true;
59
+ }
60
+ }
61
+ return false;
62
+ }
@@ -1,28 +1,28 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEntityTypeByExtract = void 0;
4
- const simpleEntity_1 = require("./simpleEntity");
5
- const MAP = {
6
- ro: [
7
- { reg: /\beste un (sat|oraș|județ|raion)\b/i, type: simpleEntity_1.SimpleEntityType.PLACE },
8
- { reg: /\beste o (comună|biserică|mănăstire)\b/i, type: simpleEntity_1.SimpleEntityType.PLACE },
9
- { reg: /\beste o (organizație)\b/i, type: simpleEntity_1.SimpleEntityType.ORG },
10
- { reg: /\beste un (om|scriitor|poet|cercetător|politician|businessman|cântăreț|muzician)\b/i, type: simpleEntity_1.SimpleEntityType.PERSON },
11
- { reg: /\beste o (scriitoare|poetă|cercetătoare|politiciană|cântăreață)\b/i, type: simpleEntity_1.SimpleEntityType.PERSON }
12
- ]
13
- };
14
- function getEntityTypeByExtract(extract, lang) {
15
- if (extract) {
16
- extract = extract.substr(0, 100);
17
- const map = MAP[lang];
18
- if (map && map.length) {
19
- for (var i = 0; i < map.length; i++) {
20
- const item = map[i];
21
- if (item.reg.test(extract)) {
22
- return item.type;
23
- }
24
- }
25
- }
26
- }
27
- }
28
- exports.getEntityTypeByExtract = getEntityTypeByExtract;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEntityTypeByExtract = void 0;
4
+ const simpleEntity_1 = require("./simpleEntity");
5
+ const MAP = {
6
+ ro: [
7
+ { reg: /\beste un (sat|oraș|județ|raion)\b/i, type: simpleEntity_1.SimpleEntityType.PLACE },
8
+ { reg: /\beste o (comună|biserică|mănăstire)\b/i, type: simpleEntity_1.SimpleEntityType.PLACE },
9
+ { reg: /\beste o (organizație)\b/i, type: simpleEntity_1.SimpleEntityType.ORG },
10
+ { reg: /\beste un (om|scriitor|poet|cercetător|politician|businessman|cântăreț|muzician)\b/i, type: simpleEntity_1.SimpleEntityType.PERSON },
11
+ { reg: /\beste o (scriitoare|poetă|cercetătoare|politiciană|cântăreață)\b/i, type: simpleEntity_1.SimpleEntityType.PERSON }
12
+ ]
13
+ };
14
+ function getEntityTypeByExtract(extract, lang) {
15
+ if (extract) {
16
+ extract = extract.substr(0, 100);
17
+ const map = MAP[lang];
18
+ if (map && map.length) {
19
+ for (var i = 0; i < map.length; i++) {
20
+ const item = map[i];
21
+ if (item.reg.test(extract)) {
22
+ return item.type;
23
+ }
24
+ }
25
+ }
26
+ }
27
+ }
28
+ exports.getEntityTypeByExtract = getEntityTypeByExtract;
@@ -1,7 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertToSimpleEntity = exports.SimpleEntityType = void 0;
4
- var simpleEntity_1 = require("./simpleEntity");
5
- Object.defineProperty(exports, "SimpleEntityType", { enumerable: true, get: function () { return simpleEntity_1.SimpleEntityType; } });
6
- var convertToSimpleEntity_1 = require("./convertToSimpleEntity");
7
- Object.defineProperty(exports, "convertToSimpleEntity", { enumerable: true, get: function () { return convertToSimpleEntity_1.convertToSimpleEntity; } });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToSimpleEntity = exports.SimpleEntityType = void 0;
4
+ var simpleEntity_1 = require("./simpleEntity");
5
+ Object.defineProperty(exports, "SimpleEntityType", { enumerable: true, get: function () { return simpleEntity_1.SimpleEntityType; } });
6
+ var convertToSimpleEntity_1 = require("./convertToSimpleEntity");
7
+ Object.defineProperty(exports, "convertToSimpleEntity", { enumerable: true, get: function () { return convertToSimpleEntity_1.convertToSimpleEntity; } });
@@ -1,20 +1,20 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SIMPLE_ENTITY_TYPES = exports.SimpleEntityType = void 0;
4
- var SimpleEntityType;
5
- (function (SimpleEntityType) {
6
- SimpleEntityType["EVENT"] = "E";
7
- SimpleEntityType["ORG"] = "O";
8
- SimpleEntityType["PERSON"] = "H";
9
- SimpleEntityType["PLACE"] = "P";
10
- SimpleEntityType["PRODUCT"] = "R";
11
- SimpleEntityType["WORK"] = "W";
12
- })(SimpleEntityType = exports.SimpleEntityType || (exports.SimpleEntityType = {}));
13
- exports.SIMPLE_ENTITY_TYPES = [
14
- SimpleEntityType.EVENT,
15
- SimpleEntityType.PERSON,
16
- SimpleEntityType.PLACE,
17
- SimpleEntityType.ORG,
18
- SimpleEntityType.PRODUCT,
19
- SimpleEntityType.WORK
20
- ];
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SIMPLE_ENTITY_TYPES = exports.SimpleEntityType = void 0;
4
+ var SimpleEntityType;
5
+ (function (SimpleEntityType) {
6
+ SimpleEntityType["EVENT"] = "E";
7
+ SimpleEntityType["ORG"] = "O";
8
+ SimpleEntityType["PERSON"] = "H";
9
+ SimpleEntityType["PLACE"] = "P";
10
+ SimpleEntityType["PRODUCT"] = "R";
11
+ SimpleEntityType["WORK"] = "W";
12
+ })(SimpleEntityType = exports.SimpleEntityType || (exports.SimpleEntityType = {}));
13
+ exports.SIMPLE_ENTITY_TYPES = [
14
+ SimpleEntityType.EVENT,
15
+ SimpleEntityType.PERSON,
16
+ SimpleEntityType.PLACE,
17
+ SimpleEntityType.ORG,
18
+ SimpleEntityType.PRODUCT,
19
+ SimpleEntityType.WORK
20
+ ];
package/lib/types.js CHANGED
@@ -1,13 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WikidataPropsParam = void 0;
4
- var WikidataPropsParam;
5
- (function (WikidataPropsParam) {
6
- WikidataPropsParam["info"] = "info";
7
- WikidataPropsParam["sitelinks"] = "sitelinks";
8
- WikidataPropsParam["aliases"] = "aliases";
9
- WikidataPropsParam["labels"] = "labels";
10
- WikidataPropsParam["descriptions"] = "descriptions";
11
- WikidataPropsParam["claims"] = "claims";
12
- WikidataPropsParam["datatype"] = "datatype";
13
- })(WikidataPropsParam = exports.WikidataPropsParam || (exports.WikidataPropsParam = {}));
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WikidataPropsParam = void 0;
4
+ var WikidataPropsParam;
5
+ (function (WikidataPropsParam) {
6
+ WikidataPropsParam["info"] = "info";
7
+ WikidataPropsParam["sitelinks"] = "sitelinks";
8
+ WikidataPropsParam["aliases"] = "aliases";
9
+ WikidataPropsParam["labels"] = "labels";
10
+ WikidataPropsParam["descriptions"] = "descriptions";
11
+ WikidataPropsParam["claims"] = "claims";
12
+ WikidataPropsParam["datatype"] = "datatype";
13
+ })(WikidataPropsParam = exports.WikidataPropsParam || (exports.WikidataPropsParam = {}));
package/lib/utils.js CHANGED
@@ -1,15 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidWikiId = exports.uniq = exports.eachSeries = void 0;
4
- function eachSeries(arr, iteratorFn) {
5
- return arr.reduce((p, item) => p.then(() => iteratorFn(item)), Promise.resolve());
6
- }
7
- exports.eachSeries = eachSeries;
8
- function uniq(items) {
9
- return items.filter((value, index, self) => self.indexOf(value) === index);
10
- }
11
- exports.uniq = uniq;
12
- function isValidWikiId(id) {
13
- return /^Q\d+$/.test(id);
14
- }
15
- exports.isValidWikiId = isValidWikiId;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidWikiId = exports.uniq = exports.eachSeries = void 0;
4
+ function eachSeries(arr, iteratorFn) {
5
+ return arr.reduce((p, item) => p.then(() => iteratorFn(item)), Promise.resolve());
6
+ }
7
+ exports.eachSeries = eachSeries;
8
+ function uniq(items) {
9
+ return items.filter((value, index, self) => self.indexOf(value) === index);
10
+ }
11
+ exports.uniq = uniq;
12
+ function isValidWikiId(id) {
13
+ return /^Q\d+$/.test(id);
14
+ }
15
+ exports.isValidWikiId = isValidWikiId;
@@ -1,106 +1,106 @@
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.validateParams = exports.getManyEntities = exports.getEntities = void 0;
13
- const request_1 = require("../request");
14
- const types_1 = require("../types");
15
- const utils_1 = require("../utils");
16
- const API_URL = "https://www.wikidata.org/w/api.php";
17
- function getEntities(params) {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- const qs = {
20
- action: "wbgetentities",
21
- ids: params.ids && params.ids.join("|"),
22
- titles: params.titles && params.titles.join("|"),
23
- props: (params.props || [
24
- types_1.WikidataPropsParam.info,
25
- types_1.WikidataPropsParam.sitelinks,
26
- types_1.WikidataPropsParam.aliases,
27
- types_1.WikidataPropsParam.labels,
28
- types_1.WikidataPropsParam.descriptions,
29
- types_1.WikidataPropsParam.claims,
30
- types_1.WikidataPropsParam.datatype
31
- ]).join("|"),
32
- languages: params.languages,
33
- redirects: params.redirect || "yes",
34
- format: "json",
35
- sites: ""
36
- };
37
- if (params.titles) {
38
- qs.sites = (params.language || "en") + "wiki";
39
- }
40
- else {
41
- delete qs.sites;
42
- }
43
- const data = yield (0, request_1.default)(API_URL, {
44
- params: qs,
45
- timeout: params.httpTimeout
46
- });
47
- if (hasError(data))
48
- throw getError(data);
49
- const entities = (data && data.entities) || {};
50
- for (let id of Object.keys(entities)) {
51
- const entity = entities[id];
52
- if (entity.redirects && entity.redirects.to)
53
- entity.redirectsToId = entity.redirects.to;
54
- if (entity.redirects && entity.redirects.from)
55
- entity.redirectsFromId = entity.redirects.from;
56
- if (!(0, utils_1.isValidWikiId)(id) || ~Object.keys(entities[id]).indexOf("missing")) {
57
- delete entities[id];
58
- }
59
- }
60
- return entities;
61
- });
62
- }
63
- exports.getEntities = getEntities;
64
- function getManyEntities(params) {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- validateParams(params);
67
- const keyName = !!params.ids ? "ids" : "titles";
68
- const keyValues = params[keyName];
69
- const max = 50;
70
- const countParts = keyValues.length / max + 1;
71
- const parts = [];
72
- for (var i = 0; i < countParts && i < 10; i++) {
73
- const partParams = Object.assign({}, params);
74
- partParams[keyName] = keyValues.slice(i * max, (i + 1) * max);
75
- if (partParams[keyName].length > 0) {
76
- parts.push(getEntities(partParams));
77
- }
78
- }
79
- const results = yield Promise.all(parts);
80
- if (results.length === 0) {
81
- return null;
82
- }
83
- if (results.length > 1) {
84
- for (var i = 1; i < results.length; i++) {
85
- Object.assign(results[0], results[i]);
86
- }
87
- }
88
- return results[0];
89
- });
90
- }
91
- exports.getManyEntities = getManyEntities;
92
- function validateParams(params) {
93
- if (!params.ids && !params.titles) {
94
- throw new Error("Invalid params: `ids` or `titles` are required");
95
- }
96
- }
97
- exports.validateParams = validateParams;
98
- function getError(data) {
99
- return ((data &&
100
- data.error &&
101
- new Error(data.error.info || "Wikidata Api Error")) ||
102
- new Error("NO error"));
103
- }
104
- function hasError(data) {
105
- return !!data.error;
106
- }
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.validateParams = exports.getManyEntities = exports.getEntities = void 0;
13
+ const request_1 = require("../request");
14
+ const types_1 = require("../types");
15
+ const utils_1 = require("../utils");
16
+ const API_URL = "https://www.wikidata.org/w/api.php";
17
+ function getEntities(params) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ const qs = {
20
+ action: "wbgetentities",
21
+ ids: params.ids && params.ids.join("|"),
22
+ titles: params.titles && params.titles.join("|"),
23
+ props: (params.props || [
24
+ types_1.WikidataPropsParam.info,
25
+ types_1.WikidataPropsParam.sitelinks,
26
+ types_1.WikidataPropsParam.aliases,
27
+ types_1.WikidataPropsParam.labels,
28
+ types_1.WikidataPropsParam.descriptions,
29
+ types_1.WikidataPropsParam.claims,
30
+ types_1.WikidataPropsParam.datatype
31
+ ]).join("|"),
32
+ languages: params.languages,
33
+ redirects: params.redirect || "yes",
34
+ format: "json",
35
+ sites: ""
36
+ };
37
+ if (params.titles) {
38
+ qs.sites = (params.language || "en") + "wiki";
39
+ }
40
+ else {
41
+ delete qs.sites;
42
+ }
43
+ const data = yield (0, request_1.default)(API_URL, {
44
+ params: qs,
45
+ timeout: params.httpTimeout
46
+ });
47
+ if (hasError(data))
48
+ throw getError(data);
49
+ const entities = (data && data.entities) || {};
50
+ for (let id of Object.keys(entities)) {
51
+ const entity = entities[id];
52
+ if (entity.redirects && entity.redirects.to)
53
+ entity.redirectsToId = entity.redirects.to;
54
+ if (entity.redirects && entity.redirects.from)
55
+ entity.redirectsFromId = entity.redirects.from;
56
+ if (!(0, utils_1.isValidWikiId)(id) || ~Object.keys(entities[id]).indexOf("missing")) {
57
+ delete entities[id];
58
+ }
59
+ }
60
+ return entities;
61
+ });
62
+ }
63
+ exports.getEntities = getEntities;
64
+ function getManyEntities(params) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ validateParams(params);
67
+ const keyName = !!params.ids ? "ids" : "titles";
68
+ const keyValues = params[keyName];
69
+ const max = 50;
70
+ const countParts = keyValues.length / max + 1;
71
+ const parts = [];
72
+ for (var i = 0; i < countParts && i < 10; i++) {
73
+ const partParams = Object.assign({}, params);
74
+ partParams[keyName] = keyValues.slice(i * max, (i + 1) * max);
75
+ if (partParams[keyName].length > 0) {
76
+ parts.push(getEntities(partParams));
77
+ }
78
+ }
79
+ const results = yield Promise.all(parts);
80
+ if (results.length === 0) {
81
+ return null;
82
+ }
83
+ if (results.length > 1) {
84
+ for (var i = 1; i < results.length; i++) {
85
+ Object.assign(results[0], results[i]);
86
+ }
87
+ }
88
+ return results[0];
89
+ });
90
+ }
91
+ exports.getManyEntities = getManyEntities;
92
+ function validateParams(params) {
93
+ if (!params.ids && !params.titles) {
94
+ throw new Error("Invalid params: `ids` or `titles` are required");
95
+ }
96
+ }
97
+ exports.validateParams = validateParams;
98
+ function getError(data) {
99
+ return ((data &&
100
+ data.error &&
101
+ new Error(data.error.info || "Wikidata Api Error")) ||
102
+ new Error("NO error"));
103
+ }
104
+ function hasError(data) {
105
+ return !!data.error;
106
+ }
@@ -1,60 +1,60 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEntityTypesByName = void 0;
4
- const request_1 = require("../request");
5
- const PREFIXES_MAP = {
6
- "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#": "dul",
7
- "http://dbpedia.org/ontology/": "dbo",
8
- "http://www.w3.org/2002/07/owl#": "owl",
9
- "http://www.wikidata.org/entity/": "wikidata",
10
- "http://schema.org/": "schema",
11
- "http://xmlns.com/foaf/0.1/": "foaf",
12
- "http://www.w3.org/2003/01/geo/wgs84_pos#": "geo"
13
- };
14
- const PREFIXES = Object.keys(PREFIXES_MAP).map((key) => PREFIXES_MAP[key]);
15
- const PREFIXES_REG = new RegExp("^(" + Object.keys(PREFIXES_MAP).join("|") + ")");
16
- function getEntityTypesByName(name, prefixes) {
17
- if (!prefixes || !prefixes.length) {
18
- prefixes = PREFIXES;
19
- }
20
- return dbpediaTypes(name)
21
- .then((types) => parseTypes(types))
22
- .then((types) => {
23
- return types
24
- .filter((item) => PREFIXES_REG.test(item))
25
- .map((item) => {
26
- const key = PREFIXES_REG.exec(item)[1];
27
- return PREFIXES_MAP[key] + ":" + item.substr(key.length);
28
- });
29
- })
30
- .then((types) => repairTypes(types));
31
- }
32
- exports.getEntityTypesByName = getEntityTypesByName;
33
- function parseTypes(types) {
34
- return types.map((item) => item.type.value);
35
- }
36
- function dbpediaTypes(name) {
37
- const url = `http://dbpedia.org/resource/${name
38
- .replace(/\s+/g, "_")
39
- .replace(/"/g, "%22")}`;
40
- const query = `SELECT ?type WHERE { <${url}> rdf:type ?type }`;
41
- return (0, request_1.default)("http://dbpedia.org/sparql", {
42
- params: { query: query },
43
- timeout: 30 * 1000
44
- })
45
- .then((data) => data.results && data.results.bindings)
46
- .catch((error) => {
47
- console.error(`${error.message}: ${url}`);
48
- return [];
49
- });
50
- }
51
- function repairTypes(types) {
52
- const personIndex = types.findIndex((item) => /:Person$/.test(item));
53
- if (personIndex > -1) {
54
- const placeIndex = types.findIndex((item) => /:Place$/.test(item));
55
- if (placeIndex > -1) {
56
- types.splice(personIndex, 1);
57
- }
58
- }
59
- return types;
60
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEntityTypesByName = void 0;
4
+ const request_1 = require("../request");
5
+ const PREFIXES_MAP = {
6
+ "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#": "dul",
7
+ "http://dbpedia.org/ontology/": "dbo",
8
+ "http://www.w3.org/2002/07/owl#": "owl",
9
+ "http://www.wikidata.org/entity/": "wikidata",
10
+ "http://schema.org/": "schema",
11
+ "http://xmlns.com/foaf/0.1/": "foaf",
12
+ "http://www.w3.org/2003/01/geo/wgs84_pos#": "geo"
13
+ };
14
+ const PREFIXES = Object.keys(PREFIXES_MAP).map((key) => PREFIXES_MAP[key]);
15
+ const PREFIXES_REG = new RegExp("^(" + Object.keys(PREFIXES_MAP).join("|") + ")");
16
+ function getEntityTypesByName(name, prefixes) {
17
+ if (!prefixes || !prefixes.length) {
18
+ prefixes = PREFIXES;
19
+ }
20
+ return dbpediaTypes(name)
21
+ .then((types) => parseTypes(types))
22
+ .then((types) => {
23
+ return types
24
+ .filter((item) => PREFIXES_REG.test(item))
25
+ .map((item) => {
26
+ const key = PREFIXES_REG.exec(item)[1];
27
+ return PREFIXES_MAP[key] + ":" + item.substr(key.length);
28
+ });
29
+ })
30
+ .then((types) => repairTypes(types));
31
+ }
32
+ exports.getEntityTypesByName = getEntityTypesByName;
33
+ function parseTypes(types) {
34
+ return types.map((item) => item.type.value);
35
+ }
36
+ function dbpediaTypes(name) {
37
+ const url = `http://dbpedia.org/resource/${name
38
+ .replace(/\s+/g, "_")
39
+ .replace(/"/g, "%22")}`;
40
+ const query = `SELECT ?type WHERE { <${url}> rdf:type ?type }`;
41
+ return (0, request_1.default)("http://dbpedia.org/sparql", {
42
+ params: { query: query },
43
+ timeout: 30 * 1000
44
+ })
45
+ .then((data) => data.results && data.results.bindings)
46
+ .catch((error) => {
47
+ console.error(`${error.message}: ${url}`);
48
+ return [];
49
+ });
50
+ }
51
+ function repairTypes(types) {
52
+ const personIndex = types.findIndex((item) => /:Person$/.test(item));
53
+ if (personIndex > -1) {
54
+ const placeIndex = types.findIndex((item) => /:Place$/.test(item));
55
+ if (placeIndex > -1) {
56
+ types.splice(personIndex, 1);
57
+ }
58
+ }
59
+ return types;
60
+ }