Skip to content

Commit

Permalink
ankiclient: refactor magic numbers and for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
precondition committed Aug 29, 2024
1 parent 6c679a8 commit 5ef62da
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/anki/ankiclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1786,7 +1792,7 @@ std::vector<int> AnkiClient::getFrequencyNumbers(
QString previousDictionary;
std::vector<int> frequencyNumbers;

for (const auto& frequencyEntry : frequencies)
for (const Frequency &frequencyEntry : frequencies)
{
if (frequencyEntry.dictionary == previousDictionary
|| frequencyEntry.freq.isNull())
Expand Down Expand Up @@ -1850,7 +1856,7 @@ int AnkiClient::getFrequencyAverage(const QList<Frequency> &frequencies)
return -1;
}

// Sum the elements in the vector
/* Sum the elements in the vector */
double total = std::accumulate(frequencyNumbers.begin(),
frequencyNumbers.end(), 0);

Expand Down

0 comments on commit 5ef62da

Please sign in to comment.