X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FZhClient.php;h=a04220c6a195d3696dc6911bf054c3712e6772a6;hb=3629f272c403ed3addd067a5143a5be84b18150a;hp=13b195eda95b6674c05235e393027a7aa9974fe6;hpb=4b42f18b66e843f362e5e159d95fee9d2c26ef9c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/ZhClient.php b/includes/ZhClient.php index 13b195eda9..a04220c6a1 100644 --- a/includes/ZhClient.php +++ b/includes/ZhClient.php @@ -1,10 +1,9 @@ mHost = $host; $this->mPort = $port; $this->mConnected = $this->connect(); @@ -21,8 +20,6 @@ class ZhClient { /** * Check if connection to zhdaemon is successful - * - * @access public */ function isconnected() { return $this->mConnected; @@ -35,9 +32,10 @@ class ZhClient { */ function connect() { wfSuppressWarnings(); + $errno = $errstr = ''; $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30); wfRestoreWarnings(); - if(!$this->mFP) { + if ( !$this->mFP ) { return false; } return true; @@ -49,8 +47,9 @@ class ZhClient { * @access private */ function query($request) { - if(!$this->mConnected) + if ( !$this->mConnected ) { return false; + } fwrite($this->mFP, $request); @@ -70,41 +69,42 @@ class ZhClient { $data .= $str; } //data should be of length $len. otherwise something is wrong - if(strlen($data) != $len) + if ( strlen($data) != $len ) { return false; + } return $data; } /** * Convert the input to a different language variant * - * @param string $text input text - * @param string $tolang language variant + * @param $text string: input text + * @param $tolang string: language variant * @return string the converted text - * @access public */ function convert($text, $tolang) { $len = strlen($text); $q = "CONV $tolang $len\n$text"; $result = $this->query($q); - if(!$result) + if ( !$result ) { $result = $text; + } return $result; } /** - * Convert the input to all possible variants + * Convert the input to all possible variants * - * @param string $text input text + * @param $text string: input text * @return array langcode => converted_string - * @access public - */ + */ function convertToAllVariants($text) { $len = strlen($text); $q = "CONV ALL $len\n$text"; $result = $this->query($q); - if(!$result) + if ( !$result ) { return false; + } list($infoline, $data) = explode('|', $result, 2); $info = explode(";", $infoline); $ret = array(); @@ -112,169 +112,30 @@ class ZhClient { foreach($info as $variant) { list($code, $len) = explode(' ', $variant); $ret[strtolower($code)] = substr($data, $i, $len); - $r = $ret[strtolower($code)]; $i+=$len; } return $ret; - } + } /** * Perform word segmentation * - * @param string $text input text + * @param $text string: input text * @return string segmented text - * @access public */ function segment($text) { $len = strlen($text); $q = "SEG $len\n$text"; $result = $this->query($q); - if(!$result) {// fallback to character based segmentation - $result = ZhClientFake::segment($text); + if ( !$result ) {// fallback to character based segmentation + $result = $this->segment($text); } return $result; } /** * Close the connection - * - * @access public */ function close() { fclose($this->mFP); } } - - -class ZhClientFake { - function ZhClientFake() { - global $wgMemc, $wgDBname; - $this->zh2TW = $wgMemc->get($key1 = "$wgDBname:zhConvert:tw"); - $this->zh2CN = $wgMemc->get($key2 = "$wgDBname:zhConvert:cn"); - $this->zh2SG = $wgMemc->get($key3 = "$wgDBname:zhConvert:sg"); - $this->zh2HK = $wgMemc->get($key4 = "$wgDBname:zhConvert:hk"); - if(empty($this->zh2TW) || empty($this->zh2CN) || empty($this->zh2SG) || empty($this->zh2HK)) { - require("includes/ZhConversion.php"); - $this->zh2TW = $zh2TW; - $this->zh2CN = $zh2CN; - $this->zh2HK = $zh2HK; - $this->zh2SG = $zh2SG; - $wgMemc->set($key1, $this->zh2TW); - $wgMemc->set($key2, $this->zh2CN); - $wgMemc->set($key3, $this->zh2SG); - $wgMemc->set($key4, $this->zh2HK); - } - } - - function isconnected() { - return true; - } - - /** - * Convert to zh-tw - * - * @access private - */ - function zh2tw($text) { - return strtr($text, $this->zh2TW); - } - - /** - * Convert to zh-cn - * - * @access private - */ - function zh2cn($text) { - return strtr($text, $this->zh2CN); - } - - /** - * Convert to zh-sg - * - * @access private - */ - function zh2sg($text) { - return strtr(strtr($text, $this->zh2CN), $this->zh2SG); - } - - /** - * Convert to zh-hk - * - * @access private - */ - function zh2hk($text) { - return strtr(strtr($text, $this->zh2TW), $this->zh2HK); - } - - /** - * Convert the input to a different language variant - * - * @param string $text input text - * @param string $tolang language variant - * @return string the converted text - * @access public - */ - function convert($text, $tolang) { - $t = ''; - switch($tolang) { - case 'zh-cn': - $t = $this->zh2cn($text); - break; - case 'zh-tw': - $t = $this->zh2tw($text); - break; - case 'zh-sg': - $t = $this->zh2sg($text); - break; - case 'zh-hk': - $t = $this->zh2hk($text); - break; - default: - $t = $text; - } - return $t; - } - - function convertToAllVariants($text) { - $ret = array(); - $ret['zh-cn'] = $this->zh2cn($text); - $ret['zh-tw'] = $this->zh2tw($text); - $ret['zh-sg'] = $this->zh2sg($text); - $ret['zh-hk'] = $this->zh2hk($text); - return $ret; - } - - /** - * Perform "fake" word segmentation, i.e. treating each character as a word - * - * @param string $text input text - * @return string segmented text - * @access public - */ - function segment($text) { - /* adapted from LanguageZh_cn::stripForSearch() - here we will first separate the single characters, - and let the caller conver it to hex - */ - if( function_exists( 'mb_strtolower' ) ) { - return preg_replace( - "/([\\xc0-\\xff][\\x80-\\xbf]*)/e", - "' ' .\"$1\"", - mb_strtolower( $text ) ); - } else { - global $wikiLowerChars; - return preg_replace( - "/([\\xc0-\\xff][\\x80-\\xbf]*)/e", - "' ' . strtr( \"\$1\", \$wikiLowerChars )", - $text ); - } - } - - /** - * Close the fake connection - * - * @access public - */ - function close() { } -} - -?> \ No newline at end of file