Created
May 14, 2012 16:59
-
-
Save alixaxel/2695066 to your computer and use it in GitHub Desktop.
helper functions
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 | |
require_once('./xpto.php'); | |
Dump(get_loaded_extensions(false)); | |
die(); | |
$xref = array | |
( | |
'a' => array(), | |
'b' => array('a'), | |
'c' => array('a', 'b'), | |
'd' => array('a'), | |
); | |
Dump(Export('$xref', $xref)); | |
// START OF PARENTESCO | |
function Parentesco($h, $refs) | |
{ | |
$result = array(); | |
if (array_key_exists($h, $refs) === true) | |
{ | |
$result = array_fill_keys(array_keys($refs), 0); // iniciar tudo a 0 | |
foreach ($refs as $n => $n_refs) // para cada outro texto n | |
{ | |
if (strcmp($h, $n) === 0) | |
{ | |
$result[$n] = -1; // para si mesmo o parentesco é -1 | |
} | |
else | |
{ | |
if (in_array($h, $n_refs) === true) // h é usado como referencia em n? | |
{ | |
$result[$n] += 1; | |
} | |
else if (in_array($n, $refs[$h]) === true) // ou vice-versa? | |
{ | |
$result[$n] += 1; | |
} | |
//$result[$n] += count(array_intersect($n_refs, $refs[$h])); // numero de referencias comuns entre h e n | |
} | |
} | |
} | |
return $result; | |
} | |
$parentesco = array(); | |
foreach (array_keys($xref) as $h) | |
{ | |
$parentesco[$h] = Parentesco($h, $xref); | |
} | |
Dump(Export('$parentesco', $parentesco)); | |
// END OF PARENTESCO (why / where will this be used?) | |
class Stelaro | |
{ | |
public static function Delve($key, $data, $depth = 0) | |
{ | |
while (--$depth >= 0) | |
{ | |
$key = Flatten(array_intersect_key($data, array_flip((array) $key))); | |
} | |
return array_intersect_key($data, array_flip((array) $key)); | |
} | |
public static function Ranking($doi, $references, $depth = 0) | |
{ | |
} | |
} | |
// START OF PROFUNDIDADE | |
for ($i = 0; $i <= 5; ++$i) | |
{ | |
Dump(sprintf('Referencias de %sC (%u)', str_repeat('referencias de ', $i), $i), Stelaro::Delve('c', $xref, $i)); | |
} | |
// END OF PROFUNDIDADE | |
function calculatePageRank($linkGraph, $dampingFactor = 0.15) { | |
$pageRank = array(); | |
$tempRank = array(); | |
$nodeCount = count($linkGraph); | |
// initialise the PR as 1/n | |
foreach($linkGraph as $node => $outbound) { | |
$pageRank[$node] = 1/$nodeCount; | |
$tempRank[$node] = 0; | |
} | |
$change = 1; | |
$i = 0; | |
while($change > 0.00005 && $i < 100) { | |
$change = 0; | |
$i++; | |
// distribute the PR of each page | |
foreach($linkGraph as $node => $outbound) { | |
$outboundCount = count($outbound); | |
foreach($outbound as $link) { | |
$tempRank[$link] += $pageRank[$node] / $outboundCount; | |
} | |
} | |
$total = 0; | |
// calculate the new PR using the damping factor | |
foreach($linkGraph as $node => $outbound) { | |
$tempRank[$node] = ($dampingFactor / $nodeCount) | |
+ (1-$dampingFactor) * $tempRank[$node]; | |
$change += abs($pageRank[$node] - $tempRank[$node]); | |
$pageRank[$node] = $tempRank[$node]; | |
$tempRank[$node] = 0; | |
$total += $pageRank[$node]; | |
} | |
// Normalise the page ranks so it's all a proportion 0-1 | |
foreach($pageRank as $node => $score) { | |
$pageRank[$node] /= $total; | |
} | |
} | |
return $pageRank; | |
} | |
Dump(calculatePageRank($xref)); | |
?> |
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 | |
function Dump() | |
{ | |
static $i = 0; | |
foreach (func_get_args() as $argument) | |
{ | |
if (is_resource($argument) === true) | |
{ | |
$result = sprintf('%s (#%u)', get_resource_type($argument), $argument); | |
} | |
else if ((is_array($argument) === true) || (is_object($argument) === true)) | |
{ | |
$result = rtrim(print_r($argument, true)); | |
} | |
else | |
{ | |
$result = stripslashes(preg_replace("~^'|'$~", '', var_export($argument, true))); | |
} | |
if (strncmp('cli', PHP_SAPI, 3) !== 0) | |
{ | |
$result = sprintf('<pre style="background: #%s; margin: 5px; padding: 5px; text-align: left;">%s</pre>', (++$i % 2) ? 'df0' : 'fda', htmlspecialchars($result, ENT_QUOTES)); | |
} | |
echo $result . "\n"; | |
} | |
} | |
function Export($name, $data) | |
{ | |
$result = null; | |
if (is_scalar($data) === true) | |
{ | |
$result .= sprintf("%s = %s;\n", $name, var_export($data, true)); | |
} | |
else if (is_array($data) === true) | |
{ | |
$result .= sprintf("%s = array();\n", $name); | |
foreach ($data as $key => $value) | |
{ | |
$result .= Export($name . '[' . var_export($key, true) . ']', $value) . "\n"; | |
} | |
if (array_keys($data) === range(0, count($data))) | |
{ | |
$result = preg_replace('~^' . sprintf(preg_quote($name . '[%s]', '~'), '\d+') . '~m', $name . '[]', $result); | |
} | |
} | |
else if (is_object($data) === true) | |
{ | |
$result .= sprintf("%s = %s;\n", $name, preg_replace('~\n\s*~', '', var_export($data, true))); | |
} | |
else | |
{ | |
$result .= sprintf("%s = %s;\n", $name, 'null'); | |
} | |
return rtrim($result, "\n"); | |
} | |
function Flatten($data) | |
{ | |
$result = array(); | |
if (is_array($data) === true) | |
{ | |
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($data)) as $value) | |
{ | |
$result[] = $value; | |
} | |
} | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment