www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Game.php (5390B)


      1 <?php
      2 
      3 require_once("db.php");
      4 
      5 class Game {
      6 
      7     private static $relationPhrases = array(
      8         0 => "%mc est en rapport avec %mn",
      9         5 => "%mc est un synonyme de %mn",
     10         6 => "%mc est une sorte de %mn",
     11         7 => "Un contraire de %mc est %mn",
     12         8 => "Un spécifique de %mc est %mn",
     13         9 => "%mn est une partie de %mc",
     14         10 => "%mc fait partie de %mn",
     15         13 => "Quoi/Qui pourrait %mc",
     16         15 => "Le lieu pour %mc est %mn",
     17         16 => "Un instrument pour %mc est %mn",
     18         17 => "Un caractéristique de %mc est %mn");
     19     private $db;
     20     private $centralWord;
     21     private $centralEID;
     22     private $centralPOSs;
     23     private $cloudEIDs;
     24     private $cloudWords;
     25 
     26     function __construct() {
     27         $this->db = getDB();
     28     }
     29 
     30     function setCentralWordWithEID($wordEID) {
     31         $this->centralEID = $wordEID;
     32     }
     33 
     34     function fetchRandomCentralEID() {
     35         $this->db = getDB();
     36         $query = "SELECT eid FROM random_center_node WHERE rowid
     37             = (abs(random()) % (SELECT max(rowid) FROM random_center_node))+1";
     38         $result = $this->db->querySingle($query, true);
     39         $this->centralEID = $result['eid'];
     40         $query = "SELECT name FROM node WHERE eid = $this->centralEID";
     41         $result = $this->db->querySingle($query, true);
     42         $this->centralWord = $result['name'];
     43         $this->centralPOSs = $this->getPOS($this->centralEID);
     44     }
     45 
     46     /**
     47      * Etant donné un mot passé en paramètre, on retourne le ou les POS.
     48      * Attention, parfois plusieurs réponses sont possibles : un mot
     49      * peut être à la fois un adjectif et un nom ou autre. Il y a quatre
     50      * POSs possible ici : adjectif, adverb, nom et verb qui sont représentées
     51      * par les strings 'adj', 'adv', 'nom' et 'verb' respectivement.
     52      * @param <type> $wordEID
     53      * @return string tableau de POS
     54      */
     55     function getPOS($wordEID) {
     56         $query = "SELECT end FROM relation WHERE type = 4 AND start = $wordEID;";
     57         $res = $this->db->query($query);
     58 
     59         $POSs = array();
     60         $cnt = 0;
     61         $adj = $adv = $nom = $ver = false;
     62 
     63         while ($tuple = $res->fetchArray()) {
     64             $endEID = $tuple['end'];
     65             $query = "SELECT name FROM node WHERE eid = $endEID";
     66             $res2 = $this->db->querySingle($query, true);
     67             $POSline = $res2['name'];
     68 
     69             if (preg_match("/^Adj:/", $POSline)) {
     70                 if ($adj == false) {
     71                     $POSs[$cnt] = "Adj";
     72                     $cnt++;
     73                     $adj = true;
     74                 }
     75             } else if (preg_match("/^Adv:/", $POSline)) {
     76                 if ($adv == false) {
     77                     $POSs[$cnt] = "Adv";
     78                     $cnt++;
     79                     $adv = true;
     80                 }
     81             } else if (preg_match("/^Nom:/", $POSline)) {
     82                 if ($nom == false) {
     83                     $POSs[$cnt] = "Nom";
     84                     $cnt++;
     85                     $nom = true;
     86                 }
     87             } else if (preg_match("/^Ver:/", $POSline)) {
     88                 if ($ver == false) {
     89                     $POSs[$cnt] = "Ver";
     90                     $cnt++;
     91                     $ver = true;
     92                 }
     93             }
     94         }
     95         $this->POSs = $POSs;
     96         return $POSs;
     97     }
     98 
     99     function getWordCloud($wordEID) {
    100 
    101 //  TODO: Find a way to enumerate indices used (to skip some)
    102         for ($i = 5; $i < 10; $i++) {
    103 
    104             $query = "SELECT end FROM relation WHERE type = $i AND start = $wordEID";
    105             $res = $this->db->query($query);
    106 
    107             $this->cloudEIDs[$i] = array();
    108             $this->cloudWords[$i] = array();
    109             $cnt = 0;
    110 
    111             while ($tuple = $res->fetchArray()) {
    112                 $eid = $this->cloudEIDs[$i][$cnt] = $tuple['end'];
    113                 $query = "SELECT name FROM node WHERE eid = $eid";
    114                 $res2 = $this->db->querySingle($query, true);
    115                 $this->cloudWords[$i][$cnt] = $res2['name'];
    116                 //echo "index " . $i . " | " . $cnt . ": ";
    117                 //echo $this->cloudWords[$i][$cnt] . "<br />";
    118                 $cnt++;
    119             }
    120         }
    121         return $this->cloudWords;
    122     }
    123 
    124     public function generateGame($wordEID) {
    125         $this->getPOS($wordEID);
    126         $this->getWordCloud($wordEID);
    127         return $this;
    128     }
    129 
    130     public function generateRandomGame() {
    131         $wordEID = $this->fetchRandomCentralEID();
    132         //$this->getPOS($wordEID);
    133         $this->getWordCloud($wordEID);
    134         echo $this->toString();
    135     }
    136 
    137     public function toString() {
    138         $s = "<dl>";
    139         //$s .= "<dt>Mot central</dt>" . "<dd>".$this->centralWord."</dd>";
    140 
    141         $s .= "<dt>POSs du mot central</dt>";
    142         echo "VARDULP : ";
    143         echo "<pre>";
    144         var_dump($this->centralPOSs);
    145         echo "</pre>";
    146         foreach ($this->centralPOSs AS $k => $v) {
    147             $s .= "<dd>" . $v . "</dd>";
    148         }
    149         foreach ($this->cloudWords AS $k1 => $v2) {
    150             echo "VARDUMP...V";
    151             var_dump($v) . "<br />";
    152             $s .= "<dt>Relation " . $v . "</dt>";
    153             foreach ($this->cloudWords[$v] AS $k2 => $v2) {
    154                 echo "VARDUMP... K2";
    155                 var_dump($k2) . "<br />";
    156                 $s .= "<dd>" . $this->cloudWords[$k1][$k2] . "<dd>";
    157             }
    158         }
    159         echo "</dl>";
    160     }
    161 }
    162 ?>