]> git.mxchange.org Git - friendica.git/blob - library/spam/b8/degenerator/degenerator_default.php
Merge remote branch 'upstream/master'
[friendica.git] / library / spam / b8 / degenerator / degenerator_default.php
1 <?php
2
3 #   Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
4 #
5 #   This file is part of the b8 package
6 #
7 #   This program is free software; you can redistribute it and/or modify it
8 #   under the terms of the GNU Lesser General Public License as published by
9 #   the Free Software Foundation in version 2.1 of the License.
10 #
11 #   This program is distributed in the hope that it will be useful, but
12 #   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 #   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14 #   License for more details.
15 #
16 #   You should have received a copy of the GNU Lesser General Public License
17 #   along with this program; if not, write to the Free Software Foundation,
18 #   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19
20 /**
21  * Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
22  *
23  * @license LGPL
24  * @access public
25  * @package b8
26  * @author Tobias Leupold
27  */
28
29 class b8_degenerator_default
30 {
31
32         public $degenerates = array();
33
34         /**
35          * Generates a list of "degenerated" words for a list of words.
36          *
37          * @access public
38          * @param array $tokens
39          * @return array An array containing an array of degenerated tokens for each token
40          */
41
42         public function degenerate(array $words)
43         {
44
45                 $degenerates = array();
46
47                 foreach($words as $word)
48                         $degenerates[$word] = $this->_degenerate_word($word);
49
50                 return $degenerates;
51
52         }
53
54         /**
55          * If the original word is not found in the database then
56          * we build "degenerated" versions of the word to lookup.
57          *
58          * @access private
59          * @param string $word
60          * @return array An array of degenerated words
61          */
62
63         protected function _degenerate_word($word)
64         {
65
66                 # Check for any stored words so the process doesn't have to repeat
67                 if(isset($this->degenerates[$word]) === TRUE)
68                         return $this->degenerates[$word];
69
70                 $degenerate = array();
71
72                 # Add different version of upper and lower case and ucfirst
73                 array_push($degenerate, strtolower($word));
74                 array_push($degenerate, strtoupper($word));
75                 array_push($degenerate, ucfirst($word));
76
77                 # Degenerate all versions
78
79                 foreach($degenerate as $alt_word) {
80
81                         # Look for stuff like !!! and ???
82
83                         if(preg_match('/[!?]$/', $alt_word) > 0) {
84
85                                 # Add versions with different !s and ?s
86
87                                 if(preg_match('/[!?]{2,}$/', $alt_word) > 0) {
88                                         $tmp = preg_replace('/([!?])+$/', '$1', $alt_word);
89                                         array_push($degenerate, $tmp);
90                                 }
91
92                                 $tmp = preg_replace('/([!?])+$/', '', $alt_word);
93                                 array_push($degenerate, $tmp);
94
95                         }
96
97                         # Look for ... at the end of the word
98
99                         $alt_word_int = $alt_word;
100
101                         while(preg_match('/[\.]$/', $alt_word_int) > 0) {
102                                 $alt_word_int = substr($alt_word_int, 0, strlen($alt_word_int) - 1);
103                                 array_push($degenerate, $alt_word_int);
104                         }
105
106                 }
107
108                 # Some degenerates are the same as the original word. These don't have
109                 # to be fetched, so we create a new array with only new tokens
110
111                 $real_degenerate = array();
112
113                 foreach($degenerate as $deg_word) {
114                         if($word != $deg_word)
115                                 array_push($real_degenerate, $deg_word);
116                 }
117
118                 # Store the list of degenerates for the token
119                 $this->degenerates[$word] = $real_degenerate;
120
121                 return $real_degenerate;
122
123         }
124
125 }
126
127 ?>