Generate candidates-elected.csv files from results.json.

This commit is contained in:
Kim Taylor
2024-11-16 14:45:18 +11:00
parent f8fd1cc20c
commit c9ec1933f6
2 changed files with 96 additions and 111 deletions

View File

@@ -1,8 +1,6 @@
<?php
//require_once("parse_generic_csv.php");
$options = getopt("", ["candidates-files:"]);
$options = getopt("", ["candidates-files:", "results-file:"]);
if (isset($options['candidates-files'])) {
$candidates_files = $options['candidates-files'];
@@ -11,10 +9,50 @@ if (isset($options['candidates-files'])) {
exit(1);
}
if (isset($options['results-file'])) {
$results_file = $options['results-file'];
$results_string = file_get_contents($results_file);
if ($results_string !== FALSE) {
$results = json_decode($results_string, true);
} else {
error_log("Error opening results.json.");
exit(1);
}
} else {
error_log("Error: Missing required option '--results-file'.");
exit(1);
}
function trim_sluggify($input) {
return strtolower(str_replace(' ', '-', trim($input)));
}
function match_words($words, $list) {
/* Match database names to VEC names */
$max_score = 0;
$best_match = "no match";
foreach ($list as $possible_match) {
$aa = preg_split("/[^a-z]/", strtolower($words));
$bb = preg_split("/[^a-z]/", strtolower($possible_match));
$score_sum = 0;
foreach ($aa as $a) {
foreach ($bb as $b) {
similar_text($a, $b, $score);
if ($score > 70) $score_sum += $score;
else $score_sum -= 1;
}
}
if ($score_sum > $max_score) {
$max_score = $score_sum;
$best_match = $possible_match;
}
}
return array($max_score, $best_match);
}
$candidates_files = explode(" ", $candidates_files);
/* Generate dictionary of candidates and LGAs */
@@ -30,6 +68,8 @@ foreach ($candidates_files as $file) {
exit(1);
}
$candidate_data[$config['councilName']]['_filename'] = $file;
if (($handle = fopen($file, "r")) !== FALSE) {
$headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
@@ -37,122 +77,66 @@ foreach ($candidates_files as $file) {
foreach ($headers as $key => $value) {
$candidate[$value] = $data[$key];
}
$candidate['Council'] = $config['councilName'];
$name_slug = trim_sluggify($candidate['Candidate Name']);
$candidate_data[$name_slug] = $candidate;
$candidate_data[$config['councilName']][$name_slug] = $candidate;
}
}
}
print_r($candidate_data);
$vec_lga_names = [];
foreach ($results as $lga => $data) {
$vec_lga_names[] = $lga;
}
/* Get list of elected candidates */
function was_elected($candidate, $vec_wards) {
foreach ($vec_wards as $vec_candidates) {
list($score, $match) = match_words($candidate, $vec_candidates);
if ($score > 100) return true;
}
return false;
}
//$lga_list = [];
/* Generate dictionary of LGAs and Wards */
//foreach ($config_files as $config_file) {
// $config_string = file_get_contents($config_file);
// if ($config_string !== FALSE) {
// $config = json_decode($config_string, true);
// } else {
// error_log("Error opening config.json.");
// exit(1);
// }
// $config['config-file'] = $config_file;
// $lga_list[] = $config;
//}
$header = ["Ward", "Candidate Name", "Elected"];
/* Match user typed LGA/Ward to our database */
//match_lga($candidate_data, $lga_list);
foreach ($candidate_data as $lga => $db_candidates) {
/* Find LGA in results dict */
list($score, $vec_lga_name) = match_words($lga, $vec_lga_names);
$vec_wards = $results[$vec_lga_name];
$header = ["Ward", "Candidate Name", "Rating", "Pledge", "Picture"];
$elected = [];
/* Go through database candidates and build list of elected candidates */
foreach ($db_candidates as $key => $value) {
if ($key === '_filename') {
$output_file = dirname($value)."/candidates-elected.csv";
continue;
}
if (was_elected($value['Candidate Name'], $vec_wards)) {
$elected[] = $value;
}
}
/* Generate candidates-generic.csv */
//foreach ($lga_list as $lga) {
// $lga_candidates = array_filter($candidate_data, function ($candidate) use ($lga) {
// return $candidate['match_lga'] === $lga['slug'];
// });
//
// if (count($lga_candidates) === 0) continue;
//
// remove_duplicates($lga_candidates);
//
// $dir = dirname($lga['config-file']);
// $dir_files = scandir($dir);
// $output_file = $dir."/candidates-generic.csv";
// $override_file = $dir."/candidates-override.csv";
//
// if (($handle = fopen($output_file, "w")) === FALSE) {
// error_log('Error opening output file');
// exit(1);
// }
//
// if (fputcsv($handle, $header) === FALSE) {
// error_log('Error writing headers to output file');
// exit(3);
// }
//
// $lines = [];
// foreach ($lga_candidates as $candidate) {
// /* Add extension to photo hash */
// if (strlen($candidate['Photo'])) {
// foreach ($dir_files as $file) {
// if (preg_match("/\.json$/", $file)) continue;
// if (strstr($file, $candidate['Photo'])) {
// $candidate['Photo'] = $file;
// }
// }
// }
//
// $lines[] = [
// $candidate['match_ward'],
// $candidate['Name'],
// $candidate['Score'],
// $candidate['Pledge'],
// $candidate['Photo'],
// ];
// }
//
// /* Apply overrides if they exist */
// $overrides = [];
// if (file_exists($override_file)) {
// if (($ovr_handle = fopen($override_file, "r")) !== FALSE) {
// $headers = fgetcsv($ovr_handle);
// while (($data = fgetcsv($ovr_handle)) !== FALSE) {
// $override = [];
// foreach ($headers as $key => $value) {
// $override[$value] = $data[$key];
// }
// $overrides[] = $override;
// }
// fclose($ovr_handle);
// } else {
// error_log('Error opening overrides file');
// exit(3);
// }
// }
//
// foreach ($overrides as $override) {
// foreach ($lines as $line_key => $line) {
// $match_index = array_search($override['Match Field'], $header);
// $replace_index = array_search($override['Replace Field'], $header);
// if ($line[$match_index] === $override['Match Value']) {
// if ($replace_index !== false)
// $lines[$line_key][$replace_index] = $override['Replace Value'];
// else /* If 'Replace Field' is not matched - delete this entry */
// $lines[$line_key]['Delete'] = 'y';
// }
// }
// }
//
// foreach ($lines as $line) {
// if (isset($line['Delete'])) continue;
// if (fputcsv($handle, $line) === FALSE) {
// error_log('Error writing candidate to output file');
// exit(3);
// }
// }
// fclose($handle);
//}
/* Don't create file if none were elected. */
if (count($elected) === 0) continue;
if (($handle = fopen($output_file, "w")) === FALSE) {
error_log('Error opening output file');
exit(1);
}
if (fputcsv($handle, $header) === FALSE) {
error_log('Error writing headers to output file');
exit(3);
}
foreach ($elected as $candidate) {
$line = array($candidate['Ward'], $candidate['Candidate Name'], "y");
if (fputcsv($handle, $line) === FALSE) {
error_log('Error writing candidate to output file');
exit(3);
}
}
fclose($handle);
}
exit(0);