| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| #include "ppport.h" |
| 5 |
|
| |
| 6 |
|
| #include "SENNA_API.h" |
| 7 |
|
| |
| 8 |
|
| #define UNUSED(x) (void)(x) |
| 9 |
|
| typedef void* Lingua__EN__SENNA; |
| 10 |
|
| |
| 11 |
|
| MODULE = Lingua::EN::SENNA PACKAGE = Lingua::EN::SENNA |
| 12 |
|
| |
| 13 |
|
| PROTOTYPES: ENABLE |
| 14 |
|
| |
| 15 |
|
| Lingua::EN::SENNA |
| 16 |
|
| new(package) |
| 17 |
|
| char* package |
| 18 |
|
| CODE: |
| 19 |
|
| UNUSED(package); |
| 20 |
|
| STRLEN len; |
| 21 |
4 |
| SV* SENNA_path = get_sv("Lingua::EN::SENNA::SENNA_path",0); |
| 22 |
4 |
| char* value = SvPV(SENNA_path, len); |
| 23 |
4 |
| RETVAL=SENNA_new(value); |
| 24 |
|
| OUTPUT: |
| 25 |
|
| RETVAL |
| 26 |
|
| |
| 27 |
|
| void |
| 28 |
|
| DESTROY(SENNA_object) |
| 29 |
|
| Lingua::EN::SENNA SENNA_object |
| 30 |
|
| |
| 31 |
|
| AV* |
| 32 |
|
| tokenize(SENNA_object, sentences) |
| 33 |
|
| Lingua::EN::SENNA SENNA_object |
| 34 |
|
| AV* sentences |
| 35 |
|
| CODE: |
| 36 |
2 |
| int i, sz = av_len(sentences); |
| 37 |
|
| STRLEN len; |
| 38 |
2 |
| AV* tokenization_results = newAV(); |
| 39 |
6 |
▶ | for (i=0; i<=sz; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<=sz; i++) { |
4 |
2 |
6 |
|
| 40 |
4 |
| SV** sentence = av_fetch(sentences, i, 0); |
| 41 |
4 |
▶ | if (sentence != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sentence != NULL) { |
4 |
0 |
4 |
|
| 42 |
|
| AV* sentence_av; |
| 43 |
8 |
| SENNA_Tokens* sentence_tokens = SENNA_Tokenize_sentence((SENNA_fields*)SENNA_object, SvPV(*sentence, len)); |
| 44 |
4 |
| sentence_av = newAV(); |
| 45 |
|
| int token_index; |
| 46 |
27 |
▶ | for (token_index=0; token_index < sentence_tokens->n; token_index++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (token_index=0; token_index < sentence_tokens->n; token_index++) { |
23 |
4 |
27 |
|
| 47 |
23 |
| char* current_word = sentence_tokens->words[token_index]; |
| 48 |
23 |
| SV* word_sv = newSVpv(current_word,strlen(current_word)); |
| 49 |
23 |
| av_push(sentence_av,word_sv); |
| 50 |
|
| } |
| 51 |
4 |
| av_push(tokenization_results, newRV_inc((SV*)sentence_av)); |
| 52 |
|
| } |
| 53 |
|
| } |
| 54 |
2 |
| RETVAL = tokenization_results; |
| 55 |
|
| OUTPUT: |
| 56 |
|
| RETVAL |
| 57 |
|
| |
| 58 |
|
| |
| 59 |
|
| |
| 60 |
|
| AV* pos_tag(Lingua_SENNA_object, sentences) |
| 61 |
|
| Lingua::EN::SENNA Lingua_SENNA_object |
| 62 |
|
| AV* sentences |
| 63 |
|
| CODE: |
| 64 |
2 |
| SENNA_fields* SENNA_object = (SENNA_fields*) Lingua_SENNA_object; |
| 65 |
2 |
| int i, sz = av_len(sentences); |
| 66 |
|
| STRLEN len; |
| 67 |
2 |
| AV* pos_results = newAV(); |
| 68 |
6 |
▶ | for (i=0; i<=sz; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<=sz; i++) { |
4 |
2 |
6 |
|
| 69 |
4 |
| SV** sentence = av_fetch(sentences, i, 0); |
| 70 |
4 |
▶ | if (sentence != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sentence != NULL) { |
4 |
0 |
4 |
|
| 71 |
|
| AV* sentence_av; |
| 72 |
8 |
| SENNA_Tokens* sentence_tokens = SENNA_Tokenize_sentence(SENNA_object,SvPV(*sentence,len)); |
| 73 |
4 |
| int *pos_labels = NULL; |
| 74 |
4 |
| pos_labels = SENNA_POS_forward(SENNA_object->pos, sentence_tokens->word_idx, sentence_tokens->caps_idx, sentence_tokens->suff_idx, sentence_tokens->n); |
| 75 |
4 |
| sentence_av = newAV(); |
| 76 |
|
| int token_index; |
| 77 |
27 |
▶ | for (token_index=0; token_index < sentence_tokens->n; token_index++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (token_index=0; token_index < sentence_tokens->n; token_index++) { |
23 |
4 |
27 |
|
| 78 |
23 |
| char* current_word = sentence_tokens->words[token_index]; |
| 79 |
23 |
| HV* word_hv = newHV(); |
| 80 |
23 |
| SV* word_sv = newSVpv(current_word,strlen(current_word)); |
| 81 |
23 |
| const char* pos_label = SENNA_Hash_key(SENNA_object->pos_hash, pos_labels[token_index]); |
| 82 |
23 |
| SV* pos_sv = newSVpv(pos_label,strlen(pos_label)); |
| 83 |
23 |
| hv_store(word_hv,"word",4,word_sv,0); |
| 84 |
23 |
| hv_store(word_hv,"POS",3,pos_sv,0); |
| 85 |
23 |
| av_push(sentence_av,newRV_inc((SV*)word_hv)); |
| 86 |
|
| } |
| 87 |
4 |
| av_push(pos_results, newRV_inc((SV*)sentence_av)); |
| 88 |
|
| } |
| 89 |
|
| } |
| 90 |
2 |
| RETVAL = pos_results; |
| 91 |
|
| OUTPUT: |
| 92 |
|
| RETVAL |
| 93 |
|
| |
| 94 |
|
| AV* analyze(Lingua_SENNA_object, sentences, options) |
| 95 |
|
| Lingua::EN::SENNA Lingua_SENNA_object |
| 96 |
|
| AV* sentences |
| 97 |
|
| HV* options |
| 98 |
|
| CODE: |
| 99 |
|
| /* First, retrieve options */ |
| 100 |
|
| //char *opt_path = NULL; |
| 101 |
|
| //int opt_verbose = 0; |
| 102 |
4 |
| int opt_notokentags = 0; |
| 103 |
|
| //int opt_offsettags = 0; |
| 104 |
|
| //int opt_iobtags = 0; |
| 105 |
|
| //int opt_brackettags = 0; |
| 106 |
|
| //int opt_posvbs = 0; |
| 107 |
|
| //int opt_usrtokens = 0; |
| 108 |
4 |
| int opt_pos = 0; |
| 109 |
4 |
| int opt_chk = 0; |
| 110 |
4 |
| int opt_ner = 0; |
| 111 |
4 |
| int opt_srl = 0; |
| 112 |
4 |
| int opt_psg = 0; |
| 113 |
4 |
▶ | if hv_exists(options,"POS",3) { opt_pos = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if hv_exists(options,"POS",3) { opt_pos = 1; } |
4 |
0 |
4 |
|
| 114 |
4 |
▶ | if hv_exists(options,"CHK",3) { opt_pos = 1; opt_chk = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if hv_exists(options,"CHK",3) { opt_pos = 1; opt_chk = 1; } |
2 |
2 |
4 |
|
| 115 |
4 |
▶ | if hv_exists(options,"NER",3) { opt_pos = 1; opt_ner = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if hv_exists(options,"NER",3) { opt_pos = 1; opt_ner = 1; } |
2 |
2 |
4 |
|
| 116 |
4 |
▶ | if hv_exists(options,"SRL",3) { opt_pos = 1; opt_chk = 1; opt_srl = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if hv_exists(options,"SRL",3) { opt_pos = 1; opt_chk = 1; opt_srl = 1; } |
2 |
2 |
4 |
|
| 117 |
4 |
▶ | if hv_exists(options,"PSG",3) { opt_pos = 1; opt_chk = 1; opt_psg = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if hv_exists(options,"PSG",3) { opt_pos = 1; opt_chk = 1; opt_psg = 1; } |
2 |
2 |
4 |
|
| 118 |
|
| |
| 119 |
|
| /* Next, analyze sentences as requested */ |
| 120 |
4 |
| SENNA_fields* SENNA_object = (SENNA_fields*) Lingua_SENNA_object; |
| 121 |
4 |
| int i, j, sz = av_len(sentences); |
| 122 |
|
| STRLEN len; |
| 123 |
4 |
| AV* results = newAV(); |
| 124 |
12 |
▶ | for (i=0; i<=sz; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<=sz; i++) { |
8 |
4 |
12 |
|
| 125 |
8 |
| SV** sentence = av_fetch(sentences, i, 0); |
| 126 |
8 |
▶ | if (sentence == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sentence == NULL) |
0 |
8 |
8 |
|
| 127 |
0 |
| continue; |
| 128 |
8 |
| AV* sentence_av = newAV(); |
| 129 |
16 |
| SENNA_Tokens* sentence_tokens = SENNA_Tokenize_sentence(SENNA_object,SvPV(*sentence,len)); |
| 130 |
8 |
▶ | if(sentence_tokens->n == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(sentence_tokens->n == 0) |
0 |
8 |
8 |
|
| 131 |
0 |
| continue; |
| 132 |
8 |
| int *pos_labels = NULL; |
| 133 |
8 |
| int *chk_labels = NULL; |
| 134 |
8 |
| int *pt0_labels = NULL; |
| 135 |
8 |
| int *ner_labels = NULL; |
| 136 |
8 |
| int *vbs_labels = NULL; |
| 137 |
8 |
| int **srl_labels = NULL; |
| 138 |
8 |
| int *psg_labels = NULL; |
| 139 |
8 |
| int n_psg_level = 0; |
| 140 |
8 |
| int is_psg_one_segment = 0; |
| 141 |
8 |
| int vbs_hash_novb_idx = 22; |
| 142 |
8 |
| int n_verbs = 0; |
| 143 |
|
| |
| 144 |
8 |
▶ | if(opt_pos) |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_pos) |
8 |
0 |
8 |
|
| 145 |
8 |
| pos_labels = SENNA_POS_forward(SENNA_object->pos, sentence_tokens->word_idx, sentence_tokens->caps_idx, sentence_tokens->suff_idx, sentence_tokens->n); |
| 146 |
8 |
▶ | if(opt_chk) |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_chk) |
4 |
4 |
8 |
|
| 147 |
4 |
| chk_labels = SENNA_CHK_forward(SENNA_object->chk, sentence_tokens->word_idx, sentence_tokens->caps_idx, pos_labels, sentence_tokens->n); |
| 148 |
8 |
▶ | if(opt_srl) |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_srl) |
4 |
4 |
8 |
|
| 149 |
4 |
| pt0_labels = SENNA_PT0_forward(SENNA_object->pt0, sentence_tokens->word_idx, sentence_tokens->caps_idx, pos_labels, sentence_tokens->n); |
| 150 |
8 |
▶ | if(opt_ner) |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_ner) |
4 |
4 |
8 |
|
| 151 |
4 |
| ner_labels = SENNA_NER_forward(SENNA_object->ner, sentence_tokens->word_idx, sentence_tokens->caps_idx, sentence_tokens->gazl_idx, |
| 152 |
4 |
| sentence_tokens->gazm_idx, sentence_tokens->gazo_idx, sentence_tokens->gazp_idx, sentence_tokens->n); |
| 153 |
8 |
▶ | if(opt_srl) |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_srl) |
4 |
4 |
8 |
|
| 154 |
|
| { |
| 155 |
4 |
| vbs_labels = SENNA_VBS_forward(SENNA_object->vbs, sentence_tokens->word_idx, sentence_tokens->caps_idx, pos_labels, sentence_tokens->n); |
| 156 |
4 |
| n_verbs = 0; |
| 157 |
|
| int vbs_token_index; |
| 158 |
30 |
▶ | for(vbs_token_index = 0; vbs_token_index < sentence_tokens->n; vbs_token_index++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(vbs_token_index = 0; vbs_token_index < sentence_tokens->n; vbs_token_index++) |
26 |
4 |
30 |
|
| 159 |
|
| { |
| 160 |
26 |
| vbs_labels[vbs_token_index] = (vbs_labels[vbs_token_index] != vbs_hash_novb_idx); |
| 161 |
26 |
| n_verbs += vbs_labels[vbs_token_index]; |
| 162 |
|
| } |
| 163 |
4 |
| srl_labels = SENNA_SRL_forward(SENNA_object->srl, sentence_tokens->word_idx, sentence_tokens->caps_idx, pt0_labels, vbs_labels, sentence_tokens->n); |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
8 |
▶ | if(opt_psg) |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_psg) |
4 |
4 |
8 |
|
| 167 |
|
| { |
| 168 |
4 |
| SENNA_PSG_forward(SENNA_object->psg, sentence_tokens->word_idx, sentence_tokens->caps_idx, pos_labels, sentence_tokens->n, &psg_labels, &n_psg_level); |
| 169 |
|
| |
| 170 |
|
| /* check if top level takes the full sentence */ |
| 171 |
|
| { |
| 172 |
4 |
| int *psg_top_labels = psg_labels + (n_psg_level-1)*sentence_tokens->n; |
| 173 |
|
| |
| 174 |
4 |
▶ | if(sentence_tokens->n == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(sentence_tokens->n == 1) |
0 |
4 |
4 |
|
| 175 |
0 |
| is_psg_one_segment = ((psg_top_labels[0]-1) % 4 == 3); /* S- ? */ |
| 176 |
|
| else |
| 177 |
4 |
▶ | is_psg_one_segment = ((psg_top_labels[0]-1) % 4 == 0) && ((psg_top_labels[sentence_tokens->n-1]-1) % 4 == 2); /* B- or E- ? */ |
Branch: 2 branches
| Branch | True | False | Total |
| is_psg_one_segment = ((psg_top_labels[0]-1) % 4 == 0) && ((psg_top_labels[sentence_tokens->n-1]-1) % 4 == 2); /* B- or E- ? */ |
4 |
0 |
4 |
| is_psg_one_segment = ((psg_top_labels[0]-1) % 4 == 0) && ((psg_top_labels[sentence_tokens->n-1]-1) % 4 == 2); /* B- or E- ? */ |
4 |
0 |
4 |
|
| 178 |
|
| |
| 179 |
|
| int psg_token_index; |
| 180 |
22 |
▶ | for(psg_token_index = 1; is_psg_one_segment && (psg_token_index < sentence_tokens->n-1); psg_token_index++) |
Branch: 2 branches
| Branch | True | False | Total |
| for(psg_token_index = 1; is_psg_one_segment && (psg_token_index < sentence_tokens->n-1); psg_token_index++) |
22 |
0 |
22 |
| for(psg_token_index = 1; is_psg_one_segment && (psg_token_index < sentence_tokens->n-1); psg_token_index++) |
18 |
4 |
22 |
|
| 181 |
|
| { |
| 182 |
18 |
▶ | if((psg_top_labels[psg_token_index]-1) % 4 != 1) /* I- ? */ |
Branch: 1 branch
| Branch | True | False | Total |
| if((psg_top_labels[psg_token_index]-1) % 4 != 1) /* I- ? */ |
0 |
18 |
18 |
|
| 183 |
0 |
| is_psg_one_segment = 0; |
| 184 |
|
| } |
| 185 |
|
| } |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
|
| int token_index; |
| 189 |
57 |
▶ | for(token_index = 0; token_index < sentence_tokens->n; token_index++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(token_index = 0; token_index < sentence_tokens->n; token_index++) |
49 |
8 |
57 |
|
| 190 |
|
| { |
| 191 |
49 |
| HV* word_hv = newHV(); |
| 192 |
49 |
▶ | if(!opt_notokentags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!opt_notokentags) { |
49 |
0 |
49 |
|
| 193 |
49 |
| char* current_word = sentence_tokens->words[token_index]; |
| 194 |
49 |
| SV* word_sv = newSVpv(current_word,strlen(current_word)); |
| 195 |
49 |
| hv_store(word_hv,"word",4,word_sv,0); |
| 196 |
|
| } |
| 197 |
49 |
▶ | if(opt_pos) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_pos) { |
49 |
0 |
49 |
|
| 198 |
49 |
| const char* pos_label = SENNA_Hash_key(SENNA_object->pos_hash, pos_labels[token_index]); |
| 199 |
49 |
| SV* pos_sv = newSVpv(pos_label,strlen(pos_label)); |
| 200 |
49 |
| hv_store(word_hv,"POS",3,pos_sv,0); |
| 201 |
|
| } |
| 202 |
49 |
▶ | if(opt_chk) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_chk) { |
26 |
23 |
49 |
|
| 203 |
26 |
| const char* chk_label = SENNA_Hash_key(SENNA_object->chk_hash, chk_labels[token_index]); |
| 204 |
26 |
| SV* chk_sv = newSVpv(chk_label,strlen(chk_label)); |
| 205 |
26 |
| hv_store(word_hv,"CHK",3,chk_sv,0); |
| 206 |
|
| } |
| 207 |
49 |
▶ | if(opt_ner) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_ner) { |
26 |
23 |
49 |
|
| 208 |
26 |
| const char* ner_label = SENNA_Hash_key(SENNA_object->ner_hash, ner_labels[token_index]); |
| 209 |
26 |
| SV* ner_sv = newSVpv(ner_label,strlen(ner_label)); |
| 210 |
26 |
| hv_store(word_hv,"NER",3,ner_sv,0); |
| 211 |
|
| } |
| 212 |
49 |
▶ | if(opt_srl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_srl) { |
26 |
23 |
49 |
|
| 213 |
26 |
| AV* srl_av = newAV(); |
| 214 |
26 |
▶ | char* srl_label = (vbs_labels[token_index] ? sentence_tokens->words[token_index] : "-"); |
Branch: 1 branch
| Branch | True | False | Total |
| char* srl_label = (vbs_labels[token_index] ? sentence_tokens->words[token_index] : "-"); |
6 |
20 |
26 |
|
| 215 |
26 |
| SV* srl_sv = newSVpv(srl_label,strlen(srl_label)); |
| 216 |
26 |
| av_push(srl_av,srl_sv); |
| 217 |
72 |
▶ | for(j = 0; j < n_verbs; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j = 0; j < n_verbs; j++) { |
46 |
26 |
72 |
|
| 218 |
46 |
| const char* current_column = SENNA_Hash_key(SENNA_object->srl_hash, srl_labels[j][token_index]); |
| 219 |
46 |
| srl_sv = newSVpv(current_column,strlen(current_column)); |
| 220 |
46 |
| av_push(srl_av,srl_sv); |
| 221 |
|
| } |
| 222 |
26 |
| hv_store(word_hv,"SRL",3,newRV_inc((SV*)srl_av),0); |
| 223 |
|
| } |
| 224 |
49 |
▶ | if(opt_psg) /* last, can be long */ |
Branch: 1 branch
| Branch | True | False | Total |
| if(opt_psg) /* last, can be long */ |
26 |
23 |
49 |
|
| 225 |
|
| { |
| 226 |
26 |
| SV* psg_sv = newSVpv("",strlen("")); |
| 227 |
26 |
▶ | if(token_index == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(token_index == 0) |
4 |
22 |
26 |
|
| 228 |
|
| { |
| 229 |
4 |
| sv_catpv(psg_sv,"(S1"); |
| 230 |
4 |
▶ | if(!is_psg_one_segment) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!is_psg_one_segment) |
0 |
4 |
4 |
|
| 231 |
0 |
| sv_catpv(psg_sv,"(S"); |
| 232 |
|
| } |
| 233 |
129 |
▶ | for(j = n_psg_level-1; j >= 0; j--) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j = n_psg_level-1; j >= 0; j--) |
103 |
26 |
129 |
|
| 234 |
103 |
| sv_catpv(psg_sv,SENNA_Hash_key(SENNA_object->psg_left_hash, psg_labels[j*sentence_tokens->n+token_index])); |
| 235 |
26 |
| sv_catpv(psg_sv,"*"); |
| 236 |
129 |
▶ | for(j = 0; j < n_psg_level; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j = 0; j < n_psg_level; j++) |
103 |
26 |
129 |
|
| 237 |
103 |
| sv_catpv(psg_sv, SENNA_Hash_key(SENNA_object->psg_right_hash, psg_labels[j*sentence_tokens->n+token_index])); |
| 238 |
26 |
▶ | if(token_index == sentence_tokens->n-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(token_index == sentence_tokens->n-1) |
4 |
22 |
26 |
|
| 239 |
|
| { |
| 240 |
4 |
▶ | if(!is_psg_one_segment) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!is_psg_one_segment) |
0 |
4 |
4 |
|
| 241 |
0 |
| sv_catpv(psg_sv,")"); |
| 242 |
4 |
| sv_catpv(psg_sv,")"); |
| 243 |
|
| } |
| 244 |
26 |
| hv_store(word_hv,"PSG",3,psg_sv,0); |
| 245 |
|
| } |
| 246 |
|
| /* Done with this word */ |
| 247 |
49 |
| av_push(sentence_av,newRV_inc((SV*)word_hv)); |
| 248 |
|
| } |
| 249 |
8 |
| av_push(results, newRV_inc((SV*)sentence_av)); |
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
4 |
| RETVAL = results; |
| 253 |
|
| OUTPUT: |
| 254 |
|
| RETVAL |