Commit f13da4cb authored by Thibault Ehrhart's avatar Thibault Ehrhart

Fetch annotations in a separate async query

parent 074cc130
......@@ -37,6 +37,39 @@ export class HomeDetailsComponent implements OnInit {
this.newsService.getNewsByUri('http://asrael.eurecom.fr/news/' + id).subscribe((data: any) => {
this.selectedItem = data[0];
if (this.selectedItem.context) {
this.newsService.getAnnotationsByContext(this.selectedItem.context).subscribe((annotations: any) => {
this.selectedItem.annotations = [];
if (annotations) {
annotations = annotations.map((annotation: any) => {
return {
label: annotation.anchorOf,
type: annotation.classRef,
source: annotation.identRef,
start: parseInt(annotation.beginIndex, 10),
end: parseInt(annotation.endIndex, 10)
};
}).reduce((acc: any, val: any) => {
// Group them by type
(acc[val.type] = acc[val.type] || []).push(val);
return acc;
}, {});
annotations = Object.keys(annotations).map((k: any) => {
let values = annotations[k];
// Keep unique values only
values = values.filter((item: any, i: number, a: any) => i === a.findIndex((item2: any) => item.source === item2.source));
return { type: k, values: values };
});
annotations.forEach((annotation: any) => {
this.selectedItem.annotations.push(annotation);
})
}
}, (error: any) => {
this.selectedItem.annotations = [];
});
}
this.newsService.getSources(this.selectedItem.identifier).subscribe((sources: any) => {
this.selectedItem.sources = [];
if (sources) {
......
......@@ -131,7 +131,7 @@ export class NewsService {
// Generate query
let query = `
${this.getPrefixes()}
SELECT DISTINCT ?s ?dateCreated ?dateModified ?datePublished ?description ?genre ?genreLabel (GROUP_CONCAT(?subject; separator="|") AS ?subjects) (GROUP_CONCAT(?subjectLabel; separator="|") AS ?subjectsLabels) ?dateline ?headline ?identifier ?language ?slug (GROUP_CONCAT(?associatedMedia; separator="|") AS ?associatedMedias) ?country ?locality ?annotations
SELECT DISTINCT ?s ?dateCreated ?dateModified ?datePublished ?description ?genre ?genreLabel (GROUP_CONCAT(?subject; separator="|") AS ?subjects) (GROUP_CONCAT(?subjectLabel; separator="|") AS ?subjectsLabels) ?dateline ?headline ?identifier ?language ?slug (GROUP_CONCAT(?associatedMedia; separator="|") AS ?associatedMedias) ?country ?locality ?context
FROM <http://asrael.eurecom.fr/agencefrancepresse/news>
WHERE {
?s a rnews:NewsItem .
......@@ -162,7 +162,6 @@ export class NewsService {
OPTIONAL { ?s rnews:inLanguage ?language . }
OPTIONAL { ?s rnews:slug ?slug . }
OPTIONAL { ?s rnews:associatedMedia ?associatedMedia . }
OPTIONAL {
?s schema:contentLocation ?location .
OPTIONAL {
......@@ -171,22 +170,7 @@ export class NewsService {
OPTIONAL { ?address schema:addressLocality ?locality }
}
}
OPTIONAL {
?s asrael:context ?context .
{
SELECT ?refContext (GROUP_CONCAT(DISTINCT ?annotations; separator="|") AS ?annotations) WHERE {
GRAPH <http://asrael.eurecom.fr/agencefrancepresse/news> { ?sp a nif:Phrase . }
?sp nif:referenceContext ?refContext .
?sp nif:anchorOf ?anchorOf .
?sp nif:beginIndex ?beginIndex .
?sp nif:endIndex ?endIndex .
?sp itsrdf:taClassRef ?classRef .
?sp itsrdf:taIdentRef ?identRef .
BIND(CONCAT(ENCODE_FOR_URI(?anchorOf), "=", ENCODE_FOR_URI(?classRef), "=", ENCODE_FOR_URI(?identRef), "=", ENCODE_FOR_URI(STR(?beginIndex)), "=", ENCODE_FOR_URI(STR(?endIndex))) AS ?annotations)
}
}
FILTER (?context = ?refContext)
}
OPTIONAL { ?s asrael:context ?context . }
}
LIMIT 1
`;
......@@ -214,27 +198,6 @@ export class NewsService {
if (binding.associatedMedias) {
binding.associatedMedias = binding.associatedMedias.split('|');
}
// Annotations
if (binding.annotations) {
// Split and decode the values
binding.annotations = binding.annotations.split('|').map((annotation: string) => {
let keyvalue = annotation.split('=');
return { label: decodeURIComponent(keyvalue[0]), type: decodeURIComponent(keyvalue[1]), source: decodeURIComponent(keyvalue[2]), start: parseInt(decodeURIComponent(keyvalue[3])), end: parseInt(decodeURIComponent(keyvalue[4])) };
});
// Group them by type
binding.annotations = binding.annotations.reduce((acc: any, val: any) => {
(acc[val.type] = acc[val.type] || []).push(val);
return acc;
}, {});
// Convert to an array
binding.annotations = Object.keys(binding.annotations).map((k: any) => {
let values = binding.annotations[k];
// Keep unique values only
values = values.filter((item: any, i: number, a: any) => i === a.findIndex((item2: any) => item.source === item2.source));
return { type: k, values: values };
});
}
});
observer.next(bindings);
......@@ -330,6 +293,7 @@ export class NewsService {
OPTIONAL { ?s rnews:dateCreated ?dateCreated . }
OPTIONAL { ?s rnews:description ?description . }
OPTIONAL { ?s rnews:headline ?headline . }
OPTIONAL { ?s asrael:context ?context . }
OPTIONAL { ?s schema:contentLocation/schema:address/schema:addressCountry ?country . }
OPTIONAL { ?s schema:contentLocation/schema:address/schema:addressLocality ?locality . }
FILTER (BOUND(?headline) || STRLEN(?headline) > 0)
......@@ -353,6 +317,47 @@ export class NewsService {
return Observable.fromPromise(client.query(query));
}
getAnnotationsByContext(context: string): Observable<any> {
let client = new Client(environment.virtuosoAddress);
client.setOptions('application/json');
// Generate query
let query = `
${this.getPrefixes()}
SELECT ?refContext ?anchorOf ?classRef ?identRef ?beginIndex ?endIndex
FROM <http://asrael.eurecom.fr/agencefrancepresse/news>
WHERE {
?sp a nif:Phrase .
?sp nif:referenceContext ?refContext .
VALUES ?refContext { <${context}> }
?sp nif:anchorOf ?anchorOf .
?sp nif:beginIndex ?beginIndex .
?sp nif:endIndex ?endIndex .
?sp itsrdf:taClassRef ?classRef .
?sp itsrdf:taIdentRef ?identRef .
}
`;
console.debug(query);
let obs = Observable.create((observer: Observer<JSON>) => {
client.query(query).then((data: any) => {
let bindings = data.results.bindings;
bindings.forEach((binding: any) => {
// Normalize objects
for (let k in binding) {
binding[k] = binding[k].value;
}
});
observer.next(bindings);
observer.complete();
});
});
return obs;
}
getSources(identifier: string): Observable<any> {
return this.http.get('assets/data/limsi/' + identifier.replace(/:/g, '_') + '.json');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment