107 lines
3.5 KiB
PHP
107 lines
3.5 KiB
PHP
<?php
|
|
|
|
function parse_generic_csv($generic_csv) {
|
|
$candidate_data = [];
|
|
if (($handle = fopen($generic_csv, "r")) !== FALSE) {
|
|
$headers = fgetcsv($handle);
|
|
while (($data = fgetcsv($handle)) !== FALSE) {
|
|
$candidate = [];
|
|
$question_no = 0;
|
|
$is_question = false;
|
|
foreach ($headers as $key => $value) {
|
|
/* Override key name for questions */
|
|
if (strstr($value, "candidate photo")) $value = "Photo";
|
|
if (strstr($value, "In which federal Division are")) continue;
|
|
if (strstr($value, "In which Federal Division are")) $value = "Division";
|
|
if (strstr($value, "Political Party")) $value = "Party";
|
|
if (strstr($value, "Protected bike lanes provide")) continue;
|
|
|
|
if ($value === "Photo") {
|
|
$candidate['photo_url'] = $data[$key];
|
|
$data[$key] = preg_filter("/.*id=/", "", $data[$key]);
|
|
}
|
|
|
|
if ($is_question) {
|
|
$candidate['q'.$question_no++] = $data[$key];
|
|
} else {
|
|
$candidate[$value] = $data[$key];
|
|
}
|
|
|
|
if ($value === "Party") {
|
|
$is_question = true;
|
|
}
|
|
}
|
|
$candidate_data[] = $candidate;
|
|
}
|
|
fclose($handle);
|
|
} else {
|
|
error_log('Error opening candidates file');
|
|
exit(1);
|
|
}
|
|
|
|
return $candidate_data;
|
|
}
|
|
|
|
function match_lga(&$candidate_data, $lga_list) {
|
|
foreach ($candidate_data as &$candidate) {
|
|
/* Match user typed LGA/Ward to our database */
|
|
$max_score = 0;
|
|
$match_lga = null;
|
|
foreach ($lga_list as $lga) {
|
|
$aa = preg_split("/[^a-z]/", strtolower($candidate['Division']));
|
|
$bb = preg_split("/[^a-z]/", $lga['slug']);
|
|
|
|
$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;
|
|
$match_lga = $lga;
|
|
}
|
|
}
|
|
|
|
/*
|
|
$max_score = 0;
|
|
foreach ($match_lga['wardNames'] as $ward) {
|
|
similar_text(strtolower($ward), strtolower($candidate['Ward']), $score);
|
|
if ($score >= $max_score) {
|
|
$max_score = $score;
|
|
$match_ward = $ward;
|
|
}
|
|
}
|
|
*/
|
|
|
|
if ($match_lga === null) {
|
|
$candidate['match_division'] = "no_match";
|
|
} else {
|
|
$candidate['match_division'] = $match_lga['slug'];
|
|
}
|
|
//$candidate['match_ward'] = $match_ward;
|
|
}
|
|
}
|
|
|
|
function remove_duplicates(&$candidate_data) {
|
|
$names = [];
|
|
$duplicates = [];
|
|
foreach ($candidate_data as $candidate_key => $candidate) {
|
|
/* If we've already had this name, remove the old entry */
|
|
foreach ($names as $name_key => $name) {
|
|
similar_text(strtolower($name), strtolower($candidate['Name']), $score);
|
|
if ($score > 90) {
|
|
$duplicates[] = $name_key;
|
|
}
|
|
}
|
|
$names[$candidate_key] = $candidate['Name'];
|
|
}
|
|
$duplicates = array_unique($duplicates);
|
|
foreach ($duplicates as $duplicate) {
|
|
unset($candidate_data[$duplicate]);
|
|
}
|
|
}
|