Paquetage TALN (loin d'être fini) mis sur GitHub (jc)
This commit is contained in:
parent
d3e051693c
commit
af8a73b53f
162
taln/Game.php
Normal file
162
taln/Game.php
Normal file
|
@ -0,0 +1,162 @@
|
|||
<?php
|
||||
|
||||
require_once("db.php");
|
||||
|
||||
class Game {
|
||||
|
||||
private static $relationPhrases = array(
|
||||
0 => "%mc est en rapport avec %mn",
|
||||
5 => "%mc est un synonyme de %mn",
|
||||
6 => "%mc est une sorte de %mn",
|
||||
7 => "Un contraire de %mc est %mn",
|
||||
8 => "Un spécifique de %mc est %mn",
|
||||
9 => "%mn est une partie de %mc",
|
||||
10 => "%mc fait partie de %mn",
|
||||
13 => "Quoi/Qui pourrait %mc",
|
||||
15 => "Le lieu pour %mc est %mn",
|
||||
16 => "Un instrument pour %mc est %mn",
|
||||
17 => "Un caractéristique de %mc est %mn");
|
||||
private $db;
|
||||
private $centralWord;
|
||||
private $centralEID;
|
||||
private $centralPOSs;
|
||||
private $cloudEIDs;
|
||||
private $cloudWords;
|
||||
|
||||
function __construct() {
|
||||
$this->db = getDB();
|
||||
}
|
||||
|
||||
function setCentralWordWithEID($wordEID) {
|
||||
$this->centralEID = $wordEID;
|
||||
}
|
||||
|
||||
function fetchRandomCentralEID() {
|
||||
$this->db = getDB();
|
||||
$query = "SELECT eid FROM random_center_node WHERE rowid
|
||||
= (abs(random()) % (SELECT max(rowid) FROM random_center_node))+1";
|
||||
$result = $this->db->querySingle($query, true);
|
||||
$this->centralEID = $result['eid'];
|
||||
$query = "SELECT name FROM node WHERE eid = $this->centralEID";
|
||||
$result = $this->db->querySingle($query, true);
|
||||
$this->centralWord = $result['name'];
|
||||
$this->centralPOSs = $this->getPOS($this->centralEID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Etant donné un mot passé en paramètre, on retourne le ou les POS.
|
||||
* Attention, parfois plusieurs réponses sont possibles : un mot
|
||||
* peut être à la fois un adjectif et un nom ou autre. Il y a quatre
|
||||
* POSs possible ici : adjectif, adverb, nom et verb qui sont représentées
|
||||
* par les strings 'adj', 'adv', 'nom' et 'verb' respectivement.
|
||||
* @param <type> $wordEID
|
||||
* @return string tableau de POS
|
||||
*/
|
||||
function getPOS($wordEID) {
|
||||
$query = "SELECT end FROM relation WHERE type = 4 AND start = $wordEID;";
|
||||
$res = $this->db->query($query);
|
||||
|
||||
$POSs = array();
|
||||
$cnt = 0;
|
||||
$adj = $adv = $nom = $ver = false;
|
||||
|
||||
while ($tuple = $res->fetchArray()) {
|
||||
$endEID = $tuple['end'];
|
||||
$query = "SELECT name FROM node WHERE eid = $endEID";
|
||||
$res2 = $this->db->querySingle($query, true);
|
||||
$POSline = $res2['name'];
|
||||
|
||||
if (preg_match("/^Adj:/", $POSline)) {
|
||||
if ($adj == false) {
|
||||
$POSs[$cnt] = "Adj";
|
||||
$cnt++;
|
||||
$adj = true;
|
||||
}
|
||||
} else if (preg_match("/^Adv:/", $POSline)) {
|
||||
if ($adv == false) {
|
||||
$POSs[$cnt] = "Adv";
|
||||
$cnt++;
|
||||
$adv = true;
|
||||
}
|
||||
} else if (preg_match("/^Nom:/", $POSline)) {
|
||||
if ($nom == false) {
|
||||
$POSs[$cnt] = "Nom";
|
||||
$cnt++;
|
||||
$nom = true;
|
||||
}
|
||||
} else if (preg_match("/^Ver:/", $POSline)) {
|
||||
if ($ver == false) {
|
||||
$POSs[$cnt] = "Ver";
|
||||
$cnt++;
|
||||
$ver = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->POSs = $POSs;
|
||||
return $POSs;
|
||||
}
|
||||
|
||||
function getWordCloud($wordEID) {
|
||||
|
||||
// TODO: Find a way to enumerate indices used (to skip some)
|
||||
for ($i = 5; $i < 10; $i++) {
|
||||
|
||||
$query = "SELECT end FROM relation WHERE type = $i AND start = $wordEID";
|
||||
$res = $this->db->query($query);
|
||||
|
||||
$this->cloudEIDs[$i] = array();
|
||||
$this->cloudWords[$i] = array();
|
||||
$cnt = 0;
|
||||
|
||||
while ($tuple = $res->fetchArray()) {
|
||||
$eid = $this->cloudEIDs[$i][$cnt] = $tuple['end'];
|
||||
$query = "SELECT name FROM node WHERE eid = $eid";
|
||||
$res2 = $this->db->querySingle($query, true);
|
||||
$this->cloudWords[$i][$cnt] = $res2['name'];
|
||||
//echo "index " . $i . " | " . $cnt . ": ";
|
||||
//echo $this->cloudWords[$i][$cnt] . "<br />";
|
||||
$cnt++;
|
||||
}
|
||||
}
|
||||
return $this->cloudWords;
|
||||
}
|
||||
|
||||
public function generateGame($wordEID) {
|
||||
$this->getPOS($wordEID);
|
||||
$this->getWordCloud($wordEID);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function generateRandomGame() {
|
||||
$wordEID = $this->fetchRandomCentralEID();
|
||||
//$this->getPOS($wordEID);
|
||||
$this->getWordCloud($wordEID);
|
||||
echo $this->toString();
|
||||
}
|
||||
|
||||
public function toString() {
|
||||
$s = "<dl>";
|
||||
//$s .= "<dt>Mot central</dt>" . "<dd>".$this->centralWord."</dd>";
|
||||
|
||||
$s .= "<dt>POSs du mot central</dt>";
|
||||
echo "VARDULP : ";
|
||||
echo "<pre>";
|
||||
var_dump($this->centralPOSs);
|
||||
echo "</pre>";
|
||||
foreach ($this->centralPOSs AS $k => $v) {
|
||||
$s .= "<dd>" . $v . "</dd>";
|
||||
}
|
||||
foreach ($this->cloudWords AS $k1 => $v2) {
|
||||
echo "VARDUMP...V";
|
||||
var_dump($v) . "<br />";
|
||||
$s .= "<dt>Relation " . $v . "</dt>";
|
||||
foreach ($this->cloudWords[$v] AS $k2 => $v2) {
|
||||
echo "VARDUMP... K2";
|
||||
var_dump($k2) . "<br />";
|
||||
$s .= "<dd>" . $this->cloudWords[$k1][$k2] . "<dd>";
|
||||
}
|
||||
}
|
||||
echo "</dl>";
|
||||
}
|
||||
}
|
||||
?>
|
530
taln/NodeTestTool.php
Normal file
530
taln/NodeTestTool.php
Normal file
|
@ -0,0 +1,530 @@
|
|||
<?php
|
||||
// Ctrl + F : TODO to find where to start working...
|
||||
/**
|
||||
*
|
||||
* TODO: analysis of relationships of relations :
|
||||
* - which are sometimes together ?
|
||||
* - which are never together ?
|
||||
* - why ?
|
||||
* - are there other interesting combinations possible ?
|
||||
* - are some always included in others ? ex. "synonym" and "related to"
|
||||
* -
|
||||
*
|
||||
* IDEAS:
|
||||
*
|
||||
* - function or class that gives more info on clourds :
|
||||
* ->make sure the same word dosn't appear twice
|
||||
* ->make sure the main word does't appear in cloud
|
||||
* ->filter to make all other words nouns, adg, etc.
|
||||
* ->word count for percentage of words possible (if few words, small cloud ; if lots
|
||||
* of words, big cloud
|
||||
* ->
|
||||
*/
|
||||
require_once("db.php");
|
||||
|
||||
class NodeTestTool {
|
||||
|
||||
//private static $typer1r2 = "type in ($r1, $r2)";
|
||||
//private static $banned_types = "4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001";
|
||||
|
||||
private static $relations = array(0 => "r_associated",
|
||||
5 => "r_syn", 6 => "r_isa", 7 => "r_anto", 8 => "r_hypo", 9 => "r_haspart",
|
||||
10 => "r_holo", 13 => "r_agent", 15 => "r_lieu", 16 => "r_inst", 17 => "r_carac");
|
||||
|
||||
/***********************GENERAL METHODS***********************************/
|
||||
|
||||
public static function getWordFromEID($eid) {
|
||||
$db = getDB();
|
||||
$query = "SELECT name FROM node WHERE eid = $eid";
|
||||
$result = $db->querySingle($query, true);
|
||||
if($result != null) return $result['name'];
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static function getEIDFromWord($word) {
|
||||
$db = getDB();
|
||||
$query = "SELECT eid FROM node WHERE name = '$word'";
|
||||
$result = $db->querySingle($query, true);
|
||||
if($result != null) return $result['eid'];
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static function isEIDInDB($eid) {
|
||||
$db = getDB();
|
||||
$query = "SELECT eid FROM node WHERE eid = $eid";
|
||||
$result = $db->querySingle($query, true);
|
||||
if ($result != null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isWordInDB($word) {
|
||||
$db = getDB();
|
||||
$query = "SELECT eid FROM node WHERE name = '$word'";
|
||||
$result = $db->querySingle($query, true);
|
||||
if ($result != null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isCentralEIDinDB($centralEID) {
|
||||
$db = getDB();
|
||||
$query = "SELECT eid FROM random_center_node WHERE eid = $centralEID";
|
||||
$result = $db->querySingle($query, true);
|
||||
if ($result != null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isCentralWordInDB($word) {
|
||||
$centralEID = NodeTestTool::getEIDFromWord($word);
|
||||
return NodeTestTool::isCentralEIDinDB($centralEID);
|
||||
}
|
||||
|
||||
public static function isCloudEIDInDB($cloudEID) {
|
||||
$db = getDB();
|
||||
$query = "SELECT eid FROM random_cloud_node WHERE eid = $cloudEID";
|
||||
$result = $db->querySingle($query, true);
|
||||
if ($result != null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isCloudWordInDB($cloudWord) {
|
||||
$db = getDB();
|
||||
$cloudEID = NodeTestTool::getEIDFromWord($cloudWord);
|
||||
return NodeTestTool::isCloudEIDinDB($cloudEID);
|
||||
}
|
||||
|
||||
|
||||
public static function getPOSsFromEID($eid) {
|
||||
$db = getDB();
|
||||
$query = "SELECT end FROM relation WHERE type = 4 AND start = $eid;";
|
||||
$res = $db->query($query);
|
||||
|
||||
$adj = $adv = $nom = $ver = false;
|
||||
$s = "";
|
||||
|
||||
while ($tuple = $res->fetchArray()) {
|
||||
$endEID = $tuple['end'];
|
||||
$query = "SELECT name FROM node WHERE eid = $endEID";
|
||||
$res2 = $db->querySingle($query, true);
|
||||
$POSline = $res2['name'];
|
||||
|
||||
if (preg_match("/^Adj:/", $POSline)) {
|
||||
if ($adj == false) {
|
||||
$s .= "Adj ";
|
||||
$adj = true;
|
||||
}
|
||||
} else if (preg_match("/^Adv:/", $POSline)) {
|
||||
if ($adv == false) {
|
||||
$s .= "Adv ";
|
||||
$adv = true;
|
||||
}
|
||||
} else if (preg_match("/^Nom:/", $POSline)) {
|
||||
if ($nom == false) {
|
||||
$s .= "Nom ";
|
||||
$nom = true;
|
||||
}
|
||||
} else if (preg_match("/^Ver:/", $POSline)) {
|
||||
if ($ver == false) {
|
||||
$s .= "Ver ";
|
||||
$ver = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $s;
|
||||
}
|
||||
|
||||
public static function getPOSsFromWord($word) {
|
||||
$eid = NodeTestTool::getEIDFromWord($word);
|
||||
$POSs = NodeTestTool::getPOSsFromEID($eid);
|
||||
return $POSs;
|
||||
}
|
||||
|
||||
|
||||
public static function getCloudEIDsFromCentralEIDAndRelNo($centralEID, $relNo) {
|
||||
if (NodeTestTool::isCentralEIDinDB($centralEID)) {
|
||||
$db = getDB();
|
||||
$query = "SELECT end FROM relation WHERE type = $relNo AND start = $centralEID";
|
||||
$res = $db->query($query);
|
||||
|
||||
$s = "";
|
||||
|
||||
while ($tuple = $res->fetchArray()) {
|
||||
$eid2 = $tuple['end'];
|
||||
$query = "SELECT eid FROM node WHERE eid = $eid2";
|
||||
$res2 = $db->querySingle($query, true);
|
||||
$s .= $res2['eid'] . ":";
|
||||
}
|
||||
if(strlen($s) > 0) $s = substr($s, 0, -1);
|
||||
return $s;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getCloudWordsFromCentralEIDAndRelNo($centralEID, $relNo) {
|
||||
if (NodeTestTool::isCentralEIDInDB($centralEID)) {
|
||||
$db = getDB();
|
||||
$query = "SELECT end FROM relation WHERE type = $relNo AND start = $centralEID";
|
||||
$res = $db->query($query);
|
||||
|
||||
$s = "";
|
||||
|
||||
while ($tuple = $res->fetchArray()) {
|
||||
$eid2 = $tuple['end'];
|
||||
$query = "SELECT name FROM node WHERE eid = $eid2";
|
||||
$res2 = $db->querySingle($query, true);
|
||||
$s .= $res2['name'] . ":";
|
||||
}
|
||||
if(strlen($s) > 0) $s = substr($s, 0, -1);
|
||||
return $s;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getCloudEIDsFromCentralWordAndRelNo($centralWord, $relNo) {
|
||||
$centralEID = NodeTestTool::getEIDFromWord($centralWord);
|
||||
if (NodeTestTool::isCentralEIDInDB($centralEID)) {
|
||||
return NodeTestTool::getCloudEIDsFromCentralEIDandRelNo($centralEID, $relNo);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getCloudWordsFromCentralWordAndRelNo($centralWord, $relNo) {
|
||||
if (NodeTestTool::isCentralWordInDB($centralWord)) {
|
||||
$centralEID = NodeTestTool::getEIDFromWord($centralWord);
|
||||
return NodeTestTool::getCloudWordsFromCentralEIDAndRelNo($centralEID, $relNo);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getAllCloudEIDsFromCentralEID($centralEID) {
|
||||
$s = "";
|
||||
foreach (NodeTestTool::$relations AS $relNo => $relName) {
|
||||
$cloud = NodeTestTool::getCloudEIDsFromCentralEIDAndRelNo($centralEID, $relNo);
|
||||
$s .= $relNo . "=>" . $relName . ": " . $cloud . "<br />";
|
||||
}
|
||||
return $s;
|
||||
}
|
||||
|
||||
public static function getAllCloudWordsFromCentralEID($centralEID) {
|
||||
$s = "";
|
||||
foreach (NodeTestTool::$relations AS $relNo => $relName) {
|
||||
$cloud = NodeTestTool::getCloudWordsFromCentralEIDAndRelNo($centralEID, $relNo);
|
||||
$s .= $relNo . "=>" . $relName . ": " . $cloud . "<br />";
|
||||
}
|
||||
return $s;
|
||||
}
|
||||
|
||||
public static function getAllCloudEIDsFromCentralWord($centralWord) {
|
||||
$s = "";
|
||||
foreach (NodeTestTool::$relations AS $relNo => $relName) {
|
||||
$cloud = NodeTestTool::getCloudEIDsFromCentralWordAndRelNo($centralWord, $relNo);
|
||||
$s .= $relNo . "=>" . $relName . ": " . $cloud . "<br />";
|
||||
}
|
||||
return $s;
|
||||
}
|
||||
|
||||
public static function getAllCloudWordsFromCentralWord($centralWord) {
|
||||
$s = "";
|
||||
foreach (NodeTestTool::$relations AS $relNo => $relName) {
|
||||
$cloud = NodeTestTool::getCloudWordsFromCentralWordAndRelNo($centralWord, $relNo);
|
||||
$s .= $relNo . "=>" . $relName . ": " . $cloud . "<br />";
|
||||
}
|
||||
return $s;
|
||||
}
|
||||
|
||||
|
||||
/*****************************RANDOM METHODS**************************/
|
||||
|
||||
public static function getRandomCentralEID() {
|
||||
$db = getDB();
|
||||
$query = "SELECT eid FROM random_center_node WHERE rowid =
|
||||
(ABS(RANDOM()) % (SELECT MAX(rowid) FROM random_center_node)) + 1";
|
||||
$res = $db->querySingle($query, true);
|
||||
if ($res != null)
|
||||
return $res['eid'];
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getRandomCentralWord() {
|
||||
$eid = NodeTestTool::getRandomCentralEID();
|
||||
return NodeTestTool::getWordFromEID($eid);
|
||||
}
|
||||
|
||||
public static function getAbsoluteRandomCloudEID() {
|
||||
$db = getDB();
|
||||
$query = "SELECT eid FROM random_cloud_node WHERE rowid =
|
||||
(ABS(RANDOM()) % (SELECT MAX(rowid) FROM random_center_node)) + 1";
|
||||
$res = $db->querySingle($query, true);
|
||||
if ($res != null)
|
||||
return $res['eid'];
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getAbsoluteRandomCloudWord() {
|
||||
$eid = NodeTestTool::getAbsoluteRandomCloudEID();
|
||||
return NodeTestTool::getWordFromEID($eid);
|
||||
}
|
||||
|
||||
public static function getRandomCloudEIDFromCentralEID($centralEID, $relNo){
|
||||
$s = NodeTestTool::getCloudEIDsFromCentralEIDAndRelNo($centralEID, $relNo);
|
||||
$ar = preg_split("/:/", $s);
|
||||
if(count($ar) > 0) return $ar[rand(0, count($ar)-1)];
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getRandomCloudWordFromCentralEID($centralEID, $relNo){
|
||||
$eid = NodeTestTool::getRandomCloudEIDFromCentralEID($centralEID, $relNo);
|
||||
if($eid != null) return NodeTestTool::getWordFromEID($eid);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getRandomCloudEIDFromCentralWord($centralWord, $relNo){
|
||||
$centralEID = NodeTestTool::getEIDFromWord($centralWord);
|
||||
if($centralEID != null) return NodeTestTool::getRandomCloudEIDFromCentralEID($centralEID, $relNo);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getRandomCloudWordFromCentralWord($centralWord, $relNo){
|
||||
$centralEID = NodeTestTool::getEIDFromWord($centralWord);
|
||||
if($centralEID != null) return NodeTestTool::getRandomCloudWordFromCentralEID($centralEID, $relNo);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getWordCloudDistance2($centralWord, $relNoDist1, $relNoDist2){
|
||||
$randomCloudWord = NodeTestTool::getRandomCloudWordFromCentralWord($centralWord, $relNoDist1);
|
||||
echo "Mot intermédiaire : " . $randomCloudWord . "<br />";
|
||||
if(strlen($randomCloudWord) > 0)
|
||||
return NodeTestTool::getCloudWordsFromCentralWordAndRelNo($randomCloudWord, $relNoDist2);
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* In the implementation of the function getWordCloudDistance2, 8 different
|
||||
* functions could be made corresponding to returning a word cloud or an
|
||||
* eid cloud, the naming would have to be changed to reflect the different
|
||||
* parameters and return values...
|
||||
*/
|
||||
|
||||
//TODO: FROM HERE DOWN ISN4T IN nodeTest01.php START FROM HERE !!
|
||||
|
||||
//getRandomCloudFromCentralEID($centralEID, $numWords)
|
||||
//getRandomCloudFromCentralWord($centralWord, $numWords)
|
||||
|
||||
|
||||
/* * *******************************WRONG OR DISTORTED CLOUD WORDS********* */
|
||||
|
||||
public static function getRandomCloudEIDofCloudEID($couldEID, $relNum) {
|
||||
|
||||
}
|
||||
|
||||
public static function getRandomCloudWordOfCloudWord($cloudWord, $relNum) {
|
||||
|
||||
}
|
||||
|
||||
/*************************ACCESSEURS************************/
|
||||
|
||||
public static function getRelations(){
|
||||
return NodeTestTool::$relations;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* * ***********************************GD****************************** */
|
||||
|
||||
/**
|
||||
* (gd) Voisins 1 saut du bon type (= relations déjà existantes)
|
||||
* @global <type> $typer1r2
|
||||
* @param <type> $r1
|
||||
* @param <type> $r2
|
||||
* @param <type> $centerEID
|
||||
*/
|
||||
public static function gd1JumpGoodType($r1, $r2, $centerEID) {
|
||||
global $typer1r2;
|
||||
$db = getDB();
|
||||
$query = "SELECT end AS eid, type = $r1 as r1, type = $r2 as r2,
|
||||
0 as r0, 0 as trash FROM relation WHERE start = $centerEID AND
|
||||
$typer1r2 order by random()";
|
||||
$result = $db->querySingle($query, true); //??
|
||||
return $result['end']; //??
|
||||
}
|
||||
|
||||
/**
|
||||
* (gd) Voisins 1 saut via r_associated (0), donc qu'on voudrait spécifier
|
||||
* si possible.
|
||||
* @param <type> $centerEID
|
||||
*/
|
||||
public static function gd1JumpViaRAssociated0($centerEID) {
|
||||
$db = getDB();
|
||||
$query = "SELECT end AS eid, 0.25 AS r1, 0.25 AS r2, 0.5 AS r0,
|
||||
0 AS trash FROM relation WHERE start = $centerEid AND
|
||||
type = 0 order by random()";
|
||||
$result = $db->querySingle($query, true); //??
|
||||
return $result['end']; //??
|
||||
}
|
||||
|
||||
/**
|
||||
* (gd) Voisins 1 saut via les autres relations
|
||||
* @global $banned_types
|
||||
* @param <type> $centerEid
|
||||
* @param <type> $r1
|
||||
* @param <type> $r2
|
||||
* @return <type>
|
||||
*/
|
||||
public static function gd1JumpViaOtherRelation($centerEid, $r1, $r2) {
|
||||
global $banned_types;
|
||||
$query = "SELECT end AS eid, 0.1 AS r1, 0.1 AS r2, 0.8 AS r0, 0 AS trash
|
||||
FROM relation WHERE start = $centerEid AND type NOT IN
|
||||
(0, $r1, $r2, $banned_types) ORDER BY random()";
|
||||
$result = $db->querySingle($query, true); //??
|
||||
return $result['end']; //??
|
||||
}
|
||||
|
||||
/**
|
||||
* (gd) Voisins 2 sauts, avec un mix de R1 et R2 pour les liens. Par ex
|
||||
* [ A -R1-> B -R2-> C ] ou bien [ A -R2-> B -R2-> C ]
|
||||
* Version optimisée de : "SELECT end AS eid FROM relation WHERE $typer1r2
|
||||
* AND start IN oneHopWithType ORDER BY random();"
|
||||
* @global $typer1r2
|
||||
* @param <type> $r1
|
||||
* @param <type> $r2
|
||||
* @param <type> $centerEid
|
||||
* @return <type>
|
||||
*/
|
||||
public static function gd2JumpWithMixR1R2ForLinks($r1, $r2, $centerEid) {
|
||||
global $typer1r2;
|
||||
$db = getDB();
|
||||
$query = "SELECT B.end AS eid, ((A.type = $r1) + (B.type = $r1)) / 3.
|
||||
AS r1, ((A.type = $r2) + (B.type = $r2)) / 3. AS r2, 1/6. AS r0, 1/6.
|
||||
AS trash FROM relation AS A, relation AS B WHERE A.start = $centerEid
|
||||
AND A.$typer1r2 AND B.start = A.end AND B.$typer1r2 ORDER BY random()";
|
||||
$result = $db->querySingle($query, true); //??
|
||||
return $result['end']; //??
|
||||
}
|
||||
|
||||
/**
|
||||
* (gd) Voisins 1 saut r1/r2 + 1 saut synonyme
|
||||
* Version optimisée de : "SELECT end AS eid FROM relation WHERE start IN
|
||||
* oneHopWithType AND type = 5 order by random()";
|
||||
* @global $typer1r2
|
||||
* @param <type> $r1
|
||||
* @param <type> $r2
|
||||
* @param <type> $centerEid
|
||||
* @return <type>
|
||||
*/
|
||||
public static function gd1JumpR1DivR2Plus1JumpSynonymOneHopWithType($r1, $r2, $centerEid) {
|
||||
global $typer1r2;
|
||||
$db = getDB();
|
||||
$query = "SELECT B.end AS eid, (A.type = $r1) * 0.75 as r1,
|
||||
(A.type = $r2) * 0.75 AS r2, 0.25 AS r0, 0 AS trash FROM relation AS A,
|
||||
relation AS B WHERE A.start = $centerEid AND A.$typer1r2 AND B.start = A.end
|
||||
AND B.type = 5 ORDER BY random()";
|
||||
//$result = $db->querySingle($query, true); //??
|
||||
//return $result['end']; //??
|
||||
}
|
||||
|
||||
/**
|
||||
* Version optimisée de : "SELECT end AS eid FROM relation WHERE start IN
|
||||
* (SELECT end FROM relation WHERE start = $centerEid AND type = 5) AND $typer1r2
|
||||
* ORDER BY random();"
|
||||
* @global $typer1r2
|
||||
* @param <type> $r1
|
||||
* @param <type> $r2
|
||||
* @param <type> $centerEid
|
||||
* @return <type>
|
||||
*/
|
||||
public static function gd1JumpR1DivR2Plus1JumpSynonym($r1, $r2, $centerEid) {
|
||||
global $typer1r2;
|
||||
$db = getDB();
|
||||
$query = "SELECT B.end AS eid, (B.type = $r1) * 0.75 as r1,
|
||||
(B.type = $r2) * 0.75 AS r2, 0.25 AS r0, 0 AS trash FROM relation AS A,
|
||||
relation AS B WHERE A.start = $centerEid AND A.type = 5 AND B.start = A.end
|
||||
AND B.$typer1r2 ORDER BY random()";
|
||||
//$result = $db->querySingle($query, true); //??
|
||||
//return $result['end']; //??
|
||||
}
|
||||
|
||||
/**
|
||||
* // Voisins 2 sauts (tous)
|
||||
* Version optimisée de : "SELECT end AS eid, 0.1 AS r1, 0.1 AS r2, 0.3 AS r0,
|
||||
* 0.5 AS trash FROM relation WHERE start IN (SELECT end FROM relation
|
||||
* WHERE start = $centerEid AND type NOT IN ($banned_types)) AND type NOT IN
|
||||
* ($banned_types) ORDER BY random();"
|
||||
* @global $banned_types $banned_types
|
||||
* @param <type> $centerEid
|
||||
* @param <type> $cloudSize
|
||||
* @return <type>
|
||||
*/
|
||||
public static function gd2JumpAll($centerEid, $cloudSize) {
|
||||
global $banned_types;
|
||||
$db = getDB();
|
||||
$query = "SELECT x AS eid, 0.1 AS r1, 0.1 AS r2, 0.3 AS r0, 0.5 AS trash
|
||||
FROM (SELECT x FROM (SELECT X.eid + Y.dumb AS x FROM (SELECT B.end AS eid
|
||||
FROM relation AS A, relation AS B WHERE A.type NOT IN ($banned_types)
|
||||
AND A.start = $centerEid AND B.type NOT IN ($banned_types) AND
|
||||
B.start = A.end LIMIT " . ($cloudSize * 4) . ") AS X, (SELECT 0 AS dumb)
|
||||
AS Y)) ORDER BY random()";
|
||||
//$result = $db->querySingle($query, true); //??
|
||||
//return $result['end']; //??
|
||||
}
|
||||
|
||||
/**
|
||||
* (gd) Centre pointe vers X, M pointe vers X aussi, on prend M.
|
||||
* Version optimisée de : "select start as eid from relation where end in (select end from relation where start = $centerEid) and type not in ($banned_types) order by random();"
|
||||
* Ce n'est toujours pas ça… : "select eid from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit 1) order by random();"
|
||||
* Tordu, mais ça marche \o/ . En fait il faut empêcher l'optimiseur de ramener le random avant le limit (et l'optimiseur est malin… :)
|
||||
* @global <type> $banned_types
|
||||
* @param <type> $cloudSize
|
||||
*/
|
||||
public static function gdXPointsToMMPointsToXTakeM($cloudSize) {
|
||||
global $banned_types;
|
||||
$db = getDB();
|
||||
$query = "SELECT x as eid, 0.1 as r1, 0.1 as r2, 0.2 as r0, 0.6 as trash
|
||||
FROM (SELECT x from (SELECT X.eid + Y.dumb AS x FROM (SELECT
|
||||
B.start AS eid FROM relation AS A, relation AS B WHERE A.type NOT IN
|
||||
($banned_types) AND A.start = $centerEid AND B.type NOT IN ($banned_types)
|
||||
AND B.end = A.end limit " . ($cloudSize * 4) . ") AS X, (SELECT 0 AS dumb) AS Y))
|
||||
ORDER BY random()";
|
||||
//$result = $db->querySingle($query, true); //??
|
||||
//return $result['end']; //??
|
||||
}
|
||||
|
||||
public static function gdGetGidFromGame() {
|
||||
return "select gid from game where gid = (abs(random()) % (select max(gid) from game))+1 or gid = (select max(gid) from game where gid > 0) order by gid limit 1;";
|
||||
}
|
||||
|
||||
// TODO Yoann : faire des tests d'erreur pour ces select ?
|
||||
public static function sqlGetGamesForId($gameId) {
|
||||
return "select gid, (select name from node where eid = eid_central_word) as name_central_word, eid_central_word, relation_1, relation_2 from game where gid = " . $gameId . ";";
|
||||
}
|
||||
|
||||
public static function sqlGetWordEidAndName($gameId) {
|
||||
return "select eid_word,(select name from node where eid=eid_word) as name_word from game_cloud where gid = " . $gameId . ";";
|
||||
}
|
||||
|
||||
public static function sqlGetInformationAboutGame($gameId) {
|
||||
return "select eid_word,(select name from node where eid=eid_word) as name_word, num, difficulty, totalWeight, probaR1, probaR2, probaR0, probaTrash from game_cloud where gid = " . $gameId . ";";
|
||||
}
|
||||
|
||||
public static function sqlGameIsOK($pgid, $gid, $user) {
|
||||
return "SELECT 'ok' FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user' and timestamp = -1;";
|
||||
}
|
||||
|
||||
public static function sqlGetScoreForUser($user) {
|
||||
return "SELECT score FROM user WHERE login='" . $user . "';";
|
||||
}
|
||||
|
||||
public static function sqlGetPlayedGameTime($pgid, $gid, $user) {
|
||||
return "SELECT timestamp FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user';";
|
||||
}
|
||||
|
||||
public static function sqlGetNumAndScoreFromGame($pgid, $gid) {
|
||||
return "SELECT num,score from played_game_cloud where pgid = $pgid and gid = $gid;";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
21
taln/Word.php
Normal file
21
taln/Word.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of Word
|
||||
*
|
||||
* @author John CHARRON
|
||||
*/
|
||||
class Word {
|
||||
private $word;
|
||||
private $eid;
|
||||
|
||||
function __construct($word, $eid){
|
||||
$this->word = $word;
|
||||
$this->eid = $eid;
|
||||
}
|
||||
}
|
||||
?>
|
25
taln/db.php
Executable file
25
taln/db.php
Executable file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
$getDBSingleton = null;
|
||||
|
||||
function getDB() {
|
||||
global $getDBSingleton;
|
||||
|
||||
if (!$getDBSingleton) {
|
||||
date_default_timezone_set('Europe/Paris');
|
||||
$SQL_DBNAME = (dirname(__FILE__) . "/db");
|
||||
if (!$getDBSingleton = new SQlite3($SQL_DBNAME)) {
|
||||
throw new Exception("Erreur lors de l'ouverture de la base de données SQLite3", 1);
|
||||
}
|
||||
}
|
||||
return $getDBSingleton;
|
||||
|
||||
}
|
||||
|
||||
function closeDB() {
|
||||
global $getDBSingleton;
|
||||
|
||||
if ($getDBSingleton) $getDBSingleton->close();
|
||||
}
|
||||
|
||||
?>
|
21
taln/index.php
Executable file
21
taln/index.php
Executable file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
require_once("NodeTestTool.php");
|
||||
/*
|
||||
* TODO: test each function entering bad data...
|
||||
*/
|
||||
$word = "maison";
|
||||
|
||||
|
||||
$relAr1 = NodeTestTool::getRelations();
|
||||
$relAr2 = NodeTestTool::getRelations();
|
||||
|
||||
foreach($relAr1 AS $k1 => $v1){
|
||||
foreach($relAr2 AS $k2 => $v2){
|
||||
echo "Mot : " . $word . " - " . $v1 . " - " . $v2 . " - " . "<br />";
|
||||
echo "Nuage : " . NodeTestTool::getWordCloudDistance2($word, $k1, $k2) . "<br /><br />";
|
||||
}
|
||||
echo "<br />";
|
||||
}
|
||||
|
||||
|
||||
?>
|
2
taln/nbproject/private/private.properties
Normal file
2
taln/nbproject/private/private.properties
Normal file
|
@ -0,0 +1,2 @@
|
|||
index.file=index.php
|
||||
url=http://localhost/~user/AlgoGenNuage01/
|
4
taln/nbproject/private/private.xml
Normal file
4
taln/nbproject/private/private.xml
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
|
||||
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
|
||||
</project-private>
|
7
taln/nbproject/project.properties
Normal file
7
taln/nbproject/project.properties
Normal file
|
@ -0,0 +1,7 @@
|
|||
include.path=${php.global.include.path}
|
||||
php.version=PHP_5
|
||||
source.encoding=UTF-8
|
||||
src.dir=.
|
||||
tags.asp=false
|
||||
tags.short=true
|
||||
web.root=.
|
9
taln/nbproject/project.xml
Normal file
9
taln/nbproject/project.xml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||
<type>org.netbeans.modules.php.project</type>
|
||||
<configuration>
|
||||
<data xmlns="http://www.netbeans.org/ns/php-project/1">
|
||||
<name>AlgoGenNuage01</name>
|
||||
</data>
|
||||
</configuration>
|
||||
</project>
|
113
taln/nodeTest01.php
Normal file
113
taln/nodeTest01.php
Normal file
|
@ -0,0 +1,113 @@
|
|||
<?php
|
||||
require_once("NodeTestTool.php");
|
||||
|
||||
$word = "chemin";
|
||||
$eid = 4455;
|
||||
$relNo = 0;
|
||||
echo '$word = ' . $word . "<br />";
|
||||
echo '$eid = ' . $eid . ' ($word and $eid DO NOT correspond here)' . "<br />";
|
||||
echo '$relNo = ' . $relNo . "<br />";
|
||||
echo "<br />";
|
||||
|
||||
echo 'NodeTestTool::getPOSsFromEID($eid) : ';
|
||||
echo NodeTestTool::getPOSsFromEID($eid);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getPOSsFromWord($word) : ';
|
||||
echo NodeTestTool::getPOSsFromWord($word);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::isEIDInDB($eid) : ';
|
||||
echo NodeTestTool::isEIDInDB($eid);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::isWordInDB($word) : ';
|
||||
echo NodeTestTool::isWordInDB($word);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::isCentralEIDinDB($eid) : ';
|
||||
echo NodeTestTool::isCentralEIDinDB($eid);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::isCentralWordInDB($word) : ';
|
||||
echo NodeTestTool::isCentralWordInDB($word);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::isCloudEIDInDB($eid) : ';
|
||||
echo NodeTestTool::isCloudEIDInDB($eid);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::isCloudWordInDB($word) : ';
|
||||
echo NodeTestTool::isCloudWordInDB($word);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getRandomCentralEID() : ';
|
||||
echo NodeTestTool::getRandomCentralEID();
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getRandomCentralWord() : ';
|
||||
echo NodeTestTool::getRandomCentralWord();
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getCloudEIDsFromCentralEIDAndRelNo($eid, $relNo) : ';
|
||||
echo NodeTestTool::getCloudEIDsFromCentralEIDAndRelNo($eid, $relNo);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getCloudWordsFromCentralEIDAndRelNo($eid, $relNo) : ';
|
||||
echo NodeTestTool::getCloudWordsFromCentralEIDAndRelNo($eid, $relNo);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getCloudEIDsFromCentralWordAndRelNo($word, $relNo) : ';
|
||||
echo NodeTestTool::getCloudEIDsFromCentralWordAndRelNo($word, $relNo);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getCloudWordsFromCentralWordAndRelNo($word, $relNo) : ';
|
||||
echo NodeTestTool::getCloudWordsFromCentralWordAndRelNo($word, $relNo);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getAllCloudEIDsFromCentralEID($eid) : <br />';
|
||||
echo NodeTestTool::getAllCloudEIDsFromCentralEID($eid);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getAllCloudWordsFromCentralEID($eid) : <br />';
|
||||
echo NodeTestTool::getAllCloudWordsFromCentralEID($eid);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getAllCloudEIDsFromCentralWord($word) : <br />';
|
||||
echo NodeTestTool::getAllCloudEIDsFromCentralWord($word);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getAllCloudWordsFromCentralWord($word) : <br />';
|
||||
echo NodeTestTool::getAllCloudWordsFromCentralWord($word);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getAbsoluteRandomCloudEID() : ';
|
||||
echo NodeTestTool::getAbsoluteRandomCloudEID();
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getAbsoluteRandomCloudWord() : ';
|
||||
echo NodeTestTool::getAbsoluteRandomCloudWord();
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getRandomCloudEIDFromCentralEID($eid, $relNo) : ';
|
||||
echo NodeTestTool::getRandomCloudEIDFromCentralEID($eid, $relNo);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getRandomCloudWordFromCentralEID($eid, $relNo) : ';
|
||||
echo NodeTestTool::getRandomCloudWordFromCentralEID($eid, $relNo);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getRandomCloudEIDFromCentralWord($word, $relNo) : ';
|
||||
echo NodeTestTool::getRandomCloudEIDFromCentralWord($word, $relNo);
|
||||
echo "<br /><br />";
|
||||
|
||||
echo 'NodeTestTool::getRandomCloudWordFromCentralWord($word, $relNo) : ';
|
||||
echo NodeTestTool::getRandomCloudWordFromCentralWord($word, $relNo);
|
||||
echo "<br /><br />";
|
||||
|
||||
$word2 = "joli";
|
||||
|
||||
echo "Antonyms of an antonym of " . $word2 . ": ";
|
||||
echo NodeTestTool::getWordCloudDistance2($word2, 5, 5);
|
||||
echo "<br /><br />";
|
||||
?>
|
21
taln/nodeTest02.php
Normal file
21
taln/nodeTest02.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
require_once("NodeTestTool.php");
|
||||
/*
|
||||
* TODO: test each function entering bad data...
|
||||
*/
|
||||
$word = "maison";
|
||||
|
||||
|
||||
$relAr1 = NodeTestTool::getRelations();
|
||||
$relAr2 = NodeTestTool::getRelations();
|
||||
|
||||
foreach($relAr1 AS $k1 => $v1){
|
||||
foreach($relAr2 AS $k2 => $v2){
|
||||
echo "Mot : " . $word . " - " . $v1 . " - " . $v2 . " - " . "<br />";
|
||||
echo "Nuage : " . NodeTestTool::getWordCloudDistance2($word, $k1, $k2) . "<br /><br />";
|
||||
}
|
||||
echo "<br />";
|
||||
}
|
||||
|
||||
|
||||
?>
|
676
taln/pticlic.php
Executable file
676
taln/pticlic.php
Executable file
|
@ -0,0 +1,676 @@
|
|||
<?php
|
||||
|
||||
require_once("relations.php");
|
||||
require_once("db.php");
|
||||
require_once("ressources/errors.inc");
|
||||
|
||||
/* Les prototypes des fonctions :
|
||||
* ===============================>
|
||||
* checkLogin($user, $passwd);
|
||||
* randomCenterNode();
|
||||
* randomCloudNode();
|
||||
* cgBuildResultSets($cloudSize, $centerEid, $r1, $r2);
|
||||
* cgChooseRelations();
|
||||
* cgBuildCloud($centerEid, $cloudSize, $sources, $sumWeights);
|
||||
* cgInsert($centerEid, $cloud, $r1, $r2, $totalDifficulty);
|
||||
* randomGameCore();
|
||||
* randomGame();
|
||||
* formatWord($word);
|
||||
* game2json($user, $gameId);
|
||||
* game2array($user, $gameId);
|
||||
* createGame($nbParties, $mode);
|
||||
* createGameCore($cloudSize);
|
||||
* getGame($user, $nbGames, $mode);
|
||||
* computeScore($probas, $difficulty, $answer, $userReputation);
|
||||
* computeUserReputation($score);
|
||||
* normalizeProbas($row);
|
||||
* setGame($user, $pgid, $gid, $answers);
|
||||
* get_game_relations();
|
||||
getGameRelationsJSON();
|
||||
* setGameGetScore($user, $pgid, $gid, $answers);
|
||||
* insertNode($node);
|
||||
* getNodeEid($node);
|
||||
*/
|
||||
|
||||
|
||||
/** Vérifie la validité du couple nom d'utilisateur / mot de passe.
|
||||
* @param user : Le nom d'utilisateur.
|
||||
* @param passwd : Le mot de passe.
|
||||
* @return boolean : true si OK sinon false.
|
||||
*/
|
||||
function checkLogin($user, $passwd) {
|
||||
$db = getDB();
|
||||
$hashPasswd = md5($passwd);
|
||||
$loginIsOk = ($hashPasswd == $db->querySingle(sqlGetPassword($user)));
|
||||
return $loginIsOk;
|
||||
}
|
||||
|
||||
/** Selectionne aléatoirement l'eid d'un mot central.
|
||||
* @return eid : Identifiant d'un mot central, NULL en cas d'erreur.
|
||||
*/
|
||||
function randomCenterNode()
|
||||
{
|
||||
$db = getDB();
|
||||
return $db->querySingle(sqlGetEIDCenterNode());
|
||||
}
|
||||
|
||||
/** Selectionne aléatoirement un noeud d'un nuage.
|
||||
* @return eid : L'identifiant du noeud.
|
||||
*/
|
||||
function randomCloudNode()
|
||||
{
|
||||
$db = getDB();
|
||||
return $db->querySingle(sqlGetEIRCloudNode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cloudSize : Taille du nuage.
|
||||
* @param centerEid : Identifiant du mot central.
|
||||
* @param r1 Type de la relation 1.
|
||||
* @param r2 Type de la relation 2.
|
||||
*/
|
||||
function cgBuildResultSets($cloudSize, $centerEid, $r1, $r2)
|
||||
{
|
||||
$db = getDB();
|
||||
// 'w' => weight (poids), 'd' => difficulté, 's' => select
|
||||
// Le select doit ranvoyer trois colonnes :
|
||||
// eid => l'eid du mot à mettre dans le nuage,
|
||||
// r1 => la probabilité pour que le mot soit dans r1, entre -1 et 1 (négatif = ne devrait pas y être, positif = devrait y être à coup sûr, 0 = on sait pas).
|
||||
|
||||
$sources = array(
|
||||
array('w'=>40, 'd'=>1, 's'=>sql1JumpGoodType($r1, $r2, $centerEid)),
|
||||
array('w'=>40, 'd'=>2, 's'=>sql1JumpViaRAssociated0($centerEid)),
|
||||
array('w'=>20, 'd'=>3.1, 's'=>sql1JumpViaOtherRelation($centerEid, $r1, $r2, $banned_types)),
|
||||
array('w'=>30, 'd'=>3.2, 's'=>sql2JumpWithMixR1R2ForLinks($r1, $r2, $centerEid)),
|
||||
array('w'=>20, 'd'=>5, 's'=>sql1JumpR1DivR2Plus1JumpSynonymOneHopWithType($r1, $r2, $centerEid)),
|
||||
array('w'=>20, 'd'=>6, 's'=>sql1JumpR1DivR2Plus1JumpSynonym($r1, $r2, $centerEid)),
|
||||
array('w'=>10, 'd'=>8, 's'=>sql2JumpAll($centerEid, $cloudSize)),
|
||||
array('w'=>10, 'd'=>8, 's'=>sqlXPointsToMMPointsToXTakeM($cloudSize)),
|
||||
'rand' => array('w'=>5, 'd'=>10, 's'=>false) // random. Le r1 et r2 de random sont juste en-dessous
|
||||
);
|
||||
|
||||
$sumWeights = 0;
|
||||
|
||||
foreach ($sources as $k => $x)
|
||||
{
|
||||
$sumWeights += $x['w'];
|
||||
$sources[$k]['rsPos'] = 0;
|
||||
$sources[$k]['rsSize'] = 0;
|
||||
|
||||
if ($x['s'] !== false)
|
||||
{
|
||||
$sources[$k]['resultSet'] = array();
|
||||
$res = $db->query($x['s']);
|
||||
$i = 0;
|
||||
|
||||
while ($i < 10 && $sources[$k]['resultSet'][] = $res->fetchArray())
|
||||
{
|
||||
$i++;
|
||||
$sources[$k]['rsSize']++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$sources[$k]['resultSet'] = array();
|
||||
|
||||
for ($i = 0; $i < 10; $i++)
|
||||
{
|
||||
$sources[$k]['resultSet'][] = array('eid'=>randomCloudNode(), 'r1'=>0, 'r2'=>0, 'r0'=>0, 'trash'=>1);
|
||||
$sources[$k]['rsSize']++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array($sources, $sumWeights);
|
||||
}
|
||||
|
||||
|
||||
/** Sélectionne aléatoirement deux relations.
|
||||
* @return array : Tableau avec la relation 1 et la relation 2.
|
||||
*/
|
||||
function cgChooseRelations()
|
||||
{
|
||||
$relations = array(5, 7, 9, 10);// /* Pas d'icônes pour celles-ci. */ 13, 14, 22);
|
||||
$r1 = rand(0,count($relations)-1);
|
||||
$r2 = rand(0,count($relations)-2);
|
||||
|
||||
if ($r2 >= $r1)
|
||||
$r2++;
|
||||
|
||||
$r1 = $relations[$r1];
|
||||
$r2 = $relations[$r2];
|
||||
|
||||
return array($r1, $r2);
|
||||
}
|
||||
|
||||
/** Génération d'un nuage pour un mot central.
|
||||
* @param cloudSize : Taille du nuage.
|
||||
* @param sources Les sources.
|
||||
* @param sumWeights La somme des poids.
|
||||
* @return array : Tableau avec comme premier élément le nuage et comme second élément le total de difficulté.
|
||||
*/
|
||||
function cgBuildCloud($centerEid, $cloudSize, $sources, $sumWeights)
|
||||
{
|
||||
$db = getDB();
|
||||
// On boucle tant qu'il n'y a pas eu au moins 2 sources épuisées
|
||||
$cloud = array();
|
||||
$nbFailed = 0;
|
||||
$i = 0;
|
||||
$totalDifficulty = 0;
|
||||
|
||||
while ($i < $cloudSize && $nbFailed < 10*$cloudSize)
|
||||
{
|
||||
// On choisit une source aléatoire en tennant compte des poids.
|
||||
$rands = rand(1,$sumWeights);
|
||||
$sumw = 0;
|
||||
if (!isset($sources['rand'])) {
|
||||
break;
|
||||
}
|
||||
$src = $sources['rand'];
|
||||
$srck = 'rand';
|
||||
|
||||
// Sélection d'une source aléatoire
|
||||
foreach ($sources as $k => $x)
|
||||
{
|
||||
$sumw += $x['w'];
|
||||
|
||||
if ($rands < $sumw)
|
||||
{
|
||||
$src = $x;
|
||||
$srck = $k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Vérification si on est à la fin du ResultSet de cette source.
|
||||
if ($src['rsPos'] >= $src['rsSize'])
|
||||
{
|
||||
$nbFailed++;
|
||||
|
||||
$sumWeights -= $src['w'];
|
||||
unset($sources[$srck]);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// On récupère un résultat de cette source.
|
||||
$res = $src['resultSet'][$src['rsPos']];
|
||||
$sources[$srck]['rsPos']++;
|
||||
|
||||
// On vérifie si le mot n'a pas déjà été sélectionné.
|
||||
$rejected = false;
|
||||
// Ne pas mettre le mot central dans le nuage.
|
||||
if ($res['eid'] == $centerEid) { continue; }
|
||||
$nodeName = $db->querySingle(sqlGetNameFromNode($res));
|
||||
if (substr($nodeName, 0, 2) == "::") { continue; }
|
||||
foreach ($cloud as $c) {
|
||||
if ($c['eid'] == $res['eid']) {
|
||||
$nbFailed++;
|
||||
$rejected = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($rejected) { continue; }
|
||||
|
||||
// position dans le nuage, difficulté, eid, probaR1, probaR2
|
||||
$totalDifficulty += $src['d'];
|
||||
$cloud[$i] = array('pos'=>$i++, 'd'=>$src['d'], 'eid'=>$res['eid'], 'probaR1'=>$res['r1'], 'probaR2'=>$res['r2'], 'probaR0'=>$res['r0'], 'probaTrash'=>$res['trash']);
|
||||
}
|
||||
|
||||
$res = $sources['rand']['resultSet'][0];
|
||||
|
||||
while ($i < $cloudSize)
|
||||
{
|
||||
$totalDifficulty += $sources['rand']['d'];
|
||||
$cloud[$i] = array('pos'=>$i++, 'd'=>$sources['rand']['d'], 'eid'=>randomCloudNode(), 'probaR1'=>$res['r1'], 'probaR2'=>$res['r2'], 'probaR0'=>$res['r0'], 'probaTrash'=>$res['trash']);
|
||||
}
|
||||
|
||||
return array($cloud, $totalDifficulty);
|
||||
}
|
||||
|
||||
|
||||
/** Insère la partie dans la base de données.
|
||||
* @param centerEid : Identifiant du mot central.
|
||||
* @param cloud : Le nuage.
|
||||
* @param r1 : Le type de la relation 1.
|
||||
* @param r2 : Le type de la relation 2.
|
||||
* @param totalDifficulty : La difficulté totale.
|
||||
*/
|
||||
function cgInsert($centerEid, $cloud, $r1, $r2, $totalDifficulty)
|
||||
{
|
||||
$db = getDB();
|
||||
// Insère dans la base une partie avec le mot central $centerEid, le nuage $cloud et les relations $r1 et $r2
|
||||
$db->exec("begin transaction;");
|
||||
$db->exec("INSERT INTO game(gid, eid_central_word, relation_1, relation_2, difficulty)
|
||||
VALUES (null, $centerEid, $r1, $r2, $totalDifficulty);");
|
||||
$gid = $db->lastInsertRowID();
|
||||
|
||||
$t = time();
|
||||
$db->exec("INSERT INTO played_game(pgid, gid, login, timestamp)
|
||||
VALUES (null, $gid, null, $t);");
|
||||
$pgid1 = $db->lastInsertRowID();
|
||||
$db->exec("INSERT INTO played_game(pgid, gid, login, timestamp)
|
||||
VALUES (null, $gid, null, $t);");
|
||||
$pgid2 = $db->lastInsertRowID();
|
||||
$db->exec("INSERT INTO played_game(pgid, gid, login, timestamp)
|
||||
VALUES (null, $gid, null, $t);");
|
||||
$pgid0 = $db->lastInsertRowID();
|
||||
$db->exec("INSERT INTO played_game(pgid, gid, login, timestamp)
|
||||
VALUES (null, $gid, null, $t);");
|
||||
$pgidT = $db->lastInsertRowID();
|
||||
|
||||
// TODO : R0 et Trash + corrections
|
||||
foreach ($cloud as $c)
|
||||
{
|
||||
$totalWeight = $c['probaR1'] + $c['probaR2'] + $c['probaR0'] + $c['probaTrash'];
|
||||
$db->exec("INSERT INTO game_cloud(gid, num, difficulty, eid_word, totalWeight, probaR1, probaR2, probaR0, probaTrash)
|
||||
VALUES ($gid, ".$c['pos'].", ".$c['d'].", ".$c['eid'].", $totalWeight, ".$c['probaR1'].", ".$c['probaR2'].", ".$c['probaR0'].", ".$c['probaTrash'].");");
|
||||
|
||||
$db->exec("INSERT INTO played_game_cloud(pgid, gid, type, num, relation, weight, score)
|
||||
VALUES ($pgid1, $gid, 0, ".$c['pos'].", $r1, ".$c['probaR1'].", 0);");
|
||||
|
||||
$db->exec("INSERT INTO played_game_cloud(pgid, gid, type, num, relation, weight, score)
|
||||
VALUES ($pgid2, $gid, 0, ".$c['pos'].", $r2, ".$c['probaR2'].", 0);");
|
||||
|
||||
$db->exec("INSERT INTO played_game_cloud(pgid, gid, type, num, relation, weight, score)
|
||||
VALUES ($pgid0, $gid, 0, ".$c['pos'].", 0, ".$c['probaR0'].", 0);");
|
||||
|
||||
$db->exec("INSERT INTO played_game_cloud(pgid, gid, type, num, relation, weight, score)
|
||||
VALUES ($pgidT, $gid, 0, ".$c['pos'].", -1, ".$c['probaTrash'].", 0);");
|
||||
}
|
||||
|
||||
$db->exec("commit;");
|
||||
}
|
||||
|
||||
/** Retourne un identifiant de partie aléatoire de la liste de parties jouables
|
||||
* @return gid : Identifiant de partie.
|
||||
*/
|
||||
function randomGameCore() {
|
||||
$db = getDB();
|
||||
return $db->querySingle(sqlGetGidFromGame());
|
||||
}
|
||||
|
||||
/** Sélection aléatoire d'une partie de la base de données parmis les parties à jouer.
|
||||
* @return gid : Identifiant de la partie selectionnée.
|
||||
*/
|
||||
function randomGame()
|
||||
{
|
||||
$gid = randomGameCore();
|
||||
|
||||
if ($gid === null) {
|
||||
// TODO : séparer ces créations de parties dans une fonction qui détecte le mode toussa.
|
||||
for ($i = 0; $i < 100; $i++)
|
||||
createGameCore(10);
|
||||
|
||||
$gid = randomGameCore();
|
||||
|
||||
if ($gid === null)
|
||||
errGetGame();
|
||||
}
|
||||
return $gid;
|
||||
}
|
||||
|
||||
/** Formatage des mots lorsqu'il y a des généralisations/spécifications par le symbole ">".
|
||||
* @param word : Le mot que l'on veut reformater.
|
||||
* @return word : le mot formaté.
|
||||
*/
|
||||
function formatWord($word) {
|
||||
$db = getDB();
|
||||
$res = "";
|
||||
$stack = array();
|
||||
|
||||
while (($pos = strpos($word, ">")) !== false) {
|
||||
$res .= substr($word,0,$pos) . " (";
|
||||
$eid = intval(substr($word,$pos+1));
|
||||
if ($eid == 0) { errFollowingPointer($word); }
|
||||
if (in_array($eid, $stack)) { errLoopDetected($word); }
|
||||
if (count($stack) > 10) { errTooMuchRecursion($word); }
|
||||
$stack[] = $eid;
|
||||
$word = $db->querySingle(sqlGetNameFromNodeWithEid($eid));
|
||||
}
|
||||
|
||||
$res .= $word;
|
||||
|
||||
for ($depth = count($stack); $depth > 0; $depth--)
|
||||
$res .= ')';
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/** Formate une partie en JSON en l'imprimant.
|
||||
* @param user : l'utilisateur.
|
||||
* @param gameId : L'identifiant d'une partie.
|
||||
*/
|
||||
function game2json($user, $gameId)
|
||||
{
|
||||
$db = getDB();
|
||||
// TODO : factoriser avec game2array() .
|
||||
// TODO : planter si la requête suivante échoue pour quelque raison que ce soit.
|
||||
$db->exec("INSERT INTO played_game(pgid, gid, login, timestamp) VALUES (null, ".$gameId.", '$user', -1);");
|
||||
$pgid = $db->lastInsertRowID();
|
||||
|
||||
$game = $db->query(sqlGetGamesForId($gameId));
|
||||
$game = $game->fetchArray();
|
||||
|
||||
$retstr = "";
|
||||
$retstr .= '{"gid":'.$gameId.',"pgid":'.$pgid.',"cat1":'.$game['relation_1'].',"cat2":'.$game['relation_2'].',"cat3":0,"cat4":-1,';
|
||||
$retstr .= '"center":{"id":'.$game['eid_central_word'].',"name":'.json_encode(''.formatWord($game['name_central_word'])).'},';
|
||||
$retstr .= '"cloudsize":10,"cloud":['; // TODO ! compter dynamiquement.
|
||||
|
||||
$res = $db->query(sqlGetWordEidAndName($gameId));
|
||||
$notfirst = false;
|
||||
|
||||
while ($x = $res->fetchArray())
|
||||
{
|
||||
if ($notfirst)
|
||||
$retstr .= ",";
|
||||
else
|
||||
$notfirst=true;
|
||||
|
||||
$retstr .= '{"id":'.$x['eid_word'].',"name":'.json_encode("".formatWord($x['name_word'])).'}';
|
||||
}
|
||||
|
||||
$retstr .= "]}";
|
||||
return $retstr;
|
||||
}
|
||||
|
||||
/** Récupère une partie sous forme de tableau.
|
||||
* @param db : descripteur de la bdd (obtenu avec getDB()).
|
||||
* @param user : Login de l'utilisateur demandant la partie.
|
||||
* @param gameId : L'identifiant d'une partie.
|
||||
*/
|
||||
function game2array($user, $gameId)
|
||||
{
|
||||
$db = getDB();
|
||||
// TODO : planter si la requête suivante échoue pour quelque raison que ce soit.
|
||||
$db->exec("INSERT INTO played_game(pgid, gid, login, timestamp) VALUES (null, ".$gameId.", '$user', -1);");
|
||||
$pgid = $db->lastInsertRowID();
|
||||
|
||||
// TODO Yoann : faire des tests d'erreur pour ces select ?
|
||||
$game = $db->query(sqlGetGamesForId($gameId));
|
||||
$game = $game->fetchArray();
|
||||
|
||||
$ret = array();
|
||||
$ret['gid'] = $gameId;
|
||||
$ret['pgid'] = $pgid;
|
||||
$ret['cat1'] = $game['relation_1'];
|
||||
$ret['cat2'] = $game['relation_2'];
|
||||
$ret['cat3'] = 0;
|
||||
$ret['cat4'] = -1;
|
||||
$ret['center'] = array('id' => $game['eid_central_word'], 'name' => formatWord($game['name_central_word']));
|
||||
$ret['cloud'] = array(); // TODO ! compter dynamiquement.
|
||||
|
||||
$res = $db->query(sqlGetInformationAboutGame($gameId));
|
||||
|
||||
while ($x = $res->fetchArray())
|
||||
{
|
||||
$ret['cloud'][$x['num']] = array(
|
||||
'id' => $x['eid_word'],
|
||||
'name' => formatWord($x['name_word']),
|
||||
'difficulty' => $x['difficulty'],
|
||||
'totalWeight' => $x['totalWeight'],
|
||||
'probaR1' => $x['probaR1'],
|
||||
'probaR2' => $x['probaR2'],
|
||||
'probaR0' => $x['probaR0'],
|
||||
'probaTrash' => $x['probaTrash'],
|
||||
'probas' => normalizeProbas($x)
|
||||
);
|
||||
}
|
||||
|
||||
$ret['cloudsize'] = count($ret['cloud']);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/** Création d'un lot de parties suivant un mode donnée.
|
||||
* @param nbParties : le nombre de parties à créer.
|
||||
* @param mode : Le mode de jeu pour les parties à créer.
|
||||
*/
|
||||
function createGame($nbParties, $mode)
|
||||
{
|
||||
for ($i = 0; $i < $nbParties; $i++)
|
||||
createGameCore(10);
|
||||
}
|
||||
|
||||
/** Génère une partie (mode normal pour l'instant) pour une certaine taille de nuage.
|
||||
* @param cloudSize : Taille du nuage.
|
||||
*
|
||||
* Est appelée par randomGame(), donc il faudra adapter quand on aura plusieurs modes, par exemple en ayant une fonction intermédiaire qui puisse être appelée par createGame et randomGame.
|
||||
*/
|
||||
function createGameCore($cloudSize)
|
||||
{
|
||||
// select random node
|
||||
$centerEid = randomCenterNode();
|
||||
|
||||
$r1 = cgChooseRelations(); $r2 = $r1[1]; $r1 = $r1[0];
|
||||
$sources = cgBuildResultSets($cloudSize, $centerEid, $r1, $r2); $sumWeights = $sources[1]; $sources = $sources[0];
|
||||
$cloud = cgBuildCloud($centerEid, $cloudSize, $sources, $sumWeights); $totalDifficulty = $cloud[1]; $cloud = $cloud[0];
|
||||
cgInsert($centerEid, $cloud, $r1, $r2, $totalDifficulty);
|
||||
}
|
||||
|
||||
/** Récupération d'une partie.
|
||||
* @param user : L'identifiant de l'utilisateur.
|
||||
* @param nbGames : Le nombre de parties à récupérer.
|
||||
* @param mode : Le mode de jeu des parties à récupérer.
|
||||
*/
|
||||
function getGame($user, $nbGames, $mode)
|
||||
{
|
||||
echo "[";
|
||||
|
||||
for ($i=0; $i < $nbGames; $i)
|
||||
{
|
||||
echo game2json($user, randomGame());
|
||||
|
||||
if ((++$i) < $nbGames)
|
||||
echo ",";
|
||||
}
|
||||
|
||||
echo "]";
|
||||
}
|
||||
|
||||
|
||||
function computeScore($probas, $difficulty, $answer, $userReputation) {
|
||||
// Calcul du score. Score = proba[réponse de l'utilisateur]*coeff1 - proba[autres reponses]*coeff2 + bonus
|
||||
// score = - proba[autres reponses]*coeff2
|
||||
// On aura donc -5 <= score <= 0
|
||||
$score = -5 * (($probas[0] + $probas[1] + $probas[2] + $probas[3]) - $probas[$answer]);
|
||||
|
||||
// score = proba[réponse de l'utilisateur]*coeff1 - proba[autres reponses]*coeff2
|
||||
// On aura donc -5 <= score <= 10
|
||||
$score += 10 * $probas[$answer];
|
||||
|
||||
// On est indulgent si la réponse est 3 (poubelle) :
|
||||
if ($answer == 3 && $score < 0) {
|
||||
$score = $score / 2;
|
||||
}
|
||||
|
||||
// Adapter le score en fonction de la réputation de l'utilisateur (quand il est jeune, augmenter le score pour le motiver).
|
||||
// On aura donc -5 <= score <= 15
|
||||
if ($score > 3) {
|
||||
$score += max(0, min(5, 5 - $userReputation));
|
||||
}
|
||||
|
||||
return round($score);
|
||||
}
|
||||
|
||||
/** Calcul de la réputation de l'utilisateur selon son score.
|
||||
* @param score : Le score du joueur.
|
||||
*/
|
||||
function computeUserReputation($score) {
|
||||
return max(round(log($score/10)*100)/100, 0);
|
||||
}
|
||||
|
||||
/** Formatage des probalitées dans un tableau.
|
||||
* @param row : le vecteur de probabilités.
|
||||
* @return array : Le vecteur de probabilités normalisé.
|
||||
*/
|
||||
function normalizeProbas($row) {
|
||||
return array($row['probaR1']/$row['totalWeight'], $row['probaR2']/$row['totalWeight'], $row['probaR0']/$row['totalWeight'], $row['probaTrash']/$row['totalWeight']);
|
||||
}
|
||||
|
||||
/** Insertion des données d'une partie joué par un utilisateur.
|
||||
* @param user : L'identifiant de l'utilisateur ayant joué à la partie.
|
||||
* @param pgid : L'identifiant de la partie jouée.
|
||||
* @param gid : L'identifiant du jeu auquel la partie appartient.
|
||||
* @return score : Le score réalisé par le joueur.
|
||||
*/
|
||||
function setGame($user, $pgid, $gid, $answers)
|
||||
{
|
||||
$db = getDB();
|
||||
if ('ok' !== $db->querySingle(sqlGameIsOK($pgid, $gid, $user))) {
|
||||
return getGameScores($user, $pgid, $gid);
|
||||
}
|
||||
|
||||
$userReputation = computeUserReputation($db->querySingle(sqlGetScoreForUser($user)));
|
||||
|
||||
$db->exec("begin transaction;");
|
||||
$db->exec("update played_game set timestamp = ".time()." where pgid = $pgid;");
|
||||
|
||||
$r0 = 0;
|
||||
$trash = -1;
|
||||
$r1 = $db->querySingle("SELECT relation_1, relation_2 FROM game WHERE gid = $gid;", true);
|
||||
$r2 = $r1['relation_2'];
|
||||
$r1 = $r1['relation_1'];
|
||||
$res = $db->query("SELECT num, difficulty, totalWeight, probaR1, probaR2, probaR0, probaTrash FROM game_cloud WHERE gid = $gid;");
|
||||
$gameScore = 0;
|
||||
$scores = array();
|
||||
$nbScores = 0;
|
||||
|
||||
while ($row = $res->fetchArray())
|
||||
{
|
||||
$num = intval($row['num']);
|
||||
$nbScores++;
|
||||
if (!isset($answers[$num])) {
|
||||
errSetPartiNoRelation($num);
|
||||
}
|
||||
$relanswer = intval($answers[$num]);
|
||||
|
||||
switch ($relanswer)
|
||||
{
|
||||
case $r1: $answer = 0; $probaRx = "probaR1"; break;
|
||||
case $r2: $answer = 1; $probaRx = "probaR2"; break;
|
||||
case $r0: $answer = 2; $probaRx = "probaR0"; break;
|
||||
case $trash: $answer = 3; $probaRx = "probaTrash"; break;
|
||||
default: errAnswerInvalidForWord($r1, $r2, $r0, $trash);
|
||||
}
|
||||
|
||||
$wordScore = computeScore(normalizeProbas($row), $row['difficulty'], $answer, $userReputation);
|
||||
$gameScore += $wordScore;
|
||||
$scores[$num] = $wordScore;
|
||||
|
||||
$db->exec("insert into played_game_cloud(pgid, gid, type, num, relation, weight, score) values($pgid, $gid, 1, $num, $r1, ".$userReputation.", ".$wordScore.");");
|
||||
$db->exec("update game_cloud set $probaRx = $probaRx + ".max(min($userReputation/2,5),0)." where gid = $gid and num = $num;");
|
||||
$db->exec("update game_cloud set totalWeight = totalWeight + ".max(min($userReputation/2,5),0)." where gid = $gid and num = $num;");
|
||||
}
|
||||
$db->exec("update user set score = score + ".$gameScore." where login = '$user';");
|
||||
|
||||
$db->exec("commit;");
|
||||
$scores['total'] = $gameScore;
|
||||
$scores['nb'] = $nbScores;
|
||||
$scores['alreadyPlayed'] = 'false';
|
||||
return $scores;
|
||||
}
|
||||
|
||||
function getGameScores($user, $pgid, $gid) {
|
||||
$db = getDB();
|
||||
$timestamp = $db->querySingle(sqlGetPlayedGameTime($pgid, $gid, $user));
|
||||
if ($timestamp == -1) {
|
||||
errGameNeverPlayed();
|
||||
} else if ($timestamp == null) {
|
||||
errGameNotAssociatedWithUser();
|
||||
}
|
||||
|
||||
$gameScore = 0;
|
||||
$scores = array();
|
||||
$nbScores = 0;
|
||||
$res = $db->query(sqlGetNumAndScoreFromGame($pgid, $gid));
|
||||
while ($row = $res->fetchArray())
|
||||
{
|
||||
$nbScores++;
|
||||
$gameScore += $row['score'];
|
||||
$scores[$row['num']] = $row['score'];
|
||||
}
|
||||
$scores['total'] = $gameScore;
|
||||
$scores['nb'] = $nbScores;
|
||||
$scores['alreadyPlayed'] = 'true';
|
||||
return $scores;
|
||||
}
|
||||
|
||||
/** Fourni l'ensembles des relations pouvant apparaître dans le jeu.
|
||||
* @return array : un tableau de realtions.
|
||||
*/
|
||||
function get_game_relations()
|
||||
{
|
||||
$reqlations = array();
|
||||
|
||||
$relations[] = 5;
|
||||
$relations[] = 7;
|
||||
$relations[] = 9;
|
||||
$relations[] = 10;
|
||||
$relations[] = 14;
|
||||
$relations[] = 22;
|
||||
|
||||
return $relations;
|
||||
}
|
||||
|
||||
function getGameRelationsJSON() {
|
||||
$json = "{";
|
||||
global $stringRelations;
|
||||
|
||||
foreach($stringRelations as $id=>$description)
|
||||
if($id == -1)
|
||||
$json .= '"'.$id.'":"'.$description.'"';
|
||||
else
|
||||
$json .= ',"'.$id.'":"'.$description.'"';
|
||||
|
||||
$json .= '}';
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
function setGameGetScore($user, $pgid, $gid, $answers) {
|
||||
$scores = setGame($user, intval($pgid), intval($gid), $answers);
|
||||
// On renvoie une nouvelle partie pour garder le client toujours bien alimenté.
|
||||
echo '{"scoreTotal":'.$scores['total'];
|
||||
echo ',"alreadyPlayed":'.$scores['alreadyPlayed'];
|
||||
echo ',"scores":[';
|
||||
for ($i = 0; $i < $scores['nb']; $i++) {
|
||||
if ($i != 0) echo ',';
|
||||
echo $scores[$i];
|
||||
}
|
||||
echo "],\"newGame\":";
|
||||
echo json_encode("".game2json($user, randomGame()));
|
||||
echo "}";
|
||||
}
|
||||
|
||||
/** Insère dans la base de données le noeud si il n'existe pas encore.
|
||||
* @param node : le noeud à ajouter.
|
||||
*/
|
||||
function insertNode($node) {
|
||||
$db = getDB();
|
||||
|
||||
if($db->querySingle(sqlGetEidFromNode($node)) == null) {
|
||||
$db->exec("INSERT INTO node(name,type,weight) VALUES('".SQLite3::escapeString($node)."',1,50);");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/** retourne l'eid d'un mot.
|
||||
* @param node : le mot dont on veut obtenir l'eid.
|
||||
*/
|
||||
|
||||
function getNodeEid($node) {
|
||||
$db = getDB();
|
||||
|
||||
return $db->querySingle(sqlGetEidFromNode($node));
|
||||
}
|
||||
|
||||
function wordExist($node) {
|
||||
$db = getDB();
|
||||
|
||||
return $db->querySingle("SELECT eid FROM node WHERE name='".SQLite3::escapeString($node)."';") ? true : false;
|
||||
}
|
||||
?>
|
46
taln/relations.php
Executable file
46
taln/relations.php
Executable file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
$stringRelations = array(
|
||||
-1 => "%mn n'est pas lié à %mc",
|
||||
0 => "%mc est en rapport avec %mn",
|
||||
//1 => "raffinement sémantique", // pas utilisé
|
||||
//2 => "raffinement morphologique", // pas utilisé
|
||||
//3 => "domaine", // pas utilisé
|
||||
//4 => "r_pos", // pas utilisé
|
||||
5 => "%mc est un synonyme de %mn",
|
||||
6 => "%mc est une sorte de %mn",
|
||||
7 => "Un contraire de %mc est %mn",
|
||||
8 => "Un spécifique de %mc est %mn",
|
||||
9 => "%mn est une partie de %mc",
|
||||
10 => "%mc fait partie de %mn",
|
||||
//11 => "locution", // pas utilisé
|
||||
//12 => "potentiel de FL", // pas utilisé
|
||||
13 => "Quoi/Qui pourrait %mc",
|
||||
//14 => "action>patient", // pas utilisé
|
||||
15 => "Le lieu pour %mc est %mn",
|
||||
16 => "Un instrument pour %mc est %mn",
|
||||
17 => "Un caractéristique de %mc est %mn",
|
||||
//18 => "r_data", // pas utilisé
|
||||
//19 => "r_lemma", // pas utilisé
|
||||
//20 => "magn", // pas utilisé
|
||||
//21 => "antimagn", // pas utilisé
|
||||
22 => "%mc est de la même famille que %mn",
|
||||
//29 => "predicat", // pas utilisé
|
||||
//30 => "lieu>action", // pas utilisé
|
||||
//31 => "action>lieu", // pas utilisé
|
||||
//32 => "sentiment", // pas utilisé
|
||||
//33 => "erreur", // pas utilisé
|
||||
//34 => "manière", // pas utilisé
|
||||
//35 => "sens/signification", // pas utilisé
|
||||
//36 => "information potentielle", // pas utilisé
|
||||
//37 => "rôle télique", // pas utilisé
|
||||
//38 => "rôle agentif", // pas utilisé
|
||||
//41 => "conséquence", // pas utilisé
|
||||
//42 => "cause", // pas utilisé
|
||||
//52 => "succession", // pas utilisé
|
||||
//53 => "produit", // pas utilisé
|
||||
//54 => "est le produit de", // pas utilisé
|
||||
//55 => "s'oppose à"
|
||||
);
|
||||
|
||||
?>
|
43
taln/ressources/errors.inc
Normal file
43
taln/ressources/errors.inc
Normal file
|
@ -0,0 +1,43 @@
|
|||
function errRequestIncomplete() {
|
||||
throw new Exception("La requête est incomplète", 1);
|
||||
}
|
||||
|
||||
function errUserUnknownOrBadPassword() {
|
||||
throw new Exception("Utilisateur non enregistré ou mauvais mot de passe", 2);
|
||||
}
|
||||
|
||||
function errGameNeverPlayed() {
|
||||
throw new Exception("Cette partie n'a jamais été jouée.", 3); // TODO : code d'erreur en doublon avec celui ci-dessous.
|
||||
}
|
||||
|
||||
function errGameNotAssociatedWithUser() {
|
||||
throw new Exception("Cette partie n'est pas associée à votre nom d'utilisateur.", 4);
|
||||
}
|
||||
|
||||
function errAnswerInvalidForWord($r1, $r2, $r0, $trash) {
|
||||
throw new Exception("Réponse ($relanswer) invalide pour le mot $num. Les réponses possibles sont : $r1, $r2, $r0, $trash", 5);
|
||||
}
|
||||
|
||||
function errSetPartiNoRelation($num) {
|
||||
throw new Exception("Cette requête \"Set partie\" ne donne pas de réponse (une relation) pour le mot numéro $num de la partie.", 6);
|
||||
}
|
||||
|
||||
function errGetGame() {
|
||||
throw new Exception("Erreur lors de la récupération de la partie. Vérifiez qu'il y a au moins une partie.", 7);
|
||||
}
|
||||
|
||||
function errFollowingPointer($word) {
|
||||
throw new Exception("Erreur lors du suivi des pointeurs de spécialisation du mot $word.", 8); }
|
||||
}
|
||||
|
||||
function errLoopDetected($word) {
|
||||
throw new Exception("Boucle rencontrée lors du suivi des pointeurs de spécialisation du mot $word.", 9); }
|
||||
}
|
||||
|
||||
function errTooMuchRecursion($word) {
|
||||
throw new Exception("Trop de niveaux de récursions lors du suivi des pointeurs de spécialisation du mot $word.", 10); }
|
||||
}
|
||||
|
||||
function errUnknownCommand() {
|
||||
throw new Exception("Commande inconnue", 11);
|
||||
}
|
120
taln/ressources/sql.inc
Normal file
120
taln/ressources/sql.inc
Normal file
|
@ -0,0 +1,120 @@
|
|||
<?php //$typer1r2 = "type in ($r1, $r2)";
|
||||
$banned_types = "4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001";
|
||||
|
||||
function sqlGetPassword($user) {
|
||||
return "SELECT hash_passwd FROM user WHERE login='".$user."';";
|
||||
}
|
||||
|
||||
function sqlGetEIDCenterNode() {
|
||||
return "select eid from random_center_node where rowid = (abs(random()) % (select max(rowid) from random_center_node))+1;";
|
||||
}
|
||||
|
||||
function sqlGetEIRCloudNode() {
|
||||
return "select eid from random_cloud_node where rowid = (abs(random()) % (select max(rowid) from random_cloud_node))+1;";
|
||||
}
|
||||
|
||||
// Voisins 1 saut du bon type (= relations déjà existantes)
|
||||
function sql1JumpGoodType($r1, $r2, $centerEid) {
|
||||
global $typer1r2;
|
||||
|
||||
return "select end as eid, type = $r1 as r1, type = $r2 as r2, 0 as r0, 0 as trash from relation where start = $centerEid and $typer1r2 order by random();";
|
||||
}
|
||||
|
||||
// Voisins 1 saut via r_associated (0), donc qu'on voudrait spécifier si possible.
|
||||
function sql1JumpViaRAssociated0($centerEid) {
|
||||
return "select end as eid, 0.25 as r1, 0.25 as r2, 0.5 as r0, 0 as trash from relation where start = $centerEid and type = 0 order by random();";
|
||||
}
|
||||
|
||||
// Voisins 1 saut via les autres relations
|
||||
function sql1JumpViaOtherRelation($centerEid, $r1, $r2) {
|
||||
global $banned_types;
|
||||
|
||||
return "select end as eid, 0.1 as r1, 0.1 as r2, 0.8 as r0, 0 as trash from relation where start = $centerEid and type not in (0, $r1, $r2, $banned_types) order by random();";
|
||||
}
|
||||
|
||||
// Voisins 2 sauts, avec un mix de R1 et R2 pour les liens. Par ex [ A -R1-> B -R2-> C ] ou bien [ A -R2-> B -R2-> C ]
|
||||
// Version optimisée de : "select end as eid from relation where $typer1r2 and start in oneHopWithType order by random();"
|
||||
function sql2JumpWithMixR1R2ForLinks($r1, $r2, $centerEid) {
|
||||
global $typer1r2;
|
||||
|
||||
return "select B.end as eid, ((A.type = $r1) + (B.type = $r1)) / 3. as r1, ((A.type = $r2) + (B.type = $r2)) / 3. as r2, 1/6. as r0, 1/6. as trash from relation as A, relation as B where A.start = $centerEid and A.$typer1r2 and B.start = A.end and B.$typer1r2 order by random();";
|
||||
}
|
||||
|
||||
// Voisins 1 saut r1/r2 + 1 saut synonyme
|
||||
// Version optimisée de : "select end as eid from relation where start in oneHopWithType and type = 5 order by random()";
|
||||
function sql1JumpR1DivR2Plus1JumpSynonymOneHopWithType($r1, $r2, $centerEid) {
|
||||
global $typer1r2;
|
||||
|
||||
return "select B.end as eid, (A.type = $r1) * 0.75 as r1, (A.type = $r2) * 0.75 as r2, 0.25 as r0, 0 as trash from relation as A, relation as B where A.start = $centerEid and A.$typer1r2 and B.start = A.end and B.type = 5 order by random();";
|
||||
}
|
||||
|
||||
// Version optimisée de : "select end as eid from relation where start in (select end from relation where start = $centerEid and type = 5) and $typer1r2 order by random();"
|
||||
function sql1JumpR1DivR2Plus1JumpSynonym($r1, $r2, $centerEid) {
|
||||
global $typer1r2;
|
||||
|
||||
return "select B.end as eid, (B.type = $r1) * 0.75 as r1, (B.type = $r2) * 0.75 as r2, 0.25 as r0, 0 as trash from relation as A, relation as B where A.start = $centerEid and A.type = 5 and B.start = A.end and B.$typer1r2 order by random();";
|
||||
}
|
||||
|
||||
// Voisins 2 sauts (tous)
|
||||
// Version optimisée de : "select end as eid, 0.1 as r1, 0.1 as r2, 0.3 as r0, 0.5 as trash from relation where start in (select end from relation where start = $centerEid and type not in ($banned_types)) and type not in ($banned_types) order by random();"
|
||||
function sql2JumpAll($centerEid, $cloudSize) {
|
||||
global $banned_types;
|
||||
|
||||
return "select x as eid, 0.1 as r1, 0.1 as r2, 0.3 as r0, 0.5 as trash from (select x from (select X.eid + Y.dumb as x from (select B.end as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.start = A.end limit ".($cloudSize*4).") as X, (select 0 as dumb) as Y)) order by random();";
|
||||
}
|
||||
|
||||
// Centre pointe vers X, M pointe vers X aussi, on prend M.
|
||||
// Version optimisée de : "select start as eid from relation where end in (select end from relation where start = $centerEid) and type not in ($banned_types) order by random();"
|
||||
// Ce n'est toujours pas ça… : "select eid from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit 1) order by random();"
|
||||
// Tordu, mais ça marche \o/ . En fait il faut empêcher l'optimiseur de ramener le random avant le limit (et l'optimiseur est malin… :)
|
||||
function sqlXPointsToMMPointsToXTakeM($cloudSize) {
|
||||
global $banned_types;
|
||||
|
||||
return "select x as eid, 0.1 as r1, 0.1 as r2, 0.2 as r0, 0.6 as trash from (select x from (select X.eid + Y.dumb as x from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit ".($cloudSize*4).") as X, (select 0 as dumb) as Y)) order by random();";
|
||||
}
|
||||
|
||||
function sqlGetNameFromNode($res) {
|
||||
return "select name from node where eid=".$res['eid'].";";
|
||||
}
|
||||
|
||||
function sqlGetGidFromGame() {
|
||||
return "select gid from game where gid = (abs(random()) % (select max(gid) from game))+1 or gid = (select max(gid) from game where gid > 0) order by gid limit 1;";
|
||||
}
|
||||
|
||||
function sqlGetNameFromNodeWithEid($eid) {
|
||||
return "select name from node where eid = $eid";
|
||||
}
|
||||
|
||||
// TODO Yoann : faire des tests d'erreur pour ces select ?
|
||||
function sqlGetGamesForId($gameId) {
|
||||
return "select gid, (select name from node where eid = eid_central_word) as name_central_word, eid_central_word, relation_1, relation_2 from game where gid = ".$gameId.";";
|
||||
}
|
||||
|
||||
function sqlGetWordEidAndName($gameId) {
|
||||
return "select eid_word,(select name from node where eid=eid_word) as name_word from game_cloud where gid = ".$gameId.";";
|
||||
}
|
||||
|
||||
function sqlGetInformationAboutGame($gameId) {
|
||||
return "select eid_word,(select name from node where eid=eid_word) as name_word, num, difficulty, totalWeight, probaR1, probaR2, probaR0, probaTrash from game_cloud where gid = ".$gameId.";";
|
||||
}
|
||||
|
||||
function sqlGameIsOK($pgid, $gid, $user) {
|
||||
return "SELECT 'ok' FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user' and timestamp = -1;";
|
||||
}
|
||||
|
||||
function sqlGetScoreForUser($user) {
|
||||
return "SELECT score FROM user WHERE login='".$user."';";
|
||||
}
|
||||
|
||||
function sqlGetPlayedGameTime($pgid, $gid, $user) {
|
||||
return "SELECT timestamp FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user';";
|
||||
}
|
||||
|
||||
function sqlGetNumAndScoreFromGame($pgid, $gid) {
|
||||
return "SELECT num,score from played_game_cloud where pgid = $pgid and gid = $gid;";
|
||||
}
|
||||
|
||||
function sqlGetEidFromNode($node) {
|
||||
return "SELECT eid FROM node WHERE name='".SQLite3::escapeString($node)."'";
|
||||
}
|
||||
?>
|
59
taln/test.php
Normal file
59
taln/test.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
require_once("db.php");
|
||||
require_once("NodeTools.php");
|
||||
?>
|
||||
|
||||
<html>
|
||||
<head></head>
|
||||
<body>
|
||||
<?php
|
||||
$db = getDB();
|
||||
//var_dump($db->querySingle('SELECT * FROM user'));
|
||||
//echo "<br /> ------------------<br />";
|
||||
//print_r($db->querySingle('SELECT * FROM user', true));
|
||||
|
||||
/*
|
||||
$res = $db->query('SELECT * FROM type_relation');
|
||||
echo "<br /><br />";
|
||||
while ($x = $res->fetchArray()){
|
||||
echo $x['name'];
|
||||
echo "<br />";
|
||||
}
|
||||
*/
|
||||
|
||||
//echo "<br /><br />";
|
||||
//echo "**************";
|
||||
$nt = new NodeTools();
|
||||
//$wordEID = $nt->fetchRandomCentralEID();
|
||||
|
||||
//$nt->generateRandomGame();
|
||||
|
||||
//echo $nt->getPOS(35798);
|
||||
//$nt->getWordCloud(35798);
|
||||
//echo $nt->toString();
|
||||
|
||||
//echo "<br /><br />";
|
||||
//echo "**************";
|
||||
|
||||
echo "<br />Part of Speech Experiment No. 1<br />";
|
||||
$POS = $nt->getPOS(35798);
|
||||
foreach($POS AS $k => $v){
|
||||
echo "POS value: ".$v."<br />";
|
||||
}
|
||||
|
||||
echo "<br />Word Cloud Experiment No. 1<br />";
|
||||
$cloudWords = $nt->getWordCloud(35798);
|
||||
foreach($cloudWords AS $k => $v){
|
||||
foreach($cloudWords[$k] AS $k2 => $v2){
|
||||
echo "Relation No. ".$k.": ".$cloudWords[$k][$k2]."<br />";
|
||||
}
|
||||
}
|
||||
|
||||
echo "<br />Experiment No. 2<br />";
|
||||
$nt2 = new NodeTools();
|
||||
$ndObject = $nt2->generateGame(35798);
|
||||
echo $ndObject->toString();
|
||||
|
||||
?>
|
||||
</body>
|
||||
</html>
|
46
taln/whiteboard.php
Normal file
46
taln/whiteboard.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
require_once("NodeTools.php");
|
||||
|
||||
|
||||
$node = NodeTools::getRandomCenterEID();
|
||||
echo $node;
|
||||
echo "<br />";
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
<html>
|
||||
<form name="input" action="whiteboard.php" method="get">
|
||||
Mot central : <input type="text" name="word" />
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
|
||||
<?php
|
||||
|
||||
if(isset($_GET['word'])){
|
||||
$word = $_GET['word'];
|
||||
unset($_GET['word']);
|
||||
$wordInDB = NodeTools::isWordInDB($word);
|
||||
if(!$wordInDB) echo "<p>ERREUR : le mot '$word' n'est pas dans la base de données !</p>";
|
||||
else echo "<p>Le mot '$word' est bien dans la base de données !</p>";
|
||||
echo "Central word: " . $_GET['word']."<br />";
|
||||
//echo "Parts of speech: "
|
||||
}
|
||||
|
||||
$myEID = 4743;
|
||||
$myWord = "chat";
|
||||
$word = NodeTools::getWordFromEID($myEID);
|
||||
echo $word . "<br />";
|
||||
$eid = NodeTools::getEIDFromWord($myWord);
|
||||
echo $eid. "<br />";
|
||||
$pos = NodeTools::getPOSsFromEID($myEID);
|
||||
echo $pos . "<br />";
|
||||
$cloud = NodeTools::getCloudFromEID($myEID, 0);
|
||||
echo $cloud . "<br />";
|
||||
$allClouds = NodeTools::getAllCloudsFromEID($myEID);
|
||||
echo $allClouds . "<br />";
|
||||
$allClouds2 = NodeTools::getAllCloudsFromWord($myWord);
|
||||
echo $allClouds2 . "<br />";
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user