forked from ivansky/php-yandex-wordstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YADWord.php
214 lines (180 loc) · 5.23 KB
/
YADWord.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
class YADWord {
private static $bind_region = array();
private static $bind_crc = array();
public $region_id = 213;
public $original;
public $word;
public $strict;
public $crc;
public $stat = -1;
public $stat_strict = -1;
public function __construct($w, $r, $s, $ss){
$this->region_id = (int)$r;
$this->original = $w;
$this->word = self::prepare($w);
$this->strict = '"!'.str_replace(' ', ' !', $this->word).'"';
$this->crc = crc32(str_replace(' ','-',$this->word));
$this->hash = crc32(str_replace(' ','-',$this->word).'-'.$this->region_id);
$this->stat = (int)$s;
$this->stat_strict = (int)$ss;
if(!isset(self::$bind_region[$this->region_id])) self::$bind_region[$this->region_id] = array();
// BIND link by region code
self::$bind_region[$this->region_id][$this->crc] = &$this;
// BIND link by crc32
self::$bind_crc[$this->crc][] = &$this;
}
public static function count($repeat = false){
if(!$repeat) return count(self::$bind_crc);
$i = 0;
foreach (self::$bind_crc as $crc => $a){
$i += count($a);
}
return $i;
}
public function setStat($stat){
$this->stat = $stat;
}
public function setStatStrict($stat){
$this->stat_strict = $stat;
}
public static function get($w, $r, $stat = -1, $stat_strict = -1){
if($i = self::findByRegionCRC($w, $r)){
if($stat >= 0) $i->stat = $stat;
if($stat_strict >= 0) $i->stat_strict = $stat_strict;
return $i;
}
$i = new self($w, $r, $stat, $stat_strict);
return $i;
}
public static function findComplete($region_id = false, $limit = false){
return self::findByStatus(true, $region_id, $limit);
}
public static function findIncomplete($region_id = false, $limit = false){
return self::findByStatus(false, $region_id, $limit);
}
public static function findByStatus($complete, $region_id, $limit = false){
$l = array();
$i = 0;
$complete = (boolean)$complete;
if($region_id){
$region_id = (int)$region_id;
if(!isset(self::$bind_region[$region_id])){
return $l;
}else{
foreach(self::$bind_region[$region_id] as $crc => $c){
if(($complete == false && ($c->stat < 0 || $c->stat_strict < 0)) || ($complete == true && $c->stat >= 0 && $c->stat_strict >= 0)){
$l[] = $c;
$i++;
}
if($limit && $limit <= $i) break 2;
}
}
}else{
foreach (self::$bind_crc as $cc){
foreach($cc as $crc => $c){
if(($complete == false && ($c->stat < 0 || $c->stat_strict < 0)) || ($complete == true && $c->stat >= 0 && $c->stat_strict >= 0)){
$l[] = $c;
$i++;
}
if($limit && $limit <= $i) break 2;
}
}
}
return $l;
}
/**
* Return array of YADWord found by region code
* @param int|string $r Region code
* @param array $filter Search Options
* return YADWord|boolean
*/
public static function findByRegion($r, $filter = array()){
$r = (int)$r;
if(isset(self::$bind_region[$r])){
if(!count($filter)) return self::$bind_region[$r];
else{
$l = array();
foreach(self::$bind_region[$r] as $crc => $c){
foreach($filer as $named => $val){
switch($named){
case 'original':
if($c->original != $val)
continue 2;
break;
case 'stat':
if($val && ($c->stat < 0 || $c->stat_strict < 0))
continue 2;
if(!$val && ($c->stat >= 0 && $c->stat_strict >= 0))
continue 2;
break;
case 'crc':
if($crc != $val)
continue 2;
break;
case 'hash':
if($c->hash != $val)
continue 2;
break;
}
}
$l[] = &$c;
}
}
}
return false;
}
/**
* Return copy of YADWord found by region code
* @param string $w Word
* @param int|string $r Region code
* return YADWord|boolean
*/
public static function findByRegionCRC($w, $r){
$word = self::prepare($w);
$r = (int)$r;
$crc = crc32(str_replace(' ','-',$word));
if(isset(self::$bind_region[$r][$crc])){
return self::$bind_region[$r][$crc];
}
return false;
}
/**
* Return found array of YADWord by CRC
* @param int|string $crc
* @return array|boolean
*/
public static function findByCRC($crc){
$crc = (int)$crc;
if(isset(self::$bind_crc[$crc])){
return self::$bind_crc[$crc];
}
return false;
}
/**
* Remove from string all invalid characters
* Leave only English, Russian, Turkish, Kazakh and Numbers
* @link http://www.unicode.org/charts/
* @param string $w
* @return string
*/
public static function prepare($w){
// АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМм
// НнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъ
// ЫыЬьЭэЮюЯя
$RUSSIAN = '\\x{0410}-\\x{045F}';
// çğışöü ÇĞİŞÖÜ
$TURKISH = '\\x{00E7}\\x{011F}\\x{0131}\\x{015F}'.
'\\x{00F6}\\x{00FC}\\x{00C7}\\x{011E}'.
'\\x{0130}\\x{015E}\\x{00D6}\\x{00DC}';
//ӘҒҚҢӨҮҰҺІ әғқңөүұһі
$KAZAKH = '\\x{04A2}\\x{04A3}\\x{0406}\\x{0456}'.
'\\x{0492}\\x{0493}\\x{049A}\\x{049B}'.
'\\x{04AE}\\x{04AF}\\x{04B0}\\x{04B1}'.
'\\x{04BA}\\x{04BB}\\x{04D8}\\x{04D9}'.
'\\x{04E8}\\x{04E9}';
$w = preg_replace('/[^a-z0-9'.$RUSSIAN.$TURKISH.$KAZAKH.']+/ui', ' ', $w);
$w = preg_replace('/[\s]+/', ' ', $w);
return mb_strtolower(trim($w),'UTF-8');
}
}