ZhClient.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Client for querying zhdaemon
  4. *
  5. */
  6. class ZhClient {
  7. var $mHost, $mPort, $mFP, $mConnected;
  8. /**
  9. * Constructor
  10. *
  11. * @access private
  12. */
  13. function ZhClient($host, $port) {
  14. $this->mHost = $host;
  15. $this->mPort = $port;
  16. $this->mConnected = $this->connect();
  17. }
  18. /**
  19. * Check if connection to zhdaemon is successful
  20. */
  21. function isconnected() {
  22. return $this->mConnected;
  23. }
  24. /**
  25. * Establish conncetion
  26. *
  27. * @access private
  28. */
  29. function connect() {
  30. wfSuppressWarnings();
  31. $errno = $errstr = '';
  32. $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
  33. wfRestoreWarnings();
  34. if(!$this->mFP) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. /**
  40. * Query the daemon and return the result
  41. *
  42. * @access private
  43. */
  44. function query($request) {
  45. if(!$this->mConnected)
  46. return false;
  47. fwrite($this->mFP, $request);
  48. $result=fgets($this->mFP, 1024);
  49. list($status, $len) = explode(" ", $result);
  50. if($status == 'ERROR') {
  51. //$len is actually the error code...
  52. print "zhdaemon error $len<br />\n";
  53. return false;
  54. }
  55. $bytesread=0;
  56. $data='';
  57. while(!feof($this->mFP) && $bytesread<$len) {
  58. $str= fread($this->mFP, $len-$bytesread);
  59. $bytesread += strlen($str);
  60. $data .= $str;
  61. }
  62. //data should be of length $len. otherwise something is wrong
  63. if(strlen($data) != $len)
  64. return false;
  65. return $data;
  66. }
  67. /**
  68. * Convert the input to a different language variant
  69. *
  70. * @param $text string: input text
  71. * @param $tolang string: language variant
  72. * @return string the converted text
  73. */
  74. function convert($text, $tolang) {
  75. $len = strlen($text);
  76. $q = "CONV $tolang $len\n$text";
  77. $result = $this->query($q);
  78. if(!$result)
  79. $result = $text;
  80. return $result;
  81. }
  82. /**
  83. * Convert the input to all possible variants
  84. *
  85. * @param $text string: input text
  86. * @return array langcode => converted_string
  87. */
  88. function convertToAllVariants($text) {
  89. $len = strlen($text);
  90. $q = "CONV ALL $len\n$text";
  91. $result = $this->query($q);
  92. if(!$result)
  93. return false;
  94. list($infoline, $data) = explode('|', $result, 2);
  95. $info = explode(";", $infoline);
  96. $ret = array();
  97. $i=0;
  98. foreach($info as $variant) {
  99. list($code, $len) = explode(' ', $variant);
  100. $ret[strtolower($code)] = substr($data, $i, $len);
  101. $i+=$len;
  102. }
  103. return $ret;
  104. }
  105. /**
  106. * Perform word segmentation
  107. *
  108. * @param $text string: input text
  109. * @return string segmented text
  110. */
  111. function segment($text) {
  112. $len = strlen($text);
  113. $q = "SEG $len\n$text";
  114. $result = $this->query($q);
  115. if(!$result) {// fallback to character based segmentation
  116. $result = ZhClientFake::segment($text);
  117. }
  118. return $result;
  119. }
  120. /**
  121. * Close the connection
  122. */
  123. function close() {
  124. fclose($this->mFP);
  125. }
  126. }