KinoSearch1
view release on metacpan or search on metacpan
lib/KinoSearch1/Analysis/TokenBatch.pm view on Meta::CPAN
package KinoSearch1::Analysis::TokenBatch;
use strict;
use warnings;
use KinoSearch1::Util::ToolSet;
use base qw( KinoSearch1::Util::CClass );
1;
__END__
__XS__
MODULE = KinoSearch1 PACKAGE = KinoSearch1::Analysis::TokenBatch
void
new(either_sv)
SV *either_sv;
PREINIT:
const char *class;
TokenBatch *batch;
PPCODE:
/* determine the class */
class = sv_isobject(either_sv)
? sv_reftype(either_sv, 0)
: SvPV_nolen(either_sv);
/* build object */
batch = Kino1_TokenBatch_new();
ST(0) = sv_newmortal();
sv_setref_pv(ST(0), class, (void*)batch);
XSRETURN(1);
void
append(batch, text_sv, start_offset, end_offset, ...)
TokenBatch *batch;
SV *text_sv;
I32 start_offset;
I32 end_offset;
PREINIT:
char *text;
STRLEN len;
I32 pos_inc = 1;
Token *token;
PPCODE:
text = SvPV(text_sv, len);
if (items == 5)
pos_inc = SvIV( ST(4) );
else if (items > 5)
Kino1_confess("Too many arguments: %d", items);
token = Kino1_Token_new(text, len, start_offset, end_offset, pos_inc);
Kino1_TokenBatch_append(batch, token);
=for comment
Add many tokens to the batch, by supplying the string to be tokenized, and
arrays of token starts and token ends (specified in bytes).
=cut
void
add_many_tokens(batch, string_sv, starts_av, ends_av)
TokenBatch *batch;
SV *string_sv;
AV *starts_av;
AV *ends_av;
PREINIT:
char *string_start;
STRLEN len, start_offset, end_offset;
I32 i, max;
SV **start_sv_ptr;
SV **end_sv_ptr;
Token *token;
PPCODE:
{
string_start = SvPV(string_sv, len);
max = av_len(starts_av);
for (i = 0; i <= max; i++) {
/* retrieve start */
start_sv_ptr = av_fetch(starts_av, i, 0);
if (start_sv_ptr == NULL)
Kino1_confess("Failed to retrieve @starts array element");
start_offset = SvIV(*start_sv_ptr);
/* retrieve end */
end_sv_ptr = av_fetch(ends_av, i, 0);
if (end_sv_ptr == NULL)
Kino1_confess("Failed to retrieve @ends array element");
end_offset = SvIV(*end_sv_ptr);
/* sanity check the offsets to make sure they're inside the string */
if (start_offset > len)
Kino1_confess("start_offset > len (%d > %"UVuf")",
start_offset, (UV)len);
if (end_offset > len)
Kino1_confess("end_offset > len (%d > %"UVuf")",
end_offset, (UV)len);
/* calculate the start of the substring and add the token */
token = Kino1_Token_new(
(string_start + start_offset),
(end_offset - start_offset),
start_offset,
end_offset,
1
);
Kino1_TokenBatch_append(batch, token);
}
}
=begin comment
Add the postings to the segment. Postings are serialized and dumped into a
SortExternal sort pool. The actual writing takes place later.
The serialization algo is designed so that postings emerge from the sort
pool in the order ideal for writing an index after a simple lexical sort.
The concatenated components are:
field number
term text
null byte
document number
positions (C array of U32)
term length
=end comment
=cut
void
build_posting_list(batch, doc_num, field_num)
TokenBatch *batch;
U32 doc_num;
U16 field_num;
PPCODE:
Kino1_TokenBatch_build_plist(batch, doc_num, field_num);
void
set_all_texts(batch, texts_av)
TokenBatch *batch;
AV *texts_av;
PREINIT:
Token *token;
I32 i, max;
SV **sv_ptr;
char *text;
STRLEN len;
PPCODE:
{
token = batch->first;
max = av_len(texts_av);
for (i = 0; i <= max; i++) {
if (token == NULL) {
Kino1_confess("Batch size %d doesn't match array size %d",
batch->size, (max + 1));
}
sv_ptr = av_fetch(texts_av, i, 0);
if (sv_ptr == NULL) {
Kino1_confess("Encountered a null SV* pointer");
}
text = SvPV(*sv_ptr, len);
Kino1_Safefree(token->text);
token->text = Kino1_savepvn(text, len);
token->len = len;
token = token->next;
}
}
void
get_all_texts(batch)
TokenBatch *batch;
PREINIT:
Token *token;
AV *out_av;
PPCODE:
{
out_av = newAV();
token = batch->first;
while (token != NULL) {
SV *text = newSVpvn(token->text, token->len);
av_push(out_av, text);
token = token->next;
}
XPUSHs(sv_2mortal( newRV_noinc((SV*)out_av) ));
XSRETURN(1);
}
SV*
_set_or_get(batch, ...)
TokenBatch *batch;
ALIAS:
set_text = 1
get_text = 2
set_start_offset = 3
get_start_offset = 4
set_end_offset = 5
get_end_offset = 6
set_pos_inc = 7
get_pos_inc = 8
set_size = 9
get_size = 10
set_postings = 11
get_postings = 12
set_tv_string = 13
get_tv_string = 14
CODE:
{
/* fail if looking for info on a single token but there isn't one */
if ((ix < 7) && (batch->current == NULL))
Kino1_confess("TokenBatch doesn't currently hold a valid token");
KINO_START_SET_OR_GET_SWITCH
case 1: {
Token *current = batch->current;
char *text;
Kino1_Safefree(current->text);
text = SvPV( ST(1), current->len );
current->text = Kino1_savepvn( text, current->len );
}
/* fall through */
case 2: RETVAL = newSVpvn(batch->current->text, batch->current->len);
break;
case 3: batch->current->start_offset = SvIV( ST(1) );
/* fall through */
case 4: RETVAL = newSViv(batch->current->start_offset);
break;
case 5: batch->current->end_offset = SvIV( ST(1) );
/* fall through */
case 6: RETVAL = newSViv(batch->current->end_offset);
break;
case 7: batch->current->pos_inc = SvIV( ST(1) );
/* fall through */
case 8: RETVAL = newSViv(batch->current->pos_inc);
break;
case 9: Kino1_confess("Can't set size on a TokenBatch object");
/* fall through */
case 10: RETVAL = newSVuv(batch->size);
break;
case 11: Kino1_confess("can't set_postings");
/* fall through */
case 12: RETVAL = newRV_inc( (SV*)batch->postings );
break;
case 13: Kino1_confess("can't set_tv_string");
/* fall through */
case 14: RETVAL = newSVsv(batch->tv_string);
break;
KINO_END_SET_OR_GET_SWITCH
}
OUTPUT: RETVAL
void
reset(batch)
TokenBatch *batch;
PPCODE:
Kino1_TokenBatch_reset(batch);
I32
next(batch)
TokenBatch *batch;
CODE:
RETVAL = Kino1_TokenBatch_next(batch);
OUTPUT: RETVAL
void
DESTROY(batch)
TokenBatch *batch;
PPCODE:
Kino1_TokenBatch_destroy(batch);
__H__
#ifndef H_KINOSEARCH_ANALYSIS_TOKENBATCH
#define H_KINOSEARCH_ANALYSIS_TOKENBATCH 1
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "KinoSearch1AnalysisToken.h"
#include "KinoSearch1IndexTerm.h"
#include "KinoSearch1UtilCarp.h"
#include "KinoSearch1UtilMathUtils.h"
#include "KinoSearch1UtilMemManager.h"
#include "KinoSearch1UtilStringHelper.h"
typedef struct tokenbatch {
Token *first;
Token *last;
Token *current;
I32 size;
I32 initialized;
AV *postings;
SV *tv_string;
} TokenBatch;
TokenBatch* Kino1_TokenBatch_new();
void Kino1_TokenBatch_destroy(TokenBatch *batch);
void Kino1_TokenBatch_append(TokenBatch *batch, Token *token);
I32 Kino1_TokenBatch_next(TokenBatch *batch);
void Kino1_TokenBatch_reset(TokenBatch *batch);
void Kino1_TokenBatch_build_plist(TokenBatch*, U32, U16);
#endif /* include guard */
__C__
#include "KinoSearch1AnalysisTokenBatch.h"
TokenBatch*
Kino1_TokenBatch_new() {
TokenBatch *batch;
/* allocate */
Kino1_New(0, batch, 1, TokenBatch);
/* init */
batch->first = NULL;
batch->last = NULL;
batch->current = NULL;
batch->size = 0;
batch->initialized = 0;
batch->tv_string = &PL_sv_undef;
batch->postings = (AV*)&PL_sv_undef;
return batch;
}
( run in 0.448 second using v1.01-cache-2.11-cpan-5511b514fd6 )