From a2cdb7726f9a247a36c2e4da5359469e1f3fbcbb Mon Sep 17 00:00:00 2001 From: Vlad Krupin Date: Tue, 8 Aug 2000 18:46:41 +0000 Subject: [PATCH] added support for personalized dictionaries. new functions: pspell_new_personal(), pspell_save_wordlist(). pspell_save_wordlist() needs to be changed to return error-codes once pspell library gets fixed --- ext/pspell/php_pspell.h | 2 + ext/pspell/pspell.c | 123 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/ext/pspell/php_pspell.h b/ext/pspell/php_pspell.h index 21f931b827b..f3a7bfdd325 100644 --- a/ext/pspell/php_pspell.h +++ b/ext/pspell/php_pspell.h @@ -27,12 +27,14 @@ extern zend_module_entry pspell_module_entry; PHP_MINIT_FUNCTION(pspell); PHP_MINFO_FUNCTION(pspell); PHP_FUNCTION(pspell_new); +PHP_FUNCTION(pspell_new_personal); PHP_FUNCTION(pspell_check); PHP_FUNCTION(pspell_suggest); PHP_FUNCTION(pspell_store_replacement); PHP_FUNCTION(pspell_add_to_personal); PHP_FUNCTION(pspell_add_to_session); PHP_FUNCTION(pspell_clear_session); +PHP_FUNCTION(pspell_save_wordlist); #else #define pspell_module_ptr NULL #endif diff --git a/ext/pspell/pspell.c b/ext/pspell/pspell.c index 95c2fccbfb0..28aede9f123 100644 --- a/ext/pspell/pspell.c +++ b/ext/pspell/pspell.c @@ -40,12 +40,14 @@ function_entry pspell_functions[] = { PHP_FE(pspell_new, NULL) + PHP_FE(pspell_new_personal, NULL) PHP_FE(pspell_check, NULL) PHP_FE(pspell_suggest, NULL) PHP_FE(pspell_store_replacement, NULL) PHP_FE(pspell_add_to_personal, NULL) PHP_FE(pspell_add_to_session, NULL) PHP_FE(pspell_clear_session, NULL) + PHP_FE(pspell_save_wordlist, NULL) }; static int le_pspell; @@ -62,7 +64,6 @@ static void php_pspell_close(PspellManager *manager){ delete_pspell_manager(manager); } - PHP_MINIT_FUNCTION(pspell){ REGISTER_MAIN_LONG_CONSTANT("PSPELL_FAST", PSPELL_FAST, CONST_PERSISTENT | CONST_CS); REGISTER_MAIN_LONG_CONSTANT("PSPELL_NORMAL", PSPELL_NORMAL, CONST_PERSISTENT | CONST_CS); @@ -148,6 +149,86 @@ PHP_FUNCTION(pspell_new){ } /* }}} */ +/* {{{ proto int pspell_new_personal(string personal, string language [, string spelling [, string jargon [, string encoding [, int mode]]]]) + Load a dictionary with a personal wordlist*/ +PHP_FUNCTION(pspell_new_personal){ + zval **personal, **language,**spelling,**jargon,**encoding,**pmode; + long mode = 0L, speed = 0L; + int argc; + int ind; + + PspellCanHaveError *ret; + PspellManager *manager; + PspellConfig *config; + + argc = ZEND_NUM_ARGS(); + if (argc < 2 || argc > 6 || zend_get_parameters_ex(argc,&personal,&language,&spelling,&jargon,&encoding,&pmode) == FAILURE) { + WRONG_PARAM_COUNT; + } + + config = new_pspell_config(); + + convert_to_string_ex(personal); + pspell_config_replace(config, "personal", (*personal)->value.str.val); + + convert_to_string_ex(language); + pspell_config_replace(config, "language-tag", (*language)->value.str.val); + + if(argc > 2){ + convert_to_string_ex(spelling); + if((*spelling)->value.str.len > 0){ + pspell_config_replace(config, "spelling", (*spelling)->value.str.val); + } + } + + if(argc > 3){ + convert_to_string_ex(jargon); + if((*jargon)->value.str.len > 0){ + pspell_config_replace(config, "jargon", (*jargon)->value.str.val); + } + } + + if(argc > 4){ + convert_to_string_ex(encoding); + if((*encoding)->value.str.len > 0){ + pspell_config_replace(config, "encoding", (*encoding)->value.str.val); + } + } + + if(argc > 5){ + convert_to_long_ex(pmode); + mode = Z_LVAL_PP(pmode); + speed = mode & PSPELL_SPEED_MASK_INTERNAL; + + /* First check what mode we want (how many suggestions) */ + if(speed == PSPELL_FAST){ + pspell_config_replace(config, "sug-mode", "fast"); + }else if(speed == PSPELL_NORMAL){ + pspell_config_replace(config, "sug-mode", "normal"); + }else if(speed == PSPELL_BAD_SPELLERS){ + pspell_config_replace(config, "sug-mode", "bad-spellers"); + } + + /* Then we see if run-together words should be treated as valid components */ + if(mode & PSPELL_RUN_TOGETHER){ + pspell_config_replace(config, "run-together", "true"); + } + } + + ret = new_pspell_manager(config); + delete_pspell_config(config); + + if(pspell_error_number(ret) != 0){ + php_error(E_WARNING, "PSPELL couldn't open the dictionary. reason: %s ", pspell_error_message(ret)); + RETURN_FALSE; + } + + manager = to_pspell_manager(ret); + ind = zend_list_insert(manager, le_pspell); + RETURN_LONG(ind); +} +/* }}} */ + /* {{{ proto int pspell_check(int pspell, string word) Returns true if word is valid */ PHP_FUNCTION(pspell_check){ @@ -219,7 +300,6 @@ PHP_FUNCTION(pspell_suggest) } /* }}} */ - /* {{{ proto int pspell_store_replacement(int pspell, string misspell, string correct) Notify the dictionary of a user-selected replacement */ PHP_FUNCTION(pspell_store_replacement) @@ -285,7 +365,6 @@ PHP_FUNCTION(pspell_add_to_personal) } /* }}} */ - /* {{{ proto int pspell_add_to_session(int pspell, string word) Adds a word to the current session */ PHP_FUNCTION(pspell_add_to_session) @@ -318,7 +397,6 @@ PHP_FUNCTION(pspell_add_to_session) } /* }}} */ - /* {{{ proto int pspell_clear_session(int pspell) Clears the current session */ PHP_FUNCTION(pspell_clear_session) @@ -350,6 +428,43 @@ PHP_FUNCTION(pspell_clear_session) } /* }}} */ +/* {{{ proto int pspell_save_wordlist(int pspell) + Saves the current (personal) wordlist */ +PHP_FUNCTION(pspell_save_wordlist) +{ + int type; + zval **scin; + PspellManager *manager; + + int argc; + argc = ZEND_NUM_ARGS(); + if (argc != 1 || zend_get_parameters_ex(argc, &scin) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_long_ex(scin); + manager = (PspellManager *) zend_list_find((*scin)->value.lval, &type); + if(!manager){ + php_error(E_WARNING, "%d is not an PSPELL result index",(*scin)->value.lval); + RETURN_FALSE; + } + + pspell_manager_save_all_word_lists(manager); + RETURN_TRUE; +/* FIXME: The following should be back in place once Kevin Atkinson + (the author of pspell & aspell) fixes pspell so that + pspell_manager_save_all_word_lists() does not attempt to write to $HOME/.*.prepl + Current version (.11.1) generates an error that can be safely ignored. + if(pspell_manager_error_number(manager) == 0){ + RETURN_TRUE; + }else{ + php_error(E_WARNING, "pspell_save_wordlist() gave error: %s", pspell_manager_error_message(manager)); + RETURN_FALSE; + } +*/ +} +/* }}} */ + PHP_MINFO_FUNCTION(pspell) {