Commit 4a1aa097 authored by Konstantin Schulz's avatar Konstantin Schulz
Browse files

frontend now supports citations for custom corpora

parent 71ceaf31
......@@ -2,7 +2,7 @@ export class Citation {
public level: string;
public label: string;
public value: number;
public validReff: Citation[];
public subcitations: { [value: number]: Citation; };
constructor(init?:Partial<Citation>) {
Object.assign(this, init);
}
......
import {Citation} from "./citation";
export class CorpusMC {
public author: string;
public cid: number;
public source_urn: string;
public source_urn: string;
public title: string;
public uri: string;
public citation_level_1: string;
public citation_level_2: string;
public citation_level_3: string;
public subCorpora: {[urn: string] : string[]};
constructor(init?:Partial<CorpusMC>) {
public citations: { [value: number]: Citation; };
constructor(init?: Partial<CorpusMC>) {
Object.assign(this, init);
}
}
......@@ -4,7 +4,6 @@ import {Reply} from "./reply";
export class GetValidReff {
public request: RequestMC;
public reply: Reply;
public isVisible = false;
constructor(init?:Partial<GetValidReff>) {
Object.assign(this, init);
}
......
export class TextRange {
public start: string[];
public end: string[];
public start: number[];
public end: number[];
constructor(init?:Partial<TextRange>) {
Object.assign(this, init);
}
......
......@@ -14,7 +14,7 @@
<ion-content padding>
<ion-grid *ngIf="corpusProvider.currentCorpus.subCorpora['']; else loading">
<ion-grid *ngIf="ObjectKeys(corpusProvider.currentCorpus.citations).length > 0; else loading">
<ion-row>
<ion-col><h1>{{ 'START' | translate }}</h1></ion-col>
<ion-col><h1>{{ 'END' | translate }}</h1></ion-col>
......@@ -26,8 +26,9 @@
</ion-label>
<ion-select [(ngModel)]="corpusProvider.currentTextRange.start[0]" name="textRangeStart0"
(ionChange)="showFurtherReferences($event.toString(), 1, true)">
<ion-option *ngFor="let urn1 of corpusProvider.currentCorpus.subCorpora['']" value="{{urn1}}">
{{urn1}}
<ion-option *ngFor="let key of ObjectKeys(corpusProvider.currentCorpus.citations)"
[value]="corpusProvider.currentCorpus.citations[key].value">
{{corpusProvider.currentCorpus.citations[key].label}}
</ion-option>
</ion-select>
</ion-col>
......@@ -37,12 +38,12 @@
</ion-label>
<ion-select [(ngModel)]="corpusProvider.currentTextRange.end[0]" name="textRangeEnd0"
(ionChange)="showFurtherReferences($event.toString(), 1, false)">
<ion-option *ngFor="let urn2 of corpusProvider.currentCorpus.subCorpora['']"
value="{{urn2}}">{{urn2}}
<ion-option *ngFor="let key2 of ObjectKeys(corpusProvider.currentCorpus.citations)"
[value]="corpusProvider.currentCorpus.citations[key2].value">
{{corpusProvider.currentCorpus.citations[key2].label}}
</ion-option>
</ion-select>
</ion-col>
</ion-row>
<ion-row *ngIf="corpusProvider.currentCorpus.citation_level_2 != CitationLevel[CitationLevel.default]">
<ion-col>
......@@ -51,10 +52,11 @@
</ion-label>
<ion-select [(ngModel)]="corpusProvider.currentTextRange.start[1]" name="textRangeStart1"
(ionChange)="showFurtherReferences($event.toString(), 2, true)"
disabled="{{corpusProvider.currentTextRange.start[0] && corpusProvider.currentCorpus.subCorpora[corpusProvider.currentTextRange.start[0]]}}">
*ngIf="corpusProvider.currentTextRange.start[0] && ObjectKeys(corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.start[0]].subcitations).length > 0">
<ion-option
*ngFor="let urn3 of corpusProvider.currentCorpus.subCorpora[corpusProvider.currentTextRange.start[0]]"
value="{{urn3}}">{{urn3}}
*ngFor="let key3 of ObjectKeys(corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.start[0]].subcitations)"
[value]="corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.start[0]].subcitations[key3].value">
{{corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.start[0]].subcitations[key3].label}}
</ion-option>
</ion-select>
</ion-col>
......@@ -64,10 +66,11 @@
</ion-label>
<ion-select [(ngModel)]="corpusProvider.currentTextRange.end[1]" name="textRangeEnd1"
(ionChange)="showFurtherReferences($event.toString(), 2, false)"
disabled="{{corpusProvider.currentTextRange.end[0] && corpusProvider.currentCorpus.subCorpora[corpusProvider.currentTextRange.end[0]]}}">
*ngIf="corpusProvider.currentTextRange.end[0] && ObjectKeys(corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.end[0]].subcitations).length > 0">
<ion-option
*ngFor="let urn4 of corpusProvider.currentCorpus.subCorpora[corpusProvider.currentTextRange.end[0]]"
value="{{urn4}}">{{urn4}}
*ngFor="let key4 of ObjectKeys(corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.end[0]].subcitations)"
[value]="corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.end[0]].subcitations[key4].value">
{{corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.end[0]].subcitations[key4].label}}
</ion-option>
</ion-select>
</ion-col>
......@@ -78,10 +81,11 @@
{{corpusProvider.currentCorpus.citation_level_3}}
</ion-label>
<ion-select [(ngModel)]="corpusProvider.currentTextRange.start[2]" name="textRangeStart2"
disabled="{{corpusProvider.currentTextRange.start[1] && corpusProvider.currentCorpus.subCorpora[[corpusProvider.currentTextRange.start[0], corpusProvider.currentTextRange.start[1]].join('.')]}}">
*ngIf="corpusProvider.currentTextRange.start[1] && ObjectKeys(corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.start[0]].subcitations[corpusProvider.currentTextRange.start[1]].subcitations).length > 0">
<ion-option
*ngFor="let urn5 of corpusProvider.currentCorpus.subCorpora[corpusProvider.currentTextRange.start[0] + '.' + corpusProvider.currentTextRange.start[1]]"
value="{{urn5}}">{{urn5}}
*ngFor="let key5 of ObjectKeys(corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.start[0]].subcitations[corpusProvider.currentTextRange.start[1]].subcitations)"
[value]="corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.start[0]].subcitations[corpusProvider.currentTextRange.start[1]].subcitations[key5].value">
{{corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.start[0]].subcitations[corpusProvider.currentTextRange.start[1]].subcitations[key5].label}}
</ion-option>
</ion-select>
</ion-col>
......@@ -89,10 +93,12 @@
<ion-label>
{{corpusProvider.currentCorpus.citation_level_3}}
</ion-label>
<ion-select [(ngModel)]="corpusProvider.currentTextRange.end[2]" name="textRangeEnd2" disabled="{{corpusProvider.currentTextRange.end[1] && corpusProvider.currentCorpus.subCorpora[[corpusProvider.currentTextRange.end[0], corpusProvider.currentTextRange.end[1]].join('.')]}}">
<ion-select [(ngModel)]="corpusProvider.currentTextRange.end[2]" name="textRangeEnd2"
*ngIf="corpusProvider.currentTextRange.end[1] && ObjectKeys(corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.end[0]].subcitations[corpusProvider.currentTextRange.end[1]].subcitations).length > 0">
<ion-option
*ngFor="let urn6 of corpusProvider.currentCorpus.subCorpora[corpusProvider.currentTextRange.end[0] + '.' + corpusProvider.currentTextRange.end[1]]"
value="{{urn6}}">{{urn6}}
*ngFor="let key6 of ObjectKeys(corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.end[0]].subcitations[corpusProvider.currentTextRange.end[1]].subcitations)"
[value]="corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.end[0]].subcitations[corpusProvider.currentTextRange.end[1]].subcitations[key6].value">
{{corpusProvider.currentCorpus.citations[corpusProvider.currentTextRange.end[0]].subcitations[corpusProvider.currentTextRange.end[1]].subcitations[key6].label}}
</ion-option>
</ion-select>
</ion-col>
......
......@@ -23,34 +23,22 @@ import {Citation} from "../../models/citation";
export class CorpusDetailPage {
// TODO: rebuild the system so it works for any corpus of arbitrary citation depth
public showMultiPartText: boolean = false;
private noTextRangeChosenString: string;
public noTextRangeChosenString: string;
public CitationLevel = CitationLevel;
private invalidTextRangeString: string;
public invalidTextRangeString: string;
public areReffEditable: boolean = true;
ObjectKeys = Object.keys;
constructor(public navCtrl: NavController, public navParams: NavParams,
public corpusProvider: CorpusProvider,
public toastCtrl: ToastController,
public translateService: TranslateService) {
this.corpusProvider.currentTextRange = new TextRange({
start: ["", "", ""],
end: ["", "", ""]
start: [0, 0, 0],
end: [0, 0, 0]
});
if (!this.corpusProvider.currentCorpus.subCorpora[""] || this.corpusProvider.currentCorpus !== this.corpusProvider.previousCorpus) {
this.corpusProvider.areCitationsComplete = false;
this.corpusProvider.getCTSvalidReff(this.corpusProvider.currentCorpus.source_urn).subscribe((result: string | object) => {
if (typeof result === "object") {
// this is a custom corpus
let textparts: object[] = result["textparts"];
this.corpusProvider.currentCitations = textparts.map(x => x["citation"]) as Citation[];
this.corpusProvider.currentCorpus.subCorpora[""] = this.corpusProvider.currentCitations.map(x => x.label);
}
else {
this.corpusProvider.xml2jsParser.parseString(result, (err, result: object) => {
this.addReferences(result, "");
});
}
});
if (Object.keys(this.corpusProvider.currentCorpus.citations).length === 0) {
this.addReferences(this.corpusProvider.currentCorpus.citation_level_1);
}
this.translateService.get("NO_TEXT_RANGE_CHOSEN").subscribe((value) => {
this.noTextRangeChosenString = value;
......@@ -67,62 +55,54 @@ export class CorpusDetailPage {
showFurtherReferences(urn: string, currentCitationLevel: number, isStart: boolean) {
if (this.areReffEditable) {
if (currentCitationLevel === 1 &&
this.corpusProvider.currentCorpus.citation_level_2 !== CitationLevel[CitationLevel.default]) {
if (currentCitationLevel === 1 && this.corpusProvider.currentCorpus.citation_level_2 !== CitationLevel[CitationLevel.default] || currentCitationLevel === 2 && this.corpusProvider.currentCorpus.citation_level_3 !== CitationLevel[CitationLevel.default]) {
// change display for further references
if (isStart) {
this.corpusProvider.currentTextRange.start[currentCitationLevel] = "";
this.corpusProvider.currentTextRange.start[currentCitationLevel] = 0;
if (this.corpusProvider.currentTextRange.start[currentCitationLevel - 1]) {
this.checkFurtherReferences(urn, isStart, currentCitationLevel);
}
}
else {
this.corpusProvider.currentTextRange.end[currentCitationLevel] = "";
this.corpusProvider.currentTextRange.end[currentCitationLevel] = 0;
if (this.corpusProvider.currentTextRange.end[currentCitationLevel - 1]) {
this.checkFurtherReferences(urn, isStart, currentCitationLevel);
}
}
this.checkFurtherReferences(urn, isStart, false);
}
else if (currentCitationLevel === 2 &&
this.corpusProvider.currentCorpus.citation_level_3 !== CitationLevel[CitationLevel.default]) {
// change display for further references
if (isStart) {
this.corpusProvider.currentTextRange.start[currentCitationLevel] = "";
}
else {
this.corpusProvider.currentTextRange.end[currentCitationLevel] = "";
}
this.checkFurtherReferences(urn, isStart, true);
}
}
}
public checkFurtherReferences(urn: string, isStart: boolean, doConcat: boolean) {
let baseUrnPart: string = isStart ? this.corpusProvider.currentTextRange.start[0] : this.corpusProvider.currentTextRange.end[0];
urn = doConcat ? (baseUrnPart + "." + urn) : urn;
if (!(urn in this.corpusProvider.currentCorpus.subCorpora)) {
// append new references and make them visible
let fullUrn: string = this.corpusProvider.currentCorpus.source_urn + ":" + urn;
this.corpusProvider.getCTSvalidReff(fullUrn).subscribe((result: string | object) => {
if (typeof result === "object") {
// this is a custom corpus
let textparts: object[] = result["textparts"];
let urnFirstPart: string = urn.split(".")[0];
let parentIndex: number = this.corpusProvider.currentCitations.findIndex(x => x.label === urnFirstPart && x.level === this.corpusProvider.currentCorpus.citation_level_1);
this.corpusProvider.currentCitations[parentIndex].validReff = textparts.map(x => x["citation"]) as Citation[];
this.corpusProvider.currentCorpus.subCorpora[urn] = this.corpusProvider.currentCitations[parentIndex].validReff.map(x => x.label);
}
else {
this.corpusProvider.xml2jsParser.parseString(result, (err, result: object) => {
this.addReferences(result, urn, ".");
});
}
});
public checkFurtherReferences(urn: string, isStart: boolean, currentCitationLevel: number) {
let citationLevel: string = "";
switch (currentCitationLevel) {
case 1:
citationLevel = this.corpusProvider.currentCorpus.citation_level_1;
break;
case 2:
citationLevel = this.corpusProvider.currentCorpus.citation_level_2;
break;
case 3:
citationLevel = this.corpusProvider.currentCorpus.citation_level_3;
break;
default:
citationLevel = CitationLevel[CitationLevel.default];
break;
}
let baseUrnPart: number = isStart ? this.corpusProvider.currentTextRange.start[0] : this.corpusProvider.currentTextRange.end[0];
if (citationLevel === this.corpusProvider.currentCorpus.citations[baseUrnPart].level) {
if (Object.keys(this.corpusProvider.currentCorpus.citations[baseUrnPart].subcitations).length === 0) {
this.addReferences(this.corpusProvider.currentCorpus.citation_level_2, [baseUrnPart]);
}
return;
}
let secondUrnPart: number = isStart ? this.corpusProvider.currentTextRange.start[1] : this.corpusProvider.currentTextRange.end[1];
if (citationLevel === this.corpusProvider.currentCorpus.citations[baseUrnPart].subcitations[secondUrnPart].level) {
if (Object.keys(this.corpusProvider.currentCorpus.citations[baseUrnPart].subcitations[secondUrnPart].subcitations).length === 0) {
this.addReferences(this.corpusProvider.currentCorpus.citation_level_3, [baseUrnPart, secondUrnPart]);
}
return;
}
// , (error: HttpErrorResponse) => {
// // no further references available, go to text display
// this.corpusProvider.currentUrn = urn;
// let urnParts = this.corpusProvider.currentUrn.split(":");
// this.corpusProvider.currentLastUrnPart = urnParts[urnParts.length - 1];
// this.corpusProvider.currentText = "";
// this.navCtrl.push(ShowTextPage).then();
// }
}
showText() {
......@@ -155,51 +135,61 @@ export class CorpusDetailPage {
}
}
private addReferences(jsonResult: object, index: string, separator: string = "") {
let gvr: GetValidReff = this.corpusProvider.ctsJson2mc(jsonResult);
gvr.isVisible = true;
let replaceString: string = this.corpusProvider.currentCorpus.source_urn + ":" + index + separator;
gvr.reply.reff.urn.forEach((value, index, array) => {
array[index] = array[index].replace(replaceString, "");
private addReferences(targetCitationLevel: string, relevantCitationIndices: number[] = []) {
let urnLastPart: string = relevantCitationIndices.map(x => x.toString()).join(".");
let fullUrn: string = this.corpusProvider.currentCorpus.source_urn + (urnLastPart ? ":" + urnLastPart : "");
this.corpusProvider.getCTSvalidReff(fullUrn).subscribe((result: string | object) => {
let newCitations: Citation[] = [];
if (typeof result === "object") {
// this is a custom corpus
let textparts: object[] = result["textparts"];
newCitations = textparts.map(x => x["citation"]) as Citation[];
}
else {
this.corpusProvider.xml2jsParser.parseString(result, (err, resultObject: object) => {
let gvr: GetValidReff = this.corpusProvider.ctsJson2mc(resultObject);
let replaceString: string = fullUrn + (urnLastPart ? "." : ":");
gvr.reply.reff.urn.forEach((urn) => {
newCitations.push(new Citation({
level: targetCitationLevel,
label: urn.replace(replaceString, ""),
value: +urn.replace(replaceString, "")
}));
});
});
}
newCitations.forEach((citation) => {
citation.subcitations = {};
if (relevantCitationIndices.length === 0) {
this.corpusProvider.currentCorpus.citations[citation.value] = citation;
}
else if (relevantCitationIndices.length === 1) {
this.corpusProvider.currentCorpus.citations[relevantCitationIndices[0]].subcitations[citation.value] = citation;
}
else if (relevantCitationIndices.length === 2) {
this.corpusProvider.currentCorpus.citations[relevantCitationIndices[0]].subcitations[relevantCitationIndices[1]].subcitations[citation.value] = citation;
}
});
});
this.corpusProvider.currentCorpus.subCorpora[index] = gvr.reply.reff.urn;
// , (error: HttpErrorResponse) => {
// // no further references available, go to text display
// this.corpusProvider.currentUrn = urn;
// let urnParts = this.corpusProvider.currentUrn.split(":");
// this.corpusProvider.currentLastUrnPart = urnParts[urnParts.length - 1];
// this.corpusProvider.currentText = "";
// this.navCtrl.push(ShowTextPage).then();
// }
}
private adjustCurrentTextRange() {
if (this.corpusProvider.currentCitations.length > 0 && !this.corpusProvider.areCitationsComplete) {
// for custom corpora: add nested citations to the already existing parent citations
let citationsWithSubCitations: Citation[] = this.corpusProvider.currentCitations.filter(x => x.hasOwnProperty("validReff"));
let nestedSubCitations: Citation[][] = citationsWithSubCitations.map(x => x.validReff);
let flattenedSubCitations: Citation[] = [].concat.apply([], nestedSubCitations);
this.corpusProvider.currentCitations = this.corpusProvider.currentCitations.concat(flattenedSubCitations);
this.corpusProvider.previousCorpus = this.corpusProvider.currentCorpus;
this.corpusProvider.areCitationsComplete = true;
}
let newUrnStart: string = "";
let newUrnEnd: string = "";
this.corpusProvider.previousCorpus = this.corpusProvider.currentCorpus;
for (let i in this.corpusProvider.currentTextRange.start) {
if (this.corpusProvider.currentTextRange.start[+i]) {
if (!this.corpusProvider.currentTextRange.end[+i]) {
this.corpusProvider.currentTextRange.end[+i] = this.corpusProvider.currentTextRange.start[+i];
}
if (this.corpusProvider.currentCitations.length > 0) {
// adjust range for custom corpus
let citationLevel: string = +i === 0 ? this.corpusProvider.currentCorpus.citation_level_1 : this.corpusProvider.currentCorpus.citation_level_2;
let relevantStartCitation: Citation = this.corpusProvider.currentCitations.find(x => x.level === citationLevel && x.label === this.corpusProvider.currentTextRange.start[+i]);
let relevantEndCitation: Citation = this.corpusProvider.currentCitations.find(x => x.level === citationLevel && x.label === this.corpusProvider.currentTextRange.end[+i]);
newUrnStart += (newUrnStart ? "." : "") + relevantStartCitation.value.toString();
newUrnEnd += (newUrnEnd ? "." : "") + relevantEndCitation.value.toString();
}
else {
newUrnStart += (newUrnStart ? "." : "") + this.corpusProvider.currentTextRange.start[+i];
newUrnEnd += (newUrnEnd ? "." : "") + this.corpusProvider.currentTextRange.end[+i];
}
if (this.corpusProvider.currentTextRange.start[+i] && !this.corpusProvider.currentTextRange.end[+i]) {
this.corpusProvider.currentTextRange.end[+i] = this.corpusProvider.currentTextRange.start[+i];
}
// if (this.corpusProvider.currentTextRange.end[+i]) {
// // remove the other newUrnEnd append line above if you uncomment this one
// newUrnEnd += (newUrnEnd ? "." : "") + this.corpusProvider.currentTextRange.end[+i];
// }
}
let newUrnStart: string = this.corpusProvider.currentTextRange.start.join(".");
let newUrnEnd: string = this.corpusProvider.currentTextRange.end.join(".");
this.corpusProvider.currentLastUrnPart = newUrnStart + "-" + newUrnEnd;
}
......
......@@ -25,9 +25,8 @@ export class HomePage {
}
test() {
let a = ["a", "b"];
for (let i in a) {
let c = 0;
}
let persons = [1,2,3];
let a = persons.join(".");
let d = 0;
}
}
......@@ -27,11 +27,10 @@ export class CorpusProvider {
public currentUrn: string;
public xml2jsParser: Parser;
public currentText: string = "";
public currentTextRange: TextRange = new TextRange({start: ["", "", ""], end: ["", "", ""]});
public currentTextRange: TextRange = new TextRange({start: [0, 0, 0], end: [0, 0, 0]});
public currentLastUrnPart: string = "";
public currentCitations: Citation[] = [];
public previousCorpus: CorpusMC;
public areCitationsComplete: boolean = false;
constructor(public translate: TranslateService,
public http: HttpClient) {
......@@ -110,7 +109,7 @@ export class CorpusProvider {
private processCorpora(corpusList: CorpusMC[]) {
corpusList.forEach((corpus: CorpusMC) => {
corpus.subCorpora = {};
corpus.citations = {};
this.availableCorpora.push(corpus);
let authorIndex = this.availableAuthors.map(author => author.name).indexOf(corpus.author);
if (authorIndex > -1) {
......
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