diff --git a/app/Console/Commands/CreateLuisData.php b/app/Console/Commands/CreateLuisData.php new file mode 100644 index 000000000000..9e4e6a5117cc --- /dev/null +++ b/app/Console/Commands/CreateLuisData.php @@ -0,0 +1,221 @@ +faker = Factory::create(); + } + + /** + * @return bool + */ + public function fire() + { + $intents = []; + $entityTypes = [ + ENTITY_INVOICE, + ENTITY_QUOTE, + ENTITY_CLIENT, + ENTITY_CREDIT, + ENTITY_EXPENSE, + ENTITY_PAYMENT, + ENTITY_PRODUCT, + ENTITY_RECURRING_INVOICE, + ENTITY_TASK, + ENTITY_VENDOR, + ]; + + foreach ($entityTypes as $entityType) { + $intents = array_merge($intents, $this->createIntents($entityType)); + } + + $intents = array_merge($intents, $this->getNavigateToIntents($entityType)); + + $this->info(json_encode($intents)); + } + + private function createIntents($entityType) + { + $intents = []; + + $intents = array_merge($intents, $this->getCreateEntityIntents($entityType)); + $intents = array_merge($intents, $this->getFindEntityIntents($entityType)); + $intents = array_merge($intents, $this->getListEntityIntents($entityType)); + + return $intents; + } + + private function getCreateEntityIntents($entityType) + { + $intents = []; + $phrases = [ + "create new {$entityType}", + "new {$entityType}", + "make a {$entityType}", + ]; + + foreach ($phrases as $phrase) { + $intents[] = $this->createIntent('CreateEntity', $phrase, [ + $entityType => 'EntityType', + ]); + if ($entityType != ENTITY_CLIENT) { + $client = $this->faker->firstName; + $phrase .= " for {$client}"; + $intents[] = $this->createIntent('CreateEntity', $phrase, [ + $entityType => 'EntityType', + $client => 'Name', + ]); + } + } + + return $intents; + } + + private function getFindEntityIntents($entityType) + { + $intents = []; + + if (in_array($entityType, [ENTITY_CLIENT, ENTITY_INVOICE, ENTITY_QUOTE])) { + $name = $entityType === ENTITY_CLIENT ? $this->faker->firstName : $this->faker->randomNumber(4); + $intents[] = $this->createIntent('FindEntity', "find {$entityType} {$name}", [ + $entityType => 'EntityType', + $name => 'Name', + ]); + if ($entityType === ENTITY_CLIENT) { + $name = $this->faker->firstName; + $intents[] = $this->createIntent('FindEntity', "find {$name}", [ + $name => 'Name', + ]); + } + } + + return $intents; + } + + private function getListEntityIntents($entityType) + { + $intents = []; + $entityTypePlural = Utils::pluralizeEntityType($entityType); + + $intents[] = $this->createIntent('ListEntity', "show me {$entityTypePlural}", [ + $entityTypePlural => 'EntityType', + ]); + $intents[] = $this->createIntent('ListEntity', "list {$entityTypePlural}", [ + $entityTypePlural => 'EntityType', + ]); + + $intents[] = $this->createIntent('ListEntity', "show me active {$entityTypePlural}", [ + $entityTypePlural => 'EntityType', + 'active' => 'Filter', + ]); + $intents[] = $this->createIntent('ListEntity', "list archived and deleted {$entityTypePlural}", [ + $entityTypePlural => 'EntityType', + 'archived' => 'Filter', + 'deleted' => 'Filter', + ]); + + if ($entityType != ENTITY_CLIENT) { + $client = $this->faker->firstName; + $intents[] = $this->createIntent('ListEntity', "list {$entityTypePlural} for {$client}", [ + $entityTypePlural => 'EntityType', + $client => 'Name', + ]); + $intents[] = $this->createIntent('ListEntity', "show me {$client}'s {$entityTypePlural}", [ + $entityTypePlural => 'EntityType', + $client . '\'s' => 'Name', + ]); + $intents[] = $this->createIntent('ListEntity', "show me {$client}'s active {$entityTypePlural}", [ + $entityTypePlural => 'EntityType', + $client . '\'s' => 'Name', + 'active' => 'Filter', + ]); + } + + return $intents; + } + + private function getNavigateToIntents($entityType) + { + $intents = []; + $locations = array_merge(Account::$basicSettings, Account::$advancedSettings); + + foreach ($locations as $location) { + $location = str_replace('_', ' ', $location); + $intents[] = $this->createIntent('NavigateTo', "go to {$location}", [ + $location => 'Location', + ]); + $intents[] = $this->createIntent('NavigateTo', "show me {$location}", [ + $location => 'Location', + ]); + } + + return $intents; + } + + private function createIntent($name, $text, $entities) + { + $intent = new stdClass(); + $intent->intent = $name; + $intent->text = $text; + $intent->entities = []; + + foreach ($entities as $value => $entity) { + $startPos = strpos($text, (string)$value); + if (! $startPos) { + dd("Failed to find {$value} in {$text}"); + } + $entityClass = new stdClass(); + $entityClass->entity = $entity; + $entityClass->startPos = $startPos; + $entityClass->endPos = $entityClass->startPos + strlen($value) - 1; + $intent->entities[] = $entityClass; + } + + return $intent; + } + + + /** + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 1606e5eec5ec..3b788a7e330e 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -18,7 +18,6 @@ use Utils; */ class CreateTestData extends Command { - //protected $name = 'ninja:create-test-data'; /** * @var string */ diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 14b26d80f515..34938ea4c590 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -20,6 +20,7 @@ class Kernel extends ConsoleKernel 'App\Console\Commands\CheckData', 'App\Console\Commands\PruneData', 'App\Console\Commands\CreateTestData', + 'App\Console\Commands\CreateLuisData', 'App\Console\Commands\SendRenewalInvoices', 'App\Console\Commands\ChargeRenewalInvoices', 'App\Console\Commands\SendReminders',