Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add frequency number handlebars #600

Merged
merged 14 commits into from
Feb 3, 2024
32 changes: 32 additions & 0 deletions ext/data/templates/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,38 @@
{{~/if~}}
{{/inline}}

{{#*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 "stroke-count"}}
{{~#scope~}}
{{~set "found" false~}}
Expand Down
67 changes: 67 additions & 0 deletions ext/js/data/sandbox/anki-note-data-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,65 @@ function getPublicContext(context) {
};
}

/**
* @param {import('dictionary').TermDictionaryEntry|import('dictionary').KanjiDictionaryEntry} dictionaryEntry
* @returns {number[]}
*/
function getFrequencyNumbers(dictionaryEntry) {
let previousDictionary;
const frequencies = [];
for (const {dictionary, frequency, displayValue} of dictionaryEntry.frequencies) {
if (dictionary === previousDictionary) {
continue;
}
previousDictionary = dictionary;

const frequencyWorking = displayValue !== null ? displayValue.match(/\d+/) : frequency;
Kuuuube marked this conversation as resolved.
Show resolved Hide resolved
if (frequencyWorking !== null) {
frequencies.push(Number(frequencyWorking));
} else {
frequencies.push(frequency);
}
}
return frequencies;
}

/**
* @param {import('dictionary').TermDictionaryEntry|import('dictionary').KanjiDictionaryEntry} dictionaryEntry
* @returns {number}
*/
function getFrequencyHarmonic(dictionaryEntry) {
const frequencies = getFrequencyNumbers(dictionaryEntry);

if (frequencies.length < 1) {
Kuuuube marked this conversation as resolved.
Show resolved Hide resolved
return -1;
}

let total = 0;
for (const frequency of frequencies) {
total += 1 / frequency;
}
return Math.floor(frequencies.length / total);
}

/**
* @param {import('dictionary').TermDictionaryEntry|import('dictionary').KanjiDictionaryEntry} dictionaryEntry
* @returns {number}
*/
function getFrequencyAverage(dictionaryEntry) {
const frequencies = getFrequencyNumbers(dictionaryEntry);

if (frequencies.length < 1) {
return -1;
}

let total = 0;
for (const frequency of frequencies) {
total += frequency;
}
return Math.floor(total / frequencies.length);
}

/**
* @param {import('dictionary').DictionaryEntry} dictionaryEntry
* @returns {import('anki-templates').PitchGroup[]}
Expand Down Expand Up @@ -272,6 +331,8 @@ function getKanjiDefinition(dictionaryEntry, context) {
const stats = createCachedValue(getKanjiStats.bind(null, dictionaryEntry));
const tags = createCachedValue(convertTags.bind(null, dictionaryEntry.tags));
const frequencies = createCachedValue(getKanjiFrequencies.bind(null, dictionaryEntry));
const frequencyHarmonic = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry));
const frequencyAverage = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry));
const cloze = createCachedValue(getCloze.bind(null, dictionaryEntry, context));

return {
Expand All @@ -284,6 +345,8 @@ function getKanjiDefinition(dictionaryEntry, context) {
get tags() { return getCachedValue(tags); },
get stats() { return getCachedValue(stats); },
get frequencies() { return getCachedValue(frequencies); },
get frequencyHarmonic() { return getCachedValue(frequencyHarmonic); },
get frequencyAverage() { return getCachedValue(frequencyAverage); },
url,
get cloze() { return getCachedValue(cloze); }
};
Expand Down Expand Up @@ -366,6 +429,8 @@ function getTermDefinition(dictionaryEntry, context, resultOutputMode) {
const termTags = createCachedValue(getTermTags.bind(null, dictionaryEntry, type));
const expressions = createCachedValue(getTermExpressions.bind(null, dictionaryEntry));
const frequencies = createCachedValue(getTermFrequencies.bind(null, dictionaryEntry));
const frequencyHarmonic = createCachedValue(getFrequencyHarmonic.bind(null, dictionaryEntry));
const frequencyAverage = createCachedValue(getFrequencyAverage.bind(null, dictionaryEntry));
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 @@ -403,6 +468,8 @@ function getTermDefinition(dictionaryEntry, context, resultOutputMode) {
get termTags() { return getCachedValue(termTags); },
get definitions() { return getCachedValue(commonInfo).definitions; },
get frequencies() { return getCachedValue(frequencies); },
get frequencyHarmonic() { return getCachedValue(frequencyHarmonic); },
get frequencyAverage() { return getCachedValue(frequencyAverage); },
get pitches() { return getCachedValue(pitches); },
get phoneticTranscriptions() { return getCachedValue(phoneticTranscriptions); },
sourceTermExactMatchCount,
Expand Down
9 changes: 9 additions & 0 deletions ext/js/pages/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export class AnkiController {
'document-title',
'expression',
'frequencies',
'frequency-harmonic-rank',
'frequency-harmonic-occurrence',
'frequency-average-rank',
'frequency-average-occurrence',
'furigana',
'furigana-plain',
'glossary',
Expand Down Expand Up @@ -169,6 +173,11 @@ export class AnkiController {
'cloze-suffix',
'dictionary',
'document-title',
'frequencies',
'frequency-harmonic-rank',
'frequency-harmonic-occurrence',
'frequency-average-rank',
'frequency-average-occurrence',
'glossary',
'kunyomi',
'onyomi',
Expand Down
Loading
Loading