-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc-data.php
executable file
·64 lines (49 loc) · 1.52 KB
/
proc-data.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
60
61
62
63
64
#!/usr/bin/php
<?php
const ROWS = 4;
const COLS = 12;
const MAX_CHARS = ROWS * COLS;
const VOID = ' ';
$inputData = json_decode(file_get_contents('./data-input.json'));
$procData = null;
$outputData = (object) ['proverb' => [], 'movie' => [], 'band' => []];
$sentence = '';
$blocks = '';
foreach (['proverb', 'movie', 'band'] as $category) {
$procData = $inputData->{$category};
for ($i = 0; $i < count($procData); $i++) {
$sentence = $procData[$i][0];
if (strlen($sentence) > MAX_CHARS) continue;
$blocks = '';
for ($line = 0; $line < 4; $line++) {
$chunk = nextChunk($sentence);
if (strlen($chunk) === 0 || strlen($chunk) > COLS) break;
$blocks .= center($chunk);
}
if (strlen($sentence) !== 0) continue;
$outputData->{$category}[] = $blocks;
}
}
if (is_file('./data.json')) unlink('./data.json');
file_put_contents('./data.json', json_encode($outputData, JSON_PRETTY_PRINT));
exit(0);
// ----
function sanitize($sentence) {
return strtoupper(trim($sentence));
}
function center($partial) {
return str_pad($partial, COLS, VOID, STR_PAD_BOTH);
}
function nextChunk(&$sentence) {
$words = explode(' ', $sentence);
$wordSet = '';
for ($i = 0; $i < count($words); $i++) {
if (strlen($wordSet . $words[$i] . ' ') <= COLS) {
$wordSet .= $words[$i] . ' ';
} else {
break;
}
}
$sentence = substr($sentence, strlen($wordSet) - 1);
return $wordSet;
}