-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstem.c
162 lines (144 loc) · 4.45 KB
/
stem.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2009 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Snowball sources copyright (c) M.F. Porter |
+----------------------------------------------------------------------+
| Author: Jay Smith <[email protected]> |
| Olivier Hill <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_stem.h"
/* {{{ stem_functions[]
*/
zend_function_entry stem_functions[] = {
PHP_FE(stem, NULL)
PHP_FE(stem_algos, NULL)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ stem_module_entry
*/
zend_module_entry stem_module_entry = {
STANDARD_MODULE_HEADER,
"stem",
stem_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(stem),
PHP_STEM_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_STEM
ZEND_GET_MODULE(stem)
#endif
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(stem)
{
php_info_print_table_start();
php_info_print_table_row(2, "Stem support", "enabled");
php_info_print_table_row(2, "Version", PHP_STEM_VERSION);
php_info_print_table_row(2, "Supported Algorithms",
#define STEMMER(php_func, c_func, constant, name) \
name " "
#include "stemmers.def"
#undef STEMMER
);
php_info_print_table_end();
}
/* }}} */
/* {{{ string stem(string word, string algo)
word is a string to be stemmed, algo is the stemming algorithm to be used.
Returns false on Snowball error or the stemmed word on success.
*/
PHP_FUNCTION(stem)
{
struct SN_env* z;
struct SN_env* (*create_env)(void);
void (*close_env)(struct SN_env*);
int (*stem)(struct SN_env*);
char * word;
char * algo;
int word_len;
int algo_len;
short int algo_found = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &algo, &algo_len, &word, &word_len) == FAILURE) {
return;
}
/* Empty string */
if (word_len <= 0) {
RETURN_STRINGL(word, word_len, 1);
}
#ifdef PHP_WIN32
#define INIT_FUNCS(x) \
create_env = (struct SN_env*) x ## _create_env; \
stem = (int*) x ## _stem; \
close_env = (void*) x ## _close_env;
#else
#define INIT_FUNCS(x) \
create_env = x ## _create_env; \
stem = x ## _stem; \
close_env = x ## _close_env;
#endif
#define STEMMER(php_func, c_func, constant, name) \
if (strcmp(name, algo) == 0) { \
INIT_FUNCS(c_func) \
algo_found = 1; \
}
#include "stemmers.def"
#undef STEMMER
#undef INIT_FUNCS
// TODO
if (algo_found == 0) {
php_error(E_NOTICE, "%s() couldn't stem word, unknown stemming algorithm", get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
z = create_env();
SN_set_current(z, word_len, word);
php_strtolower(z->p, word_len);
stem(z);
z->p[z->l]= '\0';
RETVAL_STRINGL(z->p, z->l, 1);
close_env(z);
}
/* }}} */
/* {{{ proto array stem_algos(void)
Return a list of registered stemming algorithms */
PHP_FUNCTION(stem_algos)
{
array_init(return_value);
# define STEMMER(php_func, c_func, constant, name) \
add_next_index_stringl(return_value, name, sizeof(name)-1, 1);
# include "stemmers.def"
# undef STEMMER
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 tw=78 fdm=marker
* vim<600: sw=4 ts=4 tw=78
*/