wiki-entity 0.7.0 → 1.0.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/CHANGELOG.md +172 -0
- package/LICENSE +15 -0
- package/README.md +287 -89
- package/dist/index.cjs +1312 -0
- package/dist/index.d.cts +370 -0
- package/dist/index.d.ts +370 -0
- package/dist/index.js +1296 -0
- package/package.json +59 -24
- package/data/countries.json +0 -1
- package/data/entity_types.json +0 -1
- package/lib/index.js +0 -129
- package/lib/request.js +0 -37
- package/lib/simpleEntity/convertToSimpleEntity.js +0 -53
- package/lib/simpleEntity/getEntityCountry.js +0 -25
- package/lib/simpleEntity/getEntityData.js +0 -27
- package/lib/simpleEntity/getEntityInstanceType.js +0 -32
- package/lib/simpleEntity/getEntityType.js +0 -62
- package/lib/simpleEntity/getEntityTypeByExtract.js +0 -28
- package/lib/simpleEntity/index.js +0 -7
- package/lib/simpleEntity/simpleEntity.js +0 -20
- package/lib/types.js +0 -13
- package/lib/utils.js +0 -15
- package/lib/wikidata/api.js +0 -106
- package/lib/wikidata/get_entity_types.js +0 -60
- package/lib/wikidata/index.js +0 -112
- package/lib/wikidata/simplify_claims.js +0 -100
- package/lib/wikidata/simplify_entity.js +0 -76
- package/lib/wikipedia/api.js +0 -146
- package/types/index.d.ts +0 -9
- package/types/request.d.ts +0 -5
- package/types/simpleEntity/convertToSimpleEntity.d.ts +0 -6
- package/types/simpleEntity/getEntityCountry.d.ts +0 -2
- package/types/simpleEntity/getEntityData.d.ts +0 -3
- package/types/simpleEntity/getEntityInstanceType.d.ts +0 -3
- package/types/simpleEntity/getEntityType.d.ts +0 -3
- package/types/simpleEntity/getEntityTypeByExtract.d.ts +0 -2
- package/types/simpleEntity/index.d.ts +0 -2
- package/types/simpleEntity/simpleEntity.d.ts +0 -29
- package/types/types.d.ts +0 -68
- package/types/utils.d.ts +0 -8
- package/types/wikidata/api.d.ts +0 -4
- package/types/wikidata/get_entity_types.d.ts +0 -1
- package/types/wikidata/index.d.ts +0 -3
- package/types/wikidata/simplify_claims.d.ts +0 -4
- package/types/wikidata/simplify_entity.d.ts +0 -21
- package/types/wikipedia/api.d.ts +0 -31
package/lib/index.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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,62 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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;
|
package/lib/wikidata/api.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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
|
-
}
|