X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FZhClient.php;h=8bb36b231a897161d7efc83526ef1b9a5e2a3566;hb=3fd88e945d3b4048cd5a387a8b21b4d839fc3f0f;hp=4ca4e1fcdaa60e7b7d2b8a0461942fe74de0dac8;hpb=a26d5a49d755ff4b8039b11d1f26abb5d7bc7e8c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/ZhClient.php b/includes/ZhClient.php index 4ca4e1fcda..8bb36b231a 100644 --- a/includes/ZhClient.php +++ b/includes/ZhClient.php @@ -1,12 +1,7 @@ mHost = $host; $this->mPort = $port; $this->mConnected = $this->connect(); @@ -25,7 +23,7 @@ class ZhClient { /** * Check if connection to zhdaemon is successful * - * @access public + * @return bool */ function isconnected() { return $this->mConnected; @@ -35,12 +33,15 @@ class ZhClient { * Establish conncetion * * @access private + * + * @return bool */ function connect() { wfSuppressWarnings(); - $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30); + $errno = $errstr = ''; + $this->mFP = fsockopen( $this->mHost, $this->mPort, $errno, $errstr, 30 ); wfRestoreWarnings(); - if(!$this->mFP) { + if ( !$this->mFP ) { return false; } return true; @@ -50,100 +51,99 @@ class ZhClient { * Query the daemon and return the result * * @access private + * + * @return string */ - function query($request) { - if(!$this->mConnected) + function query( $request ) { + if ( !$this->mConnected ) { return false; + } - fwrite($this->mFP, $request); + fwrite( $this->mFP, $request ); - $result=fgets($this->mFP, 1024); + $result = fgets( $this->mFP, 1024 ); - list($status, $len) = explode(" ", $result); - if($status == 'ERROR') { - //$len is actually the error code... + list( $status, $len ) = explode( ' ', $result ); + if( $status == 'ERROR' ) { + // $len is actually the error code... print "zhdaemon error $len
\n"; return false; } - $bytesread=0; - $data=''; - while(!feof($this->mFP) && $bytesread<$len) { - $str= fread($this->mFP, $len-$bytesread); - $bytesread += strlen($str); + $bytesread = 0; + $data = ''; + while( !feof( $this->mFP ) && $bytesread < $len ) { + $str = fread( $this->mFP, $len - $bytesread ); + $bytesread += strlen( $str ); $data .= $str; } - //data should be of length $len. otherwise something is wrong - if(strlen($data) != $len) + // data should be of length $len. otherwise something is wrong + 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); + function convert( $text, $tolang ) { + $len = strlen( $text ); $q = "CONV $tolang $len\n$text"; - $result = $this->query($q); - if(!$result) + $result = $this->query( $q ); + if ( !$result ) { $result = $text; + } return $result; } /** * 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); + function convertToAllVariants( $text ) { + $len = strlen( $text ); $q = "CONV ALL $len\n$text"; - $result = $this->query($q); - if(!$result) + $result = $this->query( $q ); + if ( !$result ) { return false; - list($infoline, $data) = explode('|', $result, 2); - $info = explode(";", $infoline); + } + list( $infoline, $data ) = explode( '|', $result, 2 ); + $info = explode( ';', $infoline ); $ret = array(); - $i=0; - foreach($info as $variant) { - list($code, $len) = explode(' ', $variant); - $ret[strtolower($code)] = substr($data, $i, $len); - $r = $ret[strtolower($code)]; - $i+=$len; + $i = 0; + foreach( $info as $variant ) { + list( $code, $len ) = explode( ' ', $variant ); + $ret[strtolower( $code )] = substr( $data, $i, $len ); + $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); + 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); + $result = $this->query( $q ); + if ( !$result ) { // fallback to character based segmentation + $result = $this->segment( $text ); } return $result; } /** * Close the connection - * - * @access public */ function close() { - fclose($this->mFP); + fclose( $this->mFP ); } } -?> \ No newline at end of file