function number2db value larr localeconv search array larr decimal_poi

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?
function number2db($value)
{
$larr = localeconv();
$search = array(
$larr['decimal_point'],
$larr['mon_decimal_point'],
$larr['thousands_sep'],
$larr['mon_thousands_sep'],
$larr['currency_symbol'],
$larr['int_curr_symbol']
);
$replace = array('.', '.', '', '', '', '');
return str_replace($search, $replace, $value);
}
function get_sec()
{
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
return $mtime;
}
/**
*
*/
class Categorizer {
static $info_tpl = array(
'matches' => array(),
'scores' => array()
);
public static $info;
private static $KEYWORDS = array();
private static $CATS = array();
public static function lcount( $word ) {
//echo "<p>$string</p>";
//$word = iconv("cp1251", 'UTF-8', $word);
//$word = preg_replace("/[^а-яА-ЯёЁa-zA-Z]/", "", trim( $word ));
return strlen($word) > 2 ? array( preg_replace('/[^\w\-\d]/','', $word) => 1) : null;
}
public static function rcount($w1, $w2) {
$w1 = !is_array($w1) ? array() : $w1;
if($w2)
foreach ($w2 as $word => $count) {
//$word = preg_replace("/[^а-яА-ЯёЁa-zA-Z]/", "", trim( $word ));
//echo "<p>$word - !!</p>";
$w1[ $word ] += $count;
}
return $w1;
}
static function catParent(&$ar) {
foreach($ar as $a){
if($a['Value']) return $a['Value'];
}
return false;
}
static function loadKeywords(){
$kws = array();
$mysql = mysql_query("select * from Message66 order by Category ASC");
while($tmp = mysql_fetch_assoc($mysql)) $kws[] = $tmp;
self::$KEYWORDS = $kws;
$cats = array();
$mysql = mysql_query("SELECT * FROM `Classificator_deal_cats` where Value < 1 ");
while($tmp = mysql_fetch_assoc($mysql)) $cats[] = $tmp;
self::$CATS = $cats;
$res = array();
$lastcat = 0;
$lastcatPid = 0;
return $kws;
foreach($kws as &$kw){
// if($lastcat != $kw['Category']){
// $lastcat = $kw['Category'];
// $lastcatPid = self::catParent($cats);
// }
}
}
public static function execute( $text, $k = 1 ){
self::$info = self::$info_tpl;
self::loadKeywords();
$tkeywords = self::$KEYWORDS;
$text = preg_replace("/\d+/", "", strip_tags(strtolower($text)));
$splitters = '\s.,\(\)?:;';
$array = preg_split( "/[" . $splitters . "]*\\\"([^\\\"]+)\\\"[" . $splitters . "]*|[" . $splitters . "]+/", $text, 0, PREG_SPLIT_DELIM_CAPTURE );
$array = array_map('strtolower', $array);
$array = array_map('trim', $array);
$array = array_map(array('Categorizer','lcount'), $array);
$result = array_reduce($array, array('Categorizer','rcount'));
//var_dump($result);
arsort($result);
$scores = array();
$scoreIndex = array();
$index = 0;
$printed = array();
$loaded = array();
foreach($result as $word => $n) {
$word = trim($word);
foreach( self::$KEYWORDS as &$kw) {
$name = $kw['Category'];
$scoreIndex[$name] = ++$index;
$regexp = $kw['Word'];
$weight = $kw['Weight'];
if(
(preg_match("/^$regexp$/i", iconv("cp1251", 'utf-8', $word))
|| preg_match("/^$".iconv('utf-8', "cp1251", $regexp)."$/i", $word)
|| preg_match("/^$regexp$/i", $word))
&& !$loaded[$word.$name]
) {
$loaded[$word.$name] = true;
//echo "<p><MATCHED: $word - $n ($regexp) - $name<</p>";
$scores[$name] += $weight * $k * $n;
self::$info['matches'][$word] = array('count'=> $n, 'score' => $scores[$name], 'group' => $name, 'regex' => iconv('UTF-8', 'cp1251', $regexp));
}
}
}
$ls = 0;$li = 0;
foreach($scores as $i => $s){
if( $s > $ls ) {
$ls = $s;
$li = $i;
}
}
self::$info['scores'] = $scores;
return $li;
}
static function updateAll(){
$start_time = get_sec();
$i = 0;
ini_set('memory_limit', '240M');
set_time_limit(500);
self::$KEYWORDS = self::loadKeywords();
$deals = array();
$mysql = mysql_query("select * from Message65 where Category = 0 order by Category ASC LIMIT 500");
while($tmp = mysql_fetch_assoc($mysql)) $deals[] = $tmp;
foreach($deals as &$deal){
//if(++$i > 500) break;
//echo $deal['Name'];
$cat_id = self::execute($deal['Description'] . ' ' . $deal['Name']);
mysql_query("update Message65 set Category=$cat_id Where Message_ID=" . $deal['Message_ID']);
}
$exec_time = get_sec() - $start_time;
echo "exec time : $exec_time sec.";
}
static function dump(){
// $tmp = self::$info['scores'];
// foreach( self::$info['matches'] as $k => $v) {
// foreach(self::$info['scores'] as $name => $total){
// $tmp[$name] = array()
// }
// }
return serialize(self::$info);
}
}