sq_snowball.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4. #ifdef USE_SNOWBALL
  5. #include "squirrel.h"
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h> /* for malloc */
  9. #include <assert.h> /* for a few sanity tests */
  10. SQ_OPT_STRING_STRLEN();
  11. #include "libstemmer.h"
  12. static const SQChar SQ_LIBNAME[] = _SC("Snowball");
  13. static const SQChar SNOWBALL_Tag[] = _SC("sq_Snowball");
  14. #define GET_Snowball_INSTANCE() SQ_GET_INSTANCE(v, 1, sb_stemmer, SNOWBALL_Tag) \
  15. if(self == NULL) return sq_throwerror(v, _SC("Snowball object already closed"));
  16. static SQRESULT Snowball_release_hook(SQUserPointer p, SQInteger size, void */*ep*/)
  17. {
  18. sb_stemmer *self = (sb_stemmer*)p;
  19. if(self) sb_stemmer_delete(self);
  20. return 0;
  21. }
  22. static SQRESULT sq_Snowball_constructor(HSQUIRRELVM v)
  23. {
  24. SQ_FUNC_VARS(v);
  25. SQ_GET_STRING(v, 2, algorithm);
  26. SQ_OPT_STRING(v, 3, charenc, NULL);
  27. sb_stemmer *Snowball = sb_stemmer_new(algorithm, charenc);
  28. if(!Snowball)
  29. return sq_throwerror(v, _SC("Could'nt create an Snowball object."));
  30. SQInteger rc = sq_setinstanceup(v, 1, Snowball);
  31. sq_setreleasehook(v, 1, Snowball_release_hook);
  32. return rc;
  33. }
  34. // int sb_stemmer_length(struct sb_stemmer * stemmer)
  35. static SQRESULT sq_Snowball_length(HSQUIRRELVM v){
  36. SQ_FUNC_VARS_NO_TOP(v);
  37. GET_Snowball_INSTANCE();
  38. sq_pushinteger(v, sb_stemmer_length(self));
  39. return 1;
  40. }
  41. // const sb_symbol * sb_stemmer_stem(struct sb_stemmer * stemmer, const sb_symbol * word, int size)
  42. static SQRESULT sq_Snowball_stem(HSQUIRRELVM v){
  43. SQ_FUNC_VARS_NO_TOP(v);
  44. GET_Snowball_INSTANCE();
  45. SQ_GET_STRING(v, 2, str_word);
  46. const sb_symbol *res = sb_stemmer_stem(self,
  47. (const sb_symbol*)str_word, str_word_size);
  48. if(res) sq_pushstring(v, (const SQChar*)res, -1);
  49. else sq_pushnull(v);
  50. return 1;
  51. }
  52. // const char ** sb_stemmer_list(void)
  53. static SQRESULT sq_Snowball_list(HSQUIRRELVM v){
  54. const char ** list = sb_stemmer_list();
  55. if (*list == 0) sq_pushnull(v);
  56. else
  57. {
  58. SQInteger lcount = 0;
  59. const char **ptr = list;
  60. while(*(ptr++)) ++lcount;
  61. ptr = list;
  62. sq_newarray(v, lcount);
  63. for(SQInteger i=0; i < lcount; ++i)
  64. {
  65. sq_pushstring(v, *(ptr++), -1);
  66. sq_arrayset(v, -2, i);
  67. }
  68. }
  69. return 1;
  70. }
  71. #define _DECL_SNOWBALL_FUNC(name,nparams,pmask) {_SC(#name),sq_Snowball_##name,nparams,pmask}
  72. static SQRegFunction Snowball_obj_funcs[]={
  73. _DECL_SNOWBALL_FUNC(constructor, -2, _SC("xss")),
  74. _DECL_SNOWBALL_FUNC(length, 1, _SC("x")),
  75. _DECL_SNOWBALL_FUNC(stem, 2, _SC("xs")),
  76. _DECL_SNOWBALL_FUNC(list, 1, _SC(".")),
  77. {0,0}
  78. };
  79. #undef _DECL_SNOWBALL_FUNC
  80. /* This defines a function that opens up your library. */
  81. SQRESULT sqext_register_Snowball (HSQUIRRELVM v) {
  82. //add a namespace Snowball
  83. sq_pushstring(v, SQ_LIBNAME, -1);
  84. sq_newclass(v,SQFalse);
  85. sq_settypetag(v,-1,(SQUserPointer)SNOWBALL_Tag);
  86. sq_insert_reg_funcs(v, Snowball_obj_funcs);
  87. sq_newslot(v,-3,SQFalse); //add Snowball table to the root table
  88. return SQ_OK;
  89. }
  90. #ifdef __cplusplus
  91. }
  92. #endif //USE_SNOWBALL
  93. #endif