| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Util for Tagging Operations |
|---|
| 5 | * |
|---|
| 6 | * @author Torben Brodt |
|---|
| 7 | * @package de.easy-coding.wcf.taggingreloaded |
|---|
| 8 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
|---|
| 9 | */ |
|---|
| 10 | class TaggingReloadedUtil { |
|---|
| 11 | protected $stopwordList; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * returns the stopwordlist |
|---|
| 15 | */ |
|---|
| 16 | public function getStopwordList() { |
|---|
| 17 | if($this->stopwordList === null) { |
|---|
| 18 | $this->stopwordList = array(); |
|---|
| 19 | |
|---|
| 20 | $sql = "SELECT stowprd |
|---|
| 21 | FROM wcf".WCF_N."_taggingreloaded_stopwords |
|---|
| 22 | WHERE languagecode = '".WCF::getLanguage()->getLanguageCode()."'; "; |
|---|
| 23 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 24 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 25 | $this->stopwordList[] = $row['stopword']; |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | return $this->stopwordList; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * returns true if the word is NOT a stopword |
|---|
| 34 | * @param word |
|---|
| 35 | */ |
|---|
| 36 | protected function noStopword($word) { |
|---|
| 37 | if($this->stopwordList === null) { |
|---|
| 38 | //$this->getStopwordList(); |
|---|
| 39 | $this->stopwordList = array('wie','ich','mit'); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | return !in_array($word, $this->stopwordList); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * removes bbcodes and adds some tagging optimizations (just for tagging usage) |
|---|
| 47 | * @param text |
|---|
| 48 | * @return text |
|---|
| 49 | */ |
|---|
| 50 | public static function bbcode2text($text) { |
|---|
| 51 | // if there is bold text, then repeat the words |
|---|
| 52 | $text = preg_replace('/\[b\](.+)\[\/b\]/', '$1 $1', $text); |
|---|
| 53 | |
|---|
| 54 | // make pseudo html |
|---|
| 55 | $text = str_replace(array('[',']'), array('<','>'), $text); |
|---|
| 56 | |
|---|
| 57 | // strip this html |
|---|
| 58 | $text = strip_tags($text); |
|---|
| 59 | |
|---|
| 60 | return $text; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * gets a text and returns a list of weighted tags |
|---|
| 65 | * @param text |
|---|
| 66 | * @param limit (optional) |
|---|
| 67 | * @return array |
|---|
| 68 | */ |
|---|
| 69 | public static function text2tags($text, $limit=15) { |
|---|
| 70 | $tags = array(); |
|---|
| 71 | |
|---|
| 72 | // find words beginning with capitals |
|---|
| 73 | preg_match_all('/([A-Z][a-zA-Z]+)/e', $text, $words); |
|---|
| 74 | |
|---|
| 75 | // lower them |
|---|
| 76 | $words = array_map(create_function('$a','return trim(strtolower($a));'), $words[1]); |
|---|
| 77 | |
|---|
| 78 | // filter stopwords |
|---|
| 79 | //$words = array_filter($words, array($this, "noStopword")); |
|---|
| 80 | |
|---|
| 81 | // search weights |
|---|
| 82 | foreach($words as $word) { |
|---|
| 83 | $tags[$word] = array_key_exists($word, $tags) ? $tags[$word]+1 : 1; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // limit to words with minimum one occurent |
|---|
| 87 | $tags = array_filter($tags, create_function('$a','return $a > 1;')); |
|---|
| 88 | |
|---|
| 89 | // sort by weight |
|---|
| 90 | arsort($tags); |
|---|
| 91 | |
|---|
| 92 | // extend to beautify |
|---|
| 93 | array_walk($tags, create_function('&$val, $key','$val = array("weight"=>$val*100);')); |
|---|
| 94 | |
|---|
| 95 | $tags = TaggingReloadedUtil::beautify($tags, 100, 200); |
|---|
| 96 | |
|---|
| 97 | // replace weights with sizes TODO: replace through array_walk |
|---|
| 98 | array_walk($tags, create_function('&$val, $key','$val = $val["size"];')); |
|---|
| 99 | |
|---|
| 100 | $tags = array_slice($tags, 0, $limit); |
|---|
| 101 | |
|---|
| 102 | return $tags; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * saves tags |
|---|
| 107 | * @param taggingID -> taggingID |
|---|
| 108 | * @param tags -> array of tags |
|---|
| 109 | * @param userID -> (optional) |
|---|
| 110 | */ |
|---|
| 111 | public static function tags2db($taggingID, $tags, $userID=null) { |
|---|
| 112 | if(count($tags) == 0) return; |
|---|
| 113 | |
|---|
| 114 | $userID = $userID === null ? WCF::getUser()->userID : -1; |
|---|
| 115 | |
|---|
| 116 | // INSERT tags |
|---|
| 117 | $sql = "INSERT INTO wcf".WCF_N."_taggingreloaded |
|---|
| 118 | (taggingID,userID,tag,weight) |
|---|
| 119 | VALUES "; |
|---|
| 120 | |
|---|
| 121 | $sql_append = array(); |
|---|
| 122 | foreach($tags as $tag => $weight) { |
|---|
| 123 | $sql_append[] = "({$taggingID}, {$userID}, '".escapeString($tag)."', '".escapeString($weight)."')"; |
|---|
| 124 | $i++; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | $sql .= implode(',', $sql_append); |
|---|
| 128 | if($i>0) WCF::getDB()->sendQuery($sql); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * beautify tags with size and color |
|---|
| 133 | * @param tags |
|---|
| 134 | * @param minsize -> optional |
|---|
| 135 | * @param maxsize -> optional |
|---|
| 136 | * @param array |
|---|
| 137 | */ |
|---|
| 138 | public static function beautify($tags, $forced_minsize=null, $forced_maxsize=null) { |
|---|
| 139 | // TODO: auslagern |
|---|
| 140 | $minsize = $forced_minsize === null ? 75 : $forced_minsize; |
|---|
| 141 | $maxsize = $forced_maxsize === null ? 275 : $forced_maxsize; |
|---|
| 142 | $mincolor = 75; |
|---|
| 143 | $maxcolor = 255; |
|---|
| 144 | |
|---|
| 145 | $min = array_reduce($tags, create_function('$a, $b', 'return $a !== null && $a < $b["weight"] ? $a : $b["weight"];')); |
|---|
| 146 | $max = array_reduce($tags, create_function('$a, $b', 'return $a !== null && $a > $b["weight"] ? $a : $b["weight"];')); |
|---|
| 147 | |
|---|
| 148 | if($min == $max) { |
|---|
| 149 | $max = $max + 1; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | if($forced_minsize === null && $min > $minsize) { |
|---|
| 153 | $minsize = $min; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if($forced_maxsize === null && $max < $maxsize) { |
|---|
| 157 | $maxsize = $max; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | $b = $maxsize -($max * ($maxsize-$minsize) / ($max-$min)); |
|---|
| 161 | $c = $maxcolor -($max * ($maxcolor-$mincolor) / ($max-$min)); |
|---|
| 162 | |
|---|
| 163 | $tmp = array(); |
|---|
| 164 | foreach($tags as $key => $tag) { |
|---|
| 165 | $key = strtolower($key); |
|---|
| 166 | $size = ($tag['weight'] * ($maxsize-$minsize) / ($max-$min) )+$b; |
|---|
| 167 | $color = 255 - (($tag['weight'] * ($maxcolor-$mincolor) / ($max-$min) )+$c); |
|---|
| 168 | |
|---|
| 169 | $tmp[$key] = array( |
|---|
| 170 | 'weight'=> $tag['weight'], |
|---|
| 171 | 'color'=> intval($color), |
|---|
| 172 | 'size'=> intval($size) |
|---|
| 173 | ); |
|---|
| 174 | } |
|---|
| 175 | return $tmp; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | ?> |
|---|