3 * Client for querying zhdaemon
9 var $mHost, $mPort, $mFP, $mConnected;
16 function ZhClient($host, $port) {
19 $this->mConnected
= $this->connect();
23 * Check if connection to zhdaemon is successful
27 function isconnected() {
28 return $this->mConnected
;
32 * Establish conncetion
38 $this->mFP
= fsockopen($this->mHost
, $this->mPort
, $errno, $errstr, 30);
47 * Query the daemon and return the result
51 function query($request) {
52 if(!$this->mConnected
)
55 fwrite($this->mFP
, $request);
57 $result=fgets($this->mFP
, 1024);
59 list($status, $len) = explode(" ", $result);
60 if($status == 'ERROR') {
61 //$len is actually the error code...
62 print "zhdaemon error $len<br />\n";
67 while(!feof($this->mFP
) && $bytesread<$len) {
68 $str= fread($this->mFP
, $len-$bytesread);
69 $bytesread +
= strlen($str);
72 //data should be of length $len. otherwise something is wrong
73 if(strlen($data) != $len)
79 * Convert the input to a different language variant
81 * @param string $text input text
82 * @param string $tolang language variant
83 * @return string the converted text
86 function convert($text, $tolang) {
88 $q = "CONV $tolang $len\n$text";
89 $result = $this->query($q);
96 * Convert the input to all possible variants
98 * @param string $text input text
99 * @return array langcode => converted_string
102 function convertToAllVariants($text) {
103 $len = strlen($text);
104 $q = "CONV ALL $len\n$text";
105 $result = $this->query($q);
108 list($infoline, $data) = explode('|', $result, 2);
109 $info = explode(";", $infoline);
112 foreach($info as $variant) {
113 list($code, $len) = explode(' ', $variant);
114 $ret[strtolower($code)] = substr($data, $i, $len);
115 $r = $ret[strtolower($code)];
121 * Perform word segmentation
123 * @param string $text input text
124 * @return string segmented text
127 function segment($text) {
128 $len = strlen($text);
129 $q = "SEG $len\n$text";
130 $result = $this->query($q);
131 if(!$result) {// fallback to character based segmentation
132 $result = ZhClientFake
::segment($text);
138 * Close the connection
149 function ZhClientFake() {
150 global $wgMemc, $wgDBname;
151 $this->zh2TW
= $wgMemc->get($key1 = "$wgDBname:zhConvert:tw");
152 $this->zh2CN
= $wgMemc->get($key2 = "$wgDBname:zhConvert:cn");
153 $this->zh2SG
= $wgMemc->get($key3 = "$wgDBname:zhConvert:sg");
154 $this->zh2HK
= $wgMemc->get($key4 = "$wgDBname:zhConvert:hk");
155 if(empty($this->zh2TW
) ||
empty($this->zh2CN
) ||
empty($this->zh2SG
) ||
empty($this->zh2HK
)) {
156 require("includes/ZhConversion.php");
157 $this->zh2TW
= $zh2TW;
158 $this->zh2CN
= $zh2CN;
159 $this->zh2HK
= $zh2HK;
160 $this->zh2SG
= $zh2SG;
161 $wgMemc->set($key1, $this->zh2TW
);
162 $wgMemc->set($key2, $this->zh2CN
);
163 $wgMemc->set($key3, $this->zh2SG
);
164 $wgMemc->set($key4, $this->zh2HK
);
168 function isconnected() {
177 function zh2tw($text) {
178 return strtr($text, $this->zh2TW
);
186 function zh2cn($text) {
187 return strtr($text, $this->zh2CN
);
195 function zh2sg($text) {
196 return strtr(strtr($text, $this->zh2CN
), $this->zh2SG
);
204 function zh2hk($text) {
205 return strtr(strtr($text, $this->zh2TW
), $this->zh2HK
);
209 * Convert the input to a different language variant
211 * @param string $text input text
212 * @param string $tolang language variant
213 * @return string the converted text
216 function convert($text, $tolang) {
220 $t = $this->zh2cn($text);
223 $t = $this->zh2tw($text);
226 $t = $this->zh2sg($text);
229 $t = $this->zh2hk($text);
237 function convertToAllVariants($text) {
239 $ret['zh-cn'] = $this->zh2cn($text);
240 $ret['zh-tw'] = $this->zh2tw($text);
241 $ret['zh-sg'] = $this->zh2sg($text);
242 $ret['zh-hk'] = $this->zh2hk($text);
247 * Perform "fake" word segmentation, i.e. treating each character as a word
249 * @param string $text input text
250 * @return string segmented text
253 function segment($text) {
254 /* adapted from LanguageZh_cn::stripForSearch()
255 here we will first separate the single characters,
256 and let the caller conver it to hex
258 if( function_exists( 'mb_strtolower' ) ) {
260 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
262 mb_strtolower( $text ) );
264 global $wikiLowerChars;
266 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
267 "' ' . strtr( \"\$1\", \$wikiLowerChars )",
273 * Close the fake connection