UNPKG

wiki-entity

Version:
54 lines (53 loc) 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertToSimpleEntity = void 0; const getEntityType_1 = require("./getEntityType"); const getEntityInstanceType_1 = require("./getEntityInstanceType"); const getEntityTypeByExtract_1 = require("./getEntityTypeByExtract"); const getEntityData_1 = require("./getEntityData"); const getEntityCountry_1 = require("./getEntityCountry"); const utils_1 = require("../utils"); function convertToSimpleEntity(wikiEntity, lang, options) { options = options || {}; const entity = {}; entity.lang = lang.toLowerCase(); entity.wikiDataId = wikiEntity.id; entity.name = wikiEntity.label; entity.description = wikiEntity.description; entity.wikiPageId = wikiEntity.pageid; entity.about = wikiEntity.extract; if (wikiEntity.redirectsToId) entity.redirectsToId = wikiEntity.redirectsToId; if (wikiEntity.redirectsFromId) entity.redirectsFromId = wikiEntity.redirectsFromId; if (wikiEntity.types) { entity.types = (0, utils_1.uniq)(wikiEntity.types.filter((item) => !/:(Thing|Agent)$/.test(item))); } entity.type = (0, getEntityType_1.getEntityType)(wikiEntity); if (!entity.type) { entity.type = (0, getEntityInstanceType_1.getEntityType)(wikiEntity); if (!entity.type && entity.about && !entity.types) { entity.type = (0, getEntityTypeByExtract_1.getEntityTypeByExtract)(entity.about, lang); } if (!entity.type && options.defaultType) { entity.type = options.defaultType; } } if (wikiEntity.sitelinks) { entity.wikiPageTitle = wikiEntity.sitelinks[lang]; } entity.categories = createCategories(wikiEntity); if (wikiEntity.claims) { const ids = Object.keys(wikiEntity.claims); if (ids.length) { entity.data = (0, getEntityData_1.getEntityData)(wikiEntity); } entity.countryCodes = (0, getEntityCountry_1.getEntityCountryCode)(wikiEntity); } return entity; } exports.convertToSimpleEntity = convertToSimpleEntity; function createCategories(entity) { let categories = entity.categories || []; return (0, utils_1.uniq)(categories); }