From d2956cdac01ff1418b18dd2b810306cdc2c7a50c Mon Sep 17 00:00:00 2001 From: sahalali Date: Tue, 2 Jul 2024 10:33:40 +0530 Subject: [PATCH] Updated data response model --- src/constant/recon-queries.constants.ts | 14 +++--- src/dto/recon.dto.ts | 15 +++--- src/enum/reconciliation-types.enum.ts | 6 +++ src/helper/reconciliation-service.helper.ts | 53 ++++++++++++++++----- 4 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/constant/recon-queries.constants.ts b/src/constant/recon-queries.constants.ts index 9aef799..c59b595 100644 --- a/src/constant/recon-queries.constants.ts +++ b/src/constant/recon-queries.constants.ts @@ -15,9 +15,9 @@ SELECT DISTINCT ?name ?type ?type_label -?disambiguatingDescription -?disambiguatingDescription_en -?disambiguatingDescription_fr +?description +?description_en +?description_fr WHERE { values ?query { "QUERY_PLACE_HOLDER" } @@ -41,11 +41,11 @@ WHERE BIND(COALESCE(?type_label_en, ?type_label_raw, "") as ?type_label) # DISAMBIGUATING DESCRIPTION - OPTIONAL { ?entity schema:disambiguatingDescription ?disambiguatingDescription_en. FILTER( LANG(?disambiguatingDescription_en) = "en") } - OPTIONAL { ?entity schema:disambiguatingDescription ?disambiguatingDescription_fr. FILTER( LANG(?disambiguatingDescription_fr) = "fr")} - OPTIONAL { ?entity schema:disambiguatingDescription ?disambiguatingDescription. FILTER ( LANG(?disambiguatingDescription) = "")} + OPTIONAL { ?entity schema:disambiguatingDescription ?description_en. FILTER( LANG(?description_en) = "en") } + OPTIONAL { ?entity schema:disambiguatingDescription ?description_fr. FILTER( LANG(?description_fr) = "fr")} + OPTIONAL { ?entity schema:disambiguatingDescription ?description. FILTER ( LANG(?description) = "")} -} group by ?entity ?score ?name_en ?name_fr ?name ?type ?type_label ?disambiguatingDescription_en ?disambiguatingDescription_fr ?disambiguatingDescription +} group by ?entity ?score ?name_en ?name_fr ?name ?type ?type_label ?description_en ?description_fr ?description ` }; diff --git a/src/dto/recon.dto.ts b/src/dto/recon.dto.ts index 6b060b3..2f3497a 100644 --- a/src/dto/recon.dto.ts +++ b/src/dto/recon.dto.ts @@ -7,13 +7,16 @@ class ReconciliationType { name: string; } -export class MultilingualString { +export class MultilingualValues { @ApiProperty() - en: string; + str: string; @ApiProperty() - fr: string; - @ApiProperty() - none: string; + lang?: string; +} + + class MultilingualString { + @ApiProperty({ type: [MultilingualValues] }) + values: MultilingualValues[]; } export class ReconciliationResponse { @@ -22,7 +25,7 @@ export class ReconciliationResponse { @ApiProperty({ type: [MultilingualString] }) name: MultilingualString; @ApiProperty({ type: [MultilingualString] }) - disambiguatingDescription: MultilingualString; + description: MultilingualString; @ApiProperty({ type: [ReconciliationType] }) type: ReconciliationType[]; @ApiProperty() diff --git a/src/enum/reconciliation-types.enum.ts b/src/enum/reconciliation-types.enum.ts index 6f85d45..28e14ac 100644 --- a/src/enum/reconciliation-types.enum.ts +++ b/src/enum/reconciliation-types.enum.ts @@ -3,4 +3,10 @@ export enum ReconciliationTypesEnum { PLACE = "schema:Place", PERSON = "schema:Person", ORGANIZATION = "schema:Organization" +} + + +export enum LanguageTagEnum { + ENGLISH = "en", + FRENCH = "fr" } \ No newline at end of file diff --git a/src/helper/reconciliation-service.helper.ts b/src/helper/reconciliation-service.helper.ts index 0c6b7f1..c2f0ffb 100644 --- a/src/helper/reconciliation-service.helper.ts +++ b/src/helper/reconciliation-service.helper.ts @@ -1,4 +1,5 @@ -import { MultilingualString, ReconciliationResponse } from "../dto"; +import { MultilingualValues, ReconciliationResponse } from "../dto"; +import { LanguageTagEnum } from "../enum"; import { GRAPHDB_INDEX } from "../config"; export class ReconciliationServiceHelper { @@ -6,21 +7,49 @@ export class ReconciliationServiceHelper { static formatReconciliationResponse(query: string, sparqlResponse: any): ReconciliationResponse[] { return sparqlResponse?.results?.bindings?.map((binding: any) => { + const nameValues: MultilingualValues[] = []; + const descriptionValues: MultilingualValues[] = []; const result = new ReconciliationResponse(); - result.id = binding["entity"].value; - result.disambiguatingDescription = new MultilingualString(); - result.disambiguatingDescription.none = binding["disambiguatingDescription"]?.value; - result.disambiguatingDescription.en = binding["disambiguatingDescription_en"]?.value; - result.disambiguatingDescription.fr = binding["disambiguatingDescription_fr"]?.value; - result.name = new MultilingualString(); - result.name.none = binding["name"]?.value; - result.name.en = binding["name_en"]?.value; - result.name.fr = binding["name_fr"]?.value; + const uri = binding["entity"].value; + result.id = uri?.split("http://kg.artsdata.ca/resource/").pop(); + + //NAME + const name = binding["name"]?.value; + const nameEn = binding["name_en"]?.value; + const nameFr = binding["name_fr"]?.value; + if (nameEn) { + nameValues.push({ str: nameEn, lang: LanguageTagEnum.ENGLISH }); + } + if (nameFr) { + nameValues.push({ str: nameFr, lang: LanguageTagEnum.FRENCH }); + } + if (name && !nameEn || !nameFr) { + nameValues.push({ str: name }); + } + result.name = { values: nameValues }; + + //DESCRIPTION + const description = binding["description"]?.value; + const descriptionEn = binding["description_en"]?.value; + const descriptionFr = binding["description_fr"]?.value; + if (descriptionEn) { + descriptionValues.push({ str: descriptionEn, lang: LanguageTagEnum.ENGLISH }); + } + if (descriptionFr) { + descriptionValues.push({ str: descriptionFr, lang: LanguageTagEnum.FRENCH }); + } + if (description && !descriptionEn && !descriptionFr) { + descriptionValues.push({ str: description }); + } + result.description = { values: descriptionValues }; + + //SCORE result.score = binding["score"]?.value; - //TODO match is incorrect when query contains accent characters + + //TODO match is incorrect when query contains accented characters result.match = binding["name"]?.value.toLowerCase() === query.toLowerCase(); - result.type = [{ id: binding["type"]?.value, name: binding["typeLabel"]?.value }]; + result.type = [{ id: binding["type"]?.value, name: binding["type_label"]?.value }]; return result; });