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 */ $candidate_data = []; foreach ($candidates_files as $file) { $config_file = dirname($file)."/config.json"; $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); } $candidate_data[$config['councilName']]['_filename'] = $file; if (($handle = fopen($file, "r")) !== FALSE) { $headers = fgetcsv($handle); while (($data = fgetcsv($handle)) !== FALSE) { $candidate = []; foreach ($headers as $key => $value) { $candidate[$value] = $data[$key]; } $name_slug = trim_sluggify($candidate['Candidate Name']); $candidate_data[$config['councilName']][$name_slug] = $candidate; } } } $vec_lga_names = []; foreach ($results as $lga => $data) { $vec_lga_names[] = $lga; } function was_elected($candidate, $vec_wards) { foreach ($vec_wards as $vec_candidates) { list($score, $match) = match_words($candidate, $vec_candidates); if ($score > 180) return true; } return false; } $header = ["Ward", "Candidate Name", "Elected"]; 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]; $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; } } /* 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);