-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataProcessor6.php
More file actions
484 lines (450 loc) · 15.4 KB
/
DataProcessor6.php
File metadata and controls
484 lines (450 loc) · 15.4 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
// DataProcessor6.php
// Adds textual indicators from annual reports to financial subset CSV files.
ini_set('display_errors', '1');
error_reporting(E_ALL);
set_time_limit(0);
ignore_user_abort(true);
ob_implicit_flush(true);
define('REPORTS_SUBSET_FILE', __DIR__ . '/reports_subset.csv');
define('REPORTS_SOLVENT_SUBSET_FILE', __DIR__ . '/reports_solvent_subset.csv');
define('FINANCIALS_SUBSET_FILE', __DIR__ . '/financials_subset.csv');
define('FINANCIALS_SOLVENT_SUBSET_FILE', __DIR__ . '/financials_solvent_subset.csv');
define('BANKRUPTCY_WORDS', [
'substantial doubt',
'going concern',
'going-concern',
'ability to continue as a going concern',
'going concern uncertainty',
'going concern warning',
'going concern qualification',
'material uncertainty',
'adverse conditions and events',
'doubt about our ability to continue',
'substantial doubt exists',
'significant doubt exists',
'unable to meet obligations as they become due',
'inability to meet obligations',
'recurring losses',
'negative operating cash flows',
'working capital deficit',
'working capital deficiency',
'substantial working capital deficit',
'liquidity crisis',
'severe liquidity',
'liquidity constraints',
'refinancing risk',
'covenant breach',
'covenant default',
'event of default',
'payment default',
'missed interest payment',
'missed debt payment',
'arrears of interest',
'delinquent interest',
'non-compliance with financial covenants',
'not in compliance with covenants',
'waiver of covenants',
'forbearance agreement',
'restructuring support agreement',
'debt restructuring',
'debt-for-equity swap',
'asset sales required to fund operations',
'court-supervised',
'reorganization plan',
'plan of reorganization',
'prepackaged plan',
'pre-negotiated plan',
'debtor-in-possession',
'DIP financing',
'petition for relief',
'voluntary petition',
'bankruptcy',
'insolvency',
'insolvent',
'chapter 11',
'chapter 7',
'liquidation',
'wind-down',
'cease operations',
'discontinue operations',
'delisting notice',
'noncompliance with listing requirements',
'acceleration of indebtedness',
'cross-default',
'limited cash runway',
'going concern basis may not be appropriate',
'going concern disclosure',
]);
define('SOLVENCY_WORDS', [
'adequate liquidity',
'ample liquidity',
'robust liquidity',
'healthy liquidity',
'liquidity remains strong',
'strong liquidity position',
'strong cash position',
'sufficient cash resources',
'financial flexibility',
'ample financial flexibility',
'positive operating cash flow',
'net cash provided by operating activities',
'free cash flow',
'positive free cash flow',
'cash flow generation',
'strong cash generation',
'operating cash flows will fund',
'sufficient liquidity to meet obligations',
'ability to meet obligations as they become due',
'we believe our cash flows will be sufficient',
'sufficient to meet our obligations for at least the next 12 months',
'liquidity sources include',
'access to capital markets',
'continued access to capital markets',
'investment grade',
'investment-grade credit rating',
'credit rating upgrade',
'positive outlook',
'stable outlook',
'in compliance with financial covenants',
'covenant compliance',
'no covenant breaches',
'significant covenant headroom',
'headroom under covenants',
'no material debt maturities until',
'no significant debt maturities until',
'extended debt maturities',
'refinanced on favorable terms',
'committed revolving credit facility',
'unused revolving credit facility',
'undrawn revolver',
'repaid debt',
'reduced leverage',
'deleveraging',
'net leverage declined',
'interest coverage of',
'strong balance sheet',
'solid balance sheet',
'sustained profitability',
'consistent profitability',
'profitable operations',
'net income',
'earnings growth',
'maintained dividend',
'increased dividend',
'share repurchases',
'return of capital',
'positive working capital',
'working capital surplus',
'unqualified audit opinion',
'no going concern uncertainty',
'no substantial doubt exists',
'cash from operations exceeded capital expenditures',
'net cash from operating activities increased',
'committed credit facilities',
'diversified funding sources',
'well-capitalized',
'capital ratios above regulatory requirements',
]);
$runningInCli = (php_sapi_name() === 'cli');
if (!$runningInCli) {
header('Content-Type: text/html; charset=UTF-8');
echo "<!doctype html><meta charset='utf-8'><style>body{background:#000;color:#0f0;font:14px/1.4 monospace;padding:16px}</style><pre>";
}
logmsg('Starting annual report textual indicator processing…');
function logmsg(string $msg): void {
$ts = date('H:i:s');
echo "[$ts] $msg\n";
flush();
}
function read_csv(string $file): array {
$rows = [];
if (!($fh = fopen($file, 'r'))) return [[], []];
$header = fgetcsv($fh, 0, ',', '"', '\\');
if ($header === false) {
fclose($fh);
return [[], []];
}
while (($data = fgetcsv($fh, 0, ',', '"', '\\')) !== false) {
$row = [];
foreach ($header as $i => $col) {
$row[$col] = $data[$i] ?? '';
}
$rows[] = $row;
}
fclose($fh);
return [$header, $rows];
}
function write_csv(string $file, array $header, array $rows): void {
$fh = fopen($file, 'w');
fputcsv($fh, $header);
foreach ($rows as $row) {
$line = [];
foreach ($header as $col) {
$line[] = $row[$col] ?? '';
}
fputcsv($fh, $line);
}
fclose($fh);
}
function fetch_report(string $url): string {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; DataProcessor6/1.0; +https://example.com)');
curl_setopt($ch, CURLOPT_ENCODING, '');
$resp = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($resp === false) {
logmsg(' cURL error while fetching report: ' . curl_error($ch));
curl_close($ch);
return '';
}
curl_close($ch);
if ($status < 200 || $status >= 300) {
logmsg(' HTTP status ' . $status . ' when fetching report.');
return '';
}
return $resp !== false ? $resp : '';
}
function html_to_text(string $html): string {
$html = preg_replace('#<script\b[^>]*>.*?</script>#is', ' ', $html);
$html = preg_replace('#<style\b[^>]*>.*?</style>#is', ' ', $html);
$html = preg_replace('#<(table|thead|tbody|tfoot|tr|td|th)\b[^>]*>#i', "\n", $html);
$html = preg_replace('#<(br|p|div|li|ul|ol|h[1-6])\b[^>]*>#i', "\n", $html);
$text = strip_tags($html);
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$text = preg_replace("/\r/", '', $text);
$text = preg_replace("/\n{3,}/", "\n\n", $text);
$text = preg_replace('/[\t ]+/', ' ', $text);
$text = preg_replace('/\n[ \t]+/', "\n", $text);
return trim($text);
}
function normalize_cik(?string $value): string {
$value = trim((string)$value);
if ($value === '') return '';
$normalized = ltrim($value, '0');
if ($normalized === '') {
$normalized = '0';
}
return $normalized;
}
function normalize_year(?string $value): string {
$value = trim((string)$value);
if ($value === '') return '';
if (preg_match('/\b(\d{4})\b/', $value, $matches)) {
return $matches[1];
}
if (is_numeric($value)) {
$intYear = (int)$value;
return $intYear === 0 ? '' : (string)$intYear;
}
return $value;
}
function make_financial_key(?string $cik, ?string $year): string {
$normCik = normalize_cik($cik);
$normYear = normalize_year($year);
if ($normCik === '' || $normYear === '') return '';
return $normCik . '|' . $normYear;
}
function text_length(string $text): int {
if (function_exists('mb_strlen')) {
return mb_strlen($text, 'UTF-8');
}
return strlen($text);
}
function safe_strtolower(string $text): string {
if (function_exists('mb_strtolower')) {
return mb_strtolower($text, 'UTF-8');
}
return strtolower($text);
}
function count_indicator_words(string $text, array $words): int {
$count = 0;
$lowerTextCache = null;
foreach ($words as $word) {
$word = trim($word);
if ($word === '') continue;
$pattern = '/\b' . preg_quote($word, '/') . '\b/iu';
$matches = [];
$result = preg_match_all($pattern, $text, $matches);
if ($result === false) {
if ($lowerTextCache === null) {
$lowerTextCache = safe_strtolower($text);
}
$lowerWord = safe_strtolower($word);
$count += substr_count($lowerTextCache, $lowerWord);
} else {
$count += count($matches[0] ?? []);
}
}
return $count;
}
function build_possible_financial_keys(array $row): array {
$keys = [];
$keys[] = make_financial_key($row['CIK'] ?? '', $row['year'] ?? '');
if (isset($row['EntityCentralIndexKey'])) {
$keys[] = make_financial_key($row['EntityCentralIndexKey'], $row['year'] ?? '');
}
$keys = array_filter($keys, function ($value) {
return $value !== '';
});
return array_values(array_unique($keys));
}
function apply_metrics_to_rows(array &$rows, array $metricsMap, array $newColumns): array {
$updated = 0;
$overwrittenCells = 0;
foreach ($rows as &$row) {
$possibleKeys = build_possible_financial_keys($row);
foreach ($possibleKeys as $key) {
if (!isset($metricsMap[$key])) {
continue;
}
foreach ($newColumns as $col) {
$newValue = (string)($metricsMap[$key][$col] ?? '');
$currentValue = array_key_exists($col, $row) ? (string)$row[$col] : '';
if ($currentValue !== $newValue) {
if ($currentValue !== '') {
$overwrittenCells++;
}
$row[$col] = $newValue;
} else {
$row[$col] = $newValue;
}
}
$updated++;
break;
}
}
unset($row);
return [$updated, $overwrittenCells];
}
logmsg('Reading report CSV files …');
[$reportHeader1, $reportRows1] = read_csv(REPORTS_SUBSET_FILE);
[$reportHeader2, $reportRows2] = read_csv(REPORTS_SOLVENT_SUBSET_FILE);
$items = [];
foreach ($reportRows1 as $idx => $row) {
$link = trim($row['AnnualReportLink'] ?? '');
if ($link === '') continue;
$rawCik = (string)($row['CIK'] ?? '');
$rawYear = (string)($row['year'] ?? '');
$items[] = [
'source' => 1,
'index' => $idx,
'CIK' => $rawCik,
'year' => $rawYear,
'normalized_cik' => normalize_cik($rawCik),
'normalized_year' => normalize_year($rawYear),
'normalized_key' => make_financial_key($rawCik, $rawYear),
'link' => $link,
];
}
foreach ($reportRows2 as $idx => $row) {
$link = trim($row['AnnualReportLink'] ?? '');
if ($link === '') continue;
$rawCik = (string)($row['CIK'] ?? '');
$rawYear = (string)($row['year'] ?? '');
$items[] = [
'source' => 2,
'index' => $idx,
'CIK' => $rawCik,
'year' => $rawYear,
'normalized_cik' => normalize_cik($rawCik),
'normalized_year' => normalize_year($rawYear),
'normalized_key' => make_financial_key($rawCik, $rawYear),
'link' => $link,
];
}
if (empty($items)) {
logmsg('No annual reports found to process.');
logmsg('Done.');
exit;
}
logmsg('Processing ' . count($items) . ' annual report(s)…');
$processedItems = [];
foreach ($items as $item) {
$displayCik = $item['CIK'] !== '' ? $item['CIK'] : $item['normalized_cik'];
$displayYear = $item['year'] !== '' ? $item['year'] : $item['normalized_year'];
logmsg(' Downloading report for CIK ' . $displayCik . ' (' . $displayYear . ')…');
$html = fetch_report($item['link']);
if ($html === '') {
logmsg(' Skipping due to download failure.');
continue;
}
$text = html_to_text($html);
if ($text === '') {
logmsg(' Empty text after parsing, skipping.');
continue;
}
$charCount = text_length($text);
$bankruptcyCount = count_indicator_words($text, BANKRUPTCY_WORDS);
$solvencyCount = count_indicator_words($text, SOLVENCY_WORDS);
logmsg(' -> characters: ' . $charCount . ', bankruptcy words: ' . $bankruptcyCount . ', solvency words: ' . $solvencyCount . '.');
$processedItems[] = $item + [
'characters' => $charCount,
'bankruptcy_words' => $bankruptcyCount,
'solvency_words' => $solvencyCount,
];
}
if (empty($processedItems)) {
logmsg('No reports yielded usable metrics. Aborting.');
logmsg('Done.');
exit;
}
$metricsMap1 = [];
$metricsMap2 = [];
foreach ($processedItems as $row) {
$key = $row['normalized_key'];
if ($key === '') {
$key = make_financial_key($row['normalized_cik'] ?? '', $row['normalized_year'] ?? '');
}
if ($key === '') {
$key = make_financial_key($row['CIK'] ?? '', $row['year'] ?? '');
}
if ($key === '') continue;
$metrics = [
'BankruptcyIndicatingWordsCount' => (string)$row['bankruptcy_words'],
'SolvencyIndicatingWordsCount' => (string)$row['solvency_words'],
'TotalCharactersCount' => (string)$row['characters'],
];
if ($row['source'] === 1) {
$metricsMap1[$key] = $metrics;
} else {
$metricsMap2[$key] = $metrics;
}
}
if (empty($metricsMap1) && empty($metricsMap2)) {
logmsg('No metrics matched to financial keys. Nothing to update.');
logmsg('Done.');
exit;
}
logmsg('Reading financial subset CSV files …');
[$finHeader1, $finRows1] = read_csv(FINANCIALS_SUBSET_FILE);
[$finHeader2, $finRows2] = read_csv(FINANCIALS_SOLVENT_SUBSET_FILE);
$newColumns = [
'BankruptcyIndicatingWordsCount',
'SolvencyIndicatingWordsCount',
'TotalCharactersCount',
];
foreach ($newColumns as $col) {
if (!in_array($col, $finHeader1, true)) $finHeader1[] = $col;
if (!in_array($col, $finHeader2, true)) $finHeader2[] = $col;
}
[$updated1, $overwrittenCells1] = apply_metrics_to_rows($finRows1, $metricsMap1, $newColumns);
[$updated2, $overwrittenCells2] = apply_metrics_to_rows($finRows2, $metricsMap2, $newColumns);
$expectedTotal = count($metricsMap1) + count($metricsMap2);
$actualTotal = $updated1 + $updated2;
if ($expectedTotal > $actualTotal) {
logmsg(' Warning: ' . ($expectedTotal - $actualTotal) . ' metric set(s) did not match any financial subset row.');
}
$overwrittenTotal = $overwrittenCells1 + $overwrittenCells2;
if ($overwrittenTotal > 0) {
logmsg('Overwrote existing metric values in ' . $overwrittenTotal . ' cell(s).');
}
logmsg('Writing updated financial subset CSV files …');
write_csv(FINANCIALS_SUBSET_FILE, $finHeader1, $finRows1);
write_csv(FINANCIALS_SOLVENT_SUBSET_FILE, $finHeader2, $finRows2);
logmsg('Updated ' . $updated1 . ' bankrupt subset row(s) and ' . $updated2 . ' solvent subset row(s).');
logmsg('Done.');