-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmmseg.cpp
141 lines (113 loc) · 3.21 KB
/
mmseg.cpp
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#include "php.h"
#include "php_mmseg.h"
}
#include "token.h"
#include "dict.h"
#include "algor.h"
int le_mmseg_algor;
#define PHP_MMSEG_ALGORITHM "MMSeg Algorithm"
static zend_function_entry mmseg_functions[] = {
PHP_FE(mmseg_load_chars, NULL)
PHP_FE(mmseg_load_words, NULL)
PHP_FE(mmseg_dic_add, NULL)
PHP_FE(mmseg_algor_create, NULL)
PHP_FE(mmseg_algor_destroy, NULL)
PHP_FE(mmseg_next_token, NULL)
{NULL, NULL, NULL}
};
zend_module_entry mmseg_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_MMSEG_EXTNAME,
mmseg_functions, /* Functions */
PHP_MINIT(mmseg), /* MINIT */
NULL, /* MSHUTDOWN */
NULL, /* RINIT */
NULL, /* RSHUTDOWN */
NULL, /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
PHP_MMSEG_EXTVER,
#endif
STANDARD_MODULE_PROPERTIES
};
extern "C" {
#ifdef COMPILE_DL_MMSEG
ZEND_GET_MODULE(mmseg)
#endif
}
PHP_MINIT_FUNCTION(mmseg) {
le_mmseg_algor = zend_register_list_destructors_ex(NULL, NULL, PHP_MMSEG_ALGORITHM, module_number);
return SUCCESS;
}
PHP_FUNCTION(mmseg_load_chars) {
char *path;
int path_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) {
RETURN_NULL();
}
if (rmmseg::dict::load_chars(path)) {
RETURN_LONG(1);
}
RETURN_LONG(0);
}
PHP_FUNCTION(mmseg_load_words) {
char *path;
int path_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) {
RETURN_NULL();
}
if (rmmseg::dict::load_words(path)) {
RETURN_LONG(1);
}
RETURN_LONG(0);
}
// TODO: !!
PHP_FUNCTION(mmseg_dic_add) {
char *word;
int len;
int freq;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &word, &len, &freq) == FAILURE) {
RETURN_NULL();
}
rmmseg::Word *w = rmmseg::make_word(word, len, freq, strlen(word));
rmmseg::dict::add(w);
}
PHP_FUNCTION(mmseg_algor_create) {
char *text;
int len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &text, &len) == FAILURE) {
RETURN_NULL();
}
rmmseg::Algorithm *algor = new rmmseg::Algorithm(text, len);
ZEND_REGISTER_RESOURCE(return_value, algor, le_mmseg_algor);
}
PHP_FUNCTION(mmseg_algor_destroy) {
rmmseg::Algorithm *algor;
zval *zalgor;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zalgor) == FAILURE) {
RETURN_NULL();
}
ZEND_FETCH_RESOURCE(algor, rmmseg::Algorithm*, &zalgor, -1, PHP_MMSEG_ALGORITHM, le_mmseg_algor);
delete algor;
}
PHP_FUNCTION(mmseg_next_token) {
rmmseg::Algorithm *algor;
zval *zalgor;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zalgor) == FAILURE) {
RETURN_NULL();
}
ZEND_FETCH_RESOURCE(algor, rmmseg::Algorithm*, &zalgor, -1, PHP_MMSEG_ALGORITHM, le_mmseg_algor);
rmmseg::Token rtk = algor->next_token();
if (rtk.length <= 0) {
RETURN_NULL();
}
array_init(return_value);
add_assoc_string(return_value, "text", estrndup(rtk.text, rtk.length), 1);
add_assoc_long(return_value, "offset", rtk.text - algor->get_text());
add_assoc_long(return_value, "length", rtk.length);
}