From d841d6b62129e57702615128981467e79a9ef8de Mon Sep 17 00:00:00 2001 From: Kuuuube <61125188+Kuuuube@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:41:30 -0500 Subject: [PATCH] Set up getFrequencyOrder for other languages (#1616) --- .../sort-frequency-dictionary-controller.js | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/ext/js/pages/settings/sort-frequency-dictionary-controller.js b/ext/js/pages/settings/sort-frequency-dictionary-controller.js index e6f55f201e..b5ef018094 100644 --- a/ext/js/pages/settings/sort-frequency-dictionary-controller.js +++ b/ext/js/pages/settings/sort-frequency-dictionary-controller.js @@ -153,9 +153,35 @@ export class SortFrequencyDictionaryController { * @returns {Promise} */ async _getFrequencyOrder(dictionary) { - const moreCommonTerms = ['来る', '言う', '出る', '入る', '方', '男', '女', '今', '何', '時']; - const lessCommonTerms = ['行なう', '論じる', '過す', '行方', '人口', '猫', '犬', '滝', '理', '暁']; - const terms = [...moreCommonTerms, ...lessCommonTerms]; + const dictionaryInfo = await this._settingsController.application.api.getDictionaryInfo(); + const dictionaryLang = dictionaryInfo.find(({title}) => title === dictionary)?.sourceLanguage ?? ''; + + /** @type {Record} */ + const moreCommonTerms = { + ja: ['来る', '言う', '出る', '入る', '方', '男', '女', '今', '何', '時'], + }; + /** @type {Record} */ + const lessCommonTerms = { + ja: ['行なう', '論じる', '過す', '行方', '人口', '猫', '犬', '滝', '理', '暁'], + }; + let langMoreCommonTerms = moreCommonTerms[dictionaryLang]; + let langLessCommonTerms = lessCommonTerms[dictionaryLang]; + if (dictionaryLang === '') { + langMoreCommonTerms = []; + for (const key in moreCommonTerms) { + if (Object.hasOwn(moreCommonTerms, key)) { + langMoreCommonTerms.push(...moreCommonTerms[key]); + } + } + langLessCommonTerms = []; + for (const key in lessCommonTerms) { + if (Object.hasOwn(lessCommonTerms, key)) { + langLessCommonTerms.push(...lessCommonTerms[key]); + } + } + } + + const terms = [...langMoreCommonTerms, ...langLessCommonTerms]; const frequencies = await this._settingsController.application.api.getTermFrequencies( terms.map((term) => ({term, reading: null})), @@ -166,12 +192,12 @@ export class SortFrequencyDictionaryController { const termDetails = new Map(); const moreCommonTermDetails = []; const lessCommonTermDetails = []; - for (const term of moreCommonTerms) { + for (const term of langMoreCommonTerms) { const details = {hasValue: false, minValue: Number.MAX_SAFE_INTEGER, maxValue: Number.MIN_SAFE_INTEGER}; termDetails.set(term, details); moreCommonTermDetails.push(details); } - for (const term of lessCommonTerms) { + for (const term of langLessCommonTerms) { const details = {hasValue: false, minValue: Number.MAX_SAFE_INTEGER, maxValue: Number.MIN_SAFE_INTEGER}; termDetails.set(term, details); lessCommonTermDetails.push(details);