[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_finder/src/Indexer/ -> Token.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_finder
   6   *
   7   * @copyright   (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Finder\Administrator\Indexer;
  12  
  13  use Joomla\String\StringHelper;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('_JEXEC') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Token class for the Finder indexer package.
  21   *
  22   * @since  2.5
  23   */
  24  class Token
  25  {
  26      /**
  27       * This is the term that will be referenced in the terms table and the
  28       * mapping tables.
  29       *
  30       * @var    string
  31       * @since  2.5
  32       */
  33      public $term;
  34  
  35      /**
  36       * The stem is used to match the root term and produce more potential
  37       * matches when searching the index.
  38       *
  39       * @var    string
  40       * @since  2.5
  41       */
  42      public $stem;
  43  
  44      /**
  45       * If the token is numeric, it is likely to be short and uncommon so the
  46       * weight is adjusted to compensate for that situation.
  47       *
  48       * @var    boolean
  49       * @since  2.5
  50       */
  51      public $numeric;
  52  
  53      /**
  54       * If the token is a common term, the weight is adjusted to compensate for
  55       * the higher frequency of the term in relation to other terms.
  56       *
  57       * @var    boolean
  58       * @since  2.5
  59       */
  60      public $common;
  61  
  62      /**
  63       * Flag for phrase tokens.
  64       *
  65       * @var    boolean
  66       * @since  2.5
  67       */
  68      public $phrase;
  69  
  70      /**
  71       * The length is used to calculate the weight of the token.
  72       *
  73       * @var    integer
  74       * @since  2.5
  75       */
  76      public $length;
  77  
  78      /**
  79       * The weight is calculated based on token size and whether the token is
  80       * considered a common term.
  81       *
  82       * @var    integer
  83       * @since  2.5
  84       */
  85      public $weight;
  86  
  87      /**
  88       * The simple language identifier for the token.
  89       *
  90       * @var    string
  91       * @since  2.5
  92       */
  93      public $language;
  94  
  95      /**
  96       * The container for matches.
  97       *
  98       * @var    array
  99       * @since  3.8.12
 100       */
 101      public $matches = array();
 102  
 103      /**
 104       * Is derived token (from individual words)
 105       *
 106       * @var    boolean
 107       * @since  3.8.12
 108       */
 109      public $derived;
 110  
 111      /**
 112       * The suggested term
 113       *
 114       * @var    string
 115       * @since  3.8.12
 116       */
 117      public $suggestion;
 118  
 119      /**
 120       * Method to construct the token object.
 121       *
 122       * @param   mixed   $term    The term as a string for words or an array for phrases.
 123       * @param   string  $lang    The simple language identifier.
 124       * @param   string  $spacer  The space separator for phrases. [optional]
 125       *
 126       * @since   2.5
 127       */
 128      public function __construct($term, $lang, $spacer = ' ')
 129      {
 130          if (!$lang) {
 131              $this->language = '*';
 132          } else {
 133              $this->language = $lang;
 134          }
 135  
 136          // Tokens can be a single word or an array of words representing a phrase.
 137          if (is_array($term)) {
 138              // Populate the token instance.
 139              $this->term = implode($spacer, $term);
 140              $this->stem = implode($spacer, array_map(array(Helper::class, 'stem'), $term, array($lang)));
 141              $this->numeric = false;
 142              $this->common = false;
 143              $this->phrase = true;
 144              $this->length = StringHelper::strlen($this->term);
 145  
 146              /*
 147               * Calculate the weight of the token.
 148               *
 149               * 1. Length of the token up to 30 and divide by 30, add 1.
 150               * 2. Round weight to 4 decimal points.
 151               */
 152              $this->weight = (($this->length >= 30 ? 30 : $this->length) / 30) + 1;
 153              $this->weight = round($this->weight, 4);
 154          } else {
 155              // Populate the token instance.
 156              $this->term = $term;
 157              $this->stem = Helper::stem($this->term, $lang);
 158              $this->numeric = (is_numeric($this->term) || (bool) preg_match('#^[0-9,.\-\+]+$#', $this->term));
 159              $this->common = $this->numeric ? false : Helper::isCommon($this->term, $lang);
 160              $this->phrase = false;
 161              $this->length = StringHelper::strlen($this->term);
 162  
 163              /*
 164               * Calculate the weight of the token.
 165               *
 166               * 1. Length of the token up to 15 and divide by 15.
 167               * 2. If common term, divide weight by 8.
 168               * 3. If numeric, multiply weight by 1.5.
 169               * 4. Round weight to 4 decimal points.
 170               */
 171              $this->weight = ($this->length >= 15 ? 15 : $this->length) / 15;
 172              $this->weight = $this->common === true ? $this->weight / 8 : $this->weight;
 173              $this->weight = $this->numeric === true ? $this->weight * 1.5 : $this->weight;
 174              $this->weight = round($this->weight, 4);
 175          }
 176      }
 177  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer