forked from FooSoft/yomichan
-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathtranslator-frequency-sort.test.js
More file actions
129 lines (113 loc) · 5.17 KB
/
Copy pathtranslator-frequency-sort.test.js
File metadata and controls
129 lines (113 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (C) 2026 Yomitan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {describe, expect, test} from 'vitest';
import {Translator} from '../ext/js/language/translator.js';
/**
* Creates a term frequency in the internal shape produced by the translator.
* @param {number} index
* @param {string} dictionary
* @param {number} dictionaryIndex
* @param {number} frequency
* @returns {import('dictionary').TermFrequency}
*/
function createFrequency(index, dictionary, dictionaryIndex, frequency) {
return {
index,
headwordIndex: 0,
dictionary,
dictionaryAlias: dictionary,
dictionaryIndex,
frequencyMode: 'rank-based',
hasReading: false,
frequency,
displayValue: null,
displayValueParsed: false,
};
}
/**
* Builds a minimal single-headword term dictionary entry carrying only the fields
* used by the harmonic frequency sort.
* @param {import('dictionary').TermFrequency[]} frequencies
* @returns {import('translation-internal').TermDictionaryEntry}
*/
function createEntry(frequencies) {
return /** @type {import('translation-internal').TermDictionaryEntry} */ (/** @type {unknown} */ ({
headwords: [{}],
definitions: [{headwordIndices: [0], frequencyOrder: 0}],
frequencies,
frequencyOrder: 0,
}));
}
describe('Translator harmonic frequency sort', () => {
const translator = new Translator(/** @type {import('../ext/js/dictionary/dictionary-database.js').DictionaryDatabase} */ (/** @type {unknown} */ ({})));
// Accessed via a string key so the underscore-prefixed method name does not trip no-underscore-dangle.
// eslint-disable-next-line dot-notation
const updateSortFrequenciesHarmonic = translator['_updateSortFrequenciesHarmonic'].bind(translator);
test('averages the frequency across all dictionaries (matching the displayed harmonic value)', () => {
// harmonic(1, 3) = 2 / (1/1 + 1/3) = 1.5 -> floor 1
const entryLow = createEntry([
createFrequency(0, 'A', 0, 1),
createFrequency(1, 'B', 1, 3),
]);
// harmonic(4, 12) = 2 / (1/4 + 1/12) = 6
const entryHigh = createEntry([
createFrequency(0, 'A', 0, 4),
createFrequency(1, 'B', 1, 12),
]);
/** @type {import('translation-internal').TermDictionaryEntry[]} */
const entries = [entryHigh, entryLow];
updateSortFrequenciesHarmonic(entries, true);
expect(entryLow.frequencyOrder).toStrictEqual(1);
expect(entryHigh.frequencyOrder).toStrictEqual(6);
expect(entryLow.definitions[0].frequencyOrder).toStrictEqual(1);
expect(entryHigh.definitions[0].frequencyOrder).toStrictEqual(6);
// Ascending places the lower average first.
entries.sort((a, b) => a.frequencyOrder - b.frequencyOrder);
expect(entries).toStrictEqual([entryLow, entryHigh]);
});
test('descending negates the order so the higher average sorts first', () => {
const entryLow = createEntry([createFrequency(0, 'A', 0, 2)]);
const entryHigh = createEntry([createFrequency(0, 'A', 0, 50)]);
const entries = [entryLow, entryHigh];
updateSortFrequenciesHarmonic(entries, false);
expect(entryLow.frequencyOrder).toStrictEqual(-2);
expect(entryHigh.frequencyOrder).toStrictEqual(-50);
entries.sort((a, b) => a.frequencyOrder - b.frequencyOrder);
expect(entries).toStrictEqual([entryHigh, entryLow]);
});
test('counts at most one frequency per dictionary', () => {
// The second value from dictionary A must be ignored: harmonic(1, 3) = 1, not harmonic(1, 100, 3).
const entry = createEntry([
createFrequency(0, 'A', 0, 1),
createFrequency(1, 'A', 0, 100),
createFrequency(2, 'B', 1, 3),
]);
updateSortFrequenciesHarmonic([entry], true);
expect(entry.frequencyOrder).toStrictEqual(1);
});
test('entries without frequency data sort last', () => {
const entryWithData = createEntry([createFrequency(0, 'A', 0, 5)]);
const entryNoData = createEntry([]);
const ascending = [entryWithData, entryNoData];
updateSortFrequenciesHarmonic(ascending, true);
expect(entryNoData.frequencyOrder).toStrictEqual(Number.MAX_SAFE_INTEGER);
expect(entryWithData.frequencyOrder).toStrictEqual(5);
updateSortFrequenciesHarmonic([entryWithData, entryNoData], false);
expect(entryNoData.frequencyOrder).toStrictEqual(0);
expect(entryWithData.frequencyOrder).toStrictEqual(-5);
});
});