Skip to content

Commit

Permalink
mapRedirects
Browse files Browse the repository at this point in the history
  • Loading branch information
mitica committed Dec 10, 2024
1 parent 25f78a3 commit a647291
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
56 changes: 55 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,42 @@ export { WikipediaApi };
export * from "./simpleEntity";
export * from "./types";

const getAllEntities = async (params: WikiEntitiesParams) => {
const entities = await getWikidataEntities(params);
const titles = params.titles || [];
if (params.redirect === "no" || !titles.length || params.ids) return entities;
const lang = params.language || "en";
const notFoundTitle = titles.find(
(title) =>
!Object.entries(entities).find(([, entity]) => {
return (
!entity.sitelinks ||
!entity.sitelinks[lang] ||
entity.sitelinks[lang] !== title
);
})
);
if (notFoundTitle.length === 0) return entities;

const redirects = await mapRedirects(titles, lang);
const redirectTitles = [...new Set(Object.values(redirects))];
if (redirectTitles.length === 0) return entities;

const redirectEntities = await getWikidataEntities({
...params,
ids: undefined,
titles: redirectTitles
});

return { ...entities, ...redirectEntities };
};

export async function getEntities(
params: WikiEntitiesParams
): Promise<WikiEntity[]> {
const lang = params.language || "en";

const entities = await getWikidataEntities(params);
const entities = await getAllEntities(params);

const ids = Object.keys(entities).filter((id) => isValidWikiId(id));

Expand Down Expand Up @@ -96,3 +126,27 @@ export async function getEntities(
.map((id) => entities[id])
.filter((it) => !!it);
}

export async function mapRedirects(
titles: string[],
lang: string
): Promise<Record<string, string>> {
const wikiApi = new WikipediaApi();
return wikiApi
.redirects()
.query(lang, {
titles: titles.map((it) => it.replace(/\s+/g, "_")).join("|"),
redirects: "yes"
})
.then((apiResults) => {
const result: Record<string, string> = {};
apiResults.forEach((r) => {
if (r.redirects?.length) {
const it = r.redirects.find((it) => titles.includes(it));
if (it && it !== r.title) result[it] = r.title;
}
});

return result;
});
}
4 changes: 2 additions & 2 deletions src/wikidata/get_entity_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ function dbpediaTypes(name: string): Promise<any[]> {
timeout: 30 * 1000
})
.then((data) => data.results && data.results.bindings)
.catch((error) => {
.catch((error: Error) => {
console.error(`${error.message}: ${url}`);
return [];
return [] as any[];
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/wikidata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function exploreEntitiesProperties(
}
});
});
return null;
return entities;
});
}

Expand Down
18 changes: 9 additions & 9 deletions src/wikipedia/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ export class Api {
if (data && data.query && data.query.pages) {
return Object.keys(data.query.pages)
.map((id) => data.query.pages[id])
.map((data) => {
.map((page) => {
const item: ApiResult = {
pageid: data.pageid,
title: data.title
pageid: page.pageid,
title: page.title
};

if (data["categories"]) {
item.categories = data["categories"].map((it: any) => it.title);
if (page["categories"]) {
item.categories = page["categories"].map((it: any) => it.title);
}

if (data["redirects"]) {
item.redirects = data["redirects"].map((it: any) => it.title);
if (page["redirects"]) {
item.redirects = page["redirects"].map((it: any) => it.title);
}

if (data["extract"]) {
item.extract = data["extract"];
if (page["extract"]) {
item.extract = page["extract"];
}

return item;
Expand Down
19 changes: 19 additions & 0 deletions test/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,23 @@ describe("redirects", function () {
assert.equal(results[0].pageid, 3198752);
});
});

it("redirect Brashov to Brașov", function () {
return index
.getEntities({
language: "ro",
titles: ["Brashov"],
redirects: true
})
.then(function (results) {
assert.equal(results[0].id, "Q82174");
});
});

it("mapRedirects Brashov to Brașov", function () {
return index.mapRedirects(["Brashov"], "ro").then(function (results) {
assert.equal(Object.keys(results).length, 1);
assert.equal(results["Brashov"], "Brașov");
});
});
});

0 comments on commit a647291

Please sign in to comment.