summaryrefslogtreecommitdiff
path: root/ext/js/data/sandbox
diff options
context:
space:
mode:
authorKuuuube <61125188+Kuuuube@users.noreply.github.com>2024-02-02 21:53:42 -0500
committerGitHub <noreply@github.com>2024-02-03 02:53:42 +0000
commit5a2bc4e542b7f22d1e6a4ba318cfcdc33817d34a (patch)
tree81a112708329ca8b2866ed4d05cf7203252854d6 /ext/js/data/sandbox
parent6dc7e90f8d5679b0de298c820a5b88b93590ed31 (diff)
Add frequency number handlebars (#600)
* Add frequencies-num handlebar * Fix handlebar name * Fix inconsistent variable name format * Add frequency-number to anki tests * Use full names for total and frequency variables * Migrate handlebars to JS, split rank and occurance, add average * Remove left behind frequency-number * Fix "occurrence" spelling * Cleanup types * Add frequency support for kanji cards * Fix code style * Clean up most of duplicate code and merge functions * Fix frequencies.length check * Use less witchcraft and remove redundant type casting
Diffstat (limited to 'ext/js/data/sandbox')
-rw-r--r--ext/js/data/sandbox/anki-note-data-creator.js69
1 files changed, 69 insertions, 0 deletions
diff --git a/ext/js/data/sandbox/anki-note-data-creator.js b/ext/js/data/sandbox/anki-note-data-creator.js
index 51679662..79570532 100644
--- a/ext/js/data/sandbox/anki-note-data-creator.js
+++ b/ext/js/data/sandbox/anki-note-data-creator.js
@@ -171,6 +171,67 @@ 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;
+
+ if (displayValue !== null) {
+ const frequencyMatch = displayValue.match(/\d+/);
+ if (frequencyMatch !== null) {
+ frequencies.push(Number.parseInt(frequencyMatch[0], 10));
+ continue;
+ }
+ }
+ 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 === 0) {
+ 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 === 0) {
+ 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[]}
*/
@@ -272,6 +333,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 {
@@ -284,6 +347,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); }
};
@@ -366,6 +431,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));
@@ -403,6 +470,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,