-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
41 lines (31 loc) · 1.26 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
echo "<h3>Word Rank From One File</h3>";
$counted = strtolower(file_get_contents("docs/one.txt"));
$wordArray = preg_split('/[^a-z]/', $counted, -1, PREG_SPLIT_NO_EMPTY);
/* get associative array of values from $filteredArray as keys and their frequency count as value */
$wordFrequencyArray = array_count_values($wordArray);
/* Sort array from higher to lower, keeping keys */
arsort($wordFrequencyArray);
/* grab Top 100 - sorted */
$top10words = array_slice($wordFrequencyArray,0,100);
/* display them */
foreach ($top10words as $topWord => $frequency)
echo "$topWord -- $frequency<br/>";
echo "<h3>Total From All Files</h3>";
$path = realpath('docs');
$wordArrayTotal = [];
foreach (glob($path.'/*.*') as $file) {
$counted = strtolower(file_get_contents($file));
$wordArray = preg_split('/[^a-z]/', $counted, -1, PREG_SPLIT_NO_EMPTY);
$wordArrayTotal = array_merge($wordArrayTotal, $wordArray);
}
$wordFrequencyArray = array_count_values($wordArrayTotal);
/* Sort array from higher to lower, keeping keys */
arsort($wordFrequencyArray);
/* grab Top 100 - sorted */
$top10words = array_slice($wordFrequencyArray, 0, 100);
/* display them */
foreach ($top10words as $topWord => $frequency) {
echo "$topWord -- $frequency<br/>";
}
?>