Installation:
composer require djunehor/laravel-grammar
Publish the config (if needed):
php artisan vendor:publish --provider="Djunehor\Grammar\GrammarServiceProvider"
First Use Case: Detect the part of speech for a single word:
use Djunehor\Grammar\Facades\Grammar;
$pos = Grammar::detect('running');
// Returns: ['VERB', 'NOUN', 'ADJECTIVE', 'PRESENT_PARTICIPLE']
Where to Look First:
config/grammar.php for customization (e.g., language support).app/Providers/GrammarServiceProvider.php for service binding overrides.Basic Detection:
$word = 'quickly';
$partsOfSpeech = Grammar::detect($word);
// Use in validation, content analysis, or NLP pipelines.
Batch Processing:
$words = ['run', 'jumps', 'happily'];
$results = Grammar::detectBatch($words);
// Returns associative array: ['run' => ['VERB', 'NOUN'], ...]
Integration with Laravel Features:
public function rules()
{
return [
'sentence' => [
'required',
function ($attribute, $value, $fail) {
$words = explode(' ', $value);
$adjectives = Grammar::detectBatch($words);
if (!array_filter($adjectives, fn($pos) => in_array('ADJECTIVE', $pos))) {
$fail('Sentence must contain at least one adjective.');
}
},
],
];
}
public function saving(Model $model)
{
if ($model->isTaggable()) {
$tags = explode(',', $model->tags);
$tagPos = Grammar::detectBatch($tags);
$model->grammar_tags = json_encode($tagPos);
}
}
Custom Logic with Results:
$pos = Grammar::detect('darkness');
if (in_array('NOUN', $pos)) {
// Trigger noun-specific logic (e.g., semantic analysis).
}
Language Switching:
Grammar::setLanguage('es'); // Spanish
$pos = Grammar::detect('correr'); // Returns Spanish POS tags.
$cacheKey = 'grammar:'.md5($word);
$pos = Cache::remember($cacheKey, now()->addHours(1), function() use ($word) {
return Grammar::detect($word);
});
detectBatch() for multiple words to reduce overhead.Outdated Data:
Grammar::detect('AI') may return unexpected results (e.g., no POS match).Language Limitations:
es, fr) may have incomplete support.Djunehor\Grammar\Contracts\GrammarDetector interface to add custom detectors.False Positives:
VERB, NOUN). Validate logic to handle ambiguity:
$pos = Grammar::detect('run');
if (count($pos) > 1) {
// Implement context-aware resolution (e.g., check surrounding words).
}
Case Sensitivity:
trim() and strtolower() inputs).\Log::debug('POS for "'.$word.'":', Grammar::detect($word));
djunehor/grammar PHP library. Review its source for edge cases.Custom Detectors:
Override the default detector in GrammarServiceProvider:
$this->app->bind(
\Djunehor\Grammar\Contracts\GrammarDetector::class,
\App\Services\CustomGrammarDetector::class
);
Add New Languages:
Extend the GrammarDetector to support unsupported languages by integrating with APIs like WordNet or spaCy.
Mocking for Tests:
$this->app->instance(
\Djunehor\Grammar\Contracts\GrammarDetector::class,
Mockery::mock(\Djunehor\Grammar\Contracts\GrammarDetector::class)
);
config/grammar.php (default_language). Override via Facade:
Grammar::setLanguage('de'); // German
config/grammar.php if using external detectors:
'cache_enabled' => false,
if (in_array('ADJECTIVE', Grammar::detect($word))) {
$sentimentScore = analyzeSentiment($word); // Custom logic.
}
How can I help you explore Laravel packages today?