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