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).
This package is part of the Apie library. The code is maintained in a monorepo, so PR's need to be sent to the monorepo
This small package contains a class to count words in a text. All words are returned lowercase.
Usage
use Apie\CountWords\WordCounter;
var_dump(WordCounter::countFromString('This is the text with many words like the or and'));
This will echo:
array(10) {
["this"]=>
int(1)
["is"]=>
int(1)
["the"]=>
int(2)
["text"]=>
int(1)
["with"]=>
int(1)
["many"]=>
int(1)
["words"]=>
int(1)
["like"]=>
int(1)
["or"]=>
int(1)
["and"]=>
int(1)
}
It also supports reading (large) files as long the indexing does still fit in memory:
use Apie\CountWords\WordCounter;
var_dump(WordCounter::countFromResource(fopen('large_file.txt', 'r')));
How can I help you explore Laravel packages today?