Skip to content

Commit

Permalink
Merge pull request #1727 from CentreForDigitalHumanities/feature/n-gr…
Browse files Browse the repository at this point in the history
…am-update

Clean up routing behaviour in ngram component
  • Loading branch information
BeritJanssen authored Dec 23, 2024
2 parents 04e1a63 + 6bf5203 commit 3f3d887
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 208 deletions.
4 changes: 2 additions & 2 deletions frontend/src/app/common-test-bed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { EntityService } from './services/entity.service';
import { WordmodelsService } from './services/wordmodels.service';
import { WordmodelsServiceMock } from '../mock-data/wordmodels';
import { VisualizationService } from './services/visualization.service';
import { visualizationServiceMock } from '../mock-data/visualization';
import { VisualizationServiceMock } from '../mock-data/visualization';
import { TagService } from './services/tag.service';
import { TagServiceMock } from '../mock-data/tag';
import { RouterStoreService } from './store/router-store.service';
Expand Down Expand Up @@ -72,7 +72,7 @@ export const commonTestBed = () => {
},
{
provide: VisualizationService,
useValue: new visualizationServiceMock(),
useValue: new VisualizationServiceMock(),
},
{
provide: TagService,
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/app/models/field-filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { convertToParamMap } from '@angular/router';
import { mockFieldMultipleChoice, mockFieldDate, mockField } from '../../mock-data/corpus';
import { EsDateFilter, EsTermsFilter } from './elasticsearch';
import { BooleanFilter, DateFilter, DateFilterData, MultipleChoiceFilter } from './field-filter';
import { of } from 'rxjs';
import { distinct } from 'rxjs/operators';
import { SimpleStore } from '../store/simple-store';
import { Store } from '../store/types';

Expand Down
45 changes: 45 additions & 0 deletions frontend/src/app/models/ngram.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { SimpleStore } from '../store/simple-store';
import { RouterStoreService } from '../store/router-store.service';
import { NgramParameters, NgramSettings } from './ngram';

describe('NgramParameters', ()=> {
let store: RouterStoreService = new SimpleStore() as any;
let ngramParameters: NgramParameters;
const testState = {
size: 3,
positions: 'first',
freqCompensation: true,
analysis: 'clean',
maxDocuments: 100,
numberOfNgrams: 20,
} as NgramSettings;
const testParams = {ngramSettings: 's:3,p:first,c:true,a:clean,m:100,n:20'}

beforeEach(() => {
ngramParameters = new NgramParameters(store);
});

it('should correctly convert internal state to a route parameter', () => {
const params = ngramParameters.stateToStore(testState);
expect(params).toEqual(testParams);
});

it('should correctly convert a route parameter to an internal state', () => {
const state = ngramParameters.storeToState(testParams);
expect(state).toEqual(testState);
});

it('should return default values if no relevant route parameter is present', () => {
const defaultSettings = {
size: 2,
positions: 'any',
freqCompensation: false,
analysis: 'none',
maxDocuments: 50,
numberOfNgrams: 10,
} as NgramSettings;
const state = ngramParameters.storeToState({irrelevant: 'parameter'})
expect(state).toEqual(defaultSettings);
})

});
60 changes: 60 additions & 0 deletions frontend/src/app/models/ngram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Params } from '@angular/router';
import * as _ from 'lodash';

import { StoreSync } from '../store/store-sync';
import { Store } from '../store/types';

export interface NgramSettings {
size: number;
positions: string;
freqCompensation: boolean;
analysis: string;
maxDocuments: number;
numberOfNgrams: number;
}

export class NgramParameters extends StoreSync<NgramSettings> {

keysInStore = ['ngramSettings'];

constructor(store: Store) {
super(store);
this.connectToStore();
}

stringifyNgramSettings(state: NgramSettings): string {
return [`s:${state.size}`,`p:${state.positions}`,`c:${state.freqCompensation}`,
`a:${state.analysis}`,`m:${state.maxDocuments}`,`n:${state.numberOfNgrams}`].join(',')
}

stateToStore(state: NgramSettings): Params {
return { ngramSettings: this.stringifyNgramSettings(state)}
}

storeToState(params: Params): NgramSettings {
if (_.has(params, 'ngramSettings')) {
const stringComponents = params['ngramSettings'].split(',');
return {
size: parseInt(this.findSetting('s', stringComponents), 10),
positions: this.findSetting('p', stringComponents),
freqCompensation: this.findSetting('c', stringComponents) === 'true',
analysis: this.findSetting('a', stringComponents),
maxDocuments: parseInt(this.findSetting('m', stringComponents), 10),
numberOfNgrams: parseInt(this.findSetting('n', stringComponents), 10),
}
}
return {
size: 2,
positions: 'any',
freqCompensation: false,
analysis: 'none',
maxDocuments: 50,
numberOfNgrams: 10,
} as NgramSettings;
}

findSetting(abbreviation: string, stringComponents: string[]): string | undefined{
const setting = stringComponents.find(s => s[0] === abbreviation);
return setting.split(':')[1];
}
}
37 changes: 0 additions & 37 deletions frontend/src/app/models/visualization.spec.ts

