Commit 26a4f9ea authored by Thibault Ehrhart's avatar Thibault Ehrhart

Fix ADEL annotations highlighting offsets

parent b19705de
......@@ -101,13 +101,27 @@ export class HomeDetailsComponent implements OnInit {
getAnnotationName(annotationType: string) {
switch (annotationType) {
case 'http://adel.eurecom.fr/Stanford/Person': return 'Person';
case 'http://adel.eurecom.fr/Stanford/Location': return 'Location';
case 'http://adel.eurecom.fr/Stanford/Misc': return 'Misc';
case 'http://adel.eurecom.fr/Stanford/Organization': return 'Organization';
case 'http://adel.eurecom.fr/Stanford/Thing': return 'Thing';
case 'http://adel.eurecom.fr/Stanford/Event': return 'Event';
case 'http://adel.eurecom.fr/Stanford/Product': return 'Product';
case 'http://adel.eurecom.fr/Stanford/Person':
case 'http://adel.eurecom.fr/Stanford/PERSON':
return 'Person';
case 'http://adel.eurecom.fr/Stanford/Location':
case 'http://adel.eurecom.fr/Stanford/LOCATION':
return 'Location';
case 'http://adel.eurecom.fr/Stanford/Misc':
case 'http://adel.eurecom.fr/Stanford/MISC':
return 'Misc';
case 'http://adel.eurecom.fr/Stanford/Organization':
case 'http://adel.eurecom.fr/Stanford/ORGANIZATION':
return 'Organization';
case 'http://adel.eurecom.fr/Stanford/Thing':
case 'http://adel.eurecom.fr/Stanford/THING':
return 'Thing';
case 'http://adel.eurecom.fr/Stanford/Event':
case 'http://adel.eurecom.fr/Stanford/EVENT':
return 'Event';
case 'http://adel.eurecom.fr/Stanford/Product':
case 'http://adel.eurecom.fr/Stanford/PRODUCT':
return 'Product';
}
return annotationType;
}
......
......@@ -5,30 +5,35 @@ import { NewsService } from '../../services/news/news.service';
@Pipe({ name: 'colorize' })
export class ColorizePipe implements PipeTransform {
// Each html tag created by this transform increases the total offset
tagsOffset: number;
constructor(public sanitizer: DomSanitizer) {
this.tagsOffset = 0;
}
calculateAdelOffset(htmlText: string, start: number, end: number) {
// Adel is based on stripped html text
var pattern = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
var pattern = /<(?:(?! ))(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
var match = pattern.exec(htmlText);
var totalTagsLength = 0;
while (match != null) {
if (match.index > start + totalTagsLength) {
if (match.index > start + totalTagsLength + this.tagsOffset) {
break;
}
console.log(match[0], match.index);
// console.log(match[0], match.index);
totalTagsLength += match[0].length;
if (match[0].indexOf('</') === 0) {
if (match[0].startsWith('</')) {
totalTagsLength -= 1; // Adel Annotator adds a space after closing tags
}
match = pattern.exec(htmlText)
}
return {
start: start + totalTagsLength,
end: end + totalTagsLength
start: start + totalTagsLength + this.tagsOffset,
end: end + totalTagsLength + this.tagsOffset
};
}
......@@ -50,19 +55,23 @@ export class ColorizePipe implements PipeTransform {
return a.start > b.start ? 1 : -1;
});
console.log(flatAnnotations);
// console.log(flatAnnotations);
flatAnnotations.forEach((value: any) => {
var colorType = NewsService.getAnnotationColor(value.type);
var adelOffset = this.calculateAdelOffset(text, value.start, value.end);
console.log('"' + text.substr(adelOffset.start, adelOffset.end - adelOffset.start) + '"');
// console.log('"' + text.substr(adelOffset.start, adelOffset.end - adelOffset.start) + '"');
let replacement = '<span asrael class="colorize color-' + colorType + '">' + text.substr(adelOffset.start, adelOffset.end - adelOffset.start) + '</span>';
let replacement = '<a href="' + value.source + '" target="_blank"><span asrael class="colorize color-' + colorType + '">' + text.substr(adelOffset.start, adelOffset.end - adelOffset.start) + '</span></a>';
text = text.substr(0, adelOffset.start) + replacement + text.substr(adelOffset.end);
this.tagsOffset += 2; // </span></a>
});
this.tagsOffset = 0;
return text;
} else {
return text;
......
......@@ -328,13 +328,27 @@ export class NewsService {
static getAnnotationColor(annotationType: string) {
switch (annotationType) {
case 'http://adel.eurecom.fr/Stanford/Person': return 'person';
case 'http://adel.eurecom.fr/Stanford/Location': return 'location';
case 'http://adel.eurecom.fr/Stanford/Misc': return 'misc';
case 'http://adel.eurecom.fr/Stanford/Organization': return 'organization';
case 'http://adel.eurecom.fr/Stanford/Thing': return 'thing';
case 'http://adel.eurecom.fr/Stanford/Event': return 'event';
case 'http://adel.eurecom.fr/Stanford/Product': return 'product';
case 'http://adel.eurecom.fr/Stanford/Person':
case 'http://adel.eurecom.fr/Stanford/PERSON':
return 'person';
case 'http://adel.eurecom.fr/Stanford/Location':
case 'http://adel.eurecom.fr/Stanford/LOCATION':
return 'location';
case 'http://adel.eurecom.fr/Stanford/Misc':
case 'http://adel.eurecom.fr/Stanford/MISC':
return 'misc';
case 'http://adel.eurecom.fr/Stanford/Organization':
case 'http://adel.eurecom.fr/Stanford/ORGANIZATION':
return 'organization';
case 'http://adel.eurecom.fr/Stanford/Thing':
case 'http://adel.eurecom.fr/Stanford/THING':
return 'thing';
case 'http://adel.eurecom.fr/Stanford/Event':
case 'http://adel.eurecom.fr/Stanford/EVENT':
return 'event';
case 'http://adel.eurecom.fr/Stanford/Product':
case 'http://adel.eurecom.fr/Stanford/PRODUCT':
return 'product';
}
return 'default';
}
......
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