1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| <?php
$toto = '((((1:0.a1,2:a2)3:0.a3,((4:0.b1,5:0.b2)6:0.b3,(7:0.c1,8:0.c2)9:0.c3)10:0.bc3)11:0.abc3,(12:0.d1,13:0.d2):0.d3)14:0.abcd3,15:0.e2)';
$toto ='((((1:0.01,2:0.01):0.05,(5:0.02,19:0.02):0.04):0.05,(3:0.03,4:0.03):0.08):0.59,(7:0.13,20:0.13):0.57):0.14,6:0.84)';
$arbre = new Newik( $toto);
echo '<pre>';
print_r( $arbre->parse() );
print_r( $arbre->showGroups() );
Class Newik {
protected $arbre;
private $position;
private $max_leng;
private $groupes;
static $DEBUG = false;
function __construct($arbre){
$this->arbre = $arbre;
$this->init();
}
function init(){
$this->position=0;
$this->max_leng = strlen($this->arbre);
}
function debug($msg, $tampon, $flag){
if ( self::$DEBUG ) {
echo $this->arbre,"\n";
$turing = str_repeat('.',$this->max_leng)."\n";
$turing[$this->position]= "^";
echo $turing;
echo $msg,' ',$this->arbre[$this->position],' position:',$this->position,' tampon:',$tampon,' flag:',$flag,"\n\n";
}
}
function appendChild(&$resultat, $flag,$tampon){
$this->debug('naming ', $tampon, $flag );
if ( $flag == 2 ){
$last = array_pop($resultat);
$resultat[$tampon]=$last;
}
else {
$resultat[]=$tampon;
}
}
function parse(){
$flagNomGroupe = 0; // a 2 lorsque ce qui suit peut être un nom de sous groupe.
$tampon = '';
$this->debug(' begin ', $tampon, $flagNomGroupe );
$resultat=array();
while( $this->position < $this->max_leng ){
$tmp = $this->arbre[$this->position];
if( $tmp == '(' ) {
$this->debug('open ', $tampon,$flagNomGroupe );
$this->position++;
$resultat[]= $this->parse(); //passage par référence!
$flagNomGroupe = 1; //d'apres wikipedia, on peut avoir un nom de groupe apres une ), dans ce cas pas de ,
}
else if ($tmp == ')') {
//on a fini de lire le fils de droite
$this->debug('closing ', $tampon,$flagNomGroupe );
$this->appendChild($resultat, $flagNomGroupe, $tampon);
return $resultat;
}
else if( $tmp == ',' ) {
// on a fini de lire le fils de gauche
$this->debug('inter ', $tampon,$flagNomGroupe );
$this->appendChild($resultat, $flagNomGroupe, $tampon);
$flagNomGroupe = 0;
$tampon = '';
}
else {
$tampon .= $tmp;
if ( $flagNomGroupe == 1 ){
$flagNomGroupe = 2;
}
}
$this->position++;
}
// ne doit pas s'afficher.
return $resultat;
}
function showGroups(){
$this->init();
$arbre = $this->parse();
$this->groupes = array(); //c'est pourquoi c'est privé.
$this->readGroups($arbre);
return $this->groupes;
}
private function readGroups( $racine ){
$feuillage = array(); // la liste de toute les feuilles issue d'une racine !
if ( is_array($racine) ) {
// on n'est pas sur une feuille
foreach ( $racine as $tronc => $branches ){
//echo 'tronc = ', $tronc,"\n";
$feuilles = $this->readGroups( $branches );
// d'abord on prépare ceci.
$feuillage = array_merge($feuillage, $feuilles);
// ensuite, chaque fois qu'on a autre chose qu'un noeud non nommé qui a un nom non automatique
// il se trouve que les noms "automatiques" sont des entiers alors que les noms explicites des chaines...
if ( is_string( $tronc) )$this->registerGroup($tronc, $feuilles);
}
}
else {
//on est sur une feuille,
// on ajoute une feuille et rien d'autre a faire !
$feuillage[]=self::readID($racine);//ici c'est pas vraiment une branche, c'est une feuille.
}
return $feuillage;
}
//protected pour autoriser la dérivation ...
static protected function readID($string){
$tab = explode(':',$string,2);
return $tab[0];
}
//protected pour autoriser la dérivation ...
protected function registerGroup($clee, $feuilles){
//print('registering '.$clee."\n");
$tab = explode(':',$clee,2);
if( count($tab) == 2 ){
$distance = $tab[1];
if( empty($tab[0]) ) {
$nom = 'Anonymous';
}
else{
$nom = $tab[0];
}
}else { //count == 1{
$distance = 'Unknown';
$nom = 'Anonymous';
}
$this->groupes[]= array(
'Nom'=> $nom,
'Apprenants' => count($feuilles), //??
'Distance' => $distance,
'ID' => $feuilles );//??
}
} |