@internetarchive/collection-browser 2.9.0 → 2.9.1-alpha-webdev7090.1
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/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.js +5 -4
- package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.js.map +1 -1
- package/package.json +1 -1
- package/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.ts +7 -5
|
@@ -52,7 +52,6 @@ export class WikidataHeuristic {
|
|
|
52
52
|
* @inheritdoc
|
|
53
53
|
*/
|
|
54
54
|
async getRecommendedFacets(query) {
|
|
55
|
-
var _a;
|
|
56
55
|
const recommendations = [];
|
|
57
56
|
try {
|
|
58
57
|
const wikidataURL = this.getWikidataURL(query);
|
|
@@ -60,9 +59,11 @@ export class WikidataHeuristic {
|
|
|
60
59
|
const searchResults = await wikidataResponse.json();
|
|
61
60
|
for (const [keyword, facets] of Object.entries(WIKIDATA_ENTITIES)) {
|
|
62
61
|
const keywordRegex = new RegExp(`\\b${keyword}\\b`);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
for (const searchResult of searchResults.search.slice(0, 3)) {
|
|
63
|
+
if (keywordRegex.test(searchResult === null || searchResult === void 0 ? void 0 : searchResult.description)) {
|
|
64
|
+
const entityName = searchResult.label;
|
|
65
|
+
recommendations.push(...this.replaceQueryPlaceholders(facets, entityName));
|
|
66
|
+
}
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
return recommendations;
|
package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wikidata-heuristic.js","sourceRoot":"","sources":["../../../../../../src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE1D,4EAA4E;AAC5E,mBAAmB;AACnB,0BAA0B;AAC1B,2BAA2B;AAC3B,8CAA8C;AAC9C,+CAA+C;AAC/C,8CAA8C;AAC9C,8CAA8C;AAC9C,OAAO;AACP,MAAM,OAAO,iBAAiB;IAA9B;QACmB,sBAAiB,GAAG,oCAAoC,CAAC;QAEzD,0BAAqB,GACpC,wFAAwF,CAAC;
|
|
1
|
+
{"version":3,"file":"wikidata-heuristic.js","sourceRoot":"","sources":["../../../../../../src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE1D,4EAA4E;AAC5E,mBAAmB;AACnB,0BAA0B;AAC1B,2BAA2B;AAC3B,8CAA8C;AAC9C,+CAA+C;AAC/C,8CAA8C;AAC9C,8CAA8C;AAC9C,OAAO;AACP,MAAM,OAAO,iBAAiB;IAA9B;QACmB,sBAAiB,GAAG,oCAAoC,CAAC;QAEzD,0BAAqB,GACpC,wFAAwF,CAAC;IA2E7F,CAAC;IAzEC;;;OAGG;IACK,cAAc,CAAC,KAAa;QAClC,MAAM,QAAQ,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAC3C,OAAO,GAAG,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC,qBAAqB,WAAW,QAAQ,EAAE,CAAC;IACrF,CAAC;IAED;;;;;OAKG;IACK,wBAAwB,CAC9B,WAAyB,EACzB,KAAa;QAEb,OAAO,WAAW,CAAC,GAAG,CACpB,UAAU,CAAC,EAAE;;YACX,OAAA,CAAC;gBACC,oDAAoD;gBACpD,KAAK,EAAE,MAAA,UAAU,CAAC,KAAK,0CAAE,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC;gBAClD,6EAA6E;gBAC7E,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;;oBACpC,MAAM,QAAQ,GAAG;wBACf,GAAG,KAAK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AC;QACzB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC;CACF","sourcesContent":["import type { SmartQueryHeuristic, SmartFacet } from '../../models';\nimport { WIKIDATA_ENTITIES } from './wikidata-entity-map';\n\n// If wikidata describes the top query result as X, recommend facet Y, e.g.:\n// X Y\n// written work mt:texts\n// film mt:movies\n// author mt:texts and creator:<query>\n// filmmaker mt:movies and creator:<query>\n// photographer mt:image and creator:<query>\n// visual artist mt:image and creator:<query>\n// etc.\nexport class WikidataHeuristic implements SmartQueryHeuristic {\n private readonly WIKIDATA_BASE_URL = 'https://www.wikidata.org/w/api.php';\n\n private readonly WIKIDATA_DEFAULT_ARGS =\n '?action=wbsearchentities&format=json&language=en&uselang=en&origin=*&type=item&limit=5';\n\n /**\n * Returns the full URL for a Wikidata search for the given query\n * @param query The query to search for\n */\n private getWikidataURL(query: string): string {\n const urlQuery = encodeURIComponent(query);\n return `${this.WIKIDATA_BASE_URL}${this.WIKIDATA_DEFAULT_ARGS}&search=${urlQuery}`;\n }\n\n /**\n * Replaces query placeholders with an actual query string, within a collection of smart facets\n * @param smartFacets The array of smart facets\n * @param query The query string to replace placeholders with\n * @returns A new array of smart facets with all query placeholders replaced\n */\n private replaceQueryPlaceholders(\n smartFacets: SmartFacet[],\n query: string,\n ): SmartFacet[] {\n return smartFacets.map(\n smartFacet =>\n ({\n // Replace placeholders within the smart facet label\n label: smartFacet.label?.replace('__QUERY', query),\n // Replace placeholders within the facets themselves (buckets & display text)\n facets: smartFacet.facets.map(facet => {\n const replaced = {\n ...facet,\n bucketKey: facet.bucketKey.replace('__QUERY', query),\n };\n\n if (facet.displayText) {\n replaced.displayText = replaced.displayText?.replace(\n '__QUERY',\n query,\n );\n }\n\n return replaced;\n }),\n }) as SmartFacet,\n );\n }\n\n /**\n * @inheritdoc\n */\n async getRecommendedFacets(query: string): Promise<SmartFacet[]> {\n const recommendations: SmartFacet[] = [];\n\n try {\n const wikidataURL = this.getWikidataURL(query);\n const wikidataResponse = await fetch(wikidataURL);\n const searchResults = await wikidataResponse.json();\n\n for (const [keyword, facets] of Object.entries(WIKIDATA_ENTITIES)) {\n const keywordRegex = new RegExp(`\\\\b${keyword}\\\\b`);\n for (const searchResult of searchResults.search.slice(0, 3)) {\n if (keywordRegex.test(searchResult?.description)) {\n const entityName = searchResult.label;\n recommendations.push(\n ...this.replaceQueryPlaceholders(facets, entityName),\n );\n }\n }\n }\n\n return recommendations;\n } catch (err) {\n console.warn(err);\n return [];\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -73,11 +73,13 @@ export class WikidataHeuristic implements SmartQueryHeuristic {
|
|
|
73
73
|
|
|
74
74
|
for (const [keyword, facets] of Object.entries(WIKIDATA_ENTITIES)) {
|
|
75
75
|
const keywordRegex = new RegExp(`\\b${keyword}\\b`);
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
for (const searchResult of searchResults.search.slice(0, 3)) {
|
|
77
|
+
if (keywordRegex.test(searchResult?.description)) {
|
|
78
|
+
const entityName = searchResult.label;
|
|
79
|
+
recommendations.push(
|
|
80
|
+
...this.replaceQueryPlaceholders(facets, entityName),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
81
83
|
}
|
|
82
84
|
}
|
|
83
85
|
|