-
Notifications
You must be signed in to change notification settings - Fork 0
/
CKIP.php
135 lines (122 loc) · 2.9 KB
/
CKIP.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
<?php
class Term{
public $term;
public $pos;
function __construct ($t,$p){
$this->term = $t;
$this->pos = $p;
}
}
class CKIP{
public $user;
protected $passwd;
public $serverip;
public $serverport;
protected $sock;
public $data_send;
public $data_recv;
public function __construct ($ip,$port,$user,$passwd){
$this->user = $user;
$this->passwd = $passwd;
$this->serverip = $ip;
$this->serverport= $port;
$this->sock = $this->connect();
}
private function connect(){
$addr='tcp://'.$this->serverip.':'.$this->serverport;
return stream_socket_client($addr);
}
public function getsock(){
return $this->sock;
}
public function disconnect(){
if ($this->sock) fclose($this->sock);
}
public function query($text){
$head = <<<_HEADER_
<?xml version="1.0" ?>
<wordsegmentation version="0.1">
<option showcategory="1" />
<authentication username="$this->user" password="$this->passwd" />
<text>
_HEADER_;
$footer = <<<_FOOT_
</text>
</wordsegmentation>
_FOOT_;
$this->data_send = $text;
$text = str_replace ("&"," ",$text);
$querystr = $head.$text.$footer;
$tempxml = simplexml_load_string($querystr);
$resp =array();
if ($tempxml){
if (stream_socket_sendto($this->sock, $tempxml->asXML())){
do {
$ttt=stream_socket_recvfrom($this->sock, 65525);
$ttt = iconv('big5','utf-8',$ttt);
$resp[] = $ttt;
}while (! simplexml_load_string( implode ($resp)));
return $this->data_recv = html_entity_decode(implode($resp));
}
}else{
$this->data_recv =0;
return null;
}
}
public function getTerm(){
$term = Array();
$resp = $this->data_recv;
$resp = htmlspecialchars_decode($resp);
$xml_resp = simplexml_load_string($resp);
if ($xml_resp){
$sentence = $xml_resp->xpath('result/sentence');
if ($sentence){
foreach ($sentence as $line){
$line = (string)$line;
foreach( split(" ",$line) as $word){
if ($word != ""){
preg_match("/(\S*)\((\S*)\)/",$word,$pos);
$t = new Term(strtolower($pos[1]),$pos[2]);
$term[] = $t;
}
}
}
}
}
return $term;
}
public function getSents(){
$sents = Array();
$resp = $this->data_recv;
$xml_resp = simplexml_load_string($resp);
if ($xml_resp){
$sentence = $xml_resp->xpath('result/sentence');
foreach ($sentence as $line){
$line = (string)$line;
$sent = Array();
foreach( split(" ",$line) as $word){
//echo count($temp);
if ($word != ""){
preg_match("/(\S*)\((\S*)\)/",$word,$pos);
$br_array = Array('PARENTHESISCATEGORY',
'COMMACATEGORY',
'PERIODCATEGORY',
'ETCCATEGORY',
'QUESTIONCATEGORY',
'PAUSECATEGORY',
'SEMICOLONCATEGORY');
if ( in_array ($pos[2] ,$br_array )){
//break;
}else{
$t = new Term(strtolower($pos[1]),$pos[2]);
$sent[] = $t;
}
}
}
$sents[] = $sent;
}
}
return $sents;
}
}
?>