import
[web.mtrack] / inc / lib / Zend / Search / Lucene / Analysis / TokenFilter / StopWords.php
1 <?php
2 /**
3  * Zend Framework
4  *
5  * LICENSE
6  *
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.
14  *
15  * @category   Zend
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 $
21  */
22
23 /** Zend_Search_Lucene_Analysis_TokenFilter */
24 require_once 'Zend/Search/Lucene/Analysis/TokenFilter.php';
25
26 /**
27  * Token filter that removes stop words. These words must be provided as array (set), example:
28  * $stopwords = array('the' => 1, 'an' => '1');
29  *
30  * We do recommend to provide all words in lowercase and concatenate this class after the lowercase filter.
31  *
32  * @category   Zend
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
37  */
38
39 class Zend_Search_Lucene_Analysis_TokenFilter_StopWords extends Zend_Search_Lucene_Analysis_TokenFilter
40 {
41     /**
42      * Stop Words
43      * @var array
44      */
45     private $_stopSet;
46
47     /**
48      * Constructs new instance of this filter.
49      *
50      * @param array $stopwords array (set) of words that will be filtered out
51      */
52     public function __construct($stopwords = array()) {
53         $this->_stopSet = array_flip($stopwords);
54     }
55
56     /**
57      * Normalize Token or remove it (if null is returned)
58      *
59      * @param Zend_Search_Lucene_Analysis_Token $srcToken
60      * @return Zend_Search_Lucene_Analysis_Token
61      */
62     public function normalize(Zend_Search_Lucene_Analysis_Token $srcToken) {
63         if (array_key_exists($srcToken->getTermText(), $this->_stopSet)) {
64             return null;
65         } else {
66             return $srcToken;
67         }
68     }
69
70     /**
71      * Fills stopwords set from a text file. Each line contains one stopword, lines with '#' in the first
72      * column are ignored (as comments).
73      *
74      * You can call this method one or more times. New stopwords are always added to current set.
75      *
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.
78      */
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');
83         }
84         $fd = fopen($filepath, "r");
85         if (! $fd) {
86             require_once 'Zend/Search/Lucene/Exception.php';
87             throw new Zend_Search_Lucene_Exception('Cannot open file ' . $filepath);
88         }
89         while (!feof ($fd)) {
90             $buffer = trim(fgets($fd));
91             if (strlen($buffer) > 0 && $buffer[0] != '#') {
92                 $this->_stopSet[$buffer] = 1;
93             }
94         }
95         if (!fclose($fd)) {
96             require_once 'Zend/Search/Lucene/Exception.php';
97             throw new Zend_Search_Lucene_Exception('Cannot close file ' . $filepath);
98         }
99     }
100 }
101