Skip to content

Commit

Permalink
added option for average frequency, hides other tags when active
Browse files Browse the repository at this point in the history
  • Loading branch information
tadorituki committed Oct 13, 2024
1 parent d166f3c commit d9618c6
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 10 deletions.
9 changes: 9 additions & 0 deletions ext/css/display.css
Original file line number Diff line number Diff line change
Expand Up @@ -1934,6 +1934,15 @@ button.footer-notification-close-button {
:root[data-anki-enabled=false] .action-button[data-action=save-note] {
display: none;
}

:root[data-average-frequency=false] .frequency-group-item:last-child {
display: none;
}

:root[data-average-frequency=true] .frequency-group-item:not(:last-child){
display: none;
}

:root[data-audio-enabled=false] .action-button[data-action=play-audio] {
display: none;
}
Expand Down
5 changes: 5 additions & 0 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"showGuide",
"enableContextMenuScanSelected",
"compactTags",
"averageFrequency",
"glossaryLayoutMode",
"mainDictionary",
"popupTheme",
Expand Down Expand Up @@ -236,6 +237,10 @@
"type": "boolean",
"default": false
},
"averageFrequency": {
"type": "boolean",
"default": false
},
"glossaryLayoutMode": {
"type": "string",
"enum": ["default", "compact"],
Expand Down
24 changes: 14 additions & 10 deletions ext/js/dictionary/dictionary-data-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,39 @@ export function groupTermFrequencies(dictionaryEntry) {
}

const results = [];

/** @type {import('dictionary').AverageFrequencyListTerm} */
const averages = {};
for (const [dictionary, map2] of map1.entries()) {
const frequencies = [];
const dictionaryAlias = aliasMap.get(dictionary) ?? dictionary;
for (const {term, reading, values} of map2.values()) {
for (let {term, reading, values} of map2.values()) {
frequencies.push({
term,
reading,
values: [...values.values()],
});
const valuesArray = [...values.values()];
let { currentAvg, count} = averages[term]?.[reading] ?? {currentAvg:1, count:0};
// console.log(valuesArray);
if (reading === null) { reading = ''; }
let {currentAvg, count} = averages[term]?.[reading] ?? {currentAvg: 1, count: 0};
if (valuesArray[0].frequency === null) { continue; }

currentAvg = (count/(currentAvg))+(1/(valuesArray[0].frequency))
currentAvg = (count +1)/currentAvg
count += 1
currentAvg = (count / (currentAvg)) + (1 / (valuesArray[0].frequency));
currentAvg = (count + 1) / currentAvg;
count += 1;

averages[term] = {
...averages[term],
[reading]: {
currentAvg, count
}
}
currentAvg, count,
},
};
}
results.push({dictionary, frequencies, dictionaryAlias});
}
const avgFrequencies = Object.keys(averages).flatMap(termName => Object.keys(averages[termName]).map(readingName => ({term: termName, reading: readingName, values: [{frequency: Math.round(averages[termName][readingName].currentAvg), displayValue: Math.round(averages[termName][readingName].currentAvg).toString()}]})))
results.push({dictionary:'Average', frequencies: avgFrequencies, dictionaryAlias:'Average'})
const avgFrequencies = Object.keys(averages).flatMap((termName) => Object.keys(averages[termName]).map((readingName) => ({term: termName, reading: readingName, values: [{frequency: Math.round(averages[termName][readingName].currentAvg), displayValue: Math.round(averages[termName][readingName].currentAvg).toString()}]})));
results.push({dictionary: 'Average', frequencies: avgFrequencies, dictionaryAlias: 'Average'});

return results;
}
Expand Down
1 change: 1 addition & 0 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ export class Display extends EventDispatcher {
data.resultOutputMode = `${options.general.resultOutputMode}`;
data.glossaryLayoutMode = `${options.general.glossaryLayoutMode}`;
data.compactTags = `${options.general.compactTags}`;
data.averageFrequency = `${options.general.averageFrequency}`;
data.frequencyDisplayMode = `${options.general.frequencyDisplayMode}`;
data.termDisplayMode = `${options.general.termDisplayMode}`;
data.enableSearchTags = `${options.scanning.enableSearchTags}`;
Expand Down
9 changes: 9 additions & 0 deletions ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,15 @@ <h1>Yomitan Settings</h1>
</div></div>
</div>
</div>
<div class="settings-item"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Average frequencies</div>
<div class="settings-item-description">Compress frequency tags into one "Average" freqeuncy tag.</div>
</div>
<div class="settings-item-right">
<label class="toggle"><input type="checkbox" data-setting="general.averageFrequency"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
</div>
</div></div>
</div>

<!-- Popup Position & Size -->
Expand Down
24 changes: 24 additions & 0 deletions types/ext/dictionary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,27 @@ export type TermSource = {
*/
isPrimary: boolean;
};

/**
* Dictionaries containing the harmonic mean frequency of specific term-reading pairs.
*/
export type AverageFrequencyListTerm = {
/**
* Contains the average frequency of a term, with all its readings.
*/
[term: string]: {
/**
* Contains the average frequency of a reading.
*/
[reading: string]: {
/**
* The current average frequency.
*/
currentAvg: number;
/**
* The number of dictionary frequencies used to compute the average.
*/
count: number;
};
};
};
1 change: 1 addition & 0 deletions types/ext/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export type GeneralOptions = {
showGuide: boolean;
enableContextMenuScanSelected: boolean;
compactTags: boolean;
averageFrequency: boolean;
glossaryLayoutMode: GlossaryLayoutMode;
mainDictionary: string;
popupTheme: PopupTheme;
Expand Down

0 comments on commit d9618c6

Please sign in to comment.