| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #define NEED_sv_2pv_flags |
| 6 |
|
| #define NEED_sv_2pv_nolen |
| 7 |
|
| #include "../ppport.h" |
| 8 |
|
| |
| 9 |
|
| #define NUMLANG 1 |
| 10 |
|
| #define NUMSTEM 1 |
| 11 |
|
| |
| 12 |
|
| #include "include/libstemmer.h" |
| 13 |
|
| |
| 14 |
|
| /* All Lingua::Stem::Snowball::Lt objects and all calls to stem(), |
| 15 |
|
| * stem_in_place(), etc, reference the same set of Snowball struct |
| 16 |
|
| * sb_stemmers, all held in the singleton object |
| 17 |
|
| * $Lingua::Stem::Snowball::Lt::stemmifier, of class |
| 18 |
|
| * Lingua::Stem::Snowball::Lt::Stemmifier. Each sb_stemmer is created lazily, as |
| 19 |
|
| * soon as there is a need for its unique combination of language and |
| 20 |
|
| * encoding. They are destroyed during global cleanup, when |
| 21 |
|
| * $Lingua::Stem::Snowball::Lt::stemmifier is reclaimed. |
| 22 |
|
| */ |
| 23 |
|
| |
| 24 |
|
| typedef struct Stemmifier { |
| 25 |
|
| struct sb_stemmer **stemmers; |
| 26 |
|
| } Stemmifier; |
| 27 |
|
| |
| 28 |
|
| typedef struct LangEnc { |
| 29 |
|
| char *lang; |
| 30 |
|
| char *encoding; /* the real name of the encoding */ |
| 31 |
|
| char *snowenc; /* the variant that libstemmer_c needs */ |
| 32 |
|
| } LangEnc; |
| 33 |
|
| |
| 34 |
|
| LangEnc lang_encs[] = { |
| 35 |
|
| { "lt", "UTF-8", "UTF_8" }, |
| 36 |
|
| }; |
| 37 |
|
| |
| 38 |
|
| MODULE = Lingua::Stem::Snowball::Lt PACKAGE = Lingua::Stem::Snowball::Lt |
| 39 |
|
| |
| 40 |
|
| PROTOTYPES: disable |
| 41 |
|
| |
| 42 |
|
| BOOT: |
| 43 |
|
| { |
| 44 |
4 |
| SV *sb_stemmer_list_sv = newSViv(PTR2IV(sb_stemmer_list)); |
| 45 |
4 |
| SV *sb_stemmer_new_sv = newSViv(PTR2IV(sb_stemmer_new)); |
| 46 |
4 |
| SV *sb_stemmer_delete_sv = newSViv(PTR2IV(sb_stemmer_delete)); |
| 47 |
4 |
| SV *sb_stemmer_stem_sv = newSViv(PTR2IV(sb_stemmer_stem)); |
| 48 |
4 |
| SV *sb_stemmer_length_sv = newSViv(PTR2IV(sb_stemmer_length)); |
| 49 |
4 |
| hv_store(PL_modglobal, "Lingua::Stem::Snowball::Lt::sb_stemmer_list", 39, |
| 50 |
|
| sb_stemmer_list_sv, 0); |
| 51 |
4 |
| hv_store(PL_modglobal, "Lingua::Stem::Snowball::Lt::sb_stemmer_new", 38, |
| 52 |
|
| sb_stemmer_new_sv, 0); |
| 53 |
4 |
| hv_store(PL_modglobal, "Lingua::Stem::Snowball::Lt::sb_stemmer_delete", 41, |
| 54 |
|
| sb_stemmer_delete_sv, 0); |
| 55 |
4 |
| hv_store(PL_modglobal, "Lingua::Stem::Snowball::Lt::sb_stemmer_stem", 39, |
| 56 |
|
| sb_stemmer_stem_sv, 0); |
| 57 |
4 |
| hv_store(PL_modglobal, "Lingua::Stem::Snowball::Lt::sb_stemmer_length", 41, |
| 58 |
|
| sb_stemmer_length_sv, 0); |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
|
| void |
| 62 |
|
| _derive_stemmer(self_hash) |
| 63 |
|
| HV *self_hash; |
| 64 |
|
| PPCODE: |
| 65 |
|
| { |
| 66 |
|
| SV **sv_ptr; |
| 67 |
|
| char *lang; |
| 68 |
|
| char *encoding; |
| 69 |
|
| int i; |
| 70 |
65 |
| int stemmer_id = -1; |
| 71 |
|
| |
| 72 |
|
| /* Extract lang and encoding member variables. */ |
| 73 |
65 |
| sv_ptr = hv_fetch(self_hash, "lang", 4, 0); |
| 74 |
65 |
▶ | if (!sv_ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_ptr) |
0 |
65 |
65 |
|
| 75 |
0 |
| croak("Couldn't find member variable 'lang'"); |
| 76 |
65 |
| lang = SvPV_nolen(*sv_ptr); |
| 77 |
65 |
| sv_ptr = hv_fetch(self_hash, "encoding", 8, 0); |
| 78 |
65 |
▶ | if (!sv_ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_ptr) |
0 |
65 |
65 |
|
| 79 |
0 |
| croak("Couldn't find member variable 'encoding'"); |
| 80 |
65 |
| encoding = SvPV_nolen(*sv_ptr); |
| 81 |
|
| |
| 82 |
|
| /* See if the combo of lang and encoding is supported. */ |
| 83 |
65 |
▶ | for(i = 0; i < NUMSTEM; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < NUMSTEM; i++) { |
65 |
0 |
65 |
|
| 84 |
65 |
▶ | if ( strcmp(lang, lang_encs[i].lang) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| if ( strcmp(lang, lang_encs[i].lang) == 0 |
65 |
0 |
65 |
|
| 85 |
65 |
▶ | && strcmp(encoding, lang_encs[i].encoding) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| && strcmp(encoding, lang_encs[i].encoding) == 0 |
65 |
0 |
65 |
|
| 86 |
|
| ) { |
| 87 |
|
| Stemmifier *stemmifier; |
| 88 |
|
| SV *stemmifier_sv; |
| 89 |
|
| |
| 90 |
|
| /* We have a match, so we know the stemmer id now. */ |
| 91 |
65 |
| stemmer_id = i; |
| 92 |
|
| |
| 93 |
|
| /* Retrieve communal Stemmifier. */ |
| 94 |
65 |
| stemmifier_sv = get_sv("Lingua::Stem::Snowball::Lt::stemmifier", TRUE); |
| 95 |
65 |
▶ | if ( sv_isobject(stemmifier_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( sv_isobject(stemmifier_sv) |
65 |
0 |
65 |
|
| 96 |
65 |
▶ | && sv_derived_from(stemmifier_sv, |
Branch: 1 branch
| Branch | True | False | Total |
| && sv_derived_from(stemmifier_sv, |
65 |
0 |
65 |
|
| 97 |
|
| "Lingua::Stem::Snowball::Lt::Stemmifier") |
| 98 |
65 |
| ) { |
| 99 |
65 |
| IV tmp = SvIV(SvRV(stemmifier_sv)); |
| 100 |
65 |
| stemmifier = INT2PTR(Stemmifier*, tmp); |
| 101 |
|
| } |
| 102 |
|
| else { |
| 103 |
0 |
| croak("$L::S::S::stemmifier isn't a Stemmifier"); |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| /* Construct a stemmer for lang/enc if there isn't one yet. */ |
| 107 |
65 |
▶ | if ( ! stemmifier->stemmers[stemmer_id] ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! stemmifier->stemmers[stemmer_id] ) { |
2 |
63 |
65 |
|
| 108 |
2 |
| stemmifier->stemmers[stemmer_id] |
| 109 |
2 |
| = sb_stemmer_new(lang, lang_encs[stemmer_id].snowenc); |
| 110 |
2 |
▶ | if ( ! stemmifier->stemmers[stemmer_id] ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! stemmifier->stemmers[stemmer_id] ) { |
0 |
2 |
2 |
|
| 111 |
0 |
| croak("Failed to allocate an sb_stemmer for %s %s", lang, |
| 112 |
|
| encoding); |
| 113 |
|
| } |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
65 |
| break; |
| 117 |
|
| } |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| /* Set the value of $self->{stemmer_id}. */ |
| 121 |
65 |
| sv_ptr = hv_fetch(self_hash, "stemmer_id", 10, 0); |
| 122 |
65 |
▶ | if (!sv_ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_ptr) |
0 |
65 |
65 |
|
| 123 |
0 |
| croak("Couldn't access $self->{stemmer_id}"); |
| 124 |
65 |
| sv_setiv(*sv_ptr, stemmer_id); |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
|
| bool |
| 128 |
|
| _validate_language(language) |
| 129 |
|
| char *language; |
| 130 |
|
| CODE: |
| 131 |
|
| { |
| 132 |
|
| int i; |
| 133 |
0 |
| RETVAL = FALSE; |
| 134 |
0 |
▶ | for (i = 0; i < NUMLANG; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < NUMLANG; i++) { |
0 |
0 |
0 |
|
| 135 |
0 |
▶ | if ( strcmp(language, lang_encs[i].lang) == 0 ) RETVAL = TRUE; |
Branch: 1 branch
| Branch | True | False | Total |
| if ( strcmp(language, lang_encs[i].lang) == 0 ) RETVAL = TRUE; |
0 |
0 |
0 |
|
| 136 |
|
| } |
| 137 |
|
| } |
| 138 |
|
| OUTPUT: RETVAL |
| 139 |
|
| |
| 140 |
|
| void |
| 141 |
|
| stem_in_place(self_hash, words_av) |
| 142 |
|
| HV *self_hash; |
| 143 |
|
| AV *words_av; |
| 144 |
|
| PPCODE: |
| 145 |
|
| { |
| 146 |
|
| IV stemmer_id; |
| 147 |
|
| SV **sv_ptr; |
| 148 |
|
| Stemmifier *stemmifier; |
| 149 |
|
| SV *stemmifier_sv; |
| 150 |
|
| |
| 151 |
|
| /* Retrieve the stemmifier. */ |
| 152 |
244 |
| stemmifier_sv = get_sv("Lingua::Stem::Snowball::Lt::stemmifier", TRUE); |
| 153 |
244 |
▶ | if ( sv_isobject(stemmifier_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( sv_isobject(stemmifier_sv) |
244 |
0 |
244 |
|
| 154 |
244 |
▶ | && sv_derived_from(stemmifier_sv, "Lingua::Stem::Snowball::Lt::Stemmifier") |
Branch: 1 branch
| Branch | True | False | Total |
| && sv_derived_from(stemmifier_sv, "Lingua::Stem::Snowball::Lt::Stemmifier") |
244 |
0 |
244 |
|
| 155 |
244 |
| ) { |
| 156 |
244 |
| IV tmp = SvIV(SvRV(stemmifier_sv)); |
| 157 |
244 |
| stemmifier = INT2PTR(Stemmifier*, tmp); |
| 158 |
|
| } |
| 159 |
|
| else { |
| 160 |
0 |
| croak("$Lingua::Stem::Snowball::Lt::stemmifier isn't a Stemmifier"); |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
|
| /* Figure out which sb_stemmer to use. */ |
| 164 |
244 |
| sv_ptr = hv_fetch(self_hash, "stemmer_id", 10, 0); |
| 165 |
244 |
▶ | if (!sv_ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_ptr) |
0 |
244 |
244 |
|
| 166 |
0 |
| croak("Couldn't access stemmer_id"); |
| 167 |
244 |
| stemmer_id = SvIV(*sv_ptr); |
| 168 |
244 |
▶ | if ( stemmer_id < 0 |
Branch: 1 branch
| Branch | True | False | Total |
| if ( stemmer_id < 0 |
244 |
0 |
244 |
|
| 169 |
244 |
▶ | || stemmer_id >= NUMSTEM |
Branch: 1 branch
| Branch | True | False | Total |
| || stemmer_id >= NUMSTEM |
244 |
0 |
244 |
|
| 170 |
244 |
▶ | || stemmifier->stemmers[stemmer_id] == NULL |
Branch: 1 branch
| Branch | True | False | Total |
| || stemmifier->stemmers[stemmer_id] == NULL |
0 |
244 |
244 |
|
| 171 |
|
| ) { |
| 172 |
0 |
| dSP; |
| 173 |
0 |
| ENTER; |
| 174 |
0 |
| SAVETMPS; |
| 175 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 176 |
0 |
▶ | XPUSHs(ST(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(ST(0)); |
0 |
0 |
0 |
|
| 177 |
0 |
| PUTBACK; |
| 178 |
0 |
| call_method("_derive_stemmer", G_DISCARD); |
| 179 |
0 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
0 |
0 |
0 |
|
| 180 |
0 |
| LEAVE; |
| 181 |
|
| |
| 182 |
|
| /* Extract what should now be a valid stemmer_id. */ |
| 183 |
0 |
| sv_ptr = hv_fetch(self_hash, "stemmer_id", 10, 0); |
| 184 |
0 |
| stemmer_id = SvIV(*sv_ptr); |
| 185 |
|
| } |
| 186 |
244 |
▶ | if (stemmer_id != -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stemmer_id != -1) { |
244 |
0 |
244 |
|
| 187 |
244 |
| struct sb_stemmer *stemmer = stemmifier->stemmers[stemmer_id]; |
| 188 |
|
| IV i, max; |
| 189 |
|
| |
| 190 |
724 |
▶ | for (i = 0, max = av_len(words_av); i <= max; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0, max = av_len(words_av); i <= max; i++) { |
480 |
244 |
724 |
|
| 191 |
480 |
| sv_ptr = av_fetch(words_av, i, 0); |
| 192 |
480 |
▶ | if (SvOK(*sv_ptr)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(*sv_ptr)) { |
480 |
0 |
480 |
|
| 193 |
|
| STRLEN len; |
| 194 |
480 |
| sb_symbol *input_text = (sb_symbol*)SvPV(*sv_ptr, len); |
| 195 |
|
| const sb_symbol *stemmed_output |
| 196 |
480 |
| = sb_stemmer_stem(stemmer, input_text, (int)len); |
| 197 |
480 |
| len = sb_stemmer_length(stemmer); |
| 198 |
480 |
| sv_setpvn(*sv_ptr, (char*)stemmed_output, len); |
| 199 |
|
| } |
| 200 |
|
| } |
| 201 |
|
| } |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
|
| MODULE = Lingua::Stem::Snowball::Lt PACKAGE = Lingua::Stem::Snowball::Lt::Stemmifier |
| 205 |
|
| |
| 206 |
|
| SV* |
| 207 |
|
| new(class_name) |
| 208 |
|
| char* class_name; |
| 209 |
|
| CODE: |
| 210 |
|
| { |
| 211 |
|
| Stemmifier *stemmifier; |
| 212 |
4 |
| New(0, stemmifier, 1, Stemmifier); |
| 213 |
4 |
| Newz(0, stemmifier->stemmers, NUMSTEM, struct sb_stemmer*); |
| 214 |
4 |
| RETVAL = newSV(0); |
| 215 |
4 |
| sv_setref_pv(RETVAL, class_name, (void*)stemmifier); |
| 216 |
|
| } |
| 217 |
|
| OUTPUT: RETVAL |
| 218 |
|
| |
| 219 |
|
| void |
| 220 |
|
| DESTROY(self_sv) |
| 221 |
|
| SV *self_sv; |
| 222 |
|
| PPCODE: |
| 223 |
|
| { |
| 224 |
|
| int i; |
| 225 |
4 |
| IV temp = SvIV( SvRV(self_sv) ); |
| 226 |
4 |
| Stemmifier *stemmifier = INT2PTR(Stemmifier*, temp); |
| 227 |
8 |
▶ | for (i = 0; i < NUMSTEM; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < NUMSTEM; i++) { |
4 |
4 |
8 |
|
| 228 |
4 |
▶ | if (stemmifier->stemmers[i] != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (stemmifier->stemmers[i] != NULL) |
2 |
2 |
4 |
|
| 229 |
2 |
| sb_stemmer_delete(stemmifier->stemmers[i]); |
| 230 |
|
| } |
| 231 |
4 |
| Safefree(stemmifier); |
| 232 |
|
| } |
| 233 |
|
| |