Skip to content

Commit

Permalink
feat: improve intelligibility reports
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Oct 9, 2022
1 parent f683788 commit f022425
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 8,312 deletions.
85 changes: 85 additions & 0 deletions src/core/IntelligibilityReport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { IntelligibilityReport } from './IntelligibilityReport';
import {
IntelligibilityLevel,
IntelligibilityLevelDescription,
SlavicLanguage,
} from '../types';

describe('IntelligibilityReport', () => {
it.each([
['be'],
['bg'],
['bs'],
['cnr'],
['cs'],
['csb'],
['cu'],
['dsb'],
['hr'],
['hsb'],
['mk'],
['pl'],
['ru'],
['rue'],
['sk'],
['sl'],
['sr'],
['szl'],
['uk'],
])('should have autogenerated unknown for %s', (rawLang) => {
const lang = rawLang as SlavicLanguage;
const report = new IntelligibilityReport();

expect(report[lang].tag).toBe(lang);
expect(report[lang].level).toBe('');
expect(report[lang].autogenerated).toBe(true);
});

describe('toString()', () => {
it('should generate an empty string', () => {
expect(`${new IntelligibilityReport()}`).toBe('');
});

it.each([
['!be+', SlavicLanguage.Belarusian, 'DirectMatch', true],
['bs*', SlavicLanguage.Bosnian, 'HelperMatch', false],
['!bg?', SlavicLanguage.Bulgarian, 'AmbiguousDirectMatch', true],
['hr~', SlavicLanguage.Croatian, 'AmbiguousHelperMatch', false],
['!cs#', SlavicLanguage.Czech, 'FalseFriend', true],
['csb-', SlavicLanguage.Kashubian, 'NoMatch', false],
['!dsb+', SlavicLanguage.LowerSorbian, 'DirectMatch', true],
['mk*', SlavicLanguage.Macedonian, 'HelperMatch', false],
['!cnr?', SlavicLanguage.Montenegrin, 'AmbiguousDirectMatch', true],
['cu~', SlavicLanguage.ChurchSlavonic, 'AmbiguousHelperMatch', false],
['!pl#', SlavicLanguage.Polish, 'FalseFriend', true],
['ru-', SlavicLanguage.Russian, 'NoMatch', false],
['!rue+', SlavicLanguage.Rusyn, 'DirectMatch', true],
['sr*', SlavicLanguage.Serbian, 'HelperMatch', false],
['!szl?', SlavicLanguage.Silesian, 'AmbiguousDirectMatch', true],
['sk~', SlavicLanguage.Slovak, 'AmbiguousHelperMatch', false],
['!sl#', SlavicLanguage.Slovenian, 'FalseFriend', true],
['uk-', SlavicLanguage.Ukrainian, 'NoMatch', false],
['!hsb+', SlavicLanguage.UpperSorbian, 'DirectMatch', true],
] as [string, SlavicLanguage, IntelligibilityLevelDescription, boolean][])(
'should generate %s',
(expected, lang, level, autogenerated) => {
const report = new IntelligibilityReport();
report[lang].level = IntelligibilityLevel[level];
report[lang].autogenerated = autogenerated;
expect(`${report}`).toBe(expected);
},
);
});

it('should display multiple tags', () => {
const report = new IntelligibilityReport();

report.uk.autogenerated = report.ru.autogenerated = false;
report.uk.level = IntelligibilityLevel.DirectMatch;
report.be.level = IntelligibilityLevel.DirectMatch;
report.ru.level = IntelligibilityLevel.HelperMatch;
report.pl.level = IntelligibilityLevel.DirectMatch;

expect(`${report}`).toBe('!be+ !pl+ ru* uk+');
});
});
135 changes: 135 additions & 0 deletions src/core/IntelligibilityReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { IntelligibilityLevel, SlavicLanguage } from '../types';

export class IntelligibilityAssessment {
public tag: SlavicLanguage;
public level: IntelligibilityLevel;
public autogenerated: boolean;

constructor(
tag: SlavicLanguage,
level = IntelligibilityLevel.Unknown,
autogenerated = true,
) {
this.tag = tag;
this.level = level;
this.autogenerated = autogenerated;
}

toString(): string {
if (this.level === IntelligibilityLevel.Unknown) {
return '';
}

return `${this.autogenerated ? '!' : ''}${this.tag}${this.level}`;
}
}

