From 64467d43d244754d7b22e0fb2d8e48779e7d6da7 Mon Sep 17 00:00:00 2001 From: Konstantin Schulz Date: Thu, 4 Jun 2020 11:41:03 +0200 Subject: [PATCH] fixed enum handling for vocabulary corpora --- .../app/exercise-list/exercise-list.page.html | 3 +- .../exercise-list/exercise-list.page.spec.ts | 5 +- .../app/exercise-list/exercise-list.page.ts | 8 ++- mc_frontend/src/app/models/enum.ts | 7 --- mc_frontend/src/app/preview/preview.page.html | 6 +-- .../src/app/show-text/show-text.page.html | 4 +- .../src/app/show-text/show-text.page.spec.ts | 4 +- .../src/app/show-text/show-text.page.ts | 5 +- .../vocabulary-check.page.html | 2 +- .../vocabulary-check/vocabulary-check.page.ts | 3 +- .../src/app/vocabulary.service.spec.ts | 14 +++-- mc_frontend/src/app/vocabulary.service.ts | 51 +++++++++---------- 12 files changed, 52 insertions(+), 60 deletions(-) diff --git a/mc_frontend/src/app/exercise-list/exercise-list.page.html b/mc_frontend/src/app/exercise-list/exercise-list.page.html index d8458cc..e971d9f 100644 --- a/mc_frontend/src/app/exercise-list/exercise-list.page.html +++ b/mc_frontend/src/app/exercise-list/exercise-list.page.html @@ -66,8 +66,7 @@ - diff --git a/mc_frontend/src/app/vocabulary-check/vocabulary-check.page.ts b/mc_frontend/src/app/vocabulary-check/vocabulary-check.page.ts index d2a2a4f..9c0cd14 100644 --- a/mc_frontend/src/app/vocabulary-check/vocabulary-check.page.ts +++ b/mc_frontend/src/app/vocabulary-check/vocabulary-check.page.ts @@ -1,6 +1,6 @@ import {NavController, ToastController} from '@ionic/angular'; import {Component} from '@angular/core'; -import {VocabularyCorpus, VocabularyCorpusTranslation} from 'src/app/models/enum'; +import {VocabularyCorpusTranslation} from 'src/app/models/enum'; import {VocabularyService} from 'src/app/vocabulary.service'; import {HttpClient, HttpErrorResponse} from '@angular/common/http'; import {TranslateService} from '@ngx-translate/core'; @@ -19,7 +19,6 @@ import {Sentence} from '../../../openapi'; }) export class VocabularyCheckPage { ObjectKeys = Object.keys; - VocabularyCorpus = VocabularyCorpus; VocabularyCorpusTranslation = VocabularyCorpusTranslation; public adaptPassages = true; public currentRankingUnits: Sentence[][]; diff --git a/mc_frontend/src/app/vocabulary.service.spec.ts b/mc_frontend/src/app/vocabulary.service.spec.ts index dd2b370..ac1334c 100644 --- a/mc_frontend/src/app/vocabulary.service.spec.ts +++ b/mc_frontend/src/app/vocabulary.service.spec.ts @@ -4,9 +4,8 @@ import {VocabularyService} from './vocabulary.service'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {IonicStorageModule} from '@ionic/storage'; import {TranslateTestingModule} from './translate-testing/translate-testing.module'; -import {VocabularyCorpus} from './models/enum'; -import {HttpErrorResponse} from '@angular/common/http'; -import {AnnisResponse, Sentence} from '../../openapi'; +import {HttpErrorResponse, HttpParams} from '@angular/common/http'; +import {AnnisResponse, Sentence, VocabularyForm, VocabularyMC} from '../../openapi'; import Spy = jasmine.Spy; describe('VocabularyService', () => { @@ -38,6 +37,11 @@ describe('VocabularyService', () => { getSpy.and.returnValue(Promise.resolve([])); const sentences: Sentence[] = await vocabularyService.getMatchingSentences(''); expect(sentences.length).toBe(0); + const params: HttpParams = getSpy.calls.argsFor(0)[3]; + const vfObj: object = {}; + params.keys().forEach((key: string) => vfObj[key] = params.get(key)); + const vf: VocabularyForm = vfObj as VocabularyForm; + expect(vf.vocabulary).toBe(VocabularyMC.Bws); const postSpy: Spy = spyOn(vocabularyService.helperService, 'makePostRequest').and.returnValue(Promise.resolve({})); const result: AnnisResponse = await vocabularyService.getOOVwords(''); expect(result.graph_data).toBeFalsy(); @@ -60,10 +64,10 @@ describe('VocabularyService', () => { it('should update the reference range', () => { vocabularyService.frequencyUpperBound = 0; vocabularyService.ngOnInit(); - vocabularyService.currentReferenceVocabulary = VocabularyCorpus.agldt; + vocabularyService.currentReferenceVocabulary = VocabularyMC.Agldt; vocabularyService.updateReferenceRange(); expect(vocabularyService.frequencyUpperBound).toBe(500); - vocabularyService.currentReferenceVocabulary = VocabularyCorpus.viva; + vocabularyService.currentReferenceVocabulary = VocabularyMC.Viva; vocabularyService.updateReferenceRange(); expect(vocabularyService.frequencyUpperBound).toBe(1164); }); diff --git a/mc_frontend/src/app/vocabulary.service.ts b/mc_frontend/src/app/vocabulary.service.ts index 6d588c0..d61a89e 100644 --- a/mc_frontend/src/app/vocabulary.service.ts +++ b/mc_frontend/src/app/vocabulary.service.ts @@ -1,7 +1,6 @@ /* tslint:disable:no-string-literal */ import {HttpClient, HttpErrorResponse, HttpParams} from '@angular/common/http'; import {Injectable, OnInit} from '@angular/core'; -import {VocabularyCorpus} from 'src/app/models/enum'; import {Vocabulary} from 'src/app/models/vocabulary'; import {HelperService} from 'src/app/helper.service'; import {TestResultMC} from 'src/app/models/testResultMC'; @@ -14,7 +13,7 @@ import {VocabularyMC} from '../../openapi'; providedIn: 'root' }) export class VocabularyService implements OnInit { - currentReferenceVocabulary: VocabularyCorpus = VocabularyCorpus.bws; + currentReferenceVocabulary: VocabularyMC = VocabularyMC.Bws; currentSentences: Sentence[] = []; currentTestResults: { [exerciseIndex: number]: TestResultMC } = {}; desiredSentenceCount = 10; @@ -32,69 +31,69 @@ export class VocabularyService implements OnInit { return this.refVocMap[this.currentReferenceVocabulary]; } - getOOVwords(queryUrn: string): Promise { - return new Promise(((resolve, reject) => { + getMatchingSentences(queryUrn: string): Promise { + return new Promise((resolve, reject) => { const url: string = configMC.backendBaseUrl + configMC.backendApiVocabularyPath; const vf: VocabularyForm = { frequency_upper_bound: this.frequencyUpperBound, query_urn: queryUrn, - vocabulary: VocabularyMC[this.currentReferenceVocabulary] + vocabulary: this.currentReferenceVocabulary }; - const formData: FormData = new FormData(); - Object.keys(vf).forEach((key: string) => formData.append(key, vf[key])); - this.helperService.makePostRequest(this.http, this.toastCtrl, url, formData).then((result: AnnisResponse) => { + let params: HttpParams = new HttpParams(); + Object.keys(vf).forEach((key: string) => { + params = params.set(key, vf[key]); + }); + this.helperService.makeGetRequest(this.http, this.toastCtrl, url, params).then((result: Sentence[]) => { return resolve(result); }, (error: HttpErrorResponse) => { return reject(error); }); - })); + }); } getMean(sentences: Sentence[]): number { return sentences.map(x => x.matching_degree).reduce((a, b) => a + b) / sentences.length; } - getPossibleSubCount(): number { - return this.getCurrentReferenceVocabulary().possibleSubcounts[0]; - } - - getMatchingSentences(queryUrn: string): Promise { - return new Promise((resolve, reject) => { + getOOVwords(queryUrn: string): Promise { + return new Promise(((resolve, reject) => { const url: string = configMC.backendBaseUrl + configMC.backendApiVocabularyPath; const vf: VocabularyForm = { frequency_upper_bound: this.frequencyUpperBound, query_urn: queryUrn, - vocabulary: VocabularyMC[this.currentReferenceVocabulary] + vocabulary: this.currentReferenceVocabulary }; - let params: HttpParams = new HttpParams(); - Object.keys(vf).forEach((key: string) => { - params = params.set(key, vf[key]); - }); - this.helperService.makeGetRequest(this.http, this.toastCtrl, url, params).then((result: Sentence[]) => { + const formData: FormData = new FormData(); + Object.keys(vf).forEach((key: string) => formData.append(key, vf[key])); + this.helperService.makePostRequest(this.http, this.toastCtrl, url, formData).then((result: AnnisResponse) => { return resolve(result); }, (error: HttpErrorResponse) => { return reject(error); }); - }); + })); + } + + getPossibleSubCount(): number { + return this.getCurrentReferenceVocabulary().possibleSubcounts[0]; } ngOnInit(): void { - this.refVocMap[VocabularyCorpus.agldt] = new Vocabulary({ + this.refVocMap[VocabularyMC.Agldt] = new Vocabulary({ hasFrequencyOrder: true, totalCount: 7182, possibleSubcounts: [] }); - this.refVocMap[VocabularyCorpus.bws] = new Vocabulary({ + this.refVocMap[VocabularyMC.Bws] = new Vocabulary({ hasFrequencyOrder: false, totalCount: 1276, possibleSubcounts: [500, 1276] }); - this.refVocMap[VocabularyCorpus.proiel] = new Vocabulary({ + this.refVocMap[VocabularyMC.Proiel] = new Vocabulary({ hasFrequencyOrder: true, totalCount: 16402, possibleSubcounts: [] }); - this.refVocMap[VocabularyCorpus.viva] = new Vocabulary({ + this.refVocMap[VocabularyMC.Viva] = new Vocabulary({ hasFrequencyOrder: false, totalCount: 1164, possibleSubcounts: [1164] -- GitLab