apie/count-words
Count word frequencies in strings or streams/files with Apie\CountWords\WordCounter. Returns an array of lowercase words mapped to their occurrence count. Supports processing large files via resources (as long as the index fits in memory).
composer require apie/count-words) with no Laravel-specific dependencies, reducing friction.tmpfile() or StreamWrapper).countFromResource().WordCounter could introduce overhead in high-throughput scenarios.php-ml) if critical.SnowballStemmer)?Problem responses?Str::words() or a dedicated NLP library (e.g., symfony/text) suffice?WordCounter to Laravel’s IoC for dependency injection:
$this->app->singleton(WordCounter::class, function () {
return new \Apie\CountWords\WordCounter();
});
php artisan analyze:keywords storage/logs/*.txt).// Job
public function handle() {
$file = Storage::disk('s3')->open($this->filePath);
$counts = WordCounter::countFromResource($file);
// Save results to DB or cache
}
spatie/array-to-xml for structured output or laravel-excel for spreadsheet-based analysis.Phase 1: Proof of Concept (1–2 days)
use Apie\CountWords\WordCounter;
$counter = new WordCounter();
$result = $counter->countFromString("Hello world");
// Assert expected output: ['hello' => 1, 'world' => 1]
Phase 2: Core Integration (3–5 days)
WordCounter in a Laravel service class to add:
class WordCountService {
public function analyze(string|resource $input): array {
if (is_string($input)) {
return WordCounter::countFromString($input);
}
return WordCounter::countFromResource($input);
}
}
Phase 3: Scaling (1–2 weeks)
function countWordsInLargeFile(string $filePath): array {
$counts = [];
$handle = fopen($filePath, 'r');
while (!feof($handle)) {
$chunk = fread($handle, 8192); // Read 8KB at a time
$chunkCounts = WordCounter::countFromString($chunk);
foreach ($chunkCounts as $word => $count) {
$counts[$word] = ($counts[$word] ?? 0) + $count;
}
}
fclose($handle);
return $counts;
}
Phase 4: Optimization (Ongoing)
memory_get_usage() and optimize chunk sizes.ext-ctype for ctype_alpha, which is enabled by default).memory_get_peak_usage()).microtime(true)).php-ml/textstat).countFromResource").$cacheKey = 'word_counts:'.$fileHash;
$counts = Cache::remember($cacheKey, now()->addHours
How can I help you explore Laravel packages today?