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

684 lines
24 KiB
TypeScript

import {HostedByCollectedFrom, Journal, Project, RelationResult} from "../../utils/result-preview/result-preview";
import {Reference} from "../../utils/entities/resultLandingInfo";
export class ParsingFunctions {
public open = 'assets/common-assets/unlock.svg';
public closed = 'assets/common-assets/lock.svg';
public unknown = 'assets/common-assets/question.svg';
constructor() {
}
public ngOnDestroy() {
}
public parseFundingByProjects(fundedByProjects: Project[], relation: any,
provenanceActionVocabulary: any): Project[] {
if (fundedByProjects == undefined) {
fundedByProjects = [];
}
let fundedByProject: Project = {
"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 (provenanceActionVocabulary != null && relation.provenanceaction in provenanceActionVocabulary) {
fundedByProject['provenanceAction'] = provenanceActionVocabulary[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: HostedByCollectedFrom[],
publisher: string, journal: Journal,
identifiers: Map<string, string[]>/*, title: { "name": string, "url": string, "accessMode": string}*/) {
if (publisher && identifiers != null && identifiers.has('doi')) {
if (hostedBy_collectedFrom == null) {
hostedBy_collectedFrom = [];
}
let available: HostedByCollectedFrom = {
downloadName: "",
downloadUrl: null,
collectedName: "",
collectedId: "",
accessMode: null,
bestAccessMode: null,
type: "",
year: "",
icon: ""
};
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);
available.icon = this.unknown;
/*
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'].classname);
}
if (this.changeBestAccessMode(mapStructure.get(key)['bestAccessMode'], instance['accessright'])) {
mapStructure.get(key)['bestAccessMode'] = instance['accessright'].classname;
}
} else if (url) {
mapStructure.get(key)['accessMode'].push("");
}
}
parseHostedBy_collectedFrom(hostedBy_collectedFrom: HostedByCollectedFrom[],
instance: any, data: any, url: string, counter: number/*,
title: { "name": string, "url": string, "accessMode": string}*/, accessMode: string): number {
let available: HostedByCollectedFrom = {
"downloadName": "",
"downloadUrl": null,
"collectedName": "",
"collectedId": "",
"accessMode": null,
"bestAccessMode": null,
"type": "",
"year": "",
"icon": ""
};
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) {
available.downloadName = url.substring(0, 30) + '...'; // substring(from, to);
}
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'].classname);
}
if (this.changeBestAccessMode(available.bestAccessMode, instance['accessright'])) {
available.bestAccessMode = instance['accessright'].classname;
/*
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'].classname;
}
}
/*
if(title != undefined) {
if(!title['url']) {
title['url'] = url;
}
}
*/
} else if (url) {
available['accessMode'].push("");
}
if (available.bestAccessMode) {
if (available.bestAccessMode.toLowerCase().indexOf('open') !== -1) {
available.icon = this.open;
} else if (available.bestAccessMode.toLowerCase().indexOf('not available') !== -1) {
available.icon = this.unknown;
} else {
available.icon = this.closed;
}
} else {
available.icon = this.unknown;
}
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: RelationResult[], relation: any, provenanceAction: string):
RelationResult[] {
if (relatedResearchResults == undefined) {
relatedResearchResults = []
}
relatedResearchResults.push(this.parseRelatedOrSimilarResearchResult(relation, "trust", provenanceAction));
return relatedResearchResults;
}
// publication & dataset & software & orp landing : for supplementaryResearchResults
parseSupplementaryResearchResults(supplementaryResearchResults: RelationResult[], relation: any): RelationResult[] {
if (supplementaryResearchResults == undefined) {
supplementaryResearchResults = [];
}
supplementaryResearchResults.push(this.parseRelatedOrSimilarResearchResult(relation, "trust"));
return supplementaryResearchResults;
}
// publication & dataset & software & orp landing : for supplementedByResearchResults
parseSupplementedByResearchResults(supplementedByResearchResults: RelationResult[], relation: any): RelationResult[] {
if (supplementedByResearchResults == undefined) {
supplementedByResearchResults = [];
}
supplementedByResearchResults.push(this.parseRelatedOrSimilarResearchResult(relation, "trust"));
return supplementedByResearchResults;
}
// publication & dataset & software & orp landing : for similarResearchResults
parseSimilarResearchResults(similarResearchResults: RelationResult[], relation: any): RelationResult[] {
if (similarResearchResults == undefined) {
similarResearchResults = [];
}
similarResearchResults.push(this.parseRelatedOrSimilarResearchResult(relation, "similarity"));
return similarResearchResults;
}
// publication & dataset & software & orp landing : for relatedResearchResults and similarResearchResults
parseRelatedOrSimilarResearchResult(relation: any, percentageName: string, provenanceAction: string = null): RelationResult {
let researchResult: RelationResult = {
name: "",
id: "",
date: "",
percentage: null,
class: "",
provenanceAction: provenanceAction
};
if(relation['resulttype']) {
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']?relation['title'].content:null);
researchResult['name'] = titleName;
if(!researchResult['name']) {
researchResult['name'] = "[no title available]";
}
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: RelationResult[]): RelationResult[] {
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("classid") && pid['classid'] != "") {
if (pid.classid == "doi" || pid.classid == "pmc" || pid.classid == "handle" || pid.classid == "pmid") {
if (!identifiers.has(pid.classid)) {
identifiers.set(pid.classid, new Array<string>());
}
identifiers.get(pid.classid).push(pid.content);
}
} else {
for (let i = 0; i < pid.length; i++) {
if (pid[i].classid == "doi" || pid[i].classid == "pmc" || pid[i].classid == "handle" || pid[i].classid == "pmid") {
if (!identifiers.has(pid[i].classid)) {
identifiers.set(pid[i].classid, new Array<string>());
}
identifiers.get(pid[i].classid).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++;
}
}
} 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 && 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): Reference[] {
let references: Reference[] = [];
citations = Array.isArray(citations) ? citations : [citations];
citations.forEach(citation => {
let reference: Reference = {name: null, ids: []};
if(citation.rawText) {
reference.name = citation.rawText;
}
if(citation.id) {
let ids: any[] = Array.isArray(citation.id) ? citation.id : [citation.id];
ids.forEach(id => {
reference.ids.push({
type: id.type,
value: id.value,
trust: id.confidenceLevel
});
});
}
references[citation.position - 1] = reference;
});
return references;
}
}