-
Notifications
You must be signed in to change notification settings - Fork 2
/
sm.c
279 lines (219 loc) · 7.79 KB
/
sm.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 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. |
+----------------------------------------------------------------------+
| Author: Ilya Sabelnikov <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id: header 297205 2010-03-30 21:09:07Z johannes $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_sm.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#if HAVE_MBSTRING
#include "ext/mbstring/mbstring.h"
#endif
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(sm) {
php_info_print_table_start();
php_info_print_table_header(2, "Dice's coefficient algorithm", "enabled");
php_info_print_table_row(2, "Version", PHP_SM_VERSION);
php_info_print_table_row(2, "Author", "Ilya Sabelnikov <[email protected]>");
php_info_print_table_end();
}
/* }}} */
/* {{{ proto string sm_version()
Return the php SM version */
PHP_FUNCTION(sm_version) {
if (return_value_used) {
RETURN_STRING(PHP_SM_VERSION, strlen(PHP_SM_VERSION));
} else {
php_printf("PHP SM Version: %s\n", PHP_SM_VERSION);
}
}
/* }}} */
static void sm_letter_pairs(const char *str, uint len, zval *return_value, int *pairs_count) /* {{{ */ {
mbfl_string mb_word, mb_result, *ret = NULL;
zval **tmp;
int i;
mbfl_string_init(&mb_word);
mb_word.no_language = mbfl_no_language_uni;
mb_word.no_encoding = mbfl_no_encoding_utf8;
mb_word.val = (unsigned char *) str;
mb_word.len = len;
int pairs_size = mbfl_strlen(&mb_word) - 1;
if (0 == pairs_size) {
return;
}
*pairs_count += pairs_size;
for (i = 0; i < pairs_size; i++) {
ret = mbfl_substr(&mb_word, &mb_result, i, 2);
if (SUCCESS == zend_hash_find(Z_ARRVAL_P(return_value), (const char *) ret->val, ret->len, (void **) &tmp)) {
Z_LVAL_PP(tmp)++;
} else {
add_assoc_long_ex(return_value, (const char *) ret->val, ret->len, 1);
}
mbfl_string_clear(&mb_result);
}
}
/* }}} */
static void sm_word_letter_pairs_exp(mbfl_string str, zval *return_value, int *pairs_count) /* {{{ */ {
char *delim = " ";
int delim_len = sizeof (" ") - 1;
zval zdelim, zstr;
zval *pairs_in_word = NULL;
zval **data;
HashPosition pos;
MAKE_STD_ZVAL(pairs_in_word);
array_init(pairs_in_word);
ZVAL_STRINGL(&zstr, str.val, str.len, 0);
ZVAL_STRINGL(&zdelim, delim, delim_len, 0);
php_explode(&zdelim, &zstr, pairs_in_word, LONG_MAX);
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pairs_in_word), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(pairs_in_word), (void**) &data, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(pairs_in_word), &pos)) {
if (0 < Z_STRLEN_PP(data)) {
sm_letter_pairs(Z_STRVAL_PP(data), (uint) Z_STRLEN_PP(data), return_value, pairs_count);
}
}
zval_ptr_dtor(&pairs_in_word);
}
/* }}} */
PHP_SM_API double sm_strike_match(const char *str_a_val, uint str_a_len, const char *str_b_val, uint str_b_len) /* {{{ */ {
zval *word_a_pairs;
zval *word_b_pairs;
int pairs_a_count = 0;
int pairs_b_count = 0;
HashTable *ht_pairs_short;
HashTable *ht_pairs_long;
HashPosition position;
mbfl_string str_a, str_b;
int intersection = 0;
int union_num = 0;
char *key;
uint key_len;
ulong index;
mbfl_string_init_set(&str_a, mbfl_no_language_uni, mbfl_no_encoding_utf8);
mbfl_string_init_set(&str_b, mbfl_no_language_uni, mbfl_no_encoding_utf8);
str_a.val = (unsigned char *) str_a_val;
str_a.len = str_a_len;
str_b.val = (unsigned char *) str_b_val;
str_b.len = str_b_len;
/* empty lines are equal and equal strings are matching at 100% */
if (0 == (str_a.len + str_b.len) || 0 == strcmp((const char *) str_a.val, (const char *) str_b.val)) {
return 1.0;
}
/* if one of strings is empty - strings is completely different */
if (0 == str_a.len || 0 == str_b.len) {
return 0.0;
}
MAKE_STD_ZVAL(word_a_pairs);
array_init(word_a_pairs);
MAKE_STD_ZVAL(word_b_pairs);
array_init(word_b_pairs);
sm_word_letter_pairs_exp(str_a, word_a_pairs, &pairs_a_count);
sm_word_letter_pairs_exp(str_b, word_b_pairs, &pairs_b_count);
union_num = pairs_a_count + pairs_b_count;
/* no pairs at all, does not matches at all */
if (0 == union_num) {
zval_ptr_dtor(&word_a_pairs);
zval_ptr_dtor(&word_b_pairs);
return 0.0;
}
if (pairs_a_count < pairs_b_count) {
ht_pairs_short = Z_ARRVAL_P(word_a_pairs);
ht_pairs_long = Z_ARRVAL_P(word_b_pairs);
} else {
ht_pairs_short = Z_ARRVAL_P(word_b_pairs);
ht_pairs_long = Z_ARRVAL_P(word_a_pairs);
}
zval **pp_pairs_short_count;
zval **pp_pairs_long_count;
for (zend_hash_internal_pointer_reset_ex(ht_pairs_short, &position);
SUCCESS == zend_hash_get_current_data_ex(ht_pairs_short, (void**) &pp_pairs_short_count, &position);
zend_hash_move_forward_ex(ht_pairs_short, &position)) {
zend_hash_get_current_key_ex(ht_pairs_short, &key, &key_len, &index, 0, &position);
if (SUCCESS == zend_hash_find(ht_pairs_long, key, key_len, (void**) &pp_pairs_long_count)) {
intersection += Z_LVAL_PP(pp_pairs_short_count) < Z_LVAL_PP(pp_pairs_long_count)
? Z_LVAL_PP(pp_pairs_short_count)
: Z_LVAL_PP(pp_pairs_long_count)
;
}
}
/* free useless variables */
zval_ptr_dtor(&word_a_pairs);
zval_ptr_dtor(&word_b_pairs);
return (2.0 * intersection) / union_num;
}
/* }}} */
/* {{{ proto double strike_match(string str_a, string str_b)
Calculates match between two strings. */
PHP_FUNCTION(strike_match) {
char *str_a_val;
char *str_b_val;
int str_a_len;
int str_b_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &str_a_val, &str_a_len, &str_b_val, &str_b_len) == FAILURE) {
return;
}
RETURN_DOUBLE(sm_strike_match(str_a_val, str_a_len, str_b_val, str_b_len));
}
/* }}}*/
PHP_MINFO_FUNCTION(sm);
PHP_FUNCTION(strike_match);
PHP_FUNCTION(sm_version);
/* {{{ sm_functions[]
*
* Every user visible function must have an entry in sm_functions[].
*/
const zend_function_entry sm_functions[] = {
PHP_FE(sm_version, NULL)
PHP_FE(strike_match, NULL) {
NULL, NULL, NULL
}
};
/* }}} */
/* {{{ sm_module_entry
*/
zend_module_entry sm_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"sm",
sm_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(sm),
#if ZEND_MODULE_API_NO >= 20010901
PHP_SM_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_SM
ZEND_GET_MODULE(sm)
#endif
/*
* 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
*/