chore: General commenting & refactoring

This commit is contained in:
Dendy 2025-05-01 15:50:57 +02:00
parent cfaafb58e1
commit e2c178e998
1 changed files with 27 additions and 28 deletions

View File

@ -6,6 +6,7 @@ use App\Entity\SentenceListeningNote;
use App\Entity\SentenceNote; use App\Entity\SentenceNote;
use App\Entity\Term; use App\Entity\Term;
use App\Service\AnkiService; use App\Service\AnkiService;
use App\Utils\Progress;
use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
@ -88,28 +89,38 @@ class CreateProductionCommand extends Command
} }
} }
protected function execute(InputInterface $input, OutputInterface $output): int /** @return list<SentenceNote> */
private function getAllSentenceNotes(): array
{ {
printf('Getting all SentenceCards...'); printf('Getting all SentenceNote...');
$allIds = $this->ankiService->getAllSentenceNoteIds(); $allIds = $this->ankiService->getAllSentenceNoteIds();
$allNotes = $this->ankiService->getNotes($allIds); $allNotes = $this->ankiService->getNotes($allIds);
printf(" OK (%d)\n", count($allNotes)); printf(" OK (%d)\n", count($allNotes));
return $allNotes;
}
printf('Getting all SentenceCards...'); /** @return list<SentenceListeningNote> */
private function getAllSentenceListeningNotes(): array
{
printf('Getting all SentenceListeningNote...');
$allListeningIds = $this->ankiService->getAllSentenceListeningNoteIds(); $allListeningIds = $this->ankiService->getAllSentenceListeningNoteIds();
$allListeningNotes = $this->ankiService->getNotes($allListeningIds); $allListeningNotes = $this->ankiService->getNotes($allListeningIds);
printf(" OK (%d)\n", count($allListeningNotes)); printf(" OK (%d)\n", count($allListeningNotes));
return $allListeningNotes;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$allSentenceNotes = $this->getAllSentenceNotes();
$allListeningNotes = $this->getAllSentenceListeningNotes();
// --------- Getting list<SentenceNote> into array<TermKanji, Term> ---
printf('Indexing all terms...');
$knownTerms = []; $knownTerms = [];
$knownKanji = []; $knownKanji = [];
$termCounts = []; $termCounts = [];
foreach ($allNotes as $note) { printf('Indexing all terms...');
if (!$note instanceof SentenceNote) throw new \Exception(sprintf( foreach ($allSentenceNotes as $note) {
'Expected SentenceNote, got %s',
$note::class,
));
foreach ($note->getTerms() as &$term) { foreach ($note->getTerms() as &$term) {
assert($term instanceof Term); assert($term instanceof Term);
@ -127,26 +138,14 @@ class CreateProductionCommand extends Command
foreach ($allSentenceNotes as $note) { foreach ($allSentenceNotes as $note) {
$progress->tick(); $progress->tick();
assert($note instanceof SentenceNote);
$sentKanji = str_replace( $sentKanji = str_replace(
"\u{200E}", "\u{200E}",
'', '',
strip_tags($note->getFields()['SentKanji']) strip_tags($note->getFields()['SentKanji'])
); );
//foreach ($knownTerms as &$term) {
// assert($term instanceof Term);
// if (str_contains($sentKanji, $term->getKanji())) {
// $termCounts[$term->getKanji()] += 1;
// }
//}
foreach ($knownKanji as $kanji => &$count) { foreach ($knownKanji as $kanji => &$count) {
if (str_contains($sentKanji, $kanji)) { if (str_contains($sentKanji, $kanji)) $count++;
$count++;
}
} }
} }
printf("\n"); printf("\n");
@ -165,7 +164,7 @@ class CreateProductionCommand extends Command
// First pass: Calculate the weight // First pass: Calculate the weight
foreach ($knownKanji as $kanji => $count) { foreach ($knownKanji as $kanji => $count) {
if (str_contains($termKanji, $kanji)) { if (str_contains($termKanji, $kanji)) {
$termCounts[$term->getKanji()] += ceil($count * $weight); $termCounts[$term->getKanji()] += (int) ceil($count * $weight);
} }
} }
} }
@ -173,8 +172,8 @@ class CreateProductionCommand extends Command
arsort($termCounts); arsort($termCounts);
// Have into account the ones that have already been created. // Have into account the ones that have already been created.
// This will not only skip them but take into account the kanjis they // This will not only skip them but also update the general array for
// have. // already seen kanji.
foreach ($allListeningNotes as $listeningNote) { foreach ($allListeningNotes as $listeningNote) {
assert($listeningNote instanceof SentenceListeningNote); assert($listeningNote instanceof SentenceListeningNote);
@ -185,7 +184,7 @@ class CreateProductionCommand extends Command
foreach ($termCounts as $term => $count) { foreach ($termCounts as $term => $count) {
$termKanji = self::getOnlyKanji($term); $termKanji = self::getOnlyKanji($term);
// Second pass: Penalize terms with no new kanji at all // Second pass: Remove terms with no new kanji at all
if (!self::kanjiDiff($seenKanji, $termKanji)) { if (!self::kanjiDiff($seenKanji, $termKanji)) {
unset($termCounts[$term]); unset($termCounts[$term]);
//unset($knownTerms[$term->getKanji()]); //unset($knownTerms[$term->getKanji()]);
@ -211,7 +210,7 @@ class CreateProductionCommand extends Command
printf(" - %s: %0.2f\n", $iKanji, $knownKanji[$iKanji] / $len); printf(" - %s: %0.2f\n", $iKanji, $knownKanji[$iKanji] / $len);
} }
$this->createProductionNoteFromTerm($knownTerms[$term]); //$this->createProductionNoteFromTerm($knownTerms[$term]);
$newNotesCount -= 1; $newNotesCount -= 1;
}; };