openaire-library/landingPages/landing-utils/parsingFunctions.class.ts

642 lines
25 KiB
TypeScript

export class ParsingFunctions {
constructor () {}
public ngOnDestroy() {}
public parseFundingByProjects(fundedByProjects: { "id": string, "acronym": string, "title": string,
"funderShortname": string, "funderName": string,
"funding": string, "code": string,
"provenanceAction": string, "inline": boolean
}[],
relation: any, projectsProvenanceVocabulary: any):
{ "id": string, "acronym": string, "title": string,
"funderShortname": string, "funderName": string,
"funding": string, "code": string,
"provenanceAction": string, "inline": boolean
}[] {
if(fundedByProjects == undefined) {
fundedByProjects = new Array<{"id": string, "acronym": string, "title": string,
"funderShortname": string, "funderName": string,
"funding": string, "code": string,
"provenanceAction": string, "inline": boolean
}>();
}
let fundedByProject: { "id": string, "acronym": string, "title": string,
"funderShortname": string, "funderName": string,
"funding": string, "code": string,
"provenanceAction": string, "inline": boolean
} = { "id": "", "acronym": "", "title": "",
"funderShortname": "", "funderName": "",
"funding": "", "code": "", "provenanceAction": "", "inline": false
};
if(relation.title != 'unidentified') {
fundedByProject['id'] = relation['to'].content;
fundedByProject['acronym'] = relation.acronym;
fundedByProject['title'] = relation.title;
fundedByProject['code'] = relation.code;
if(relation.provenanceaction in projectsProvenanceVocabulary) {
fundedByProject['provenanceAction'] = projectsProvenanceVocabulary[relation.provenanceaction];
}
} else {
fundedByProject['id'] = "";
fundedByProject['acronym'] = "";
fundedByProject['title'] = "";
fundedByProject['code'] = "";
fundedByProject['provenanceAction'] = "";
}
if(relation.hasOwnProperty("funding")) {
let funding: {"funderName": string, "funderShortname": string, "stream": string};
funding = this.parseFundingTrees(relation.funding);
if(funding.funderName) {
fundedByProject['funderName'] = funding.funderName;
}
if(funding.funderShortname) {
fundedByProject['funderShortname'] = funding.funderShortname;
}
if(funding.stream) {
fundedByProject['funding'] = funding.stream;
}
}
fundedByProjects.push(fundedByProject);
return fundedByProjects;
}
// publication & research data : for fundedByProjects | project landing : for funding
public parseFundingTrees(fundingTree: any): {"funderName": string, "funderShortname": string, "stream": string} {
let funding: {"funderName": string, "funderShortname": string, "stream": string} = {"funderName": "", "funderShortname": "", "stream": ""};
let length = Array.isArray(fundingTree) ? fundingTree.length : 1;
for(let i=0; i<length; i++) {
let fundingData = Array.isArray(fundingTree) ? fundingTree[i] : fundingTree;
if(fundingData.hasOwnProperty("funder")) {
funding.funderShortname = fundingData['funder'].shortname;
funding.funderName = fundingData['funder'].name;
}
funding.stream = this.addFundingLevel0(fundingData, funding.stream);
funding.stream = this.addFundingLevel1(fundingData, funding.stream);
funding.stream = this.addFundingLevel2(fundingData, funding.stream);
}
return funding;
}
addFundingLevel0(parent: string, fundingStream: string) : string {
if(parent.hasOwnProperty("funding_level_0")) {
let level0 = parent['funding_level_0'];
fundingStream += (fundingStream) ? " ; " : "";
fundingStream += level0.name;
}
return fundingStream;
}
addFundingLevel1(parent: string, fundingStream: string): string {
if(parent.hasOwnProperty("funding_level_1")) {
let level1 = parent['funding_level_1'];
// For projects' parsing
if(level1.hasOwnProperty("parent")) {
fundingStream = this.addFundingLevel0(level1.parent, fundingStream);
}
fundingStream += (fundingStream) ? " | " : "";
fundingStream += level1.name;
}
return fundingStream;
}
addFundingLevel2(parent: string, fundingStream: string): string {
if(parent.hasOwnProperty("funding_level_2")) {
let level2 = parent['funding_level_2'];
// For projects' parsing
if(level2.hasOwnProperty("parent")) {
fundingStream = this.addFundingLevel1(level2.parent, fundingStream);
}
fundingStream += (fundingStream) ? " | " : "";
fundingStream += level2.name;
}
return fundingStream;
}
// publication & dataset landing : for collectedFrom
parseCollectedFrom( collectedFrom: {"name": string, "id": string}[],
_collectedFrom: any) {
let length: number = collectedFrom.length;
collectedFrom[length] = {"name": "", "id": ""};
collectedFrom[length]['name'] = _collectedFrom.name;
collectedFrom[length]['id'] = _collectedFrom.id;
}
// publication & dataset landing : for downloadFrom
addPublisherToHostedBy_collectedFrom( hostedBy_collectedFrom: {"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string}[],
publisher: string, journal:{"journal": string, "issn": string, "lissn": string},
identifiers: Map<string, string[]>/*, title: { "name": string, "url": string, "accessMode": string}*/) {
if( publisher && identifiers != null && identifiers.has('doi')) {
if( hostedBy_collectedFrom == null) {
hostedBy_collectedFrom = new Array<{"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string}>();
}
let available: {"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string};
available = {"downloadName": "", "downloadUrl": null, "collectedName": "", "collectedId": "", "accessMode": null, "bestAccessMode": null, "type": "", "year": ""};
if(journal && journal.journal) {
available.downloadName = publisher + "/ "+journal['journal'];
} else {
available.downloadName = publisher;
}
let url = "https://dx.doi.org/"+identifiers.get("doi")[0];
available.downloadUrl = new Array<string>();
available.accessMode = new Array<string>();
available.downloadUrl.push(url);
/*
if(title != undefined && title['url'] == "") {
title['url'] = url;
}
*/
hostedBy_collectedFrom.push(available);
}
return hostedBy_collectedFrom;
}
// publication & dataset landing : for downloadFrom
parseDownloadFrom(downloadFrom: Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>, instance: any, url: string)
{
let key: string = instance['hostedby'].name;
if(key) {
this.addUrlAndAccessMode(downloadFrom, instance, key, url);
}
}
// publication & dataset landing : for publishedIn
parsePublishedIn(publishedIn: Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>, instance: any, result: any, url: string, counter: number): number {
if(result != null && result.hasOwnProperty("source")) {
let key: string;
if(Array.isArray(result.source)) {
if(counter==result.source.length) {
counter--;
}
key = result['source'][counter];
} else {
key = result['source'];
}
if(key) {
this.addUrlAndAccessMode(publishedIn, instance, key, url);
counter++;
}
}
return counter;
}
// publication & dataset landing : for downloadFrom and publishedIn
addUrlAndAccessMode(mapStructure: Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>, instance: any, key: string, url: string) {
if(!mapStructure.has(key)) {
mapStructure.set(key, {"url": null, "accessMode": null, "bestAccessMode": null});
}
if(mapStructure.get(key)['url'] == null) {
mapStructure.get(key)['url'] = new Array<string>();
}
if(url) {
mapStructure.get(key)['url'].push(url);
}
if(mapStructure.get(key)['accessMode'] == null) {
mapStructure.get(key)['accessMode'] = new Array<string>();
}
if(instance.hasOwnProperty("accessright")) {
if(url) {
mapStructure.get(key)['accessMode'].push(instance['accessright'].classid);
}
if(this.changeBestAccessMode(mapStructure.get(key)['bestAccessMode'], instance['accessright'])) {
mapStructure.get(key)['bestAccessMode'] = instance['accessright'].classid;
}
} else if(url) {
mapStructure.get(key)['accessMode'].push("");
}
}
parseHostedBy_collectedFrom(hostedBy_collectedFrom: { "downloadName": string, "downloadUrl": string[],
"collectedName": string, "collectedId": string,
"accessMode": string[], "bestAccessMode": string,
"type": string, "year":string }[],
instance: any, data: any, url: string, counter: number/*,
title: { "name": string, "url": string, "accessMode": string}*/, accessMode: string): number {
let available: {"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string};
available = {"downloadName": "", "downloadUrl": null, "collectedName": "", "collectedId": "", "accessMode": null, "bestAccessMode": null, "type": "", "year": ""};
if(instance['hostedby'].name && instance['hostedby'].name != "other resources" && instance['hostedby'].name != "Unknown Repository") {
available.downloadName = instance['hostedby'].name;
} else {
if(data != null && data.hasOwnProperty("source")) {
let downloadName: string;
if(Array.isArray(data.source)) {
if(counter==data.source.length) {
counter--;
}
downloadName = data['source'][counter];
} else {
downloadName = data['source'];
}
if(downloadName) {
counter++;
available.downloadName = downloadName;
}
}
}
if(available.downloadName) {
if(instance.hasOwnProperty("collectedfrom")) {
available.collectedId = instance['collectedfrom'].id;
available.collectedName = instance['collectedfrom'].name;
}
if(instance.hasOwnProperty("instancetype") && instance['instancetype'].classname) {
available.type = instance['instancetype'].classname;
}
if(instance.hasOwnProperty("dateofacceptance")) {
var date:string = (instance.dateofacceptance)+""; // transform to string in case it is an integer
available.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
}
available.accessMode = new Array<string>();
available.downloadUrl = new Array<string>();
available['downloadUrl'].push(url);
if(instance.hasOwnProperty("accessright")) {
if(url) {
available['accessMode'].push(instance['accessright'].classid);
}
if(this.changeBestAccessMode(available.bestAccessMode, instance['accessright'])) {
available.bestAccessMode = instance['accessright'].classid;
/*
if(title != undefined) {
if(this.changeBestAccessMode(title['accessMode'], instance['accessright'])) {
title['accessMode'] = instance['accessright'].classid;
title['url'] = url;
}
}
*/
if(this.changeBestAccessMode(accessMode, instance['accessright'])) {
accessMode = instance['accessright'].classid;
}
}
/*
if(title != undefined) {
if(!title['url']) {
title['url'] = url;
}
}
*/
} else if(url) {
available['accessMode'].push("");
}
hostedBy_collectedFrom.push(available);
}
return counter;
}
// publication & dataset landing : for downloadFrom and publishedIn
changeBestAccessMode(currentAccessMode: string, accessMode: any): boolean {
if(!accessMode) {
return false;
}
accessMode = accessMode.classid;
switch (currentAccessMode) {
case null:
if(accessMode != "UNKNOWN") {
return true;
}
return false;
case "CLOSED":
if( accessMode == "OPEN" ||
accessMode == "OPEN SOURCE" ||
accessMode == "EMBARGO" ||
accessMode == "RESTRICTED") {
return true;
}
return false;
case "RESTRICTED":
if( accessMode == "OPEN" ||
accessMode == "OPEN SOURCE" ||
accessMode == "EMBARGO") {
return true;
}
return false;
case "EMBARGO":
if( accessMode == "OPEN" ||
accessMode == "OPEN SOURCE") {
return true;
}
return false;
case "OPEN SOURCE":
if( accessMode == "OPEN") {
return true;
}
return false;
}
return false;
}
// publication & dataset & software & orp landing : for relatedResearchResults
parseRelatedResearchResults(relatedResearchResults: Map<string, { "name": string, "id": string, "date": string,
"percentage": number, "class": string}[]>, relation: any, provenanceAction: string) :
Map<string, { "name": string, "id": string, "date": string,
"percentage": number, "class": string}[]> {
if(relatedResearchResults == undefined) {
relatedResearchResults = new Map<string, {"name": string, "id": string, "date": string,
"percentage": number, "class": string }[]>();
}
if(!relatedResearchResults.has(provenanceAction)) {
relatedResearchResults.set(provenanceAction,
new Array<{ "name": string, "id": string, "date": string,
"percentage": number, "class": string }>());
}
relatedResearchResults.get(provenanceAction).push(this.parseRelatedOrSimilarResearchResult(relation, "trust"));
return relatedResearchResults;
}
// publication & dataset & software & orp landing : for similarResearchResults
parseSimilarResearchResults(similarResearchResults: { "name": string, "id": string, "date": string,
"percentage": number, "class": string}[], relation: any) :
{ "name": string, "id": string, "date": string, "percentage": number, "class": string }[] {
if(similarResearchResults == undefined) {
similarResearchResults = new Array<{"name": string, "id": string, "date": string,
"percentage": number, "class": string}>();
}
similarResearchResults.push(this.parseRelatedOrSimilarResearchResult(relation, "similarity"));
return similarResearchResults;
}
// publication & dataset & software & orp landing : for relatedResearchResults and similarResearchResults
parseRelatedOrSimilarResearchResult(relation: any, percentageName: string): {"name": string, "id": string, "date": string, "percentage": number, "class": string} {
let researchResult: {"name": string, "id": string, "date": string, "percentage": number, "class": string}
= {"name": "", "id": "", "date": "", "percentage": null, "class": ""}
if(relation['resulttype'].classname == "publication") {
researchResult['class'] = "publication";
} else if(relation['resulttype'].classname == "dataset") {
researchResult['class'] = "dataset";
} else if(relation['resulttype'].classname == "software") {
researchResult['class'] = "software";
} else if(relation['resulttype'].classname == "other") {
researchResult['class'] = "other";
}
researchResult['id'] = relation['to'].content;
let titleName = Array.isArray(relation['title']) ? relation['title'][0].content : relation['title'].content;
researchResult['name'] = titleName;
if(relation.hasOwnProperty("dateofacceptance")) {
var date:string = ((Array.isArray(relation.dateofacceptance))?(relation.dateofacceptance[0]):(relation.dateofacceptance))+""; // transform to string in case it is an integer
researchResult['date'] = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
}
//researchResult['date'] = relation.dateofacceptance.substring(0,4);;
researchResult['percentage'] = Math.round(relation[percentageName]*100);
return researchResult;
}
sortByPercentage(results: { "name": string, "id": string, "date": string, "percentage": number, "class": string}[]) : { "name": string, "id": string, "date": string, "percentage": number, "class": string}[] {
if(results) {
return results.sort(function(a, b){return b["percentage"] - a["percentage"]});
}
return results;
}
// publication & dataset landing : for identifiers
parseIdentifiers(pid: any): Map<string, string[]> {
let identifiers = new Map<string, string[]>();
if(pid.hasOwnProperty("classname") && pid['classname'] != "") {
if(pid.classname == "doi" || pid.classname == "pmc" || pid.classname == "handle") {
if(!identifiers.has(pid.classname)) {
identifiers.set(pid.classname, new Array<string>());
}
identifiers.get(pid.classname).push(pid.content);
}
} else {
for(let i=0; i<pid.length; i++) {
if(pid[i].classname == "doi" || pid[i].classname == "pmc" || pid[i].classname == "handle") {
if(!identifiers.has(pid[i].classname)) {
identifiers.set(pid[i].classname, new Array<string>());
}
identifiers.get(pid[i].classname).push(pid[i].content);
}
}
}
return identifiers;
}
// publication & dataset landing : for subjects and otherSubjects and classifiedSubjects
parseAllSubjects(_subjects: any): [string[], Map<string, string[]>, Map<string, string[]>] {
let subjects: string[];
let otherSubjects: Map<string, string[]>;
let classifiedSubjects: Map<string, string[]>;
let subject;
let length = Array.isArray(_subjects) ? _subjects.length : 1;
for(let i=0; i<length; i++) {
subject = Array.isArray(_subjects) ? _subjects[i] : _subjects;
if(subject.classid != "") {
if(subject.inferred && subject.inferred == true) {
if(classifiedSubjects == undefined) {
classifiedSubjects = new Map<string, string[]>();
}
if(!classifiedSubjects.has(subject.classname)) {
classifiedSubjects.set(subject.classname, new Array<string>());
}
classifiedSubjects.get(subject.classname).push(subject.content);
} else {
if(subject.classid == "keyword") {
if(subjects == undefined) {
subjects = new Array<string>();
}
subjects.push(subject.content);
} else {
if(otherSubjects == undefined) {
otherSubjects = new Map<string, string[]>();
}
if(!otherSubjects.has(subject.classname)) {
otherSubjects.set(subject.classname, new Array<string>());
}
otherSubjects.get(subject.classname).push(subject.content);
}
}
}
}
return [subjects, otherSubjects, classifiedSubjects];
}
parseContexts(_contexts: any): {"labelContext": string, "labelCategory": string,
"labelConcept": string, inline:boolean}[] {
let contexts = new Array<{"labelContext": string, "labelCategory": string,
"labelConcept": string, inline:boolean}>();
let position = 0;
let labels = "";
let context;
let length = Array.isArray(_contexts) ? _contexts.length : 1;
for(let i=0; i<length; i++) {
context = Array.isArray(_contexts) ? _contexts[i] : _contexts;
if(context.hasOwnProperty("type") && (context['type'] == "community" || context['type'] == "ri")) {
if(context.hasOwnProperty("category")) {
let category;
let length2 = Array.isArray(context['category']) ? context['category'].length : 1;
for (let z = 0; z < length2; z++) {
category = Array.isArray(context['category']) ? context['category'][z] : context['category'];
if (category.hasOwnProperty("concept")) {
let categoryConcept;
let length1 = Array.isArray(category['concept']) ? category['concept'].length : 1;
for (let j = 0; j < length1; j++) {
categoryConcept = Array.isArray(category['concept']) ? category['concept'][j] : category['concept'];
contexts[position] = {"labelContext": "", "labelCategory": "", "labelConcept": "", inline: false};
contexts[position]['labelContext'] = context.label;
contexts[position]['labelCategory'] = category.label;
contexts[position]['labelConcept'] = categoryConcept.label;
position++;
}
} else {
contexts[position] = {"labelContext": "", "labelCategory": "", "labelConcept": "", inline: false};
contexts[position]['labelContext'] = context.label;
contexts[position]['labelCategory'] = category.label;
contexts[position]['labelConcept'] = null;
position++;
console.info(contexts);
}
}
} else {
contexts[position] = {"labelContext": "", "labelCategory": "", "labelConcept": "", inline: false};
contexts[position]['labelContext'] = context.label;
contexts[position]['labelCategory'] = null;
contexts[position]['labelConcept'] = null;
contexts[position]['new'] = false;
position++;
}
}
}
return contexts;
}
parseTypes(types: string[], uniqueTypes: Set<string>, instance: any) {
if(instance.hasOwnProperty("instancetype") && instance['instancetype'].classname) {
if(!uniqueTypes.has(instance['instancetype'].classname)) {
types.push(instance['instancetype'].classname);
uniqueTypes.add(instance['instancetype'].classname);
}
}
}
parseLanguages(_languages: any) {
var languages = new Array<string>();
if(!Array.isArray(_languages)) {
if(_languages.classname != "Undetermined" && _languages.classname) {
languages.push(_languages.classname);
}
} else {
for(let i=0; i<_languages.length; i++) {
if(_languages[i].classname != "Undetermined" && _languages[i].classname) {
languages.push(_languages[i].classname);
}
}
}
return languages;
}
parseCountries(_countries: any) {
var countries = new Array<string>();
if(!Array.isArray(_countries)) {
if(_countries.classname != "Undetermined" && _countries.classname) {
countries.push(_countries.classname);
}
} else {
for(let i=0; i<countries.length; i++) {
if(_countries[i].classname != "Undetermined" && _countries[i].classname) {
countries.push(_countries[i].classname);
}
}
}
return countries;
}
parseProgrammingLanguages(_pLanguages) {
var pLanguages = new Array<string>();
if(!Array.isArray(_pLanguages)) {
if(_pLanguages.classname != "Undetermined" && _pLanguages.classname) {
pLanguages.push(_pLanguages.classname);
}
} else {
for(let i=0; i<_pLanguages.length; i++) {
if(_pLanguages[i].classname != "Undetermined" && _pLanguages[i].classname) {
pLanguages.push(_pLanguages[i].classname);
}
}
}
return pLanguages;
}
parseReferences(citations: any): {"name": string, "url": string}[] {
let references = new Array<{"name": string, "url": string}>();
let citation;
let length = Array.isArray(citations) ? citations.length : 1;
for(let i=0; i<length; i++) {
citation = Array.isArray(citations) ? citations[i] : citations;
let url;
if(citation.hasOwnProperty("id")) {
let citationId;
let length1 = Array.isArray(citation['id']) ? citation['id'].length : 1;
for(let j=0; j<length1; j++) {
citationId = Array.isArray(citation['id']) ? citation['id'][j] : citation['id'];
if(citationId.type == "pmid") {
url = "http://www.ncbi.nlm.nih.gov/pubmed/"+citationId.value;
}
}
}
references[citation.position-1] = { "name": "", "url": ""};
references[citation.position-1]['name'] = citation.rawText;
references[citation.position-1]['url'] = url;
}
return references;
}
}