<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2006 - 2013 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flTextStemmer Class
 *
 * Base class for all language specific stemmers.
 *
 * @package fusionLib
 */
class flTextStemmer {

	/**
	 * Load the stemmer based on the language code.
	 *
	 * If no stemmer is available then an instance of flSearchStemmer is returned.
	 *
	 * @param string $lang The language to load the stemmer for, e.g. en, see ISO 639 for the list of 2 letter codes.
	 * @return flTextStemmer The instance of the appropriate stemmer.
	 */
	static function load($lang) {
		try {
			class_exists($cls = 'flTextStemmer' . ucfirst(preg_replace('/_.*$/', '', $lang)));
		} catch(flExceptionLoadClass $e) {
			$cls = 'flTextStemmer';
		}

		return new $cls();
	}

	/**
	 * Calculate and return the stemmed version of a word.
	 *
	 * Note: The default implementation returns the original word unchanged.
	 *
	 * @param string $word The lowercase word to stem.
	 * @return string The stemmed word.
	 */
	function stem($word) {
		return $word;
	}

	/**
	 * For each word in an array calculate it's stemmed equivalent.
	 *
	 * Duplicates are not removed from the array, there is exactly the same number of
	 * words in the results as the source.
	 *
	 * @param array $words Array of words to stem
	 * @return array Array holding the stemmed words
	 */
	function stemWords($words) {
		// Stem each words in turn and store it
		$stemmed = array();
		foreach($words as $w) {
			$stemmed[] = $this->stem($w);
		}
		return $stemmed;
	}
}
