Changeset 2162

Show
Ignore:
Timestamp:
09/20/08 15:37:55 (4 months ago)
Author:
karpet
Message:

refactor to simplify signatures for TokenIterator?. Now a TI always creates and manages its own TokenList?, and references a s3 rather than a config

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/src/libswish3/libswish3.h

    r2159 r2162  
    303303{ 
    304304    swish_TokenList     *tl; 
    305     swish_Config        *config
     305    swish_3             *s3
    306306    unsigned int         pos;           // position in iteration 
    307307    int                  ref_cnt; 
     
    330330    unsigned int           minwordlen;         // min word length 
    331331    boolean                tokenize;           // should we parse into TokenList 
    332     int                  (*tokenizer) (swish_3*, xmlChar*, swish_TokenList*, swish_MetaName*, xmlChar*); 
     332    int                  (*tokenizer) (swish_TokenIterator*, xmlChar*, swish_MetaName*, xmlChar*); 
    333333    xmlChar*             (*stemmer)   (xmlChar*); 
    334334    unsigned int           lc;                 // should tokens be lowercased 
     
    537537swish_Token *       swish_init_token(); 
    538538void                swish_free_token( swish_Token *t ); 
    539 swish_TokenIterator *swish_init_token_iterator( swish_Config *config, swish_TokenList *tl ); 
     539swish_TokenIterator *swish_init_token_iterator( swish_3 *s3 ); 
    540540void                swish_free_token_iterator( swish_TokenIterator *ti ); 
    541541swish_Token *       swish_next_token( swish_TokenIterator *it ); 
    542 int                 swish_tokenize3(    swish_3 *s3,  
     542int                 swish_tokenize3(    swish_TokenIterator *ti,  
    543543                                        xmlChar *buf,  
    544                                         swish_TokenList *tl,  
    545544                                        swish_MetaName *meta, 
    546545                                        xmlChar *context ); 
    547546int                 swish_tokenize3_ascii(     
    548                                         swish_3 *s3,  
     547                                        swish_TokenIterator *ti,  
    549548                                        xmlChar *buf,  
    550                                         swish_TokenList * tl,  
    551549                                        swish_MetaName *meta, 
    552550                                        xmlChar *context ); 
    553551int                 swish_tokenize3_utf8(     
    554                                         swish_3 *s3,  
     552                                        swish_TokenIterator *ti,  
    555553                                        xmlChar *buf,  
    556                                         swish_TokenList * tl, 
    557554                                        swish_MetaName *meta, 
    558555                                        xmlChar *context ); 
  • libswish3/trunk/src/libswish3/parser.c

    r2158 r2162  
    10181018 
    10191019    ptr->tag = NULL; 
    1020     ptr->token_iterator = swish_init_token_iterator(s3->config, swish_init_token_list()); 
     1020    ptr->token_iterator = swish_init_token_iterator(s3); 
    10211021    ptr->token_iterator->ref_cnt++; 
    10221022    ptr->properties = swish_init_nb(s3->config->properties); 
     
    20562056 
    20572057    parser_data->docinfo->nwords += 
    2058             (*parser_data->s3->analyzer->tokenizer) (parser_data->s3, string, 
    2059                                                      parser_data->token_iterator->tl, 
    2060                                                      meta, context); 
     2058            (*parser_data->s3->analyzer->tokenizer) (parser_data->token_iterator,  
     2059                                                    string, meta, context); 
    20612060    return; 
    20622061 
  • libswish3/trunk/src/libswish3/tokenizer.c

    r2160 r2162  
    501501swish_TokenIterator * 
    502502swish_init_token_iterator( 
    503     swish_Config *config, 
    504     swish_TokenList *tl 
     503    swish_3 *s3 
    505504) 
    506505{ 
    507506    swish_TokenIterator *it; 
    508507    it = swish_xmalloc(sizeof(swish_TokenIterator)); 
    509     it->config = config
    510     it->config->ref_cnt++; 
     508    it->s3 = s3
     509    it->s3->ref_cnt++; 
    511510    it->pos = 0; 
    512     it->tl = tl
     511    it->tl = swish_init_token_list()
    513512    it->tl->ref_cnt++; 
    514513    it->ref_cnt = 0; 
     
    524523        SWISH_WARN("freeing TokenIterator with ref_cnt != 0 (%d)", it->ref_cnt); 
    525524    } 
    526     it->config->ref_cnt--; 
    527     if (it->config->ref_cnt == 0) 
    528         swish_free_config(it->config); 
     525     
     526    it->s3->ref_cnt--; 
    529527         
    530528    it->tl->ref_cnt--; 
     
    557555int 
    558556swish_tokenize3( 
    559     swish_3 *s3, 
    560     xmlChar *buf, 
    561     swish_TokenList *tl, 
     557    swish_TokenIterator *ti,  
     558    xmlChar *buf,  
    562559    swish_MetaName *meta, 
    563560    xmlChar *context 
     
    565562{ 
    566563    if (swish_is_ascii(buf)) { 
    567         return swish_tokenize3_ascii(s3, buf, tl, meta, context); 
     564        return swish_tokenize3_ascii(ti, buf, meta, context); 
    568565    } 
    569566    else { 
    570         return swish_tokenize3_utf8(s3, buf, tl, meta, context); 
     567        return swish_tokenize3_utf8(ti, buf, meta, context); 
    571568    } 
    572569} 
     
    574571int 
    575572swish_tokenize3_utf8( 
    576     swish_3 *s3, 
    577     xmlChar *buf, 
    578     swish_TokenList *tl, 
     573    swish_TokenIterator *ti,  
     574    xmlChar *buf,  
    579575    swish_MetaName *meta, 
    580576    xmlChar *context 
    581577) 
    582578{ 
    583     int nstart, byte_pos, prev_pos, i, chr_len, cp, token_len; 
     579    int nstart, byte_pos, prev_pos, i, chr_len, cp, token_len, maxwordlen, minwordlen; 
     580    swish_TokenList *tl; 
    584581    boolean inside_token; 
    585582    xmlChar chr[5];             /*  max len of UCS32 plus NULL */ 
    586583    xmlChar *token, *copy, *buf_lower; 
    587     token = swish_xmalloc(sizeof(xmlChar) * s3->analyzer->maxwordlen); 
    588     buf_lower = swish_utf8_str_tolower(buf); 
    589  
    590     nstart = tl->n; 
     584     
     585    tl          = ti->tl; 
     586    maxwordlen      = ti->s3->analyzer->maxwordlen; 
     587    minwordlen      = ti->s3->analyzer->minwordlen; 
     588    token       = swish_xmalloc(sizeof(xmlChar) * maxwordlen); 
     589    buf_lower   = swish_utf8_str_tolower(buf); 
     590    nstart      = tl->n; 
    591591    inside_token = 0; 
    592     byte_pos = 0; 
    593     prev_pos = 0; 
    594     token_len = 0; 
     592    byte_pos    = 0; 
     593    prev_pos    = 0; 
     594    token_len   = 0; 
    595595 
    596596    if (SWISH_DEBUG & SWISH_DEBUG_TOKENIZER) 
     
    640640                token_len = strip_utf8_chrs(token, token_len); 
    641641 
    642                 if (token[0] != '\0' && token_len >= s3->analyzer->minwordlen) { 
     642                if (token[0] != '\0' && token_len >= minwordlen) { 
    643643 
    644644                    swish_add_token(tl, token, token_len, meta, context); 
     
    684684 
    685685                /* edge case */ 
    686                 if ((chr_len + token_len) > s3->analyzer->maxwordlen) { 
     686                if ((chr_len + token_len) > maxwordlen) { 
    687687                    if (SWISH_DEBUG & SWISH_DEBUG_TOKENIZER) 
    688688                        SWISH_DEBUG_MSG("token_len = %d  forcing end of token: '%s'", 
     
    698698                token_len += chr_len; 
    699699 
    700                 if (token_len >= s3->analyzer->maxwordlen || buf[byte_pos] == '\0') { 
     700                if (token_len >= maxwordlen || buf[byte_pos] == '\0') { 
    701701 
    702702                    if (SWISH_DEBUG & SWISH_DEBUG_TOKENIZER) 
     
    710710                    token_len = strip_utf8_chrs(token, token_len); 
    711711 
    712                     if (token[0] != '\0' && token_len >= s3->analyzer->minwordlen) { 
     712                    if (token[0] != '\0' && token_len >= minwordlen) { 
    713713 
    714714                        swish_add_token(tl, token, token_len, meta, context); 
     
    743743                inside_token = 1;       /*  turn on flag */ 
    744744                /* edge case */ 
    745                 if (chr_len > s3->analyzer->maxwordlen) 
     745                if (chr_len > maxwordlen) 
    746746                    continue; 
    747747 
     
    751751                 
    752752                /* special case for one-character tokens */ 
    753                 if (buf_lower[prev_pos] == '\0' && s3->analyzer->minwordlen == 1) { 
     753                if (buf_lower[prev_pos] == '\0' && minwordlen == 1) { 
    754754                    inside_token        = 0; 
    755755                    token[token_len++]  = '\0'; 
     
    778778int 
    779779swish_tokenize3_ascii( 
    780     swish_3 *s3, 
    781     xmlChar *buf, 
    782     swish_TokenList *tl, 
     780    swish_TokenIterator *ti,  
     781    xmlChar *buf,  
    783782    swish_MetaName *meta, 
    784783    xmlChar *context 
     
    787786    char c, nextc; 
    788787    boolean inside_token; 
    789     int i, token_len, nstart
     788    int i, token_len, nstart, maxwordlen, minwordlen
    790789    xmlChar *token, *copy; 
    791     token = swish_xmalloc(sizeof(xmlChar) * s3->analyzer->maxwordlen); 
    792  
    793     nstart = tl->n; 
    794     token_len = 0; 
    795     token[0] = '\0'; 
    796     inside_token = 0; 
     790    swish_TokenList *tl; 
     791     
     792    tl              = ti->tl; 
     793    maxwordlen      = ti->s3->analyzer->maxwordlen; 
     794    minwordlen      = ti->s3->analyzer->minwordlen; 
     795    token           = swish_xmalloc(sizeof(xmlChar) * maxwordlen); 
     796    nstart          = tl->n; 
     797    token_len       = 0; 
     798    token[0]        = '\0'; 
     799    inside_token    = 0; 
    797800 
    798801    if (SWISH_DEBUG & SWISH_DEBUG_TOKENIZER) 
     
    818821                token_len = strip_ascii_chrs(token, token_len); 
    819822 
    820                 if (token[0] != '\0' && token_len >= s3->analyzer->minwordlen) { 
     823                if (token[0] != '\0' && token_len >= minwordlen) { 
    821824                    swish_add_token(tl, token, token_len, meta, context); 
    822825                } 
     
    862865                token[token_len++] = c; 
    863866 
    864                 if (token_len >= s3->analyzer->maxwordlen || nextc == '\0') { 
     867                if (token_len >= maxwordlen || nextc == '\0') { 
    865868 
    866869                    if (SWISH_DEBUG & SWISH_DEBUG_TOKENIZER) 
     
    873876                    token_len = strip_ascii_chrs(token, token_len); 
    874877 
    875                     if (token[0] != '\0' && token_len >= s3->analyzer->minwordlen) { 
     878                    if (token[0] != '\0' && token_len >= minwordlen) { 
    876879                        swish_add_token(tl, token, token_len, meta, context); 
    877880                    } 
     
    905908                 
    906909                /* special case for one-character tokens */ 
    907                 if (nextc == '\0' && s3->analyzer->minwordlen == 1) { 
     910                if (nextc == '\0' && minwordlen == 1) { 
    908911                    inside_token        = 0; 
    909912                    token[token_len++]  = '\0'; 
  • libswish3/trunk/src/swish_tokenize.c

    r2148 r2162  
    7171    extern int optind; 
    7272    xmlChar *string; 
    73     swish_TokenList *list; 
    7473    swish_TokenIterator *iterator; 
    7574    xmlChar *meta; 
     
    8180 
    8281    s3 = swish_init_swish3(NULL, NULL); 
    83     list = swish_init_token_list(); 
    84     iterator = swish_init_token_iterator(s3->config, list); 
     82    iterator = swish_init_token_iterator(s3); 
    8583 
    8684    while ((ch = getopt_long(argc, argv, "f:h", longopts, &option_index)) != -1) { 
     
    114112    for (; i < argc; i++) { 
    115113        ntokens = 
    116             swish_tokenize3(s3, (xmlChar *)argv[i], list,  
     114            swish_tokenize3(iterator, (xmlChar *)argv[i], 
    117115                            swish_hash_fetch(s3->config->metanames, meta), meta); 
    118116        printf("parsed %d tokens: %s\n", ntokens, argv[i]); 
     
    122120    if (string != NULL) { 
    123121        ntokens = 
    124             swish_tokenize3(s3, string, list,  
     122            swish_tokenize3(iterator, string,  
    125123                            swish_hash_fetch(s3->config->metanames, meta), meta); 
    126124        swish_debug_token_list(iterator); 
     
    129127 
    130128    swish_free_token_iterator(iterator); 
    131     swish_free_token_list(list); 
    132129    swish_free_swish3(s3); 
    133130