Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions ext/data/templates/anki-field-templates-upgrade-v75.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{{<<<<<<<}}
{{#*inline "frequency-harmonic-rank"}}
{{~#if (op "===" definition.frequencyHarmonic -1) ~}}
9999999
{{~else ~}}
{{definition.frequencyHarmonic}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-harmonic-occurrence"}}
{{~#if (op "===" definition.frequencyHarmonic -1) ~}}
0
{{~else ~}}
{{definition.frequencyHarmonic}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-average-rank"}}
{{~#if (op "===" definition.frequencyAverage -1) ~}}
9999999
{{~else ~}}
{{definition.frequencyAverage}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-average-occurrence"}}
{{~#if (op "===" definition.frequencyAverage -1) ~}}
0
{{~else ~}}
{{definition.frequencyAverage}}
{{~/if~}}
{{/inline}}
{{=======}}
{{#*inline "frequency-harmonic-rank"}}
{{~#if (op "===" definition.frequencyHarmonicRank -1) ~}}
9999999
{{~else ~}}
{{definition.frequencyHarmonicRank}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-harmonic-occurrence"}}
{{~#if (op "===" definition.frequencyHarmonicOccurrence -1) ~}}
0
{{~else ~}}
{{definition.frequencyHarmonicOccurrence}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-average-rank"}}
{{~#if (op "===" definition.frequencyAverageRank -1) ~}}
9999999
{{~else ~}}
{{definition.frequencyAverageRank}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-average-occurrence"}}
{{~#if (op "===" definition.frequencyAverageOccurrence -1) ~}}
0
{{~else ~}}
{{definition.frequencyAverageOccurrence}}
{{~/if~}}
{{/inline}}
{{>>>>>>>}}
16 changes: 8 additions & 8 deletions ext/data/templates/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -382,34 +382,34 @@
{{/inline}}

{{#*inline "frequency-harmonic-rank"}}
{{~#if (op "===" definition.frequencyHarmonic -1) ~}}
{{~#if (op "===" definition.frequencyHarmonicRank -1) ~}}
9999999
{{~else ~}}
{{definition.frequencyHarmonic}}
{{definition.frequencyHarmonicRank}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-harmonic-occurrence"}}
{{~#if (op "===" definition.frequencyHarmonic -1) ~}}
{{~#if (op "===" definition.frequencyHarmonicOccurrence -1) ~}}
0
{{~else ~}}
{{definition.frequencyHarmonic}}
{{definition.frequencyHarmonicOccurrence}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-average-rank"}}
{{~#if (op "===" definition.frequencyAverage -1) ~}}
{{~#if (op "===" definition.frequencyAverageRank -1) ~}}
9999999
{{~else ~}}
{{definition.frequencyAverage}}
{{definition.frequencyAverageRank}}
{{~/if~}}
{{/inline}}

{{#*inline "frequency-average-occurrence"}}
{{~#if (op "===" definition.frequencyAverage -1) ~}}
{{~#if (op "===" definition.frequencyAverageOccurrence -1) ~}}
0
{{~else ~}}
{{definition.frequencyAverage}}
{{definition.frequencyAverageOccurrence}}
{{~/if~}}
{{/inline}}

Expand Down
40 changes: 33 additions & 7 deletions ext/js/data/anki-note-data-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,26 @@ function getPublicContext(context) {
};
}


/**
* @param {import('dictionary').TermDictionaryEntry|import('dictionary').KanjiDictionaryEntry} dictionaryEntry
* @param {number?} requestedHeadwordIndex
* @param {import('dictionary-data').FrequencyMode|undefined} [requestedFrequencyMode]
* @returns {import('anki-templates').FrequencyNumber[]}
*/
function getFrequencyNumbers(dictionaryEntry, requestedHeadwordIndex) {
function getFrequencyNumbers(dictionaryEntry, requestedHeadwordIndex, requestedFrequencyMode) {
let previousDictionary;
const frequencies = [];
for (const dictionaryEntryFrequency of dictionaryEntry.frequencies) {
const {dictionary, frequency, displayValue} = dictionaryEntryFrequency;
const {dictionary, frequency, displayValue, frequencyMode} = dictionaryEntryFrequency;
const wrongHeadwordIndex = Number.isInteger(requestedHeadwordIndex) && ('headwordIndex' in dictionaryEntryFrequency) && dictionaryEntryFrequency.headwordIndex !== requestedHeadwordIndex;
if (dictionary === previousDictionary || wrongHeadwordIndex) {
const wrongFrequencyMode = (
typeof requestedFrequencyMode !== 'undefined' &&
frequencyMode !== null &&
typeof frequencyMode !== 'undefined' &&
frequencyMode !== requestedFrequencyMode
);
if (dictionary === previousDictionary || wrongHeadwordIndex || wrongFrequencyMode) {
continue;
}
previousDictionary = dictionary;
Expand All @@ -205,10 +213,11 @@ function getFrequencyNumbers(dictionaryEntry, requestedHeadwordIndex) {
/**
* @param {import('dictionary').TermDictionaryEntry|import('dictionary').KanjiDictionaryEntry} dictionaryEntry
* @param {number?} headwordIndex
* @param {import('dictionary-data').FrequencyMode|undefined} [frequencyMode]
* @returns {number}
*/
export function getFrequencyHarmonic(dictionaryEntry, headwordIndex) {
const frequencies = getFrequencyNumbers(dictionaryEntry, headwordIndex);
export function getFrequencyHarmonic(dictionaryEntry, headwordIndex, frequencyMode) {
const frequencies = getFrequencyNumbers(dictionaryEntry, headwordIndex, frequencyMode);

if (frequencies.length === 0) {
return -1;
Expand All @@ -224,10 +233,11 @@ export function getFrequencyHarmonic(dictionaryEntry, headwordIndex) {
/**
* @param {import('dictionary').TermDictionaryEntry|import('dictionary').KanjiDictionaryEntry} dictionaryEntry
* @param {number?} headwordIndex
* @param {import('dictionary-data').FrequencyMode|undefined} [frequencyMode]
* @returns {number}
*/
function getFrequencyAverage(dictionaryEntry, headwordIndex) {
const frequencies = getFrequencyNumbers(dictionaryEntry, headwordIndex);
function getFrequencyAverage(dictionaryEntry, headwordIndex, frequencyMode) {
const frequencies = getFrequencyNumbers(dictionaryEntry, headwordIndex, frequencyMode);

if (frequencies.length === 0) {
return -1;
Expand Down Expand Up @@ -345,7 +355,11 @@ function getKanjiDefinition(dictionaryEntry, context) {
const tags = createCachedValue(convertTags.bind(null, dictionaryEntry.tags));
const frequencies = createCachedValue(getKanjiFrequencies.bind(null, dictionaryEntry));
const frequencyHarmonic = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry, null));
const frequencyHarmonicRank = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry, null, 'rank-based'));
const frequencyHarmonicOccurrence = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry, null, 'occurrence-based'));
const frequencyAverage = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry, null));
const frequencyAverageRank = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry, null, 'rank-based'));
const frequencyAverageOccurrence = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry, null, 'occurrence-based'));
const cloze = createCachedValue(getCloze.bind(null, dictionaryEntry, context));

return {
Expand All @@ -360,7 +374,11 @@ function getKanjiDefinition(dictionaryEntry, context) {
get stats() { return getCachedValue(stats); },
get frequencies() { return getCachedValue(frequencies); },
get frequencyHarmonic() { return getCachedValue(frequencyHarmonic); },
get frequencyHarmonicRank() { return getCachedValue(frequencyHarmonicRank); },
get frequencyHarmonicOccurrence() { return getCachedValue(frequencyHarmonicOccurrence); },
get frequencyAverage() { return getCachedValue(frequencyAverage); },
get frequencyAverageRank() { return getCachedValue(frequencyAverageRank); },
get frequencyAverageOccurrence() { return getCachedValue(frequencyAverageOccurrence); },
url,
get cloze() { return getCachedValue(cloze); },
};
Expand Down Expand Up @@ -449,7 +467,11 @@ function getTermDefinition(dictionaryEntry, context, resultOutputMode, dictionar
const frequencies = createCachedValue(getTermFrequencies.bind(null, dictionaryEntry));
const frequencyNumbers = createCachedValue(getFrequencyNumbers.bind(null, dictionaryEntry, null));
const frequencyHarmonic = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry, null));
const frequencyHarmonicRank = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry, null, 'rank-based'));
const frequencyHarmonicOccurrence = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry, null, 'occurrence-based'));
const frequencyAverage = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry, null));
const frequencyAverageRank = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry, null, 'rank-based'));
const frequencyAverageOccurrence = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry, null, 'occurrence-based'));
const pitches = createCachedValue(getTermPitches.bind(null, dictionaryEntry));
const phoneticTranscriptions = createCachedValue(getTermPhoneticTranscriptions.bind(null, dictionaryEntry));
const glossary = createCachedValue(getTermGlossaryArray.bind(null, dictionaryEntry, type));
Expand Down Expand Up @@ -492,7 +514,11 @@ function getTermDefinition(dictionaryEntry, context, resultOutputMode, dictionar
get frequencies() { return getCachedValue(frequencies); },
get frequencyNumbers() { return getCachedValue(frequencyNumbers); },
get frequencyHarmonic() { return getCachedValue(frequencyHarmonic); },
get frequencyHarmonicRank() { return getCachedValue(frequencyHarmonicRank); },
get frequencyHarmonicOccurrence() { return getCachedValue(frequencyHarmonicOccurrence); },
get frequencyAverage() { return getCachedValue(frequencyAverage); },
get frequencyAverageRank() { return getCachedValue(frequencyAverageRank); },
get frequencyAverageOccurrence() { return getCachedValue(frequencyAverageOccurrence); },
get pitches() { return getCachedValue(pitches); },
get phoneticTranscriptions() { return getCachedValue(phoneticTranscriptions); },
sourceTermExactMatchCount,
Expand Down
9 changes: 9 additions & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ export class OptionsUtil {
this._updateVersion72,
this._updateVersion73,
this._updateVersion74,
this._updateVersion75,
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1831,6 +1832,14 @@ export class OptionsUtil {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v74.handlebars');
}

/**
* - Split rank-based and occurrence-based frequency field templates.
* @type {import('options-util').UpdateFunction}
*/
async _updateVersion75(options) {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v75.handlebars');
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
13 changes: 9 additions & 4 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export class Translator {
this._textProcessors = new Map();
/** @type {import('translation-internal').ReadingNormalizerMap} */
this._readingNormalizers = new Map();

/** @type {?Map<string, (import('dictionary-importer').Summary['frequencyMode'])>} */
this._dictionaryFrequencyModeMap = null;
/** @type {?Promise<Map<string, (import('dictionary-importer').Summary['frequencyMode'])>>} */
Expand Down Expand Up @@ -203,6 +202,7 @@ export class Translator {

const termList = termReadingList.map(({term}) => term);
const metas = await this._database.findTermMetaBulk(termList, dictionarySet);
const dictionaryFrequencyModeMap = await this._getDictionaryFrequencyModeMap();

/** @type {import('translator').TermFrequencySimple[]} */
const results = [];
Expand All @@ -224,6 +224,7 @@ export class Translator {
frequency: frequencyValue,
displayValue,
displayValueParsed,
frequencyMode: dictionaryFrequencyModeMap.get(dictionary),
});
}
return results;
Expand Down Expand Up @@ -1265,7 +1266,6 @@ export class Translator {
for (const {mode, data, dictionary, index} of metas) {
const {index: dictionaryIndex} = this._getDictionaryOrder(dictionary, enabledDictionaryMap);
const dictionaryAlias = this._getDictionaryAlias(dictionary, enabledDictionaryMap);
const frequencyMode = dictionaryFrequencyModeMap.get(dictionary);
const map2 = headwordReadingMaps[index];
for (const [reading, targets] of map2.entries()) {
switch (mode) {
Expand All @@ -1274,6 +1274,7 @@ export class Translator {
const hasReading = (data !== null && typeof data === 'object' && typeof data.reading === 'string');
if (hasReading && data.reading !== reading) { continue; }
const frequency = hasReading ? data.frequency : /** @type {import('dictionary-data').GenericFrequencyData} */ (data);
const frequencyMode = dictionaryFrequencyModeMap.get(dictionary);
for (const {frequencies, headwordIndex} of targets) {
const {frequency: frequencyValue, displayValue, displayValueParsed} = this._getFrequencyInfo(frequency);
frequencies.push(this._createTermFrequency(
Expand Down Expand Up @@ -1368,13 +1369,15 @@ export class Translator {
}

const metas = await this._database.findKanjiMetaBulk(kanjiList, enabledDictionaryMap);
const dictionaryFrequencyModeMap = await this._getDictionaryFrequencyModeMap();
for (const {character, mode, data, dictionary, index} of metas) {
const {index: dictionaryIndex} = this._getDictionaryOrder(dictionary, enabledDictionaryMap);
const dictionaryAlias = this._getDictionaryAlias(dictionary, enabledDictionaryMap);
switch (mode) {
case 'freq':
{
const {frequencies} = dictionaryEntries[index];
const frequencyMode = dictionaryFrequencyModeMap.get(dictionary);
const {frequency, displayValue, displayValueParsed} = this._getFrequencyInfo(data);
frequencies.push(this._createKanjiFrequency(
frequencies.length,
Expand All @@ -1385,6 +1388,7 @@ export class Translator {
frequency,
displayValue,
displayValueParsed,
frequencyMode,
));
}
break;
Expand Down Expand Up @@ -1575,10 +1579,11 @@ export class Translator {
* @param {number} frequency
* @param {?string} displayValue
* @param {boolean} displayValueParsed
* @param {import('dictionary-data').FrequencyMode|undefined} frequencyMode
* @returns {import('dictionary').KanjiFrequency}
*/
_createKanjiFrequency(index, dictionary, dictionaryIndex, dictionaryAlias, character, frequency, displayValue, displayValueParsed) {
return {index, dictionary, dictionaryIndex, dictionaryAlias, character, frequency, displayValue, displayValueParsed};
_createKanjiFrequency(index, dictionary, dictionaryIndex, dictionaryAlias, character, frequency, displayValue, displayValueParsed, frequencyMode) {
return {index, dictionary, dictionaryIndex, dictionaryAlias, character, frequency, displayValue, displayValueParsed, frequencyMode: frequencyMode ?? null};
}

/**
Expand Down
Loading
Loading