show-text.page.ts 4.39 KB
Newer Older
1
/* tslint:disable:no-string-literal */
2
import {Component, OnInit} from '@angular/core';
3
4
5
6
7
8
import {NavController, ToastController} from '@ionic/angular';
import {CorpusService} from 'src/app/corpus.service';
import {VocabularyService} from 'src/app/vocabulary.service';
import {ExerciseService} from 'src/app/exercise.service';
import {HelperService} from 'src/app/helper.service';
import {TranslateService} from '@ngx-translate/core';
9
import {FileType, VocabularyCorpus} from '../models/enum';
10
import {HttpClient} from '@angular/common/http';
11
12

@Component({
13
14
15
    selector: 'app-show-text',
    templateUrl: './show-text.page.html',
    styleUrls: ['./show-text.page.scss'],
16
})
17
export class ShowTextPage implements OnInit {
18
    FileType = FileType;
19
    HelperService = HelperService;
Konstantin Schulz's avatar
Konstantin Schulz committed
20
    ObjectKeys = Object.keys;
21
    public highlightOOV = false;
Konstantin Schulz's avatar
Konstantin Schulz committed
22
    public text: string;
23
    public urlBase: string;
24
    public isDownloading = false;
25
    public showTextComplexity = false;
26
    public showTextComplexityDoc = false;
27
    public textComplexityMap = {
28
        all: 'TEXT_COMPLEXITY_ALL',
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
        n_w: 'TEXT_COMPLEXITY_WORD_COUNT',
        n_sent: 'TEXT_COMPLEXITY_SENTENCE_COUNT',
        avg_w_per_sent: 'TEXT_COMPLEXITY_AVERAGE_SENTENCE_LENGTH',
        avg_w_len: 'TEXT_COMPLEXITY_AVERAGE_WORD_LENGTH',
        n_types: 'TEXT_COMPLEXITY_TYPE_COUNT',
        pos: 'TEXT_COMPLEXITY_PART_OF_SPEECH_COUNT',
        lex_den: 'TEXT_COMPLEXITY_LEXICAL_DENSITY',
        n_punct: 'TEXT_COMPLEXITY_PUNCTUATION_COUNT',
        n_clause: 'TEXT_COMPLEXITY_CLAUSE_COUNT',
        n_subclause: 'TEXT_COMPLEXITY_SUBCLAUSE_COUNT',
        n_inf: 'TEXT_COMPLEXITY_INFINITIVE_COUNT',
        n_part: 'TEXT_COMPLEXITY_PARTICIPLE_COUNT',
        n_gerund: 'TEXT_COMPLEXITY_GERUND_COUNT',
        n_abl_abs: 'TEXT_COMPLEXITY_ABLATIVI_ABSOLUTI_COUNT'
    };
44

45
46
47
48
49
    constructor(public navCtrl: NavController,
                public corpusService: CorpusService,
                public exerciseService: ExerciseService,
                public toastCtrl: ToastController,
                public translateService: TranslateService,
50
51
                public vocService: VocabularyService,
                public http: HttpClient) {
52
53
54
        HelperService.waitForConfig().then(() => {
            this.urlBase = HelperService.config['backendBaseUrl'] + HelperService.config['backendApiFilePath'];
        });
55
56
57
58
    }

    generateDownloadLink(fileType: string) {
        const formData = new FormData();
59
        let content: string = document.querySelector('.text').outerHTML;
60
        // add underline elements so we do not need to specify CSS options in the backend's PDF generator
61
        content = content.replace(/(oov">)(.+?)(<\/span>)/g, '$1<u>$2</u>$3');
62
        const authorTitle: string = this.corpusService.currentCorpus.author + ', ' + this.corpusService.currentCorpus.title;
63
64
        content = `<p>${authorTitle} ${this.corpusService.currentUrn.split(':').slice(-1)[0]}</p>` + content;
        formData.append('html_content', content);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        formData.append('file_type', fileType);
        formData.append('urn', this.corpusService.currentUrn);
        this.isDownloading = true;
        this.http.post(this.urlBase, formData).subscribe((response: string) => {
            this.isDownloading = false;
            const responseParts: string[] = response.split('.');
            const link: HTMLLinkElement = document.querySelector('#download');
            link.href = HelperService.config['backendBaseUrl'] + HelperService.config['backendApiFilePath'] + '?id='
                + responseParts[0] + '&type=' + responseParts[1];
            link.style.display = 'block';
        }, async (error: any) => {
            this.isDownloading = false;
            HelperService.currentError = error;
            const toast = await this.toastCtrl.create({
                message: HelperService.generalErrorAlertMessage,
                duration: 3000,
                position: 'top'
            });
            toast.present().then();
        });
85
    }
86

Konstantin Schulz's avatar
Konstantin Schulz committed
87
    getWhiteSpace(index: number) {
88
89
        if (this.corpusService.annisResponse.nodes[index + 1]) {
            if ('.,\\/#!$%\\^&\\*;:{}=\\-_`~()'.indexOf(this.corpusService.annisResponse.nodes[index + 1].annis_tok) > -1) {
Konstantin Schulz's avatar
Konstantin Schulz committed
90
91
92
93
94
95
                return '';
            }
            return ' ';
        }
        return '';
    }
96
97
98
99

    ngOnInit(): void {
        this.vocService.currentReferenceVocabulary = this.vocService.currentReferenceVocabulary || VocabularyCorpus.bws;
    }
100
}