kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file Bablotron.php

Documentation is available at Bablotron.php

  1. <?php
  2. /**
  3.  * Spellcheck Library
  4.  *
  5.  * @package kernel
  6.  * @version $Header$
  7.  *
  8.  *  Copyright (c) 2004 bitweaver.org
  9.  *  Copyright (c) 2003 tikwiki.org
  10.  *  Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
  11.  *  All Rights Reserved. See below for details and a complete list of authors.
  12.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  13.  *
  14.  *  $Id$
  15.  *
  16.  *  A spell checking library.
  17.  *
  18.  *  Currently used for BitBase.
  19.  * @author lrargerich <lrargerich@yahoo.com>
  20.  *  created 2002/11/14
  21.  */
  22.  
  23. /**
  24.  * Spellcheck Library
  25.  *
  26.  * @package kernel
  27.  * @todo does not need to inherit BitBase class. Should hold a BitDb connection as a
  28.  *  global variable.
  29.  */
  30. class Bablotron extends BitBase
  31. {
  32.     /**
  33.     * @todo Variable is scoped here but not really used in this scope below.
  34.     */
  35.     var $words;
  36.     /**
  37.     * Used to store the current language.
  38.     *
  39.     * @todo Not sure where this gets set from. Is used in other libraries.
  40.     */
  41.     var $lan;
  42.     /**
  43.     * @todo Empty variable - does nothing
  44.     */
  45.     var $db;
  46.     /**
  47.     * Spellchecking and finding of alternative words
  48.     */
  49.     function __construct$lan )
  50.     {
  51.         parent::__construct();
  52.         $this->lan = $lan;
  53.     }
  54.     /**
  55.     * @todo Empty function - does nothing
  56.     */
  57.     function sql_error($query$result)
  58.     {
  59.         return;
  60.     }
  61.     /**
  62.     * Spellchecks a line of text
  63.     * @param text line of text
  64.     * @param threshold the similarity threshold
  65.     * @returns array a list of alternative words if spelt incorrectly
  66.     */
  67.     function spellcheck_text($text$threshold 5)
  68.     {
  69.         $words preg_split("/\s/"$text);
  70.         $results array();
  71.         foreach ($words as $word)
  72.         {
  73.             if (!$this->word_exists($word))
  74.             {
  75.                 $results[$word$this->find_similar_words($word$threshold);
  76.             }
  77.         }
  78.         return $results;
  79.     }
  80.     /**
  81.     * Spellchecks a word
  82.     * @param word the word
  83.     * @param threshold the similarity threshold
  84.     * @returns array a list of alternative words if spelt incorrectly
  85.     */
  86.     function spellcheck_word($word$threshold 5)
  87.     {
  88.         $results array();
  89.         if (!$this->word_exists($word))
  90.         {
  91.             $results[$word$this->find_similar_words($word$threshold);
  92.         }
  93.         return $results;
  94.     }
  95.     /**
  96.     * Spellchecks a line of text
  97.     * @param text line of text
  98.     * @param threshold Not used @todo param threshold Not used
  99.     * @return array a list of incorrectly spelt words or words not found in the database
  100.     */
  101.     function quick_spellcheck_text($text$threshold 5)
  102.     {
  103.         $words preg_split("/\s/"$text);
  104.         $results array();
  105.         foreach ($words as $word)
  106.         {
  107.             if (!$this->word_exists($word))
  108.             {
  109.                 $results[$word;
  110.             }
  111.         }
  112.         return $results;
  113.     }
  114.     /**
  115.     * Lists similar words by relevance threshold.
  116.     * @param word the word
  117.     * @param threshold the similarity threshold
  118.     * @return array of similar words and Levenshtein distance
  119.     */
  120.     function find_similar_words($word$threshold)
  121.     {
  122.         $similar array();
  123.         $tbl 'babl_words_' $this->lan;
  124.         $word addslashes( ( trim$word ) ) );
  125.         $sndx substr($word02);
  126.         $query "select `word` AS word from `$tbl` where `di`=?";
  127.         @$result $this->mDb->query($queryarray($sndx));
  128.         while ($res $result->fetchRow() )
  129.         {
  130.             $tword $res["word"];
  131.             $lev levenshtein($tword$word);
  132.             if (count($similar$threshold)
  133.             {
  134.                 $similar[$tword$lev;
  135.                 asort ($similar);
  136.             }
  137.             else
  138.             {
  139.                 // If the array is full then if the lev is better than the worst lev
  140.                 // then update $keys = array_keys($similar);
  141.                 $last_key $keys[count($keys1];
  142.                 if ($lev $similar[$last_key])
  143.                 {
  144.                     unset ($similar[$last_key]);
  145.                     $similar[$tword$lev;
  146.                     asort ($similar);
  147.                 }
  148.             }
  149.         }
  150.         return $similar;
  151.     }
  152.     /**
  153.     * Checks if a word exists
  154.     * @param word the word
  155.     * @return int number of matches
  156.     */
  157.     function word_exists($word)
  158.     {
  159.         $tbl 'babl_words_' $this->lan;
  160.         $word addslashes( ( trim$word ) ) );
  161.         $query "select `word` AS word from `$tbl` where `word`=?";
  162.         $result $this->mDb->query($query,array($word));
  163.         return $result->numRows();
  164.     }
  165.     /**
  166.     * @todo Empty function - does nothing
  167.     */
  168.     function find_similar($word$threshold)
  169.     {
  170.     }
  171. }
  172. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:36 +0000 by phpDocumentor 1.5.0-lsces