forked from ixqbar/phpjieba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jieba.c
376 lines (311 loc) · 9.31 KB
/
jieba.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_jieba.h"
#include <unistd.h>
ZEND_DECLARE_MODULE_GLOBALS(jieba)
static int le_jieba;
#ifndef BUFSIZE
#define BUFSIZE 2048
#endif
#define JZ_JIEBA_DICT_NAME "jieba.dict.utf8"
#define JZ_JIEBA_DICT_HMM_NAME "hmm_model.utf8"
#define JZ_JIEBA_USER_DICT_NAME "user.dict.utf8"
#define JZ_JIEBA_IDF_NAME "idf.utf8"
#define JZ_JIEBA_STP_WORDS_NAME "stop_words.utf8"
ZEND_BEGIN_ARG_INFO_EX(arg_info_jieba, 0, 0, 3)
ZEND_ARG_INFO(0, sentence)
ZEND_ARG_INFO(0, extract)
ZEND_ARG_INFO(0, extract_limit)
ZEND_END_ARG_INFO()
/* {{{ jieba_functions[]
*
* Every user visible function must have an entry in jieba_functions[].
*/
const zend_function_entry jieba_functions[] = {
PHP_FE(jieba, arg_info_jieba)
PHP_FE_END
};
/* }}} */
/* {{{ jieba_module_entry
*/
zend_module_entry jieba_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"jieba",
jieba_functions,
PHP_MINIT(jieba),
PHP_MSHUTDOWN(jieba),
PHP_RINIT(jieba),
PHP_RSHUTDOWN(jieba),
PHP_MINFO(jieba),
#if ZEND_MODULE_API_NO >= 20010901
PHP_JIEBA_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_JIEBA
ZEND_GET_MODULE(jieba)
#endif
static int jieba_init()
{
int not_change = 1;
if (JIEBA_G(enable) != INI_INT("jieba.enable")) {
JIEBA_G(enable) = (zend_bool)INI_INT("jieba.enable");
not_change = 0;
}
if (JIEBA_G(dict_path) != INI_STR("jieba.dict_path")) {
JIEBA_G(dict_path) = INI_STR("jieba.dict_path");
not_change = 0;
}
if (1 == not_change
|| JIEBA_G(enable) == 0
|| JIEBA_G(dict_path) == ""
|| JIEBA_G(dict_path) == NULL) {
return FAILURE;
}
size_t jz_dict_path_len = strlen(JIEBA_G(dict_path));
char dict_path[BUFSIZE];
char dict_hmm_path[BUFSIZE];
char user_dict_path[BUFSIZE];
char idf_path[BUFSIZE];
char stop_words_path[BUFSIZE];
memcpy(dict_path, JIEBA_G(dict_path), jz_dict_path_len);
memcpy(dict_hmm_path, JIEBA_G(dict_path), jz_dict_path_len);
memcpy(user_dict_path, JIEBA_G(dict_path), jz_dict_path_len);
memcpy(idf_path, JIEBA_G(dict_path), jz_dict_path_len);
memcpy(stop_words_path, JIEBA_G(dict_path), jz_dict_path_len);
if (dict_path[jz_dict_path_len - 1] != '/') {
dict_path[jz_dict_path_len] = '/';
dict_hmm_path[jz_dict_path_len] = '/';
user_dict_path[jz_dict_path_len] = '/';
idf_path[jz_dict_path_len] = '/';
stop_words_path[jz_dict_path_len] = '/';
jz_dict_path_len += 1;
}
memcpy(dict_path + jz_dict_path_len, JZ_JIEBA_DICT_NAME, sizeof(JZ_JIEBA_DICT_NAME));
dict_path[jz_dict_path_len + sizeof(JZ_JIEBA_DICT_NAME)] = 0;
memcpy(dict_hmm_path + jz_dict_path_len, JZ_JIEBA_DICT_HMM_NAME, sizeof(JZ_JIEBA_DICT_HMM_NAME));
dict_hmm_path[jz_dict_path_len + sizeof(JZ_JIEBA_DICT_HMM_NAME)] = 0;
memcpy(user_dict_path + jz_dict_path_len, JZ_JIEBA_USER_DICT_NAME, sizeof(JZ_JIEBA_USER_DICT_NAME));
user_dict_path[jz_dict_path_len + sizeof(JZ_JIEBA_USER_DICT_NAME)] = 0;
memcpy(idf_path + jz_dict_path_len, JZ_JIEBA_IDF_NAME, sizeof(JZ_JIEBA_IDF_NAME));
idf_path[jz_dict_path_len + sizeof(JZ_JIEBA_IDF_NAME)] = 0;
memcpy(stop_words_path + jz_dict_path_len, JZ_JIEBA_STP_WORDS_NAME, sizeof(JZ_JIEBA_STP_WORDS_NAME));
stop_words_path[jz_dict_path_len + sizeof(JZ_JIEBA_STP_WORDS_NAME)] = 0;
if (access(dict_path, R_OK|F_OK) != 0
|| access(dict_hmm_path, R_OK|F_OK) != 0
|| access(user_dict_path, R_OK|F_OK) != 0
|| access(idf_path, R_OK|F_OK) != 0
|| access(stop_words_path, R_OK|F_OK) != 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error jieba dict path %s in php.ini", dict_path);
return FAILURE;
}
if (JIEBA_G(jieba) != NULL) {
FreeJieba(JIEBA_G(jieba));
JIEBA_G(jieba) = NULL;
}
if (JIEBA_G(extractor) != NULL) {
FreeExtractor(JIEBA_G(extractor));
JIEBA_G(extractor) = NULL;
}
JIEBA_G(jieba) = NewJieba(dict_path, dict_hmm_path, user_dict_path, idf_path, stop_words_path);
JIEBA_G(extractor) = NewExtractor(dict_path, dict_hmm_path, idf_path, stop_words_path, user_dict_path);
return SUCCESS;
}
static void jieba_deinit()
{
if (JIEBA_G(jieba) != NULL) {
FreeJieba(JIEBA_G(jieba));
JIEBA_G(jieba) = NULL;
}
if (JIEBA_G(extractor) != NULL) {
FreeExtractor(JIEBA_G(extractor));
JIEBA_G(extractor) = NULL;
}
}
PHP_INI_BEGIN()
PHP_INI_ENTRY("jieba.enable", "0", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("jieba.dict_path", "", PHP_INI_ALL, NULL)
PHP_INI_END()
static void php_jieba_init_globals(zend_jieba_globals *jieba_globals)
{
jieba_globals->enable = 0;
jieba_globals->jieba = NULL;
jieba_globals->extractor = NULL;
jieba_globals->dict_path = NULL;
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(jieba)
{
ZEND_INIT_MODULE_GLOBALS(jieba, php_jieba_init_globals, NULL);
REGISTER_INI_ENTRIES();
jieba_init();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(jieba)
{
jieba_deinit();
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(jieba)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(jieba)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(jieba)
{
php_info_print_table_start();
php_info_print_table_header(2, "jieba support", "enabled");
php_info_print_table_row(2, "version", PHP_JIEBA_VERSION);
php_info_print_table_row(2, "author", "QQ:174171262");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
PHP_FUNCTION(jieba)
{
char *sentence = NULL;
#if PHP_MAJOR_VERSION >= 7
size_t sentence_len = 0;
zend_long action = 0;
zend_long limit = 50;
#else
int sentence_len = 0;
long action = 0;
long limit = 50;
#endif
jieba_init();
if (JIEBA_G(enable) == 0) {
php_error_docref(NULL, E_WARNING, "phpjieba not enable in your php.ini");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &sentence, &sentence_len, &action, &limit) == FAILURE
|| sentence_len == 0
|| action > 3
|| action < 0
|| limit <= 0) {
RETURN_FALSE;
}
CJiebaWord *words;
CJiebaWord *x;
zval arr;
array_init(return_value);
switch (action) {
case 0:
words = Extract(JIEBA_G(extractor), sentence, sentence_len, limit);
break;
case 1:
words = CutForSearch(JIEBA_G(jieba), sentence, sentence_len);
break;
case 2:
case 3:
words = CutWithTag(JIEBA_G(jieba), sentence, sentence_len);
break;
}
int total = 0, index = 0, next_index;
for (x = words; x && x->word; x++) {
total++;
}
while (index < total) {
if (limit <= 0) {
break;
}
switch ( action ) {
case 0:
case 1:
#if PHP_MAJOR_VERSION >= 7
add_next_index_stringl(return_value, words[index].word, words[index].len);
#else
add_next_index_stringl(return_value, words[index].word, words[index].len, 1);
#endif
limit--;
break;
case 2:
next_index = index + 1;
#if PHP_MAJOR_VERSION >= 7
add_assoc_stringl_ex(return_value, words[index].word, words[index].len, (char *)words[next_index].word, words[next_index].len);
#else
add_assoc_stringl_ex(return_value, words[index].word, words[index].len + 1, (char *)words[next_index].word, words[next_index].len, 1);
#endif
index++;
break;
case 3:
array_init(&arr);
next_index = index + 1;
#if PHP_MAJOR_VERSION >= 7
add_assoc_stringl_ex(&arr, "Word", 4, (char *)words[index].word, words[index].len);
add_assoc_stringl_ex(&arr, "Pos", 3, (char *)words[next_index].word, words[next_index].len);
add_next_index_zval(return_value, &arr);
#else
add_assoc_stringl_ex(&arr, "Word", 4, (char *)words[index].word, words[index].len, 1);
add_assoc_stringl_ex(&arr, "Pos", 3, (char *)words[next_index].word, words[next_index].len, 1);
add_next_index_zval(return_value, &arr);
#endif
index++;
break;
default:
break;
}
if (words[index].free) {
free((char *)words[index].word);
words[index].free = 0;
}
if (index > 0 && words[index - 1].free) {
free((char *)words[index - 1].word);
words[index - 1].free = 0;
}
index++;
}
FreeWords(words);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/