7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Search_Lucene
17 * @subpackage Analysis
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: StopWords.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 /** Zend_Search_Lucene_Analysis_TokenFilter */
24 require_once 'Zend/Search/Lucene/Analysis/TokenFilter.php';
27 * Token filter that removes stop words. These words must be provided as array (set), example:
28 * $stopwords = array('the' => 1, 'an' => '1');
30 * We do recommend to provide all words in lowercase and concatenate this class after the lowercase filter.
33 * @package Zend_Search_Lucene
34 * @subpackage Analysis
35 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
36 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_Search_Lucene_Analysis_TokenFilter_StopWords extends Zend_Search_Lucene_Analysis_TokenFilter
48 * Constructs new instance of this filter.
50 * @param array $stopwords array (set) of words that will be filtered out
52 public function __construct($stopwords = array()) {
53 $this->_stopSet = array_flip($stopwords);
57 * Normalize Token or remove it (if null is returned)
59 * @param Zend_Search_Lucene_Analysis_Token $srcToken
60 * @return Zend_Search_Lucene_Analysis_Token
62 public function normalize(Zend_Search_Lucene_Analysis_Token $srcToken) {
63 if (array_key_exists($srcToken->getTermText(), $this->_stopSet)) {
71 * Fills stopwords set from a text file. Each line contains one stopword, lines with '#' in the first
72 * column are ignored (as comments).
74 * You can call this method one or more times. New stopwords are always added to current set.
76 * @param string $filepath full path for text file with stopwords
77 * @throws Zend_Search_Exception When the file doesn`t exists or is not readable.
79 public function loadFromFile($filepath = null) {
80 if (! $filepath || ! file_exists($filepath)) {
81 require_once 'Zend/Search/Lucene/Exception.php';
82 throw new Zend_Search_Lucene_Exception('You have to provide valid file path');
84 $fd = fopen($filepath, "r");
86 require_once 'Zend/Search/Lucene/Exception.php';
87 throw new Zend_Search_Lucene_Exception('Cannot open file ' . $filepath);
90 $buffer = trim(fgets($fd));
91 if (strlen($buffer) > 0 && $buffer[0] != '#') {
92 $this->_stopSet[$buffer] = 1;
96 require_once 'Zend/Search/Lucene/Exception.php';
97 throw new Zend_Search_Lucene_Exception('Cannot close file ' . $filepath);