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,112 +1,112 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exploreEntityClaims = exports.getEntities = void 0;
4
- const types_1 = require("../types");
5
- const api_1 = require("./api");
6
- const simplify_entity_1 = require("./simplify_entity");
7
- const utils_1 = require("../utils");
8
- function getEntities(params) {
9
- const claims = params.claims || "none";
10
- const lang = params.language || "en";
11
- return (0, api_1.getManyEntities)(params).then(function (entities) {
12
- const ids = Object.keys(entities).filter((id) => (0, utils_1.isValidWikiId)(id));
13
- ids.forEach((id) => {
14
- entities[id] = (0, simplify_entity_1.simplifyEntity)(lang, entities[id]);
15
- });
16
- const tasks = [];
17
- if (~["all", "property"].indexOf(claims)) {
18
- tasks.push(exploreEntitiesProperties(entities, lang));
19
- }
20
- if (~["all", "item"].indexOf(claims)) {
21
- tasks.push((0, utils_1.eachSeries)(ids, (id) => exploreEntityClaims(entities[id].claims, { language: lang })));
22
- }
23
- return Promise.all(tasks).then(() => entities);
24
- });
25
- }
26
- exports.getEntities = getEntities;
27
- function exploreEntitiesProperties(entities, lang) {
28
- let ids = [];
29
- const entitiesIds = Object.keys(entities);
30
- entitiesIds.forEach((entityId) => {
31
- const entity = entities[entityId];
32
- if (entity.claims) {
33
- ids = ids.concat(Object.keys(entity.claims));
34
- }
35
- });
36
- if (!ids.length) {
37
- return Promise.resolve();
38
- }
39
- ids = (0, utils_1.uniq)(ids);
40
- return getEntities({
41
- ids: ids,
42
- language: lang,
43
- props: [
44
- types_1.WikidataPropsParam.info,
45
- types_1.WikidataPropsParam.labels,
46
- types_1.WikidataPropsParam.descriptions,
47
- types_1.WikidataPropsParam.datatype
48
- ],
49
- claims: "none"
50
- }).then(function (properties) {
51
- Object.keys(properties).forEach((propertyId) => {
52
- entitiesIds.forEach((entityId) => {
53
- const entity = entities[entityId];
54
- if (entity.claims && entity.claims[propertyId]) {
55
- if (entity.claims && entity.claims[propertyId]) {
56
- for (var prop in properties[propertyId]) {
57
- if (~["label", "description"].indexOf(prop)) {
58
- entity.claims[propertyId][prop] = properties[propertyId][prop];
59
- }
60
- }
61
- }
62
- }
63
- });
64
- });
65
- return entities;
66
- });
67
- }
68
- function exploreEntityClaims(claims, params) {
69
- if (!claims) {
70
- return Promise.resolve();
71
- }
72
- const ids = [];
73
- const paths = {};
74
- Object.keys(claims).forEach((property) => {
75
- claims[property].values.forEach((propertyValue, index) => {
76
- if (propertyValue.datatype === "wikibase-item") {
77
- const id = propertyValue.value;
78
- paths[id] = paths[id] || [];
79
- paths[id].push({ pid: property, value: propertyValue, index });
80
- if (ids.indexOf(id) < 0) {
81
- ids.push(id);
82
- }
83
- }
84
- });
85
- });
86
- if (ids.length === 0) {
87
- return Promise.resolve();
88
- }
89
- params.ids = ids;
90
- params.props = params.props || [
91
- types_1.WikidataPropsParam.info,
92
- types_1.WikidataPropsParam.labels,
93
- types_1.WikidataPropsParam.descriptions,
94
- types_1.WikidataPropsParam.datatype
95
- ];
96
- params.claims = params.claims || "none";
97
- return getEntities(params).then((entities) => {
98
- Object.keys(entities).forEach((id) => {
99
- const item = entities[id];
100
- const pa = paths[item.id] || [];
101
- pa.forEach((pai) => {
102
- const val = claims[pai.pid].values[pai.index];
103
- for (var prop in item) {
104
- if (~["label", "description"].indexOf(prop)) {
105
- val[prop] = item[prop];
106
- }
107
- }
108
- });
109
- });
110
- });
111
- }
112
- exports.exploreEntityClaims = exploreEntityClaims;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.exploreEntityClaims = exports.getEntities = void 0;
4
+ const types_1 = require("../types");
5
+ const api_1 = require("./api");
6
+ const simplify_entity_1 = require("./simplify_entity");
7
+ const utils_1 = require("../utils");
8
+ function getEntities(params) {
9
+ const claims = params.claims || "none";
10
+ const lang = params.language || "en";
11
+ return (0, api_1.getManyEntities)(params).then(function (entities) {
12
+ const ids = Object.keys(entities).filter((id) => (0, utils_1.isValidWikiId)(id));
13
+ ids.forEach((id) => {
14
+ entities[id] = (0, simplify_entity_1.simplifyEntity)(lang, entities[id]);
15
+ });
16
+ const tasks = [];
17
+ if (~["all", "property"].indexOf(claims)) {
18
+ tasks.push(exploreEntitiesProperties(entities, lang));
19
+ }
20
+ if (~["all", "item"].indexOf(claims)) {
21
+ tasks.push((0, utils_1.eachSeries)(ids, (id) => exploreEntityClaims(entities[id].claims, { language: lang })));
22
+ }
23
+ return Promise.all(tasks).then(() => entities);
24
+ });
25
+ }
26
+ exports.getEntities = getEntities;
27
+ function exploreEntitiesProperties(entities, lang) {
28
+ let ids = [];
29
+ const entitiesIds = Object.keys(entities);
30
+ entitiesIds.forEach((entityId) => {
31
+ const entity = entities[entityId];
32
+ if (entity.claims) {
33
+ ids = ids.concat(Object.keys(entity.claims));
34
+ }
35
+ });
36
+ if (!ids.length) {
37
+ return Promise.resolve();
38
+ }
39
+ ids = (0, utils_1.uniq)(ids);
40
+ return getEntities({
41
+ ids: ids,
42
+ language: lang,
43
+ props: [
44
+ types_1.WikidataPropsParam.info,
45
+ types_1.WikidataPropsParam.labels,
46
+ types_1.WikidataPropsParam.descriptions,
47
+ types_1.WikidataPropsParam.datatype
48
+ ],
49
+ claims: "none"
50
+ }).then(function (properties) {
51
+ Object.keys(properties).forEach((propertyId) => {
52
+ entitiesIds.forEach((entityId) => {
53
+ const entity = entities[entityId];
54
+ if (entity.claims && entity.claims[propertyId]) {
55
+ if (entity.claims && entity.claims[propertyId]) {
56
+ for (var prop in properties[propertyId]) {
57
+ if (~["label", "description"].indexOf(prop)) {
58
+ entity.claims[propertyId][prop] = properties[propertyId][prop];
59
+ }
60
+ }
61
+ }
62
+ }
63
+ });
64
+ });
65
+ return entities;
66
+ });
67
+ }
68
+ function exploreEntityClaims(claims, params) {
69
+ if (!claims) {
70
+ return Promise.resolve();
71
+ }
72
+ const ids = [];
73
+ const paths = {};
74
+ Object.keys(claims).forEach((property) => {
75
+ claims[property].values.forEach((propertyValue, index) => {
76
+ if (propertyValue.datatype === "wikibase-item") {
77
+ const id = propertyValue.value;
78
+ paths[id] = paths[id] || [];
79
+ paths[id].push({ pid: property, value: propertyValue, index });
80
+ if (ids.indexOf(id) < 0) {
81
+ ids.push(id);
82
+ }
83
+ }
84
+ });
85
+ });
86
+ if (ids.length === 0) {
87
+ return Promise.resolve();
88
+ }
89
+ params.ids = ids;
90
+ params.props = params.props || [
91
+ types_1.WikidataPropsParam.info,
92
+ types_1.WikidataPropsParam.labels,
93
+ types_1.WikidataPropsParam.descriptions,
94
+ types_1.WikidataPropsParam.datatype
95
+ ];
96
+ params.claims = params.claims || "none";
97
+ return getEntities(params).then((entities) => {
98
+ Object.keys(entities).forEach((id) => {
99
+ const item = entities[id];
100
+ const pa = paths[item.id] || [];
101
+ pa.forEach((pai) => {
102
+ const val = claims[pai.pid].values[pai.index];
103
+ for (var prop in item) {
104
+ if (~["label", "description"].indexOf(prop)) {
105
+ val[prop] = item[prop];
106
+ }
107
+ }
108
+ });
109
+ });
110
+ });
111
+ }
112
+ exports.exploreEntityClaims = exploreEntityClaims;
@@ -1,100 +1,100 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.simplifyClaim = exports.simplifyPropertyClaims = exports.simplifyClaims = void 0;
4
- function simplifyClaims(claims) {
5
- const simpleClaims = {};
6
- for (let id in claims) {
7
- let propClaims = claims[id];
8
- simpleClaims[id] = simplifyPropertyClaims(propClaims, id);
9
- if (simpleClaims[id].values.length === 0) {
10
- delete simpleClaims[id];
11
- }
12
- }
13
- return simpleClaims;
14
- }
15
- exports.simplifyClaims = simplifyClaims;
16
- function simplifyPropertyClaims(propClaims, id) {
17
- const prop = {
18
- id,
19
- values: propClaims
20
- .map((claim) => simplifyClaim(claim))
21
- .filter((item) => item && (item.value_string || item.value !== null))
22
- };
23
- return prop;
24
- }
25
- exports.simplifyPropertyClaims = simplifyPropertyClaims;
26
- function simplifyClaim(claim) {
27
- const { mainsnak, qualifiers } = claim;
28
- if (mainsnak === null)
29
- return null;
30
- const { datatype, datavalue } = mainsnak;
31
- if (datavalue === null || datavalue === undefined || datavalue.value === null)
32
- return null;
33
- let value = null;
34
- let value_string;
35
- switch (datatype) {
36
- case "string":
37
- case "commonsMedia":
38
- case "url":
39
- case "external-id":
40
- value = datavalue.value;
41
- break;
42
- case "monolingualtext":
43
- value = datavalue.value.text;
44
- break;
45
- case "wikibase-item":
46
- value = datavalue.value.id;
47
- break;
48
- case "wikibase-property":
49
- value = datavalue.value;
50
- break;
51
- case "time":
52
- value = datavalue.value;
53
- value_string = stringDatetime(datavalue.value);
54
- break;
55
- case "quantity":
56
- value = datavalue.value;
57
- value_string = parseFloat(datavalue.value.amount).toString();
58
- break;
59
- case "globe-coordinate":
60
- value = datavalue.value;
61
- value_string = getLatLngFromCoordinates(datavalue.value).join(",");
62
- break;
63
- }
64
- const simpleQualifiers = {};
65
- for (let qualifierProp in qualifiers) {
66
- simpleQualifiers[qualifierProp] = qualifiers[qualifierProp].map(prepareQualifierClaim);
67
- }
68
- const result = {
69
- datatype,
70
- value,
71
- qualifiers: simplifyClaims(simpleQualifiers)
72
- };
73
- if (value_string) {
74
- result["value_string"] = value_string;
75
- }
76
- return result;
77
- }
78
- exports.simplifyClaim = simplifyClaim;
79
- const getLatLngFromCoordinates = (value) => [
80
- value.latitude.toFixed(4).replace(/[0]+$/, "").replace(/\.$/, ""),
81
- value.longitude.toFixed(4).replace(/[0]+$/, "").replace(/\.$/, "")
82
- ];
83
- const prepareQualifierClaim = (claim) => ({ mainsnak: claim });
84
- const stringDatetime = (value) => {
85
- let date = value.time;
86
- const p = value.precision;
87
- if (p >= 9) {
88
- date = date.substr(1);
89
- if (p < 12) {
90
- date = date.split("T")[0];
91
- if (p === 10) {
92
- date = date.split("-").slice(0, 2).join("-");
93
- }
94
- else if (p === 9) {
95
- date = date.split("-")[0];
96
- }
97
- }
98
- }
99
- return date;
100
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.simplifyClaim = exports.simplifyPropertyClaims = exports.simplifyClaims = void 0;
4
+ function simplifyClaims(claims) {
5
+ const simpleClaims = {};
6
+ for (let id in claims) {
7
+ let propClaims = claims[id];
8
+ simpleClaims[id] = simplifyPropertyClaims(propClaims, id);
9
+ if (simpleClaims[id].values.length === 0) {
10
+ delete simpleClaims[id];
11
+ }
12
+ }
13
+ return simpleClaims;
14
+ }
15
+ exports.simplifyClaims = simplifyClaims;
16
+ function simplifyPropertyClaims(propClaims, id) {
17
+ const prop = {
18
+ id,
19
+ values: propClaims
20
+ .map((claim) => simplifyClaim(claim))
21
+ .filter((item) => item && (item.value_string || item.value !== null))
22
+ };
23
+ return prop;
24
+ }
25
+ exports.simplifyPropertyClaims = simplifyPropertyClaims;
26
+ function simplifyClaim(claim) {
27
+ const { mainsnak, qualifiers } = claim;
28
+ if (mainsnak === null)
29
+ return null;
30
+ const { datatype, datavalue } = mainsnak;
31
+ if (datavalue === null || datavalue === undefined || datavalue.value === null)
32
+ return null;
33
+ let value = null;
34
+ let value_string;
35
+ switch (datatype) {
36
+ case "string":
37
+ case "commonsMedia":
38
+ case "url":
39
+ case "external-id":
40
+ value = datavalue.value;
41
+ break;
42
+ case "monolingualtext":
43
+ value = datavalue.value.text;
44
+ break;
45
+ case "wikibase-item":
46
+ value = datavalue.value.id;
47
+ break;
48
+ case "wikibase-property":
49
+ value = datavalue.value;
50
+ break;
51
+ case "time":
52
+ value = datavalue.value;
53
+ value_string = stringDatetime(datavalue.value);
54
+ break;
55
+ case "quantity":
56
+ value = datavalue.value;
57
+ value_string = parseFloat(datavalue.value.amount).toString();
58
+ break;
59
+ case "globe-coordinate":
60
+ value = datavalue.value;
61
+ value_string = getLatLngFromCoordinates(datavalue.value).join(",");
62
+ break;
63
+ }
64
+ const simpleQualifiers = {};
65
+ for (let qualifierProp in qualifiers) {
66
+ simpleQualifiers[qualifierProp] = qualifiers[qualifierProp].map(prepareQualifierClaim);
67
+ }
68
+ const result = {
69
+ datatype,
70
+ value,
71
+ qualifiers: simplifyClaims(simpleQualifiers)
72
+ };
73
+ if (value_string) {
74
+ result["value_string"] = value_string;
75
+ }
76
+ return result;
77
+ }
78
+ exports.simplifyClaim = simplifyClaim;
79
+ const getLatLngFromCoordinates = (value) => [
80
+ value.latitude.toFixed(4).replace(/[0]+$/, "").replace(/\.$/, ""),
81
+ value.longitude.toFixed(4).replace(/[0]+$/, "").replace(/\.$/, "")
82
+ ];
83
+ const prepareQualifierClaim = (claim) => ({ mainsnak: claim });
84
+ const stringDatetime = (value) => {
85
+ let date = value.time;
86
+ const p = value.precision;
87
+ if (p >= 9) {
88
+ date = date.substr(1);
89
+ if (p < 12) {
90
+ date = date.split("T")[0];
91
+ if (p === 10) {
92
+ date = date.split("-").slice(0, 2).join("-");
93
+ }
94
+ else if (p === 9) {
95
+ date = date.split("-")[0];
96
+ }
97
+ }
98
+ }
99
+ return date;
100
+ };
@@ -1,76 +1,76 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.simplifySitelinks = exports.simplifyLabels = exports.simplifyDescriptions = exports.simplifyAliases = exports.simplifyEntity = void 0;
4
- const simplify_claims_1 = require("./simplify_claims");
5
- function simplifyEntity(lang, data, options = {}) {
6
- const entity = { id: data.id };
7
- if (data.pageid)
8
- entity.pageid = data.pageid;
9
- if (data.redirectsToId)
10
- entity.redirectsToId = data.redirectsToId;
11
- if (data.redirectsFromId)
12
- entity.redirectsFromId = data.redirectsFromId;
13
- if (options.labels !== false && data.labels) {
14
- entity.labels = simplifyLabels(data.labels);
15
- entity.label = entity.labels[lang];
16
- }
17
- if (options.descriptions !== false && data.descriptions) {
18
- entity.description = simplifyDescriptions(data.descriptions)[lang];
19
- if (!entity.description)
20
- delete entity.description;
21
- }
22
- if (options.aliases !== false && data.aliases) {
23
- entity.aliases = simplifyAliases(data.aliases)[lang];
24
- if (!entity.aliases)
25
- delete entity.aliases;
26
- }
27
- if (options.sitelinks !== false && data.sitelinks)
28
- entity.sitelinks = simplifySitelinks(data.sitelinks);
29
- if (options.claims !== false && data.claims)
30
- entity.claims = (0, simplify_claims_1.simplifyClaims)(data.claims);
31
- return entity;
32
- }
33
- exports.simplifyEntity = simplifyEntity;
34
- function simplifyAliases(data) {
35
- return getManyLangValue(data);
36
- }
37
- exports.simplifyAliases = simplifyAliases;
38
- function simplifyDescriptions(data) {
39
- return getLangValue(data);
40
- }
41
- exports.simplifyDescriptions = simplifyDescriptions;
42
- function simplifyLabels(data) {
43
- return getLangValue(data);
44
- }
45
- exports.simplifyLabels = simplifyLabels;
46
- function simplifySitelinks(data) {
47
- if (data) {
48
- const result = {};
49
- Object.keys(data).forEach((site) => {
50
- result[site.replace(/wiki$/, "")] = data[site].title;
51
- });
52
- return result;
53
- }
54
- return null;
55
- }
56
- exports.simplifySitelinks = simplifySitelinks;
57
- function getLangValue(data) {
58
- if (data) {
59
- const result = {};
60
- Object.keys(data).forEach((lang) => {
61
- result[lang] = data[lang].value;
62
- });
63
- return result;
64
- }
65
- return null;
66
- }
67
- function getManyLangValue(data) {
68
- if (data) {
69
- const result = {};
70
- Object.keys(data).forEach((lang) => {
71
- result[lang] = data[lang].map((item) => item.value);
72
- });
73
- return result;
74
- }
75
- return null;
76
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.simplifySitelinks = exports.simplifyLabels = exports.simplifyDescriptions = exports.simplifyAliases = exports.simplifyEntity = void 0;
4
+ const simplify_claims_1 = require("./simplify_claims");
5
+ function simplifyEntity(lang, data, options = {}) {
6
+ const entity = { id: data.id };
7
+ if (data.pageid)
8
+ entity.pageid = data.pageid;
9
+ if (data.redirectsToId)
10
+ entity.redirectsToId = data.redirectsToId;
11
+ if (data.redirectsFromId)
12
+ entity.redirectsFromId = data.redirectsFromId;
13
+ if (options.labels !== false && data.labels) {
14
+ entity.labels = simplifyLabels(data.labels);
15
+ entity.label = entity.labels[lang];
16
+ }
17
+ if (options.descriptions !== false && data.descriptions) {
18
+ entity.description = simplifyDescriptions(data.descriptions)[lang];
19
+ if (!entity.description)
20
+ delete entity.description;
21
+ }
22
+ if (options.aliases !== false && data.aliases) {
23
+ entity.aliases = simplifyAliases(data.aliases)[lang];
24
+ if (!entity.aliases)
25
+ delete entity.aliases;
26
+ }
27
+ if (options.sitelinks !== false && data.sitelinks)
28
+ entity.sitelinks = simplifySitelinks(data.sitelinks);
29
+ if (options.claims !== false && data.claims)
30
+ entity.claims = (0, simplify_claims_1.simplifyClaims)(data.claims);
31
+ return entity;
32
+ }
33
+ exports.simplifyEntity = simplifyEntity;
34
+ function simplifyAliases(data) {
35
+ return getManyLangValue(data);
36
+ }
37
+ exports.simplifyAliases = simplifyAliases;
38
+ function simplifyDescriptions(data) {
39
+ return getLangValue(data);
40
+ }
41
+ exports.simplifyDescriptions = simplifyDescriptions;
42
+ function simplifyLabels(data) {
43
+ return getLangValue(data);
44
+ }
45
+ exports.simplifyLabels = simplifyLabels;
46
+ function simplifySitelinks(data) {
47
+ if (data) {
48
+ const result = {};
49
+ Object.keys(data).forEach((site) => {
50
+ result[site.replace(/wiki$/, "")] = data[site].title;
51
+ });
52
+ return result;
53
+ }
54
+ return null;
55
+ }
56
+ exports.simplifySitelinks = simplifySitelinks;
57
+ function getLangValue(data) {
58
+ if (data) {
59
+ const result = {};
60
+ Object.keys(data).forEach((lang) => {
61
+ result[lang] = data[lang].value;
62
+ });
63
+ return result;
64
+ }
65
+ return null;
66
+ }
67
+ function getManyLangValue(data) {
68
+ if (data) {
69
+ const result = {};
70
+ Object.keys(data).forEach((lang) => {
71
+ result[lang] = data[lang].map((item) => item.value);
72
+ });
73
+ return result;
74
+ }
75
+ return null;
76
+ }