This file was deleted.

76 changes: 13 additions & 63 deletions frontend/src/app/models/visualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ export type WordcloudParameters = {
} & APIQuery;


export type NGramRequestParameters = {
corpus_name: string;
field: string;
ngram_size?: number;
term_position?: string;
freq_compensation?: boolean;
subfield?: string;
max_size_per_interval?: number;
number_of_ngrams?: number;
date_field: string;
} & APIQuery;


export interface FreqTableHeader {
key: string;
label: string;
Expand All @@ -96,69 +109,6 @@ export interface ChartParameters {
chartType: ChartType;
}

export type NGramRequestParameters = {
corpus_name: string;
field: string;
ngram_size?: number;
term_position?: string;
freq_compensation?: boolean;
subfield?: string;
max_size_per_interval?: number;
number_of_ngrams?: number;
date_field: string;
} & APIQuery;

export class NgramParameters {
size: number;
positions: string;
freqCompensation: boolean;
analysis: string;
maxDocuments: number;
numberOfNgrams: number;
dateField: string;

ngramSettings: string [];

constructor(size: number,
positions: string,
freqCompensation: boolean,
analysis: string,
maxDocuments: number,
numberOfNgrams: number,
dateField: string
) {
this.size = size;
this.positions = positions;
this.freqCompensation = freqCompensation;
this.analysis = analysis;
this.maxDocuments = maxDocuments;
this.numberOfNgrams = numberOfNgrams;
this.dateField = dateField;
}

toRouteParam(): string {
return [`s:${this.size}`,`p:${this.positions}`,`c:${this.freqCompensation}`,
`a:${this.analysis}`,`m:${this.maxDocuments}`,`n:${this.numberOfNgrams}`,
`f:${this.dateField}`].join(',');
}

fromRouteParam(paramString: string) {
this.ngramSettings = paramString.split(',');
this.size = parseInt(this.findSetting('s'), 10);
this.positions = this.findSetting('p');
this.freqCompensation = this.findSetting('c') === 'true';
this.analysis = this.findSetting('a');
this.maxDocuments = parseInt(this.findSetting('m'), 10);
this.numberOfNgrams = parseInt(this.findSetting('n'), 10);
this.dateField = this.findSetting('f');
}

findSetting(abbreviation: string): string | undefined{
const setting = this.ngramSettings.find(s => s[0] === abbreviation);
return setting.split(':')[1];
}
}

export interface FieldCoverage {
[field: string]: number;
};
11 changes: 6 additions & 5 deletions frontend/src/app/services/visualization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
GeoLocation,
MostFrequentWordsResult,
NGramRequestParameters,
NgramParameters,
QueryModel,
TaskResult,
TimeCategory,
} from '@models';
import { ApiService } from './api.service';
import { Observable } from 'rxjs';
import { NgramSettings } from '@models/ngram';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -96,7 +96,8 @@ export class VisualizationService {
corpus: Corpus,
queryModel: QueryModel,
field: string,
params: NgramParameters
params: NgramSettings,
dateField: string
): NGramRequestParameters {
const query = queryModel.toAPIQuery();
return {
Expand All @@ -109,7 +110,7 @@ export class VisualizationService {
subfield: params.analysis,
max_size_per_interval: params.maxDocuments,
number_of_ngrams: params.numberOfNgrams,
date_field: params.dateField
date_field: dateField
};
}

Expand All @@ -121,8 +122,8 @@ export class VisualizationService {
return this.apiService.getDateTermFrequency(params);
}

getNgramTasks(queryModel: QueryModel, corpus: Corpus, field: string, params: NgramParameters): Promise<TaskResult> {
const ngramRequestParams = this.makeNgramRequestParameters(corpus, queryModel, field, params);
getNgramTasks(queryModel: QueryModel, corpus: Corpus, field: string, params: NgramSettings, dateField: string): Promise<TaskResult> {
const ngramRequestParams = this.makeNgramRequestParameters(corpus, queryModel, field, params, dateField);
return this.apiService.ngramTasks(ngramRequestParams);
}

Expand Down
Loading

0 comments on commit 3f3d887

Please sign in to comment.