Last active
December 18, 2015 21:19
-
-
Save ppKrauss/5846826 to your computer and use it in GitHub Desktop.
Algorithm for manage a very specific data, from http://pt.wikipedia.org/wiki/Anexo_Discuss%C3%A3o:Lista_de_cidades_participantes_dos_protestos_no_Brasil_em_2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Organiza dados em "estrutura BrasilProtestos2013-estimCidadeFontes" | |
* dispostos no anexo do artigo "Protestos no Brasil em 2013" da Wikipedia. | |
* NOTA: proxima versao no forno, classes e traducao XML. | |
* http://pt.wikipedia.org/wiki/Protestos_no_Brasil_em_2013 | |
* http://pt.wikipedia.org/wiki/Anexo:Lista_de_cidades_participantes_dos_protestos_no_Brasil_em_2013 | |
* http://pt.wikipedia.org/wiki/Predefini%C3%A7%C3%A3o:BrasilProtestos2013-estimCidadeFontes . | |
*/ | |
header('Content-type: text/html; charset=UTF-8;' );// for txt and HTML source-code | |
header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 | |
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past | |
?> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>wiki2tab manifestos-br2013</title> | |
</head> | |
<body> | |
<textarea rows="20" cols="80"> | |
<?php | |
$tudo = file_get_contents('wiki_section.txt'); | |
$estado = ''; | |
$byCidade = array(); | |
$byDia = array(); | |
$cidade_item = array(); | |
$refs = array(); | |
// CONFIGS: | |
$debug = 0; | |
$delAllRefs = true; | |
$expandAllRefs = false; | |
$tudo = preg_replace('/<\-\-.+?\-\->/s','',$tudo); // remove comentários | |
// isola referências em $refs, substituindo por #ref-X;# | |
$tudo = preg_replace_callback( | |
'#<ref[^>/]*>.+?</ref>|<ref[^>]+\s*/\s*>#s', | |
function ($m) use (&$refs) { | |
$n = count($refs); | |
$refs[] = $m[0]; | |
return "#ref-$n;#"; | |
}, | |
$tudo | |
); | |
$tudo = preg_replace_callback( | |
'#{{carece de fontes}}#s', | |
function ($m) use (&$refs) { | |
$n = count($refs); | |
$refs[] = $m[0]; | |
return "#ref-$n;#"; | |
}, | |
$tudo | |
); | |
foreach (explode("\n",$tudo) as $line) { | |
if ($delAllRefs) refs_del($line); elseif ($expandAllRefs) refs_expand($line); | |
if ( preg_match('/^\|\s*(\w+)\s*=\s*(.+)$/',$line,$m) ) { | |
$cidade_item[$m[1]] = trim($m[2]); | |
if ($debug) | |
print "\n --1-- $line "; | |
} elseif (preg_match('#BrasilProtestos2013\-estimCidadeFontes#s', $line)) { | |
if (isset($cidade_item['cidade'])) | |
$byCidade[$cidade_item['cidade']] = $cidade_item; | |
foreach (array_keys($cidade_item) as $k) | |
if ( preg_match('/^(.+)_parts/',$k,$mm) ) { | |
if (!array_key_exists($mm[1], $byDia)) | |
$byDia[$mm[1]] = array(); | |
$byDia[$mm[1]][$cidade_item['cidade']] = $cidade_item; | |
} | |
$cidade_item = array('estado'=>$estado); | |
if ($debug) | |
print "\n --2-- $line "; | |
} elseif (preg_match('/===([^=]+)===/', $line,$m)) { | |
$estado = trim($m[1]); | |
if ($debug) | |
print "\n --3-- $line "; | |
} elseif ($debug) | |
print "\n --4-- $line"; | |
} // for | |
$dias = array_keys($byDia); | |
sort($dias); | |
foreach ($dias as $dia) { | |
$diaLabel = str_replace('junho', ' de junho', $dia); | |
if ($debug) print "\n$diaLabel"; | |
$reg = $byDia[$dia]; | |
$cids = array_keys($reg); | |
sort($cids); | |
$tot = $n = $tot_feridos = $tot_detidos = 0; | |
$rows = ''; | |
foreach ($cids as $cid) { | |
$feridos = @$reg[$cid][$dia.'_feridos']; | |
$detidos = @$reg[$cid][$dia.'_detidos']; | |
$cid2 = isset($reg[$cid]['apelido'])? $reg[$cid]['apelido']: $cid; | |
$regcid = $reg[$cid]; | |
$parts = trim($reg[$cid][$dia.'_parts']); | |
$parts2 = str_replace( ',', '.', $parts); | |
if (preg_match('/mil/',$parts)) { | |
$parts2 = preg_replace('/\s*mil\s*/','',$parts); | |
$mil = 1000; | |
} else | |
$mil = 1; | |
if (preg_match('/([0-9]+)\s*[\~\-a]\s*([0-9]+)\s*/',$parts2,$mm)) { | |
$p1 = $mm[1]*$mil; $p2 = $mm[2]*$mil; | |
$parts = preg_replace('/\s*[\~\-a]\s*/',' a ',$parts); | |
$p_med = ($p1+$p2)/2; | |
} else | |
$p_med = trim($parts2)*$mil; | |
$col1 = $rows? '': "rowspan=\"XX\"| $diaLabel ||"; | |
extraCoub($feridos, $tot_feridos, $p_med); // $feridos | |
extraCoub($detidos, $tot_detidos, $p_med); // $detidos | |
$rows .= "\n|-\n| $col1 $cid2 || $parts || $feridos || $detidos"; | |
$tot += $p_med; | |
$n++; | |
} | |
$COR = '||style="background-color:#BCC"|'; | |
if ($tot) { | |
$n++; | |
$rows = str_replace('"XX"',"\"$n\"",$rows); | |
$feridos = $tot_feridos; | |
$detidos = $tot_detidos; | |
extraCoub($feridos, $tot_feridos, $tot); // $feridos | |
extraCoub($detidos, $tot_detidos, $tot); // $detidos | |
if ($tot>10000) | |
$tot = 10*round($tot/10000); | |
print "$rows\n|-\n| $COR ~$tot mil $COR$feridos $COR$detidos"; | |
} | |
} //dia | |
function extraCoub(&$feridos,&$tot_feridos, $p_med) { | |
$feridos = preg_replace('/[^0-9]+/','',$feridos); | |
$tot_feridos += $feridos; | |
if ($feridos) | |
$feridos = "$feridos <small>(".(round(100*$feridos/$p_med,3))."%)</small>"; | |
else | |
$feridos = ''; // nao usar 0 | |
} | |
function refs_del(&$x) { | |
$x = preg_replace('/#ref\-([0-9]+);#/s', '', $x); | |
} | |
function refs_expand(&$x) { | |
global $refs; | |
$x = preg_replace_callback( | |
'/#ref\-([0-9]+);#/s', | |
function ($z) use (&$refs) { | |
return $refs[$z[1]]; }, | |
$x | |
); | |
} | |
?> | |
</textarea> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment