You are here

class SearchApiPorterStemmer in Search API 7

Stems words to their roots.

Hierarchy

Expanded class hierarchy of SearchApiPorterStemmer

1 string reference to 'SearchApiPorterStemmer'
search_api_search_api_processor_info in ./search_api.module
Implements hook_search_api_processor_info().

File

includes/processor_stemmer.inc, line 11
Contains SearchApiPorterStemmer and SearchApiPorter2.

View source
class SearchApiPorterStemmer extends SearchApiAbstractProcessor {

  /**
   * Static cache for already generated stems.
   *
   * @var array
   */
  protected $stems = array();

  /**
   * {@inheritdoc}
   */
  public function configurationForm() {
    $form = parent::configurationForm();
    $args = array(
      '@algorithm' => url('http://snowball.tartarus.org/algorithms/english/stemmer.html'),
    );
    $form += array(
      'help' => array(
        '#markup' => '<p>' . t('Optionally, provide an exclusion list to override the stemmer algorithm. (<a href="@algorithm">Read about the algorithm</a>.)', $args) . '</p>',
      ),
      'exceptions' => array(
        '#type' => 'textarea',
        '#title' => t('Exceptions'),
        '#description' => t('Enter exceptions in the form of WORD=STEM, where "WORD" is the term entered and "STEM" is the resulting stem. List each exception on a separate line.'),
        '#default_value' => "texan=texa",
      ),
    );
    if (!empty($this->options['exceptions'])) {
      $form['exceptions']['#default_value'] = $this->options['exceptions'];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function process(&$value) {

    // Load custom exceptions.
    $exceptions = $this
      ->getExceptions();
    $words = preg_split('/[^\\p{L}\\p{N}]+/u', $value, -1, PREG_SPLIT_DELIM_CAPTURE);
    $stemmed = array();
    foreach ($words as $i => $word) {
      if ($i % 2 == 0 && strlen($word)) {
        if (!isset($this->stems[$word])) {
          $stem = new SearchApiPorter2($word, $exceptions);
          $this->stems[$word] = $stem
            ->stem();
        }
        $stemmed[] = $this->stems[$word];
      }
      else {
        $stemmed[] = $word;
      }
    }
    $value = implode(' ', $stemmed);
  }

  /**
   * Retrieves the processor's configured exceptions.
   *
   * @return string[]
   *   An associative array of exceptions, with words as keys and stems as their
   *   replacements.
   */
  protected function getExceptions() {
    if (!empty($this->options['exceptions'])) {
      $exceptions = parse_ini_string($this->options['exceptions'], TRUE);
      return is_array($exceptions) ? $exceptions : array();
    }
    return array();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAbstractProcessor::$index protected property
SearchApiAbstractProcessor::$options protected property
SearchApiAbstractProcessor::configurationFormSubmit public function Submit callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface::configurationFormSubmit
SearchApiAbstractProcessor::configurationFormValidate public function Validation callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface::configurationFormValidate 4
SearchApiAbstractProcessor::implodeTokens protected function Internal helper function for imploding tokens into a single string.
SearchApiAbstractProcessor::normalizeTokens protected function Internal helper function for normalizing tokens.
SearchApiAbstractProcessor::postprocessSearchResults public function Does nothing. Overrides SearchApiProcessorInterface::postprocessSearchResults 2
SearchApiAbstractProcessor::preprocessIndexItems public function Calls processField() for all appropriate fields. Overrides SearchApiProcessorInterface::preprocessIndexItems
SearchApiAbstractProcessor::preprocessSearchQuery public function Calls processKeys() for the keys and processFilters() for the filters. Overrides SearchApiProcessorInterface::preprocessSearchQuery 1
SearchApiAbstractProcessor::processField protected function Method for preprocessing field data.
SearchApiAbstractProcessor::processFieldValue protected function Called for processing a single text element in a field. The default implementation just calls process(). 2
SearchApiAbstractProcessor::processFilters protected function Method for preprocessing query filters.
SearchApiAbstractProcessor::processFilterValue protected function Called for processing a single filter value. The default implementation just calls process().
SearchApiAbstractProcessor::processKey protected function Called for processing a single search keyword. The default implementation just calls process().
SearchApiAbstractProcessor::processKeys protected function Method for preprocessing search keys.
SearchApiAbstractProcessor::supportsIndex public function Check whether this processor is applicable for a certain index. Overrides SearchApiProcessorInterface::supportsIndex
SearchApiAbstractProcessor::testField protected function Determines whether to process data from the given field.
SearchApiAbstractProcessor::testType protected function Determines whether fields of the given type should normally be processed.
SearchApiAbstractProcessor::__construct public function Constructor, saving its arguments into properties. Overrides SearchApiProcessorInterface::__construct 2
SearchApiPorterStemmer::$stems protected property Static cache for already generated stems.
SearchApiPorterStemmer::configurationForm public function Display a form for configuring this processor. Since forcing users to specify options for disabled processors makes no sense, none of the form elements should have the '#required' attribute set. Overrides SearchApiAbstractProcessor::configurationForm
SearchApiPorterStemmer::getExceptions protected function Retrieves the processor's configured exceptions.
SearchApiPorterStemmer::process protected function Function that is ultimately called for all text by the standard implementation, and does nothing by default. Overrides SearchApiAbstractProcessor::process