wiki-entity 0.4.2 → 0.4.6
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 +117 -114
- package/lib/index.js +30 -14
- package/lib/request.js +14 -29
- package/lib/simpleEntity/getEntityData.js +4 -2
- package/lib/simpleEntity/getEntityType.js +19 -0
- package/lib/wikidata/api.js +62 -51
- package/lib/wikidata/get_entity_types.js +27 -24
- package/lib/wikidata/index.js +23 -23
- package/lib/wikidata/simplify_entity.js +11 -4
- package/lib/wikipedia/api.js +98 -88
- package/package.json +8 -9
- package/types/index.d.ts +5 -5
- package/types/request.d.ts +3 -1
- package/types/types.d.ts +4 -1
- package/types/wikidata/api.d.ts +1 -1
- package/types/wikidata/index.d.ts +1 -1
- package/types/wikidata/simplify_entity.d.ts +1 -1
- package/types/wikipedia/api.d.ts +6 -5
package/README.md
CHANGED
|
@@ -1,114 +1,117 @@
|
|
|
1
|
-
# wiki-entity
|
|
2
|
-
|
|
3
|
-
Wiki(pedia/data) entity extractor.
|
|
4
|
-
|
|
5
|
-
## Usage
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
import { getEntities } from 'wiki-entity';
|
|
9
|
-
|
|
10
|
-
// get Europe by title
|
|
11
|
-
getEntities({ language: 'en', titles: ['Europe'] }).then(entities => {});
|
|
12
|
-
|
|
13
|
-
// get Europe by id
|
|
14
|
-
getEntities({ language: 'en', ids: ['Q46'] }).then(entities => {});
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## API
|
|
18
|
-
|
|
19
|
-
### getEntities(params): Promise<WikiEntity[]>
|
|
20
|
-
|
|
21
|
-
Gets entities from wikidata and wikipedia.
|
|
22
|
-
|
|
23
|
-
#### params
|
|
24
|
-
|
|
25
|
-
Required. `ids` or `titles` require. Params properties:
|
|
26
|
-
|
|
27
|
-
- **ids** :*string[]* - wikidata ids; (max 200)
|
|
28
|
-
- **titles** :*string[]* - wikipedia article titles; (max 200)
|
|
29
|
-
- **language** :*string* - language of `titles` param and of the result object info; default: `en`
|
|
30
|
-
- **props** :*string[]* - entity props to get. Can by: `info`|`sitelinks`|`aliases`|`labels`|`descriptions`|`claims`|`datatype`;
|
|
31
|
-
- **claims** :*string* - How to resolve the claims. Can be: `none`, `all`, `item`, `property`. Default: `none`. `all` resolves `item` and `property` types.
|
|
32
|
-
- **extract** :*number* - Sentences in the extract. Default: `0`. Works only if `sitelinks` is present.
|
|
33
|
-
- **types** :*boolean* | *string*[] - `true` to get entity types. Filter types by [prefixes](https://dbpedia.org/sparql?nsdecl). Example: [`dbo`, `schema`] will return only types defined by `dbpedia.org/ontology/` and `schema.org`. Default: `false`.
|
|
34
|
-
- **redirects** : *boolean* - get wikipedia redirects titles. Default: `false`. Works only if `sitelinks` is present.
|
|
35
|
-
- **categories** : *boolean* - get wikipedia article categories. Default: `false`. Works only if `sitelinks` is present.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
1
|
+
# wiki-entity
|
|
2
|
+
|
|
3
|
+
Wiki(pedia/data) entity extractor.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { getEntities } from 'wiki-entity';
|
|
9
|
+
|
|
10
|
+
// get Europe by title
|
|
11
|
+
getEntities({ language: 'en', titles: ['Europe'] }).then(entities => {});
|
|
12
|
+
|
|
13
|
+
// get Europe by id
|
|
14
|
+
getEntities({ language: 'en', ids: ['Q46'] }).then(entities => {});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
### getEntities(params): Promise<WikiEntity[]>
|
|
20
|
+
|
|
21
|
+
Gets entities from wikidata and wikipedia.
|
|
22
|
+
|
|
23
|
+
#### params
|
|
24
|
+
|
|
25
|
+
Required. `ids` or `titles` require. Params properties:
|
|
26
|
+
|
|
27
|
+
- **ids** :*string[]* - wikidata ids; (max 200)
|
|
28
|
+
- **titles** :*string[]* - wikipedia article titles; (max 200)
|
|
29
|
+
- **language** :*string* - language of `titles` param and of the result object info; default: `en`
|
|
30
|
+
- **props** :*string[]* - entity props to get. Can by: `info`|`sitelinks`|`aliases`|`labels`|`descriptions`|`claims`|`datatype`;
|
|
31
|
+
- **claims** :*string* - How to resolve the claims. Can be: `none`, `all`, `item`, `property`. Default: `none`. `all` resolves `item` and `property` types.
|
|
32
|
+
- **extract** :*number* - Sentences in the extract. Default: `0`. Works only if `sitelinks` is present.
|
|
33
|
+
- **types** :*boolean* | *string*[] - `true` to get entity types. Filter types by [prefixes](https://dbpedia.org/sparql?nsdecl). Example: [`dbo`, `schema`] will return only types defined by `dbpedia.org/ontology/` and `schema.org`. Default: `false`.
|
|
34
|
+
- **redirects** : *boolean* - get wikipedia redirects titles. Default: `false`. Works only if `sitelinks` is present.
|
|
35
|
+
- **categories** : *boolean* - get wikipedia article categories. Default: `false`. Works only if `sitelinks` is present.
|
|
36
|
+
- **httpTimeout**: *number* - http requests timeout
|
|
37
|
+
- **languages**: "string"[] - languages for `WikiEntity.labels`
|
|
38
|
+
|
|
39
|
+
### convertToSimpleEntity(wikiEntity: WikiEntity): SimpleEntity
|
|
40
|
+
|
|
41
|
+
Convert a complex WikiEntity object to SimpleEntity object.
|
|
42
|
+
|
|
43
|
+
## WikiEntity
|
|
44
|
+
|
|
45
|
+
`WikiEntity` is a simple version of an item returned by Wikidata API. It also may include some extra properties from Wikipadia API.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
|
|
49
|
+
type WikiEntity = {
|
|
50
|
+
id: string;
|
|
51
|
+
label?: string;
|
|
52
|
+
labels?: IIndexType<string>;
|
|
53
|
+
description?: string;
|
|
54
|
+
extract?: string;
|
|
55
|
+
pageid?: number;
|
|
56
|
+
aliases?: string[];
|
|
57
|
+
sitelinks?: IIndexType<string>;
|
|
58
|
+
claims?: IIndexType<WikidataProperty>;
|
|
59
|
+
types?: string[];
|
|
60
|
+
redirects?: string[];
|
|
61
|
+
categories?: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type WikidataProperty = {
|
|
65
|
+
id: string;
|
|
66
|
+
label?: string;
|
|
67
|
+
description?: string;
|
|
68
|
+
values: WikidataPropertyValue[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type WikidataPropertyValue = {
|
|
72
|
+
datatype: string;
|
|
73
|
+
value: any;
|
|
74
|
+
pageid?: number;
|
|
75
|
+
value_string?: string;
|
|
76
|
+
label?: string;
|
|
77
|
+
description?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type IIndexType<T> = {
|
|
81
|
+
[index: string]: T
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## SimpleEntity
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
|
|
90
|
+
export enum SimpleEntityType {
|
|
91
|
+
EVENT = 'E',
|
|
92
|
+
ORG = 'O',
|
|
93
|
+
PERSON = 'H',
|
|
94
|
+
PLACE = 'P',
|
|
95
|
+
PRODUCT = 'R',
|
|
96
|
+
WORK = 'W',
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type SimpleEntityData = { [prop: string]: string[] }
|
|
100
|
+
|
|
101
|
+
export type SimpleEntity = {
|
|
102
|
+
lang?: string
|
|
103
|
+
wikiDataId?: string
|
|
104
|
+
name?: string
|
|
105
|
+
abbr?: string
|
|
106
|
+
description?: string
|
|
107
|
+
about?: string
|
|
108
|
+
wikiPageId?: number
|
|
109
|
+
wikiPageTitle?: string
|
|
110
|
+
type?: SimpleEntityType
|
|
111
|
+
types?: string[]
|
|
112
|
+
countryCodes?: string[]
|
|
113
|
+
data?: SimpleEntityData
|
|
114
|
+
categories?: string[]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
```
|
package/lib/index.js
CHANGED
|
@@ -13,16 +13,16 @@ exports.simplifyEntity = simplify_entity_1.simplifyEntity;
|
|
|
13
13
|
__export(require("./simpleEntity"));
|
|
14
14
|
__export(require("./types"));
|
|
15
15
|
function getEntities(params) {
|
|
16
|
-
const lang = params.language ||
|
|
16
|
+
const lang = params.language || "en";
|
|
17
17
|
return wikidata_1.getEntities(params)
|
|
18
18
|
.then(function (wikiDataEntities) {
|
|
19
19
|
const entities = wikiDataEntities;
|
|
20
|
-
const ids = Object.keys(entities).filter(id => utils_1.isValidWikiId(id));
|
|
20
|
+
const ids = Object.keys(entities).filter((id) => utils_1.isValidWikiId(id));
|
|
21
21
|
if (ids.length === 0) {
|
|
22
22
|
return entities;
|
|
23
23
|
}
|
|
24
24
|
const tasks = [];
|
|
25
|
-
const wikiApi = new api_1.Api();
|
|
25
|
+
const wikiApi = new api_1.Api({}, params.httpTimeout);
|
|
26
26
|
let callWikiApi = false;
|
|
27
27
|
if (params.extract) {
|
|
28
28
|
callWikiApi = true;
|
|
@@ -36,16 +36,26 @@ function getEntities(params) {
|
|
|
36
36
|
callWikiApi = true;
|
|
37
37
|
wikiApi.categories();
|
|
38
38
|
}
|
|
39
|
-
if (callWikiApi && entities[ids[0]].sitelinks) {
|
|
40
|
-
const listEntities = ids.map(id => entities[id]);
|
|
39
|
+
if (callWikiApi && entities[ids[0]] && entities[ids[0]].sitelinks) {
|
|
41
40
|
const titleIds = ids.reduce((prev, id) => {
|
|
42
|
-
if (entities[id]
|
|
41
|
+
if (entities[id] &&
|
|
42
|
+
entities[id].sitelinks &&
|
|
43
|
+
entities[id].sitelinks[lang]) {
|
|
43
44
|
prev[entities[id].sitelinks[lang]] = id;
|
|
44
45
|
}
|
|
45
46
|
return prev;
|
|
46
47
|
}, {});
|
|
47
|
-
tasks.push(wikiApi
|
|
48
|
-
.
|
|
48
|
+
tasks.push(wikiApi
|
|
49
|
+
.query(lang, {
|
|
50
|
+
titles: ids
|
|
51
|
+
.map((id) => (entities[id] &&
|
|
52
|
+
entities[id].sitelinks &&
|
|
53
|
+
entities[id].sitelinks[lang]) ||
|
|
54
|
+
null)
|
|
55
|
+
.filter((item) => !!item)
|
|
56
|
+
.join("|")
|
|
57
|
+
})
|
|
58
|
+
.then((apiResults) => apiResults.forEach((result) => {
|
|
49
59
|
const entity = entities[titleIds[result.title]];
|
|
50
60
|
if (params.extract) {
|
|
51
61
|
entity.extract = result.extract;
|
|
@@ -59,12 +69,16 @@ function getEntities(params) {
|
|
|
59
69
|
})));
|
|
60
70
|
}
|
|
61
71
|
if (params.types === true || Array.isArray(params.types)) {
|
|
62
|
-
const prefixes = Array.isArray(params.types)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
const prefixes = Array.isArray(params.types)
|
|
73
|
+
? params.types
|
|
74
|
+
: null;
|
|
75
|
+
ids.forEach((id) => {
|
|
76
|
+
if (entities[id] && entities[id].sitelinks) {
|
|
77
|
+
const enName = entities[id].sitelinks["en"];
|
|
66
78
|
if (enName) {
|
|
67
|
-
tasks.push(get_entity_types_1.getEntityTypesByName(enName, prefixes).then(types => {
|
|
79
|
+
tasks.push(get_entity_types_1.getEntityTypesByName(enName, prefixes).then((types) => {
|
|
80
|
+
entities[id].types = types;
|
|
81
|
+
}));
|
|
68
82
|
}
|
|
69
83
|
}
|
|
70
84
|
});
|
|
@@ -72,7 +86,9 @@ function getEntities(params) {
|
|
|
72
86
|
return Promise.all(tasks).then(() => entities);
|
|
73
87
|
})
|
|
74
88
|
.then(function (resultEntities) {
|
|
75
|
-
return Object.keys(resultEntities)
|
|
89
|
+
return Object.keys(resultEntities)
|
|
90
|
+
.map((id) => resultEntities[id])
|
|
91
|
+
.filter((it) => !!it);
|
|
76
92
|
});
|
|
77
93
|
}
|
|
78
94
|
exports.getEntities = getEntities;
|
package/lib/request.js
CHANGED
|
@@ -1,33 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
2
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
json: true,
|
|
9
|
-
encoding: 'utf8',
|
|
10
|
-
headers: {
|
|
11
|
-
'User-Agent': 'entity-finder'
|
|
12
|
-
},
|
|
13
|
-
timeout: 5 * 1000
|
|
14
|
-
}, options);
|
|
15
|
-
if (options.qs) {
|
|
16
|
-
for (var prop in options.qs) {
|
|
17
|
-
if (~[null].indexOf(options.qs[prop])) {
|
|
18
|
-
delete options.qs[prop];
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
debug('request ' + (options.uri || options.url), options.qs);
|
|
23
|
-
return new Promise(function (resolve, reject) {
|
|
24
|
-
request(options, function (error, response, body) {
|
|
25
|
-
if (error || response.statusCode >= 400) {
|
|
26
|
-
return reject(error || new Error('Invalid statusCode=' + response.statusCode + (options.uri || options.url)));
|
|
27
|
-
}
|
|
28
|
-
resolve(body);
|
|
29
|
-
});
|
|
11
|
+
const axios_1 = require("axios");
|
|
12
|
+
function default_1(url, options) {
|
|
13
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
+
const response = yield axios_1.default(url, Object.assign({ method: "GET", responseType: "json", headers: { "User-Agent": "entity-finder" } }, options, { timeout: options.timeout || 10 * 1000 }));
|
|
15
|
+
return response.data;
|
|
30
16
|
});
|
|
31
17
|
}
|
|
32
18
|
exports.default = default_1;
|
|
33
|
-
;
|
|
@@ -8,11 +8,13 @@ function getEntityData(wikiEntity) {
|
|
|
8
8
|
for (let key in wikiEntity.claims) {
|
|
9
9
|
const values = wikiEntity.claims[key].values;
|
|
10
10
|
data[key] = [];
|
|
11
|
-
values.forEach(value => {
|
|
11
|
+
values.forEach((value) => {
|
|
12
12
|
if (!value.value_string && value.value === null) {
|
|
13
13
|
throw new Error(`WikiEntity claim value cannot be null: ${wikiEntity.id}:${key}`);
|
|
14
14
|
}
|
|
15
|
-
const v = {
|
|
15
|
+
const v = {
|
|
16
|
+
value: value.value_string || value.value.toString()
|
|
17
|
+
};
|
|
16
18
|
if (value.label && v.value !== value.label) {
|
|
17
19
|
v.label = value.label;
|
|
18
20
|
}
|
|
@@ -13,6 +13,7 @@ const TYPES_MAP = {
|
|
|
13
13
|
'dbo:Location': simpleEntity_1.SimpleEntityType.PLACE,
|
|
14
14
|
'wikidata:Q515': simpleEntity_1.SimpleEntityType.PLACE,
|
|
15
15
|
'wikidata:Q486972': simpleEntity_1.SimpleEntityType.PLACE,
|
|
16
|
+
'dbo:Company': simpleEntity_1.SimpleEntityType.ORG,
|
|
16
17
|
'schema:Organization': simpleEntity_1.SimpleEntityType.ORG,
|
|
17
18
|
'dbo:Organisation': simpleEntity_1.SimpleEntityType.ORG,
|
|
18
19
|
'wikidata:Q43229': simpleEntity_1.SimpleEntityType.ORG,
|
|
@@ -29,10 +30,20 @@ const TYPES_MAP = {
|
|
|
29
30
|
'dbo:Software': simpleEntity_1.SimpleEntityType.PRODUCT,
|
|
30
31
|
'wikidata:Q7397': simpleEntity_1.SimpleEntityType.PRODUCT,
|
|
31
32
|
};
|
|
33
|
+
const TYPES_NAME_MAP = Object.keys(TYPES_MAP)
|
|
34
|
+
.reduce((map, name) => {
|
|
35
|
+
const type = TYPES_MAP[name];
|
|
36
|
+
map[type] = map[type] || [];
|
|
37
|
+
map[type].push(name);
|
|
38
|
+
return map;
|
|
39
|
+
}, {});
|
|
32
40
|
function getEntityType(wikiEntity) {
|
|
33
41
|
if (!wikiEntity.types) {
|
|
34
42
|
return null;
|
|
35
43
|
}
|
|
44
|
+
if (containsType(wikiEntity.types, simpleEntity_1.SimpleEntityType.ORG) && containsType(wikiEntity.types, simpleEntity_1.SimpleEntityType.PERSON)) {
|
|
45
|
+
return simpleEntity_1.SimpleEntityType.ORG;
|
|
46
|
+
}
|
|
36
47
|
for (var i = 0; i < wikiEntity.types.length; i++) {
|
|
37
48
|
if (TYPES_MAP[wikiEntity.types[i]]) {
|
|
38
49
|
return TYPES_MAP[wikiEntity.types[i]];
|
|
@@ -40,3 +51,11 @@ function getEntityType(wikiEntity) {
|
|
|
40
51
|
}
|
|
41
52
|
}
|
|
42
53
|
exports.getEntityType = getEntityType;
|
|
54
|
+
function containsType(types, type) {
|
|
55
|
+
for (const typeName of types) {
|
|
56
|
+
if (~TYPES_NAME_MAP[type].indexOf(typeName)) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
package/lib/wikidata/api.js
CHANGED
|
@@ -1,43 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
2
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
11
|
const request_1 = require("../request");
|
|
4
12
|
const types_1 = require("../types");
|
|
5
13
|
const utils_1 = require("../utils");
|
|
6
|
-
const API_URL =
|
|
14
|
+
const API_URL = "https://www.wikidata.org/w/api.php";
|
|
7
15
|
function getEntities(params) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (hasError(data)) {
|
|
35
|
-
return Promise.reject(getError(data));
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const qs = {
|
|
18
|
+
action: "wbgetentities",
|
|
19
|
+
ids: params.ids && params.ids.join("|"),
|
|
20
|
+
titles: params.titles && params.titles.join("|"),
|
|
21
|
+
props: (params.props || [
|
|
22
|
+
types_1.WikidataPropsParam.info,
|
|
23
|
+
types_1.WikidataPropsParam.sitelinks,
|
|
24
|
+
types_1.WikidataPropsParam.aliases,
|
|
25
|
+
types_1.WikidataPropsParam.labels,
|
|
26
|
+
types_1.WikidataPropsParam.descriptions,
|
|
27
|
+
types_1.WikidataPropsParam.claims,
|
|
28
|
+
types_1.WikidataPropsParam.datatype
|
|
29
|
+
]).join("|"),
|
|
30
|
+
languages: params.languages
|
|
31
|
+
? params.languages.join("|")
|
|
32
|
+
: params.language || "en",
|
|
33
|
+
redirects: params.redirect || "yes",
|
|
34
|
+
format: "json",
|
|
35
|
+
sites: ""
|
|
36
|
+
};
|
|
37
|
+
if (params.titles) {
|
|
38
|
+
qs.sites = qs.languages.split("|")[0] + "wiki";
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
delete qs.sites;
|
|
36
42
|
}
|
|
37
|
-
const
|
|
43
|
+
const data = yield 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) || {};
|
|
38
50
|
for (let id of Object.keys(entities)) {
|
|
39
|
-
if (!utils_1.isValidWikiId(id)) {
|
|
40
|
-
entities[id]
|
|
51
|
+
if (!utils_1.isValidWikiId(id) || ~Object.keys(entities[id]).indexOf("missing")) {
|
|
52
|
+
delete entities[id];
|
|
41
53
|
}
|
|
42
54
|
}
|
|
43
55
|
return entities;
|
|
@@ -45,25 +57,21 @@ function getEntities(params) {
|
|
|
45
57
|
}
|
|
46
58
|
exports.getEntities = getEntities;
|
|
47
59
|
function getManyEntities(params) {
|
|
48
|
-
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
61
|
validateParams(params);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
partParams[keyName] = keyValues.slice(i * max, (i + 1) * max);
|
|
62
|
-
if (partParams[keyName].length > 0) {
|
|
63
|
-
parts.push(getEntities(partParams));
|
|
62
|
+
const keyName = !!params.ids ? "ids" : "titles";
|
|
63
|
+
const keyValues = params[keyName];
|
|
64
|
+
const max = 50;
|
|
65
|
+
const countParts = keyValues.length / max + 1;
|
|
66
|
+
const parts = [];
|
|
67
|
+
for (var i = 0; i < countParts && i < 4; i++) {
|
|
68
|
+
const partParams = Object.assign({}, params);
|
|
69
|
+
partParams[keyName] = keyValues.slice(i * max, (i + 1) * max);
|
|
70
|
+
if (partParams[keyName].length > 0) {
|
|
71
|
+
parts.push(getEntities(partParams));
|
|
72
|
+
}
|
|
64
73
|
}
|
|
65
|
-
|
|
66
|
-
return Promise.all(parts).then(function (results) {
|
|
74
|
+
const results = yield Promise.all(parts);
|
|
67
75
|
if (results.length === 0) {
|
|
68
76
|
return null;
|
|
69
77
|
}
|
|
@@ -78,12 +86,15 @@ function getManyEntities(params) {
|
|
|
78
86
|
exports.getManyEntities = getManyEntities;
|
|
79
87
|
function validateParams(params) {
|
|
80
88
|
if (!params.ids && !params.titles) {
|
|
81
|
-
throw new Error(
|
|
89
|
+
throw new Error("Invalid params: `ids` or `titles` are required");
|
|
82
90
|
}
|
|
83
91
|
}
|
|
84
92
|
exports.validateParams = validateParams;
|
|
85
93
|
function getError(data) {
|
|
86
|
-
return data &&
|
|
94
|
+
return ((data &&
|
|
95
|
+
data.error &&
|
|
96
|
+
new Error(data.error.info || "Wikidata Api Error")) ||
|
|
97
|
+
new Error("NO error"));
|
|
87
98
|
}
|
|
88
99
|
function hasError(data) {
|
|
89
100
|
return !!data.error;
|
|
@@ -2,48 +2,51 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const request_1 = require("../request");
|
|
4
4
|
const PREFIXES_MAP = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
"http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#": "dul",
|
|
6
|
+
"http://dbpedia.org/ontology/": "dbo",
|
|
7
|
+
"http://www.w3.org/2002/07/owl#": "owl",
|
|
8
|
+
"http://www.wikidata.org/entity/": "wikidata",
|
|
9
|
+
"http://schema.org/": "schema",
|
|
10
|
+
"http://xmlns.com/foaf/0.1/": "foaf",
|
|
11
|
+
"http://www.w3.org/2003/01/geo/wgs84_pos#": "geo"
|
|
12
12
|
};
|
|
13
|
-
const PREFIXES = Object.keys(PREFIXES_MAP).map(key => PREFIXES_MAP[key]);
|
|
14
|
-
const PREFIXES_REG = new RegExp(
|
|
13
|
+
const PREFIXES = Object.keys(PREFIXES_MAP).map((key) => PREFIXES_MAP[key]);
|
|
14
|
+
const PREFIXES_REG = new RegExp("^(" + Object.keys(PREFIXES_MAP).join("|") + ")");
|
|
15
15
|
function getEntityTypesByName(name, prefixes) {
|
|
16
16
|
if (!prefixes || !prefixes.length) {
|
|
17
17
|
prefixes = PREFIXES;
|
|
18
18
|
}
|
|
19
19
|
return dbpediaTypes(name)
|
|
20
|
-
.then(types => parseTypes(types))
|
|
21
|
-
.then(types => {
|
|
22
|
-
return types
|
|
23
|
-
.
|
|
20
|
+
.then((types) => parseTypes(types))
|
|
21
|
+
.then((types) => {
|
|
22
|
+
return types
|
|
23
|
+
.filter((item) => PREFIXES_REG.test(item))
|
|
24
|
+
.map((item) => {
|
|
24
25
|
const key = PREFIXES_REG.exec(item)[1];
|
|
25
|
-
return PREFIXES_MAP[key] +
|
|
26
|
+
return PREFIXES_MAP[key] + ":" + item.substr(key.length);
|
|
26
27
|
});
|
|
27
28
|
})
|
|
28
|
-
.then(types => repairTypes(types));
|
|
29
|
+
.then((types) => repairTypes(types));
|
|
29
30
|
}
|
|
30
31
|
exports.getEntityTypesByName = getEntityTypesByName;
|
|
31
32
|
function parseTypes(types) {
|
|
32
|
-
return types.map(item => item.type.value);
|
|
33
|
+
return types.map((item) => item.type.value);
|
|
33
34
|
}
|
|
34
35
|
function dbpediaTypes(name) {
|
|
35
|
-
const query = `
|
|
36
|
-
SELECT ?type
|
|
37
|
-
WHERE {
|
|
38
|
-
<http://dbpedia.org/resource/${name.replace(/\s+/g,
|
|
36
|
+
const query = `
|
|
37
|
+
SELECT ?type
|
|
38
|
+
WHERE {
|
|
39
|
+
<http://dbpedia.org/resource/${name.replace(/\s+/g, "_")}> rdf:type ?type
|
|
39
40
|
}`;
|
|
40
|
-
return request_1.default(
|
|
41
|
-
|
|
41
|
+
return request_1.default("http://dbpedia.org/sparql", {
|
|
42
|
+
params: { query: query },
|
|
43
|
+
timeout: 10 * 1000
|
|
44
|
+
}).then((data) => data.results && data.results.bindings);
|
|
42
45
|
}
|
|
43
46
|
function repairTypes(types) {
|
|
44
|
-
const personIndex = types.findIndex(item => /:Person$/.test(item));
|
|
47
|
+
const personIndex = types.findIndex((item) => /:Person$/.test(item));
|
|
45
48
|
if (personIndex > -1) {
|
|
46
|
-
const placeIndex = types.findIndex(item => /:Place$/.test(item));
|
|
49
|
+
const placeIndex = types.findIndex((item) => /:Place$/.test(item));
|
|
47
50
|
if (placeIndex > -1) {
|
|
48
51
|
types.splice(personIndex, 1);
|
|
49
52
|
}
|
package/lib/wikidata/index.js
CHANGED
|
@@ -5,17 +5,18 @@ const api_1 = require("./api");
|
|
|
5
5
|
const simplify_entity_1 = require("./simplify_entity");
|
|
6
6
|
const utils_1 = require("../utils");
|
|
7
7
|
function getEntities(params) {
|
|
8
|
-
const claims = params.claims ||
|
|
9
|
-
const lang = params.language ||
|
|
10
|
-
return api_1.getManyEntities(params)
|
|
11
|
-
.
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
const claims = params.claims || "none";
|
|
9
|
+
const lang = params.language || "en";
|
|
10
|
+
return api_1.getManyEntities(params).then(function (entities) {
|
|
11
|
+
const ids = Object.keys(entities).filter((id) => utils_1.isValidWikiId(id));
|
|
12
|
+
ids.forEach((id) => {
|
|
13
|
+
entities[id] = simplify_entity_1.simplifyEntity(lang, entities[id]);
|
|
14
|
+
});
|
|
14
15
|
const tasks = [];
|
|
15
|
-
if (~[
|
|
16
|
+
if (~["all", "property"].indexOf(claims)) {
|
|
16
17
|
tasks.push(exploreEntitiesProperties(entities, lang));
|
|
17
18
|
}
|
|
18
|
-
if (~[
|
|
19
|
+
if (~["all", "item"].indexOf(claims)) {
|
|
19
20
|
tasks.push(utils_1.eachSeries(ids, (id) => exploreEntityClaims(entities[id].claims, { language: lang })));
|
|
20
21
|
}
|
|
21
22
|
return Promise.all(tasks).then(() => entities);
|
|
@@ -24,9 +25,8 @@ function getEntities(params) {
|
|
|
24
25
|
exports.getEntities = getEntities;
|
|
25
26
|
function exploreEntitiesProperties(entities, lang) {
|
|
26
27
|
let ids = [];
|
|
27
|
-
const paths = {};
|
|
28
28
|
const entitiesIds = Object.keys(entities);
|
|
29
|
-
entitiesIds.forEach(entityId => {
|
|
29
|
+
entitiesIds.forEach((entityId) => {
|
|
30
30
|
const entity = entities[entityId];
|
|
31
31
|
if (entity.claims) {
|
|
32
32
|
ids = ids.concat(Object.keys(entity.claims));
|
|
@@ -43,17 +43,17 @@ function exploreEntitiesProperties(entities, lang) {
|
|
|
43
43
|
types_1.WikidataPropsParam.info,
|
|
44
44
|
types_1.WikidataPropsParam.labels,
|
|
45
45
|
types_1.WikidataPropsParam.descriptions,
|
|
46
|
-
types_1.WikidataPropsParam.datatype
|
|
46
|
+
types_1.WikidataPropsParam.datatype
|
|
47
47
|
],
|
|
48
|
-
claims:
|
|
48
|
+
claims: "none"
|
|
49
49
|
}).then(function (properties) {
|
|
50
|
-
Object.keys(properties).forEach(propertyId => {
|
|
51
|
-
entitiesIds.forEach(entityId => {
|
|
50
|
+
Object.keys(properties).forEach((propertyId) => {
|
|
51
|
+
entitiesIds.forEach((entityId) => {
|
|
52
52
|
const entity = entities[entityId];
|
|
53
53
|
if (entity.claims && entity.claims[propertyId]) {
|
|
54
54
|
if (entity.claims && entity.claims[propertyId]) {
|
|
55
55
|
for (var prop in properties[propertyId]) {
|
|
56
|
-
if (~[
|
|
56
|
+
if (~["label", "description"].indexOf(prop)) {
|
|
57
57
|
entity.claims[propertyId][prop] = properties[propertyId][prop];
|
|
58
58
|
}
|
|
59
59
|
}
|
|
@@ -70,9 +70,9 @@ function exploreEntityClaims(claims, params) {
|
|
|
70
70
|
}
|
|
71
71
|
const ids = [];
|
|
72
72
|
const paths = {};
|
|
73
|
-
Object.keys(claims).forEach(property => {
|
|
73
|
+
Object.keys(claims).forEach((property) => {
|
|
74
74
|
claims[property].values.forEach((propertyValue, index) => {
|
|
75
|
-
if (propertyValue.datatype ===
|
|
75
|
+
if (propertyValue.datatype === "wikibase-item") {
|
|
76
76
|
const id = propertyValue.value;
|
|
77
77
|
paths[id] = paths[id] || [];
|
|
78
78
|
paths[id].push({ pid: property, value: propertyValue, index });
|
|
@@ -90,17 +90,17 @@ function exploreEntityClaims(claims, params) {
|
|
|
90
90
|
types_1.WikidataPropsParam.info,
|
|
91
91
|
types_1.WikidataPropsParam.labels,
|
|
92
92
|
types_1.WikidataPropsParam.descriptions,
|
|
93
|
-
types_1.WikidataPropsParam.datatype
|
|
93
|
+
types_1.WikidataPropsParam.datatype
|
|
94
94
|
];
|
|
95
|
-
params.claims = params.claims ||
|
|
96
|
-
return getEntities(params).then(entities => {
|
|
97
|
-
Object.keys(entities).forEach(id => {
|
|
95
|
+
params.claims = params.claims || "none";
|
|
96
|
+
return getEntities(params).then((entities) => {
|
|
97
|
+
Object.keys(entities).forEach((id) => {
|
|
98
98
|
const item = entities[id];
|
|
99
99
|
const pa = paths[item.id] || [];
|
|
100
|
-
pa.forEach(pai => {
|
|
100
|
+
pa.forEach((pai) => {
|
|
101
101
|
const val = claims[pai.pid].values[pai.index];
|
|
102
102
|
for (var prop in item) {
|
|
103
|
-
if (~[
|
|
103
|
+
if (~["label", "pageid", "description"].indexOf(prop)) {
|
|
104
104
|
val[prop] = item[prop];
|
|
105
105
|
}
|
|
106
106
|
}
|
|
@@ -7,7 +7,8 @@ function simplifyEntity(lang, data, options = {}) {
|
|
|
7
7
|
entity.pageid = data.pageid;
|
|
8
8
|
}
|
|
9
9
|
if (options.labels !== false && data.labels) {
|
|
10
|
-
entity.
|
|
10
|
+
entity.labels = simplifyLabels(data.labels);
|
|
11
|
+
entity.label = entity.labels[lang];
|
|
11
12
|
}
|
|
12
13
|
if (options.descriptions !== false && data.descriptions) {
|
|
13
14
|
entity.description = simplifyDescriptions(data.descriptions)[lang];
|
|
@@ -45,7 +46,9 @@ exports.simplifyLabels = simplifyLabels;
|
|
|
45
46
|
function simplifySitelinks(data) {
|
|
46
47
|
if (data) {
|
|
47
48
|
const result = {};
|
|
48
|
-
Object.keys(data).forEach(site => {
|
|
49
|
+
Object.keys(data).forEach((site) => {
|
|
50
|
+
result[site.replace(/wiki$/, "")] = data[site].title;
|
|
51
|
+
});
|
|
49
52
|
return result;
|
|
50
53
|
}
|
|
51
54
|
return null;
|
|
@@ -54,7 +57,9 @@ exports.simplifySitelinks = simplifySitelinks;
|
|
|
54
57
|
function getLangValue(data) {
|
|
55
58
|
if (data) {
|
|
56
59
|
const result = {};
|
|
57
|
-
Object.keys(data).forEach(lang => {
|
|
60
|
+
Object.keys(data).forEach((lang) => {
|
|
61
|
+
result[lang] = data[lang].value;
|
|
62
|
+
});
|
|
58
63
|
return result;
|
|
59
64
|
}
|
|
60
65
|
return null;
|
|
@@ -62,7 +67,9 @@ function getLangValue(data) {
|
|
|
62
67
|
function getManyLangValue(data) {
|
|
63
68
|
if (data) {
|
|
64
69
|
const result = {};
|
|
65
|
-
Object.keys(data).forEach(lang => {
|
|
70
|
+
Object.keys(data).forEach((lang) => {
|
|
71
|
+
result[lang] = data[lang].map((item) => item.value);
|
|
72
|
+
});
|
|
66
73
|
return result;
|
|
67
74
|
}
|
|
68
75
|
return null;
|
package/lib/wikipedia/api.js
CHANGED
|
@@ -1,133 +1,143 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
2
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
11
|
const request_1 = require("../request");
|
|
4
|
-
const API_URL =
|
|
12
|
+
const API_URL = "https://$lang.wikipedia.org/w/api.php";
|
|
5
13
|
class Api {
|
|
6
|
-
constructor(
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
14
|
+
constructor(qs = { action: "query", format: "json" }, httpTimeout) {
|
|
15
|
+
this.qs = qs;
|
|
16
|
+
this.httpTimeout = httpTimeout;
|
|
17
|
+
this.qs.action = this.qs.action || "query";
|
|
18
|
+
this.qs.format = "json";
|
|
19
|
+
this.httpTimeout = httpTimeout || 10 * 1000;
|
|
10
20
|
}
|
|
11
|
-
query(lang,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
query(lang, qs) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
if (qs)
|
|
24
|
+
this.qs = Object.assign({}, this.qs, qs);
|
|
25
|
+
const data = yield request_1.default(API_URL.replace("$lang", lang), {
|
|
26
|
+
params: this.qs,
|
|
27
|
+
timeout: this.httpTimeout
|
|
28
|
+
});
|
|
29
|
+
if (hasError(data))
|
|
30
|
+
throw getError(data);
|
|
20
31
|
if (data && data.query && data.query.pages) {
|
|
21
|
-
return
|
|
22
|
-
.map(id => data.query.pages[id])
|
|
23
|
-
.map(data => {
|
|
24
|
-
const item = {
|
|
25
|
-
|
|
26
|
-
|
|
32
|
+
return Object.keys(data.query.pages)
|
|
33
|
+
.map((id) => data.query.pages[id])
|
|
34
|
+
.map((data) => {
|
|
35
|
+
const item = {
|
|
36
|
+
pageid: data["pageid"],
|
|
37
|
+
title: data["title"]
|
|
38
|
+
};
|
|
39
|
+
if (data["categories"]) {
|
|
40
|
+
item.categories = data["categories"].map((item) => item.title);
|
|
27
41
|
}
|
|
28
|
-
if (data[
|
|
29
|
-
item.redirects = data[
|
|
42
|
+
if (data["redirects"]) {
|
|
43
|
+
item.redirects = data["redirects"].map((item) => item.title);
|
|
30
44
|
}
|
|
31
|
-
if (data[
|
|
32
|
-
item.extract = data[
|
|
45
|
+
if (data["extract"]) {
|
|
46
|
+
item.extract = data["extract"];
|
|
33
47
|
}
|
|
34
48
|
return item;
|
|
35
|
-
})
|
|
49
|
+
});
|
|
36
50
|
}
|
|
37
|
-
return
|
|
51
|
+
return [];
|
|
38
52
|
});
|
|
39
53
|
}
|
|
40
54
|
redirects() {
|
|
41
|
-
this.
|
|
42
|
-
this.addField(
|
|
55
|
+
this.qs["rdlimit"] = "max";
|
|
56
|
+
this.addField("prop", "redirects");
|
|
43
57
|
return this;
|
|
44
58
|
}
|
|
45
59
|
categories() {
|
|
46
|
-
this.
|
|
47
|
-
this.
|
|
48
|
-
this.addField(
|
|
60
|
+
this.qs["cllimit"] = "max";
|
|
61
|
+
this.qs["clshow"] = "!hidden";
|
|
62
|
+
this.addField("prop", "categories");
|
|
49
63
|
return this;
|
|
50
64
|
}
|
|
51
65
|
extract(sentences = 3) {
|
|
52
|
-
this.
|
|
53
|
-
this.
|
|
54
|
-
this.addField(
|
|
66
|
+
this.qs["exsentences"] = sentences.toString();
|
|
67
|
+
this.qs["explaintext"] = "true";
|
|
68
|
+
this.addField("prop", "extracts");
|
|
55
69
|
return this;
|
|
56
70
|
}
|
|
57
71
|
addField(name, value) {
|
|
58
|
-
const vals = (this.
|
|
72
|
+
const vals = (this.qs[name] || "").split("|").filter((item) => !!item);
|
|
59
73
|
vals.push(value);
|
|
60
|
-
this.
|
|
74
|
+
this.qs[name] = vals.join("|");
|
|
61
75
|
return this;
|
|
62
76
|
}
|
|
63
77
|
}
|
|
64
78
|
exports.Api = Api;
|
|
65
79
|
function getExtracts(params) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const qs = {
|
|
82
|
+
action: "query",
|
|
83
|
+
titles: params.titles && params.titles.join("|"),
|
|
84
|
+
exsentences: params.sentences || 3,
|
|
85
|
+
prop: "extracts",
|
|
86
|
+
explaintext: true,
|
|
87
|
+
exintro: true,
|
|
88
|
+
exlimit: "max",
|
|
89
|
+
exsectionformat: "plain",
|
|
90
|
+
format: "json"
|
|
91
|
+
};
|
|
92
|
+
const data = yield request_1.default(API_URL.replace("$lang", params.lang), {
|
|
93
|
+
params: qs
|
|
94
|
+
});
|
|
95
|
+
if (hasError(data))
|
|
96
|
+
throw getError(data);
|
|
82
97
|
if (data && data.query && data.query.pages) {
|
|
83
|
-
return Object.keys(data.query.pages).map(id => data.query.pages[id]);
|
|
98
|
+
return Object.keys(data.query.pages).map((id) => data.query.pages[id]);
|
|
84
99
|
}
|
|
85
|
-
return
|
|
100
|
+
return [];
|
|
86
101
|
});
|
|
87
102
|
}
|
|
88
103
|
exports.getExtracts = getExtracts;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
exports.getExtract = getExtract;
|
|
104
|
+
exports.getExtract = (lang, title, sentences) => getExtracts({
|
|
105
|
+
lang: lang,
|
|
106
|
+
titles: [title],
|
|
107
|
+
sentences: sentences
|
|
108
|
+
}).then(function (extracts) {
|
|
109
|
+
if (extracts.length) {
|
|
110
|
+
return extracts[0];
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
});
|
|
102
114
|
function getRedirects(lang, title) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
+
const qs = {
|
|
117
|
+
action: "query",
|
|
118
|
+
generator: "redirects",
|
|
119
|
+
titles: title,
|
|
120
|
+
grdlimit: "max",
|
|
121
|
+
format: "json"
|
|
122
|
+
};
|
|
123
|
+
const data = yield request_1.default(API_URL.replace("$lang", lang), {
|
|
124
|
+
params: qs
|
|
125
|
+
});
|
|
126
|
+
if (hasError(data))
|
|
127
|
+
throw getError(data);
|
|
115
128
|
if (data && data.query && data.query.pages) {
|
|
116
|
-
return Object.keys(data.query.pages).map(id => data.query.pages[id].title);
|
|
129
|
+
return Object.keys(data.query.pages).map((id) => data.query.pages[id].title);
|
|
117
130
|
}
|
|
118
|
-
return
|
|
131
|
+
return [];
|
|
119
132
|
});
|
|
120
133
|
}
|
|
121
134
|
exports.getRedirects = getRedirects;
|
|
122
135
|
function getError(data) {
|
|
123
|
-
return data &&
|
|
136
|
+
return ((data &&
|
|
137
|
+
data.error &&
|
|
138
|
+
new Error(data.error.info || "Wikidata Api Error")) ||
|
|
139
|
+
new Error("NO error"));
|
|
124
140
|
}
|
|
125
141
|
function hasError(data) {
|
|
126
142
|
return !!data.error;
|
|
127
143
|
}
|
|
128
|
-
function getStringArrayParam(value, def = null) {
|
|
129
|
-
if (!value || value.length === 0) {
|
|
130
|
-
return def;
|
|
131
|
-
}
|
|
132
|
-
return value;
|
|
133
|
-
}
|
package/package.json
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wiki-entity",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
4
4
|
"description": "Wiki entity fetcher",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"pretest": "npm run tsc",
|
|
9
|
-
"test": "mocha -t
|
|
9
|
+
"test": "mocha -t 60000",
|
|
10
10
|
"tsc": "tsc",
|
|
11
11
|
"tscw": "tsc -w",
|
|
12
12
|
"preversion": "npm run tsc",
|
|
13
13
|
"postversion": "git push --follow-tags",
|
|
14
|
-
"prepublish": "npm run tsc"
|
|
15
|
-
"lint": "tslint ./src/**/*.ts"
|
|
14
|
+
"prepublish": "npm run tsc"
|
|
16
15
|
},
|
|
17
16
|
"engines": {
|
|
18
|
-
"node": ">=
|
|
17
|
+
"node": ">=8.0.0"
|
|
19
18
|
},
|
|
20
19
|
"repository": {
|
|
21
20
|
"type": "git",
|
|
@@ -36,12 +35,12 @@
|
|
|
36
35
|
},
|
|
37
36
|
"homepage": "https://github.com/entitizer/wiki-entity#readme",
|
|
38
37
|
"devDependencies": {
|
|
39
|
-
"@types/node": "^10.12.
|
|
40
|
-
"debug": "^4.1.
|
|
38
|
+
"@types/node": "^10.12.18",
|
|
39
|
+
"debug": "^4.1.1",
|
|
41
40
|
"mocha": "^5.2.0",
|
|
42
|
-
"typescript": "^3.
|
|
41
|
+
"typescript": "^3.2.2"
|
|
43
42
|
},
|
|
44
43
|
"dependencies": {
|
|
45
|
-
"
|
|
44
|
+
"axios": "^0.21.1"
|
|
46
45
|
}
|
|
47
46
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { WikiEntity, WikiEntitiesParams } from
|
|
2
|
-
import { Api as WikipediaApi } from
|
|
3
|
-
export { simplifyEntity } from
|
|
1
|
+
import { WikiEntity, WikiEntitiesParams } from "./types";
|
|
2
|
+
import { Api as WikipediaApi } from "./wikipedia/api";
|
|
3
|
+
export { simplifyEntity } from "./wikidata/simplify_entity";
|
|
4
4
|
export { WikipediaApi };
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
5
|
+
export * from "./simpleEntity";
|
|
6
|
+
export * from "./types";
|
|
7
7
|
export declare function getEntities(params: WikiEntitiesParams): Promise<WikiEntity[]>;
|
package/types/request.d.ts
CHANGED
package/types/types.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export interface WikidataEntity extends WikidataBaseEntity {
|
|
|
26
26
|
aliases?: string[];
|
|
27
27
|
sitelinks?: PlainObject<string>;
|
|
28
28
|
claims?: WikidataEntityClaims;
|
|
29
|
+
labels?: PlainObject<string>;
|
|
29
30
|
}
|
|
30
31
|
export declare type WikidataEntities = PlainObject<WikidataEntity>;
|
|
31
32
|
export interface WikiEntity extends WikidataEntity {
|
|
@@ -35,7 +36,7 @@ export interface WikiEntity extends WikidataEntity {
|
|
|
35
36
|
categories?: string[];
|
|
36
37
|
}
|
|
37
38
|
export declare type WikiEntities = PlainObject<WikiEntity>;
|
|
38
|
-
export declare type ParamClaimsType =
|
|
39
|
+
export declare type ParamClaimsType = "none" | "all" | "item" | "property";
|
|
39
40
|
export interface WikidataEntitiesParams {
|
|
40
41
|
ids?: string[];
|
|
41
42
|
titles?: string[];
|
|
@@ -43,6 +44,8 @@ export interface WikidataEntitiesParams {
|
|
|
43
44
|
language?: string;
|
|
44
45
|
redirect?: string;
|
|
45
46
|
claims?: ParamClaimsType;
|
|
47
|
+
httpTimeout?: number;
|
|
48
|
+
languages?: string[];
|
|
46
49
|
}
|
|
47
50
|
export declare enum WikidataPropsParam {
|
|
48
51
|
info = "info",
|
package/types/wikidata/api.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { WikidataEntities, WikidataEntitiesParams } from
|
|
1
|
+
import { WikidataEntities, WikidataEntitiesParams } from "../types";
|
|
2
2
|
export declare function getEntities(params: WikidataEntitiesParams): Promise<WikidataEntities>;
|
|
3
3
|
export declare function getManyEntities(params: WikidataEntitiesParams): Promise<WikidataEntities>;
|
|
4
4
|
export declare function validateParams(params: WikidataEntitiesParams): void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { WikidataEntities, WikidataEntitiesParams, WikidataEntityClaims } from
|
|
1
|
+
import { WikidataEntities, WikidataEntitiesParams, WikidataEntityClaims } from "../types";
|
|
2
2
|
export declare function getEntities(params: WikidataEntitiesParams): Promise<WikidataEntities>;
|
|
3
3
|
export declare function exploreEntityClaims(claims: WikidataEntityClaims, params: WikidataEntitiesParams): Promise<void>;
|
package/types/wikipedia/api.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StringPlainObject } from
|
|
1
|
+
import { StringPlainObject } from "../utils";
|
|
2
2
|
export declare type ExtractType = {
|
|
3
3
|
pageid: number;
|
|
4
4
|
title: string;
|
|
@@ -17,14 +17,15 @@ export declare type ApiResult = {
|
|
|
17
17
|
redirects?: string[];
|
|
18
18
|
};
|
|
19
19
|
export declare class Api {
|
|
20
|
-
private
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
private qs;
|
|
21
|
+
private httpTimeout?;
|
|
22
|
+
constructor(qs?: StringPlainObject, httpTimeout?: number);
|
|
23
|
+
query(lang: string, qs?: StringPlainObject): Promise<ApiResult[]>;
|
|
23
24
|
redirects(): this;
|
|
24
25
|
categories(): this;
|
|
25
26
|
extract(sentences?: number): this;
|
|
26
27
|
private addField;
|
|
27
28
|
}
|
|
28
29
|
export declare function getExtracts(params: ExtractsParamsType): Promise<ExtractType[]>;
|
|
29
|
-
export declare
|
|
30
|
+
export declare const getExtract: (lang: string, title: string, sentences?: number) => Promise<ExtractType>;
|
|
30
31
|
export declare function getRedirects(lang: string, title: string): Promise<string[]>;
|