export class IntelligibilityReport
implements Readonly<Record<SlavicLanguage, IntelligibilityAssessment>>
{
private _data: Record<SlavicLanguage, IntelligibilityAssessment> = {
be: new IntelligibilityAssessment(SlavicLanguage.Belarusian),
bg: new IntelligibilityAssessment(SlavicLanguage.Bulgarian),
bs: new IntelligibilityAssessment(SlavicLanguage.Bosnian),
cnr: new IntelligibilityAssessment(SlavicLanguage.Montenegrin),
cs: new IntelligibilityAssessment(SlavicLanguage.Czech),
csb: new IntelligibilityAssessment(SlavicLanguage.Kashubian),
cu: new IntelligibilityAssessment(SlavicLanguage.ChurchSlavonic),
dsb: new IntelligibilityAssessment(SlavicLanguage.LowerSorbian),
hr: new IntelligibilityAssessment(SlavicLanguage.Croatian),
hsb: new IntelligibilityAssessment(SlavicLanguage.UpperSorbian),
mk: new IntelligibilityAssessment(SlavicLanguage.Macedonian),
pl: new IntelligibilityAssessment(SlavicLanguage.Polish),
ru: new IntelligibilityAssessment(SlavicLanguage.Russian),
rue: new IntelligibilityAssessment(SlavicLanguage.Rusyn),
sk: new IntelligibilityAssessment(SlavicLanguage.Slovak),
sl: new IntelligibilityAssessment(SlavicLanguage.Slovenian),
sr: new IntelligibilityAssessment(SlavicLanguage.Serbian),
szl: new IntelligibilityAssessment(SlavicLanguage.Silesian),
uk: new IntelligibilityAssessment(SlavicLanguage.Ukrainian),
};

get be(): IntelligibilityAssessment {
return this._data.be;
}

get bg(): IntelligibilityAssessment {
return this._data.bg;
}

get bs(): IntelligibilityAssessment {
return this._data.bs;
}

get cnr(): IntelligibilityAssessment {
return this._data.cnr;
}

get cs(): IntelligibilityAssessment {
return this._data.cs;
}

get csb(): IntelligibilityAssessment {
return this._data.csb;
}

get cu(): IntelligibilityAssessment {
return this._data.cu;
}

get dsb(): IntelligibilityAssessment {
return this._data.dsb;
}

get hr(): IntelligibilityAssessment {
return this._data.hr;
}

get hsb(): IntelligibilityAssessment {
return this._data.hsb;
}

get mk(): IntelligibilityAssessment {
return this._data.mk;
}

get pl(): IntelligibilityAssessment {
return this._data.pl;
}

get ru(): IntelligibilityAssessment {
return this._data.ru;
}

get rue(): IntelligibilityAssessment {
return this._data.rue;
}

get sk(): IntelligibilityAssessment {
return this._data.sk;
}

get sl(): IntelligibilityAssessment {
return this._data.sl;
}

get sr(): IntelligibilityAssessment {
return this._data.sr;
}

get szl(): IntelligibilityAssessment {
return this._data.szl;
}

get uk(): IntelligibilityAssessment {
return this._data.uk;
}

toString(): string {
return Object.values(this._data).map(String).filter(Boolean).join(' ');
}

toJSON(): Record<SlavicLanguage, IntelligibilityAssessment> {
return this._data;
}
}
6 changes: 6 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Annotation, AnnotationOptions, AnnotationType } from './Annotation';
import {
IntelligibilityAssessment,
IntelligibilityReport,
} from './IntelligibilityReport';
import { Lemma, LemmaOptions } from './Lemma';
import { LemmaGroup, LemmaGroupOptions } from './LemmaGroup';
import { Synset, SynsetMetadata, SynsetOptions } from './Synset';
Expand All @@ -7,6 +11,8 @@ export {
Annotation,
AnnotationOptions,
AnnotationType,
IntelligibilityAssessment,
IntelligibilityReport,
Lemma,
LemmaOptions,
LemmaGroup,
Expand Down
Loading

0 comments on commit f022425

Please sign in to comment.