-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmra.php
More file actions
77 lines (65 loc) · 2.31 KB
/
mra.php
File metadata and controls
77 lines (65 loc) · 2.31 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
$data = './data/*';
$lineToData = function($line) {
$it = explode(':::', $line);
$item['id'] = $it[0];
$item['title'] = trim($it[2]);
$item['authors'] = explode('::', $it[1]);
return $item;
};
$sumWordCounts =
function($countsL, $countsR) {
// Get all the words
$words = array_merge(array_keys($countsL), array_keys($countsR));
$out = array();
// Put them in a new (Array: Word => Count)
foreach($words as $word) {
// Sum their counts
$out[$word] = isset($countsL[$word]) ? $countsL[$word] : 0;
$out[$word] += isset($countsR[$word]) ? $countsR[$word] : 0;
}
return $out;
};
$sumDataCounts =
function($countsL, $countsR) {
if (!in_array($auth, $countsR['authors'])) {
return $countsL;
}
foreach ($countsR['authors'] as $author) {
if ($author != $auth) {
continue;
}
$title = explode(' ', trim($countsR['title'], ";:,?!."));
if (isset($countsL[$author])) {
// $countsL[$author]['items'][] = array(
// 'id' => $countsR['id'],
// 'title' => $countsR['title']
// );
$countsL[$author]['titles'] = array_count_values($title);
} else {
// $countsL[$author] = array('items'=>array(), 'titles' => array());
// $countsL[$author]['items'][] = array(
// 'id' => $countsR['id'],
// 'title' => $countsR['title'],
// );
$countsL[$author]['titles'] = array_count_values($title);
}
}
return $countsL;
};
$totals = array();
$totalsFirst = array('Michael Brady' => array());
foreach (glob($data) as $file) {
$dataStream = explode("\n", trim(file_get_contents($file)));
$dataConverted = array_map($lineToData, $dataStream);
// print_r($dataConverted);exit;
$totalsFirst = array_reduce($dataConverted, function($reduced, $current) {
foreach ($reduced as $author=>$data) {
if (in_array($author, $current['authors'])) {
$reduced[$author][] = $current['title'];
}
}
return $reduced;
}, $totalsFirst);
}
print_r($totalsFirst);