From 5ef62da31e3f97db1859e1e70ea7e0120d1465f1 Mon Sep 17 00:00:00 2001 From: precondition <57645186+precondition@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:18:32 +0200 Subject: [PATCH] ankiclient: refactor magic numbers and for loop --- src/anki/ankiclient.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/anki/ankiclient.cpp b/src/anki/ankiclient.cpp index 3900023..ee21e73 100644 --- a/src/anki/ankiclient.cpp +++ b/src/anki/ankiclient.cpp @@ -1387,17 +1387,23 @@ void AnkiClient::buildCommonNote( value.replace(REPLACE_CLOZE_SUFFIX, clozeSuffix); value.replace(REPLACE_FREQUENCIES, frequencies); + /* If the term never occurs in the corpus of any loaded frequency + * dictionary, assume it is a very rare word. + * (The higher the ranking, the rarer the term) */ + constexpr int default_freq_rank = 9999999; + constexpr int default_freq_occurrence = 0; + value.replace(REPLACE_FREQ_HARMONIC_RANK, - positiveIntToQString(frequencyHarmonic, 9999999)); + positiveIntToQString(frequencyHarmonic, default_freq_rank)); value.replace(REPLACE_FREQ_HARMONIC_OCCU, - positiveIntToQString(frequencyHarmonic, 0)); + positiveIntToQString(frequencyHarmonic, default_freq_occurrence)); value.replace(REPLACE_FREQ_AVERAGE_RANK, - positiveIntToQString(frequencyAverage, 9999999)); + positiveIntToQString(frequencyAverage, default_freq_rank)); value.replace(REPLACE_FREQ_AVERAGE_OCCU, - positiveIntToQString(frequencyAverage, 0)); + positiveIntToQString(frequencyAverage, default_freq_occurrence)); value.replace(REPLACE_SENTENCE, sentence); value.replace(REPLACE_SENTENCE_SEC, sentence2); @@ -1786,7 +1792,7 @@ std::vector AnkiClient::getFrequencyNumbers( QString previousDictionary; std::vector frequencyNumbers; - for (const auto& frequencyEntry : frequencies) + for (const Frequency &frequencyEntry : frequencies) { if (frequencyEntry.dictionary == previousDictionary || frequencyEntry.freq.isNull()) @@ -1850,7 +1856,7 @@ int AnkiClient::getFrequencyAverage(const QList &frequencies) return -1; } - // Sum the elements in the vector + /* Sum the elements in the vector */ double total = std::accumulate(frequencyNumbers.begin(), frequencyNumbers.end(), 0);