Created
June 24, 2020 14:26
-
-
Save drupol/9ea3aacc5286940725e70f250d308bc6 to your computer and use it in GitHub Desktop.
Run Text Analysis with loophp/collection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
include __DIR__ . '/vendor/autoload.php'; | |
use loophp\collection\Collection; | |
$collection = Collection::with(file_get_contents('http://loripsum.net/api')) | |
// Filter out some characters. | |
->filter( | |
static function ($item, $key): bool { | |
return (bool) preg_match('/^[a-zA-Z]+$/', $item); | |
} | |
) | |
// Lowercase each character. | |
->map(static function (string $letter): string { | |
return mb_strtolower($letter); | |
}) | |
// Run the frequency tool. | |
->frequency() | |
// Flip keys and values. | |
->flip() | |
// Sort values. | |
->sort() | |
// Convert to array. | |
->all(); | |
print_r($collection); | |
/** | |
Array | |
( | |
[x] => 6 | |
[h] => 9 | |
[g] => 10 | |
[f] => 13 | |
[b] => 17 | |
[q] => 27 | |
[v] => 32 | |
[l] => 48 | |
[d] => 51 | |
[p] => 53 | |
[c] => 58 | |
[m] => 92 | |
[o] => 94 | |
[n] => 102 | |
[r] => 103 | |
[s] => 105 | |
[u] => 124 | |
[a] => 138 | |
[t] => 150 | |
[i] => 180 | |
[e] => 189 | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment