Skip to content

Commit

Permalink
Added support to reconcile with URI or ID
Browse files Browse the repository at this point in the history
  • Loading branch information
sahalali committed Aug 1, 2024
1 parent 7bc9663 commit 4f144b3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/constant/recon-queries.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ WHERE
} group by ?entity ?score ?type_label ?type
`,

RECONCILIATION_BY_ID_QUERY: `
RECONCILIATION_QUERY_BY_URI: `
PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
Expand All @@ -72,23 +72,23 @@ SELECT DISTINCT
?type
WHERE
{
values ?entity { URI_PLACEHOLDER }
?entity a ?type.
BIND(URI_PLACEHOLDER as ?entity)
#?entity a ?type.
#NAME
OPTIONAL { ?entity schema:name ?name_en. FILTER( LANG(?name_en) = "en") }
OPTIONAL { ?entity schema:name ?name_fr. FILTER( LANG(?name_fr) = "fr")}
OPTIONAL { ?entity schema:name ?name_no. FILTER ( LANG(?name_no) = "")}
#TYPE
OPTIONAL { ?type_additional rdfs:label ?type_label_raw filter(lang(?type_label_raw) = "") }
OPTIONAL { ?type_additional rdfs:label ?type_label_en filter(lang(?type_label_en) = "en") }
BIND(COALESCE(?type_label_en, ?type_label_raw, "") as ?type_label)
#OPTIONAL { ?type_additional rdfs:label ?type_label_raw filter(lang(?type_label_raw) = "") }
# OPTIONAL { ?type_additional rdfs:label ?type_label_en filter(lang(?type_label_en) = "en") }
#BIND(COALESCE(?type_label_en, ?type_label_raw, "") as ?type_label)
#DISAMBIGUATING DESCRIPTION
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_no. FILTER ( LANG(?description_no) = "")}
} group by ?entity ?score ?type_label ?type
LIMIT 1
`
};
9 changes: 8 additions & 1 deletion src/helper/reconciliation-service.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class ReconciliationServiceHelper {
resultType.push({ id: typeUris[i], name: typeLabels[i] });
}
} else {
resultType.push({ id: typeUris[0], name: typeLabels[0] });
resultType.push({ id: typeUris?.[0], name: typeLabels?.[0] });
}
resultCandidate.type = resultType;
return resultCandidate;
Expand Down Expand Up @@ -95,4 +95,11 @@ export class ReconciliationServiceHelper {
return false;
}
}

static isQueryByURI(query: string) {
const artsdataIdPattern = "^K[0-9]+-[0-9]+$";
return !!(query.match(artsdataIdPattern) ||
(this.isValidURI(query) && query.startsWith("http://kg.artsdata.ca/resource/K")));

}
}
3 changes: 2 additions & 1 deletion src/service/artsdata/artsdata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export class ArtsdataService {

async getReconcileResultById(id: string) {
const uri = id.startsWith("http://kg.artsdata.ca/resource/") ? `<id>` : `<http://kg.artsdata.ca/resource/${id}>`;
const rawSparqlQuery = QUERIES.RECONCILIATION_BY_ID_QUERY.replace("URI_PLACEHOLDER", uri);
const rawSparqlQuery = QUERIES.RECONCILIATION_QUERY_BY_URI
.replace("URI_PLACEHOLDER", uri);
const sparqlQuery: string = "query=" + encodeURIComponent(rawSparqlQuery) + "&infer=false";
const sparqlEndpoint: string = this._getArtsdataEndPoint();
const response = await this.httpService.postRequest(sparqlEndpoint, sparqlQuery);
Expand Down
19 changes: 14 additions & 5 deletions src/service/recon/recon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export class ReconciliationService {
for (const reconciliationQuery of queries) {
const { type, limit, conditions } = reconciliationQuery;
const { name, propertyConditions } = this._resolveConditions(conditions);
const rawSparqlQuery: string = this._getSparqlQuery(name as string, type, limit);
const isQueryByURI = ReconciliationServiceHelper.isQueryByURI(name as string);
const rawSparqlQuery: string = this._getSparqlQuery(name as string, isQueryByURI, type, limit);
const rawSparqlQueryWithPropertyFilters = this._resolvePropertyConditions(rawSparqlQuery, propertyConditions);
const sparqlQuery: string = "query=" + encodeURIComponent(rawSparqlQueryWithPropertyFilters);
const candidates = await this._artsdataService.getReconciliationResult(sparqlQuery, name as string);
Expand All @@ -65,18 +66,26 @@ export class ReconciliationService {
return rawSparqlQuery;
}

private _getSparqlQuery(name: string, type: string, limit: number | undefined): string {
private _getSparqlQuery(name: string, isQueryByURI: boolean, type: string, limit: number | undefined): string {
const graphdbIndex: string = ReconciliationServiceHelper.getGraphdbIndex(type);

const queryReplacementString: string = name ? `values ?query { "${name}" }` : "";
const rawQuery = isQueryByURI ? QUERIES.RECONCILIATION_QUERY_BY_URI : QUERIES.RECONCILIATION_QUERY;
if (isQueryByURI && name.startsWith("K")) {
name = `<http://kg.artsdata.ca/resource/${name}>`;
}else{
name = `<${name}>`;
}

const queryReplacementString: string = name ? `values ?query { ${name} }` : "";
const queryFilterReplacementString: string = name ? ` luc:query ?query ;` : "";
const typePlaceholderReplace: string = type ? `values ?type { ${type} }` : "";

let rawSparqlQuery: string = QUERIES.RECONCILIATION_QUERY
let rawSparqlQuery: string = rawQuery
.replace("INDEX_PLACE_HOLDER", graphdbIndex)
.replace("QUERY_PLACE_HOLDER", queryReplacementString)
.replace("QUERY_FILTER_PLACE_HOLDER", queryFilterReplacementString)
.replace("TYPE_PLACE_HOLDER", typePlaceholderReplace);
.replace("TYPE_PLACE_HOLDER", typePlaceholderReplace)
.replace("URI_PLACEHOLDER", `${name}`);

if (limit && limit > 0) {
rawSparqlQuery = rawSparqlQuery + " LIMIT " + limit;
Expand Down

0 comments on commit 4f144b3

Please sign in to comment.