nigel / wit_parser
A custom Wit.ai parser for Laravel
1.0.3
2025-04-14 10:00 UTC
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.9
Suggests
- laravel/framework: Required to use with Laravel (^9.0|^10.0|^11.0)
README
A Laravel package for interacting with Wit.ai's API, providing easy-to-use services for message parsing and entity/intent management.
Installation
- Install the package via Composer:
composer require nigel/wit_parser
- Publish the configuration file:
php artisan vendor:publish --provider="Nigel\WitParser\WitParserServiceProvider" --tag="config"
- Add your Wit.ai credentials to your
.env
file:
WIT_AI_TOKEN=your_wit_ai_token_here
WIT_AI_BASE_URL=https://api.wit.ai/
Usage
Message Parsing
use Nigel\WitParser\WitParserService; class YourController extends Controller { public function parseMessage() { $parser = new WitParserService(); $result = $parser->parse("What's the weather in New York?"); // Access parsed data $intent = $result->intent; // e.g., "get_weather" $confidence = $result->confidence; // e.g., 0.95 $entities = $result->entities; // Array of entities $raw = $result->raw; // Raw API response } }
Managing Entities and Intents
use Nigel\WitParser\WitManagerService; class YourController extends Controller { public function manageWit() { $manager = new WitManagerService(); // Create a new entity $entity = $manager->createEntity('location', [ ['value' => 'New York'], ['value' => 'London'] ]); // Get all entities $entities = $manager->getEntities(); // Create a new intent $intent = $manager->createIntent('get_weather', [ ['text' => 'What\'s the weather in New York?'], ['text' => 'How\'s the weather in London?'] ]); // Get all intents $intents = $manager->getIntents(); // Get app info $appInfo = $manager->getAppInfo(); } }
Available Methods
WitParserService
parse(string $message): WitResult
- Parse a message and get structured data
WitManagerService
createEntity(string $name, array $values = []): array
- Create a new entitygetEntities(): array
- Get all entitiescreateIntent(string $name, array $examples = []): array
- Create a new intentgetIntents(): array
- Get all intentsgetAppInfo(): array
- Get app information
WitResult Object
The parse()
method returns a WitResult
object with the following properties:
intent
: The detected intentconfidence
: Confidence score (0-1)entities
: Array of detected entitiesraw
: Raw API response
Error Handling
The package throws RuntimeException
with descriptive messages for:
- API request failures
- Invalid responses
- Network errors
- Authentication issues
License
This package is open-sourced software licensed under the MIT license.