[dev] [commit] r991 - phplib/models wwwbase/ajax

automailer at dexonline.ro automailer at dexonline.ro
Fri Sep 20 10:23:38 EEST 2013


Author: alex.grigoras
Date: Fri Sep 20 10:23:38 2013
New Revision: 991

Log:
Added feature that selects related words for the Mill options; Fixes #288

Modified:
   phplib/models/NGram.php
   wwwbase/ajax/mill.php

Modified: phplib/models/NGram.php
==============================================================================
--- phplib/models/NGram.php	Thu Sep 19 08:40:51 2013	(r990)
+++ phplib/models/NGram.php	Fri Sep 20 10:23:38 2013	(r991)
@@ -21,20 +21,8 @@
 
   public static function searchNGram($cuv) {
     $leng = mb_strlen($cuv);
-    $ngramList = self::split($cuv);
-    $hash = array();
-    foreach($ngramList as $i => $ngram) {
-      $lexemIdList = db_getArray(sprintf("select lexemId from NGram where ngram = '%s' and pos between %d and %d",
-                                         $ngram, $i - self::$MAX_MOVE, $i + self::$MAX_MOVE));
-      $lexemIdList = array_unique($lexemIdList);
-      foreach($lexemIdList as $lexemId) {
-        if (!isset($hash[$lexemId])) {
-          $hash[$lexemId] = 1;
-        } else {
-          $hash[$lexemId]++;
-        }
-      }
-    }
+    
+    $hash = NGram::searchLexemIds($cuv);
     arsort($hash);
     $max = current($hash);
     $lexIds = array_keys($hash,$max);
@@ -60,6 +48,26 @@
 
     return $results;
   }
+  
+  public static function searchLexemIds($cuv)
+  {
+    $ngramList = self::split($cuv);
+    $hash = array();
+    foreach($ngramList as $i => $ngram) {
+      $lexemIdList = db_getArray(sprintf("select lexemId from NGram where ngram = '%s' and pos between %d and %d",
+                                         $ngram, $i - self::$MAX_MOVE, $i + self::$MAX_MOVE));
+      $lexemIdList = array_unique($lexemIdList);
+      foreach($lexemIdList as $lexemId) {
+        if (!isset($hash[$lexemId])) {
+          $hash[$lexemId] = 1;
+        } else {
+          $hash[$lexemId]++;
+        }
+      }
+    }
+    
+    return $hash;
+  }
 }
 
 ?>

Modified: wwwbase/ajax/mill.php
==============================================================================
--- wwwbase/ajax/mill.php	Thu Sep 19 08:40:51 2013	(r990)
+++ wwwbase/ajax/mill.php	Fri Sep 20 10:23:38 2013	(r991)
@@ -28,6 +28,22 @@
     return $word->lexicon;
 }
 
+function getSimpleDefinitionsForLexemIds($lexemIds)
+{
+    $defIds = Model::factory('LexemDefinitionMap')
+            ->select('DefinitionId')
+            ->distinct()
+            ->where_in('LexemId', $lexemIds)
+            ->find_many();
+    $defIds = array_map(function ($def){return $def->DefinitionId;}, $defIds);
+    
+    $defs = Model::factory('DefinitionSimple')
+            ->where_in('DefinitionId',$defIds)
+            ->find_many();
+    
+    return $defs;
+}
+
 $difficulty = util_getRequestParameterWithDefault('d', 0);
 $logAnswerId = util_getRequestParameterWithDefault('answerId', 0);
 $logGuessed = util_getRequestParameterWithDefault('guessed', 0);
@@ -58,16 +74,51 @@
 $options[$answer]['text'] = $maindef->getDisplayValue();
 $used[$maindef->definitionId] = 1;
 
+$closestLexemsDefinitionsCount = null;
+$closestLexemsDefinitions = null;
+if ($difficulty > 1)
+{
+  $nearLexemIds = NGram::searchLexemIds($word);
+  arsort($nearLexemIds);
+  $lexemPoolSize = 48/$difficulty;
+  $closestLexemIds = array_slice($nearLexemIds, 0, $lexemPoolSize, true);
+  $closestLexemIds = array_keys($closestLexemIds);
+  
+  $closestLexemsDefinitions = getSimpleDefinitionsForLexemIds($closestLexemIds);
+  $closestLexemsDefinitionsCount = count($closestLexemsDefinitions);
+  
+  //if there are no close lexem definitions to choose from 
+  //then use easier difficulty
+  if ($closestLexemsDefinitionsCount == 0)
+      $difficulty = 1;
+}
+
 for ($i = 1; $i <= 4; $i++) {
+  $def = null;  
   if ($i != $answer) {
-    do {
+    do
+    {
       if ($difficulty == 1) {
         $aux = rand(0, $count - 1);
+        $def = Model::factory('DefinitionSimple')->limit(1)->offset($aux)->find_one();
       } else {
-        $aux = getNormalRand(100 - ($difficulty * 20), $chosenDef, $count - 1);
+        $aux = rand(0, $closestLexemsDefinitionsCount - 1);
+        $def = $closestLexemsDefinitions[$aux];
+        
+        unset($closestLexemsDefinitions[$aux]);
+        $closestLexemsDefinitions = array_values($closestLexemsDefinitions);
+        $closestLexemsDefinitionsCount--;
+        
+        //if we run out of close lexem definitions to use 
+        //then use easier difficulty
+        if ($closestLexemsDefinitionsCount == 0)
+        {
+            $difficulty = 1;
+        }
       }
-      $def = Model::factory('DefinitionSimple')->limit(1)->offset($aux)->find_one();
     } while (array_key_exists($def->definitionId, $used));
+  
+    
     $used[$def->definitionId] = 1;
     $options[$i]=array();
     $options[$i]['term'] = getWordForDefitionId($def->definitionId);
@@ -75,6 +126,7 @@
   }
 }
 
+
 $xml->addChild('word', $word);
 $xml->addChild('answerId', $maindef->id);
 for ($i = 1; $i <= 4; $i++) {


More information about the Dev mailing list