Implementierung des Markov-Algorithmus

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Implementierung des Markov-Algorithmus

    Hi

    Ich habe den Markov-Algorithmus implementiert. Mich würde eure Meinung dazu interessieren.

    PHP-Code:
    <?php
       error_reporting
    (-1);
       
       
    mb_internal_encoding('UTF-8');
       
       class 
    Markov
       
    {
          private 
    $configurations = [];
          
          
    /**
           * @param null|string $phi wonach gesucht wird
           *    null, falls epsilon (leeres Wort)
           * @param string $psi wodurch ersetzt wird
           *    null, falls epsilon
           * @param int $i >= 0 Sprungziel für match
           * @param null|int $j Sprungziel für mismatch
           *    int >= 0
           *    null, falls es keinen mismatch geben kann
           */
          
    function addConfiguration($phi null$psi null$i$j null)
          {
             
    $this->configurations[] =
             [
                
    'phi' => $phi,
                
    'psi' => $psi,
                
    'i' => $i,
                
    'j' => $j
             
    ];
          }
          
    /**
           * @param string $word
           * @param int $k 0 <= k < count(configurations)
           * @return false|int >= 0 Position des ersten matches von phi in word.
           *    false, falls phi nicht in word gefunden.
           */
          
    private function getPos($word$k)
          {
             if(
    $this->configurations[$k]['phi'] === null)
             {
                return 
    0;
             }
             return 
    mb_strpos($word$this->configurations[$k]['phi']); // false oder int >= 0
          
    }
          
    /**
           * History-constrain: während run läuft, darf kein addConfiguration durchgeführt werden.
           * @param string $word
           * @return string
           * @throws Exception falls bei einem mismatch kein j definiert ist
           */
          
    function run($word)
          {
             
    $count count($this->configurations);
             
    $k 0;
             while(
    $k $count)
             {
                
    // Änderungsposition:
                
    $pos $this->getPos($word$k);
                if(
    $pos === false)
                {
                   
    // phi nicht gefunden 
                   
    $j $this->configurations[$k]['j'];
                   if(
    $j === null)
                   {
                      throw new 
    Exception('j');
                   }
                   
    $k $j;
                }
                else
                {
                   
    // phi gefunden
                   
    $psi $this->configurations[$k]['psi'];
                   if(
    $psi === null)
                   {
                      
    // phi löschen:
                      
    $word 
                         
    mb_substr($word0$pos).
                         
    mb_substr($word$pos mb_strlen($this->configurations[$k]['phi']));
                   }
                   else
                   {
                      
    // phi durch psi ersetzen:
                      
    $word 
                         
    mb_substr($word0$pos).
                         
    $psi.
                         
    mb_substr($word$pos mb_strlen($this->configurations[$k]['phi']));
                   }
                   
    $k $this->configurations[$k]['i'];
                }
             }
             return 
    $word;
          }
       }
       
       
    $m = new Markov();
       
    // Multiplikation im unären Zahlsystem
       /* 0 */ 
    $m->addConfiguration('*''**'1);
       
    /* 1 */ $m->addConfiguration(null'*'2);
       
    /* 2 */ $m->addConfiguration('**|''#**'36);
       
    /* 3 */ $m->addConfiguration('|#''#|'45);
       
    /* 4 */ $m->addConfiguration(null'|'3);
       
    /* 5 */ $m->addConfiguration('#'null2);
       
    /* 6 */ $m->addConfiguration('*|''*'67);
       
    /* 7 */ $m->addConfiguration('***'null8);
       
    $word $m->run('|||*||'); // 3 * 2 = 6 => ||||||
       
    var_dump($word);
    ?>
    Ich bin mir z. B. bei der Namensgebung zur Eigenschaft 'configurations' nicht sicher. Bei abstrakten Maschinen definiert K die Menge von Konfigurationen. Entspricht K der Markov-Tafel? Sind die Zeilen in der Markov-Tafel k ∈ K? Ich habe das so interpretiert und mich deshalb für 'configurations' entschieden. Stimmt das?

    Ach, und noch was: wo setzt man den Markov-Algorithmus praktisch ein?
    Zuletzt geändert von narutos; 30.09.2014, 18:50.
    Aufgrund Spams ignoriere ich h3ll.
Lädt...
X