-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.php
59 lines (47 loc) · 1.73 KB
/
common.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
error_reporting(0);
$configURL = 'configuration.json';
if (isset($_GET["config"])) {
$configURL = $_GET["config"];
}
function stringifyURL($string) {
setlocale(LC_CTYPE, 'en_US.UTF8');
$string = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $string);
$string = str_replace(' ', '-', $string);
// Remove the protocol
$string = str_replace('https://', '', $string);
$string = str_replace('http://', '', $string);
$string = str_replace('.', '-', $string);
$string = str_replace('/', '_', $string);
return $string;
}
function isResultsFile($fileinfo) {
global $configURL;
$filename = $fileinfo->getFilename();
return !$fileinfo->isDot() && strpos($filename, stringifyURL($configURL)) !== FALSE && strpos($fileinfo->getFilename(), '-RESULTS.csv') !== FALSE;
}
// Makes an array of things, their words and the counts.
function getResults() {
$dir = new DirectoryIterator(dirname(__FILE__));
$users = array();
$results = array();
foreach ($dir as $fileinfo) {
if (isResultsFile($fileinfo)) {
// Read the CSV
$csv = array_map( 'str_getcsv', file( $fileinfo->getFilename() ) );
// var_export($csv);
foreach ($csv as $entry) {
$image = $entry[0];
$words = $entry[1];
// Make this one big long string of words for now. We'll explode and count values later.
$results[$image] .= $words . ",";
}
}
}
// For each image count the words and replace that entry with an assoc array of word => count
foreach($results as $image => $words) {
$results[$image] = array_count_values(explode(",", $words));
}
return $results;
}
?>