Changeset 1930

Show
Ignore:
Timestamp:
04/30/07 23:08:43 (1 year ago)
Author:
karpet
Message:

refactor to buffer all MetaNames? as well as PropertyNames? in NamedBuffer?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/bindings/perl/3.xs

    r1929 r1930  
    7272 */ 
    7373 
    74 /* private package vars */ 
     74/* all XS stuff is prefixed with 'sp_' for Swish Perl */ 
    7575 
    7676#define DEFAULT_BASE_CLASS  "SWISH::3::Parser" 
    7777#define CONFIG_CLASS        "SWISH::3::Config" 
    7878#define ANALYZER_CLASS      "SWISH::3::Analyzer" 
    79 #define CONFIG_KEY          "config" 
    80 #define ANALYZER_KEY        "analyzer" 
    81 #define HANDLER_KEY         "handler" 
     79#define CONFIG_KEY          "sp_config" 
     80#define ANALYZER_KEY        "sp_analyzer" 
     81#define HANDLER_KEY         "sp_handler" 
    8282 
    8383 
     
    340340    swish_debug_docinfo( parse_data->docinfo ); 
    341341    swish_debug_wordlist( parse_data->wordlist ); 
    342     swish_debug_PropHash( parse_data->propHash ); 
     342    swish_debug_nb( parse_data->properties, "Property" ); 
     343    swish_debug_nb( parse_data->metanames, "MetaName" ); 
    343344    warn("\n"); 
    344345} 
     
    377378    SV *token_re; 
    378379    swish_WordList *list; 
     380    swish_WordList *(*token_handler)(xmlChar * start_ptr, int tok_bytes, int start, int end, ...); 
    379381    va_list args; 
    380382    va_start(args, str); 
     
    392394    xmlChar    *str_end         = str_start + str_len; 
    393395     
    394     token_re = analyzer->regex; /* TODO is this right ?? */ 
     396    token_re        = analyzer->regex; 
     397    token_handler   = analyzer->stash; 
    395398     
    396399    /* extract regexp struct from qr// entity */ 
     
    408411    SvREADONLY_on(wrapper); 
    409412    SvLEN(wrapper) = 0; 
    410     SvUTF8_on(wrapper);     /* do UTF8 matching -- TODO conditional on swish_is_ascii() ?? */ 
     413    SvUTF8_on(wrapper);     /* do UTF8 matching -- we trust str is already utf-8 encoded. */ 
    411414     
    412415    /* wrap the string in an SV to please the regex engine */ 
     
    415418    SvPOK_on(wrapper); 
    416419 
    417     list = swish_init_WordList(); 
     420    list = swish_init_wordlist(); 
    418421    num_code_points = 0; 
    419422     
     
    423426        xmlChar * end_ptr   = str + rx->endp[0]; 
    424427        int start, end, tok_bytes, tok_pts; 
    425         xmlChar * token; 
    426428 
    427429        /* get start and end offsets in Unicode code points */ 
     
    446448        tok_pts   = end - start; 
    447449        tok_bytes = end_ptr - start_ptr; 
    448          
    449         /* TODO lc() ? */ 
    450          
    451         if (tok_pts < analyzer->minwordlen) 
    452             continue; 
     450                 
     451        (*token_handler)(   start_ptr,  
     452                            tok_bytes,  
     453                            start,  
     454                            end,  
     455                            meta,  
     456                            ctxt,  
     457                            ++wpos, 
     458                            (tok_bytes + offset - 1), 
     459                            analyzer, 
     460                            list 
     461                            ); 
     462         
     463    }  
     464 
     465    return list; 
     466
     467 
     468/* 
     469    default token handler is just to append to WordList 
     470*/ 
     471swish_WordList* 
     472sp_token_handler(xmlChar * start_ptr, int tok_bytes, int start, int end, ...) 
     473
     474    xmlChar *meta, *ctxt; 
     475    int wpos, offset; 
     476    swish_Analyzer *analyzer; 
     477    swish_WordList *list; 
     478    va_list args; 
     479    va_start(args, end); 
     480    meta        = va_arg(args, xmlChar *); 
     481    ctxt        = va_arg(args, xmlChar *); 
     482    wpos        = va_arg(args, unsigned int); 
     483    offset      = va_arg(args, unsigned int);     
     484    analyzer    = va_arg(args, swish_Analyzer*); 
     485    list        = va_arg(args, swish_WordList*); 
     486    va_end(args); 
     487     
     488    if ((end - start) < analyzer->minwordlen) 
     489        return list; 
    453490             
    454         if (tok_pts > analyzer->maxwordlen) 
    455             continue; 
    456          
    457         token = xmlStrndup(start_ptr, tok_bytes);         
    458         swish_add_to_wordlist( list, token, meta, ctxt, ++wpos, (tok_bytes + offset - 1) ); 
    459          
    460         if (SWISH_DEBUG) 
    461         { 
    462             warn("%s (%d %d)\n", token, start + 1, end); 
    463         } 
    464          
    465         free(token); 
    466     }  
     491    if ((end - start) > analyzer->maxwordlen) 
     492        return list; 
     493         
     494         
     495    /* TODO: lc() and stem() */ 
     496             
     497    swish_add_to_wordlist_len(  list,  
     498                                start_ptr,  
     499                                tok_bytes,  
     500                                meta,  
     501                                ctxt,  
     502                                wpos,  
     503                                offset 
     504                                ); 
    467505 
    468506    return list; 
    469507} 
    470  
    471508 
    472509/******************************************************************************* 
     
    9801017        if (self->ref_cnt < 1) 
    9811018        { 
    982             swish_free_WordList(self); 
     1019            swish_free_wordlist(self); 
    9831020        } 
    9841021         
     
    10301067         
    10311068    CODE: 
    1032         buf = xmlHashLookup(self->propHash,p); 
     1069        buf = xmlHashLookup(self->properties->hash, p); 
    10331070        RETVAL = newSVpvn((char*)xmlBufferContent(buf), xmlBufferLength(buf)); 
    10341071         
     
    12101247        RETVAL->regex = (void*)SvREFCNT_inc( regex ); 
    12111248        RETVAL->tokenizer = &sp_tokenize; 
     1249        RETVAL->stash = &sp_token_handler; 
    12121250         
    12131251    OUTPUT: 
     
    12961334# TODO: get/set methods, including way to set tokenizer func ref 
    12971335 
     1336 
     1337# tokenize_isw() uses native libswish3 tokenizer 
     1338swish_WordList * 
     1339tokenize_isw(self, str, ...) 
     1340    SV * self; 
     1341    SV * str; 
     1342 
     1343    PREINIT: 
     1344        char * CLASS; 
     1345        xmlChar * metaname = SWISH_DEFAULT_METANAME;    
     1346        xmlChar * context  = SWISH_DEFAULT_METANAME; 
     1347        unsigned int word_pos    = 0; 
     1348        unsigned int offset      = 0; 
     1349        xmlChar * buf = SvPV(str, PL_na); 
     1350         
     1351    CODE: 
     1352        CLASS = sp_which_class("WordList"); 
     1353         
     1354        if (!SvUTF8(str)) 
     1355        { 
     1356            if (swish_is_ascii(buf)) 
     1357                SvUTF8_on(str);     /* flags original SV ?? */ 
     1358            else 
     1359                croak("%s is not flagged as a UTF-8 string and is not ASCII", buf); 
     1360        } 
     1361         
     1362        if ( items > 2 ) 
     1363        { 
     1364            word_pos = (int)SvIV(ST(2)); 
     1365             
     1366            if ( items > 3 ) 
     1367                offset = (int)SvIV(ST(3)); 
     1368                 
     1369            if ( items > 4 ) 
     1370                metaname = SvPV(ST(4), PL_na); 
     1371                 
     1372            if ( items > 5 ) 
     1373                context = SvPV(ST(5), PL_na); 
     1374                 
     1375        } 
     1376                 
     1377        swish_init_words(); /* in case it wasn't initialized elsewhere... */ 
     1378        RETVAL = swish_tokenize( 
     1379                        (swish_Analyzer*)sp_ptr_from_object(self), 
     1380                        buf, 
     1381                        word_pos, 
     1382                        offset, 
     1383                        metaname, 
     1384                        context 
     1385                        ); 
     1386         
     1387        RETVAL->ref_cnt++; 
     1388         
     1389        /* TODO do we need to worry about free()ing metaname and context ?? */ 
     1390                         
     1391    OUTPUT: 
     1392        RETVAL 
     1393         
  • libswish3/trunk/bindings/perl/typemap

    r1927 r1930  
    1111swish_Analyzer *           O_OBJECT 
    1212swish_Parser *             O_OBJECT 
     13swish_NamedBuffer *        O_OBJECT 
    1314 
    1415INPUT 
  • libswish3/trunk/src/libswish3/Makefile.am

    r1927 r1930  
    2020                        mime_types.c \ 
    2121                        parser.c \ 
    22                         properties.c \ 
     22                        namedbuffer.c \ 
    2323                        string.c \ 
    2424                        times.c \ 
  • libswish3/trunk/src/libswish3/libswish3.h

    r1928 r1930  
    5252#define SWISH_INCLUDE_FILE   "IncludeConfigFile" 
    5353#define SWISH_PROP           "PropertyNames" 
     54#define SWISH_PROP_ASIS      "nostripchars" 
    5455#define SWISH_PROP_MAX       "PropertyNamesMaxLength" 
    5556#define SWISH_PROP_SORT      "PropertyNamesSortKeyLength" 
     
    129130#define SWISH_DEBUG_TOKENIZER   5 
    130131#define SWISH_DEBUG_PARSER      9 
     132#define SWISH_DEBUG_NAMEDBUFFER 15 
    131133 
    132134#ifdef __cplusplus 
     
    170172void        swish_mem_debug(); 
    171173xmlChar *   swish_xstrdup( const xmlChar * ptr ); 
     174xmlChar *   swish_xstrndup( const xmlChar * ptr, int len ); 
    172175 
    173176/* time functions */ 
     
    205208struct swish_Config 
    206209{ 
    207     int    ref_cnt;    /* for scripting languages */ 
    208     void *          stash;      /* also for scripting languages */ 
     210    int             ref_cnt;    /* for scripting languages */ 
     211    void           *stash;      /* for scripting languages */ 
    209212    xmlHashTablePtr conf;       /* the meat */ 
    210213}; 
     
    259262typedef struct swish_Analyzer          swish_Analyzer; 
    260263typedef struct swish_Parser            swish_Parser; 
     264typedef struct swish_NamedBuffer            swish_NamedBuffer; 
     265 
     266struct swish_NamedBuffer 
     267{ 
     268    int             ref_cnt;    /* for scripting languages */ 
     269    void           *stash;      /* for scripting languages */ 
     270    xmlHashTablePtr hash;       /* the meat */ 
     271}; 
    261272 
    262273struct swish_DocInfo 
     
    299310    xmlChar            *name; 
    300311    struct swish_Tag   *next; 
    301     unsigned int       n; 
     312    unsigned int        n; 
    302313}; 
    303314 
     
    321332    void                  *stash;              // for script bindings 
    322333    void                  *regex;              // optional regex 
    323     int           ref_cnt;            // for script bindings 
     334    int                    ref_cnt;            // for script bindings 
    324335}; 
    325336 
     
    336347struct swish_ParseData 
    337348{ 
    338     xmlBufferPtr           buf_ptr;            // text buffer 
    339     xmlBufferPtr           prop_buf;           // Property buffer 
     349    xmlBufferPtr           buf_ptr;            // tmp text (MetaName) buffer 
     350    xmlBufferPtr           prop_buf;           // tmp Property buffer 
    340351    xmlChar               *tag;                // current tag name 
    341352    swish_DocInfo         *docinfo;            // document-specific properties 
    342353    swish_Config          *config;             // global config 
     354    unsigned int           context_as_meta;    // index tokens under all applicable MetaNames 
    343355    unsigned int           no_index;           // toggle flag for special comments 
    344356    unsigned int           is_html;            // shortcut flag for html parser 
     
    348360    swish_TagStack        *metastack;          // stacks for tracking the tag => metaname 
    349361    swish_TagStack        *propstack;          // stacks for tracking the tag => property 
    350     xmlParserCtxtPtr       ctxt; 
     362    xmlParserCtxtPtr       ctxt;               // so we can free at end 
    351363    swish_WordList        *wordlist;           // linked list of words 
    352     xmlHashTablePtr        propHash;           // hash of Props, one for each property 
     364    swish_NamedBuffer     *properties;         // buffer all properties 
     365    swish_NamedBuffer     *metanames;          // buffer all metanames 
    353366    swish_Analyzer        *analyzer;           // Analyzer struct 
    354     void                  *stash;          // for script bindings 
     367    void                  *stash;              // for script bindings 
    355368}; 
    356369 
     
    374387 
    375388 
    376 /* utility buffers */ 
    377 void                swish_append_buffer(xmlBufferPtr buf, const xmlChar * txt, int txtlen); 
    378  
    379  
    380  
    381389/* word functions */ 
    382390void                swish_init_words(); 
    383 swish_WordList *    swish_init_WordList(); 
    384 void                swish_free_WordList(swish_WordList * list); 
     391swish_WordList *    swish_init_wordlist(); 
     392void                swish_free_wordlist(swish_WordList * list); 
    385393swish_WordList *    swish_tokenize( swish_Analyzer * analyzer, xmlChar * str, ... ); 
    386394 
     
    418426                                            int word_pos,  
    419427                                            int offset ); 
     428 
     429int                 swish_add_to_wordlist_len(   
     430                                            swish_WordList * list,  
     431                                            xmlChar * str, 
     432                                            int len, 
     433                                            xmlChar * metaname, 
     434                                            xmlChar * context, 
     435                                            int word_pos,  
     436                                            int offset ); 
    420437                                             
    421438void                swish_debug_wordlist( swish_WordList * list ); 
     
    429446void                swish_free_docinfo( swish_DocInfo * ptr ); 
    430447int                 swish_check_docinfo(swish_DocInfo * docinfo, swish_Config * config); 
    431 int                 swish_docinfo_from_filesystem( xmlChar *filename, swish_DocInfo * i, swish_ParseData *parse_data ); 
     448int                 swish_docinfo_from_filesystem(  xmlChar *filename,  
     449                                                    swish_DocInfo * i,  
     450                                                    swish_ParseData *parse_data ); 
    432451void                swish_debug_docinfo( swish_DocInfo * docinfo ); 
    433452 
    434453 
    435 /* Property functions */ 
    436 xmlHashTablePtr swish_init_PropHash( swish_Config * config); 
    437 void            swish_free_PropHash( xmlHashTablePtr prophash); 
    438 void            swish_debug_PropHash(xmlHashTablePtr propHash); 
     454/* NamedBuffer functions */ 
     455 
     456swish_NamedBuffer * swish_init_nb( swish_Config * config, xmlChar * configKey ); 
     457void                swish_free_nb( swish_NamedBuffer * nb ); 
     458void                swish_debug_nb( swish_NamedBuffer * nb, xmlChar * label ); 
     459void                swish_add_buf_to_nb( swish_NamedBuffer *nb,  
     460                                         xmlChar * name, 
     461                                         xmlBufferPtr buf,  
     462                                         xmlChar * joiner, 
     463                                         int cleanwsp, 
     464                                         int autovivify); 
     465void                swish_add_str_to_nb( swish_NamedBuffer * nb,  
     466                                         xmlChar * name,  
     467                                         xmlChar * str, 
     468                                         unsigned int len, 
     469                                         xmlChar * joiner, 
     470                                         int cleanwsp, 
     471                                         int autovivify); 
     472void                swish_append_buffer( xmlBufferPtr buf, xmlChar * txt, int len ); 
     473 
    439474 
    440475 
  • libswish3/trunk/src/libswish3/mem.c

    r1927 r1930  
    7676} 
    7777 
     78xmlChar * swish_xstrndup( const xmlChar * ptr, int len ) 
     79{ 
     80    memcount++; 
     81    if ( SWISH_DEBUG > 20 ) 
     82        swish_debug_msg( "memcount = %ld", memcount); 
     83    return( xmlStrndup( ptr, len ) ); 
     84} 
     85 
    7886void swish_xfree( void *ptr ) 
    7987{     
  • libswish3/trunk/src/libswish3/parser.c

    r1928 r1930  
    6464static void     get_env_vars(); 
    6565 
    66 static void     flush_buffer(swish_ParseData * parse_data, xmlChar * metaname); 
    67 static void     add_to_prop_buf(xmlBufferPtr buf_ptr,  
    68                                 xmlHashTablePtr propHash,  
    69                                 xmlChar * propName); 
     66static void     flush_buffer(   swish_ParseData * parse_data,  
     67                                xmlChar * metaname, xmlChar * context 
     68                                ); 
     69 
    7070static void     tokenize(       swish_ParseData * parse_data,  
    7171                                xmlChar * string,  
     
    186186build_tag(swish_ParseData * parse_data, xmlChar * tag, xmlChar ** atts) 
    187187{ 
    188     int             i, is_html_tag; 
    189     xmlChar        *swishtag, *alias, *metaname, *metacontent; 
    190  
    191     metaname = NULL; 
     188    int      i, is_html_tag; 
     189    xmlChar  *swishtag, *alias, *metaname, *metacontent; 
     190 
     191    metaname    = NULL; 
    192192    metacontent = NULL; 
    193193     
    194194    /* normalize all tags */ 
    195195    swishtag = swish_str_tolower(tag); 
    196  
    197196 
    198197    /* html tags */ 
     
    278277 
    279278 
    280     if (SWISH_DEBUG > 2
    281     { 
    282         fprintf(stderr, " >>> startElement(%s (%s) ", tag, parse_data->tag); 
     279    if (SWISH_DEBUG == SWISH_DEBUG_PARSER
     280    { 
     281        fprintf(stderr, " >>> build_tag (%s (%s) ", tag, parse_data->tag); 
    283282        if (atts != 0) 
    284283        { 
     
    293292        } 
    294293        fprintf(stderr, ")\n"); 
    295  
    296294    } 
    297295 
    298296 
    299297    /* change our internal name for this tag if it is aliased in config */ 
    300     alias = swish_get_config_value(parse_data->config, (xmlChar*)SWISH_ALIAS, parse_data->tag); 
     298    alias = swish_get_config_value(parse_data->config, (xmlChar*)SWISH_ALIAS, swishtag); 
    301299    if (alias) 
    302300    { 
     301        //swish_debug_msg("%s alias -> %s", swishtag, alias); 
    303302        swish_xfree(swishtag); 
    304303        swishtag = swish_xstrdup(alias); 
     
    309308} 
    310309 
    311 void 
    312 swish_append_buffer(xmlBufferPtr buf, const xmlChar * txt, int txtlen) 
    313 { 
    314     int ret; 
    315      
    316     if (txtlen == 0) 
    317         /* shouldn't happen */ 
    318         return; 
    319          
    320     if (buf == NULL) 
    321     { 
    322         swish_fatal_err("bad news. buf ptr is NULL"); 
    323          
    324     } 
    325  
    326     ret = xmlBufferAdd( buf, txt, txtlen ); 
    327     if (ret) 
    328     { 
    329         swish_fatal_err("problem adding \n>>%s<<\n length %d to buffer. Err: %d",  
    330                         txt, txtlen, ret); 
    331     } 
    332      
    333 } 
    334310 
    335311static void 
    336 add_to_prop_buf(xmlBufferPtr buf, xmlHashTablePtr propHash, xmlChar * propName) 
    337 
    338  
    339     xmlChar *    nowhitesp; 
    340     xmlBufferPtr propBuf = xmlHashLookup(propHash, propName); 
    341  
    342     if (propBuf && xmlBufferLength(propBuf)) 
    343     { 
    344         /* swish_debug_msg("adding %s to propBuf", propName); */ 
    345  
    346         /* if the propBuf already exists and we're about to add more, append the 
    347          * connect string */ 
    348         if (xmlBufferLength(buf)) 
    349         { 
    350             swish_append_buffer(propBuf, 
    351                       (const xmlChar *) SWISH_PROP_CONNECTOR, 
    352                       xmlStrlen((xmlChar *) SWISH_PROP_CONNECTOR)); 
    353         } 
    354  
    355         nowhitesp = swish_str_skip_ws((xmlChar *)xmlBufferContent(buf)); 
    356         swish_str_trim_ws(nowhitesp); 
    357  
    358         swish_append_buffer(propBuf, (const xmlChar *) nowhitesp, xmlStrlen(nowhitesp)); 
    359     } 
    360  
    361 
    362  
    363 static void 
    364 flush_buffer(swish_ParseData * parse_data, xmlChar * metaname) 
    365 
    366  
     312flush_buffer(swish_ParseData * parse_data, xmlChar * metaname, xmlChar * context) 
     313
     314    swish_TagStack *s = parse_data->metastack; 
     315     
    367316    if (SWISH_DEBUG > 10) 
    368317        swish_debug_msg("buffer is >>%s<< before flush, word_pos = %d",  
     
    375324    if (parse_data->word_pos) 
    376325        parse_data->word_pos++; 
    377  
    378  
    379     tokenize(   parse_data,  
    380                 (xmlChar *)xmlBufferContent(parse_data->buf_ptr),  
    381                 xmlBufferLength(parse_data->buf_ptr), 
    382                 parse_data->metastack->head->name, 
    383                 metaname 
     326         
     327    /* add buf_ptr as-is to metanames buffer under current tag. 
     328       this gives us both tokens and raw text de-tagged but organized by metaname. 
     329    */ 
     330    swish_add_buf_to_nb( parse_data->metanames, 
     331                         metaname, 
     332                         parse_data->buf_ptr, '\0', 0, 1); 
     333                          
     334    if (parse_data->context_as_meta) 
     335    { 
     336        for (s->temp = s->head; s->temp != NULL; s->temp = s->temp->next) 
     337        { 
     338            if (xmlStrEqual(s->temp->name, metaname))    /* just added above */ 
     339                continue; 
     340             
     341            swish_add_buf_to_nb(parse_data->metanames, 
     342                                s->temp->name,  
     343                                parse_data->buf_ptr, '\0', 0, 1); 
     344        } 
     345    }                     
     346 
     347    if (parse_data->analyzer->tokenize) 
     348    { 
     349 
     350        tokenize(   parse_data,  
     351                    (xmlChar *)xmlBufferContent(parse_data->buf_ptr),  
     352                    xmlBufferLength(parse_data->buf_ptr), 
     353                    metaname, 
     354                    context 
    384355                ); 
     356    } 
    385357 
    386358    xmlBufferEmpty(parse_data->buf_ptr); 
     
    409381        swish_debug_msg("endDocument()"); 
    410382 
    411     flush_buffer(parse_data, NULL);    /* whatever's left */ 
     383    /* whatever's left */ 
     384    flush_buffer(parse_data, (xmlChar*)SWISH_DEFAULT_METANAME, (xmlChar*)SWISH_DEFAULT_METANAME); 
    412385 
    413386} 
     
    466439 
    467440 
    468  
    469     if (SWISH_DEBUG > 8) 
     441    if (SWISH_DEBUG == SWISH_DEBUG_PARSER) 
    470442        swish_debug_msg("checking config for '%s' in watched tags", parse_data->tag); 
    471443 
    472444 
    473445    /* set property if this tag is configured for it */ 
    474     if (swish_config_value_exists(parse_data->config, (xmlChar *) SWISH_PROP, parse_data->tag)) 
    475     { 
    476         if (SWISH_DEBUG > 8
     446    if (swish_config_value_exists(parse_data->config, (xmlChar*)SWISH_PROP, parse_data->tag)) 
     447    { 
     448        if (SWISH_DEBUG == SWISH_DEBUG_PARSER
    477449            swish_debug_msg(" %s = new property", parse_data->tag); 
    478450 
     
    486458 
    487459    /* likewise for metastack */ 
    488     if (swish_config_value_exists(parse_data->config, (xmlChar *) SWISH_META, parse_data->tag)) 
    489     { 
    490         if (SWISH_DEBUG > 8
     460    if (swish_config_value_exists(parse_data->config, (xmlChar*)SWISH_META, parse_data->tag)) 
     461    { 
     462        if (SWISH_DEBUG == SWISH_DEBUG_PARSER
    491463            swish_debug_msg(" %s = new metaname", parse_data->tag); 
    492  
    493         flush_buffer(parse_data, NULL); 
     464                                
     465        flush_buffer( parse_data, parse_data->metastack->head->name, parse_data->metastack->flat ); 
    494466 
    495467        parse_data->metastack = push_tag_stack(parse_data->metastack, parse_data->tag); 
    496468    } 
    497469     
    498     if (SWISH_DEBUG > 8
     470    if (SWISH_DEBUG == SWISH_DEBUG_PARSER
    499471        swish_debug_msg("config check for '%s' done", parse_data->tag); 
    500472 
     
    505477close_tag(void *data, const xmlChar * tag) 
    506478{ 
    507     xmlChar        *metaname
     479    xmlChar        *context
    508480    swish_ParseData *parse_data; 
    509481    parse_data = (swish_ParseData *) data; 
     
    519491        swish_debug_msg(" endElement(%s) (%s)", (xmlChar *) tag, parse_data->tag); 
    520492 
    521     if ((metaname = pop_tag_stack_on_match(parse_data->propstack, parse_data->tag)) != NULL) 
    522     { 
    523         //swish_debug_msg("popped %s from propstack", parse_data->tag); 
     493    if ((context = pop_tag_stack_on_match(parse_data->propstack, parse_data->tag)) != NULL) 
     494    { 
     495        //swish_debug_msg("popped %s from propstack", context); 
    524496        add_stack_to_prop_buf(parse_data->tag, parse_data); 
    525497        xmlBufferEmpty(parse_data->prop_buf); 
    526         swish_xfree(metaname); 
    527     } 
    528  
    529     if ((metaname = pop_tag_stack_on_match(parse_data->metastack, parse_data->tag)) != NULL) 
     498        swish_xfree(context); 
     499    } 
     500 
     501    if ((context = pop_tag_stack_on_match(parse_data->metastack, parse_data->tag)) != NULL) 
    530502    { 
    531503        /* swish_debug_msg("popped %s from metastack", parse_data->tag); */ 
    532         flush_buffer(parse_data, metaname); 
    533         swish_xfree(metaname); 
     504        flush_buffer(parse_data, parse_data->tag, context); 
     505        swish_xfree(context); 
    534506    } 
    535507 
     
    572544 
    573545    if (parse_data->bump_word && xmlBufferLength(parse_data->prop_buf)) 
     546    { 
     547        //swish_debug_msg("   appending ' ' to prop_buf"); 
    574548        swish_append_buffer(parse_data->prop_buf, (xmlChar *) " ", 1); 
    575  
     549    } 
     550     
     551    //swish_debug_msg("   appending '%s' to prop_buf", output); 
    576552    swish_append_buffer(parse_data->prop_buf, output, len); 
    577553 
     
    771747     
    772748    ptr->tag = NULL; 
    773     ptr->wordlist = swish_init_WordList(); 
    774     ptr->propHash = swish_init_PropHash(config); 
     749    ptr->wordlist   = swish_init_wordlist(); 
     750    ptr->properties = swish_init_nb(config, (xmlChar*)SWISH_PROP); 
     751    ptr->metanames  = swish_init_nb(config, (xmlChar*)SWISH_META); 
    775752 
    776753    /* prime the stacks */ 
     
    781758    ptr->metastack->flat = NULL; 
    782759    ptr->metastack->count = 0; 
    783     ptr->metastack = push_tag_stack(ptr->metastack, (xmlChar *) SWISH_DEFAULT_METANAME); 
     760    ptr->metastack = push_tag_stack(ptr->metastack, (xmlChar*)SWISH_DEFAULT_METANAME); 
    784761 
    785762    ptr->propstack = (swish_TagStack *) swish_xmalloc(sizeof(swish_TagStack)); 
     
    789766    ptr->propstack->flat = NULL; 
    790767    ptr->propstack->count = 0; 
    791     ptr->propstack = push_tag_stack(ptr->propstack, (xmlChar *) "_");    /* no such property -- 
    792                                          * just to seed stack */ 
     768    ptr->propstack = push_tag_stack(ptr->propstack, (xmlChar*)"_");     
     769    /* no such property just to seed stack */ 
    793770 
    794771    /* gets toggled per-tag */ 
     
    807784    ptr->offset = 0; 
    808785 
     786    /* TODO make this configurable */ 
     787    ptr->context_as_meta = 1; 
    809788 
    810789    /* pointer to the xmlParserCtxt since we want to free it only after we're 
     
    857836 
    858837    if (SWISH_DEBUG > 9) 
    859         swish_debug_msg("freeing swish_ParseData propHash"); 
    860  
    861     swish_free_PropHash(ptr->propHash); 
     838        swish_debug_msg("freeing swish_ParseData properties"); 
     839 
     840    swish_free_nb(ptr->properties); 
     841 
     842    if (SWISH_DEBUG > 9) 
     843        swish_debug_msg("freeing swish_ParseData metanames"); 
     844 
     845    swish_free_nb(ptr->metanames); 
     846 
    862847 
    863848    if (SWISH_DEBUG > 9) 
     
    905890            swish_debug_msg("free swish_ParseData wordList"); 
    906891 
    907         swish_free_WordList(ptr->wordlist); 
     892        swish_free_wordlist(ptr->wordlist); 
    908893    } 
    909894 
     
    16721657 
    16731658    parse_data->metastack = push_tag_stack( parse_data->metastack,  
    1674                                             (xmlChar *) SWISH_DEFAULT_METANAME); 
     1659                                            (xmlChar*)SWISH_DEFAULT_METANAME); 
    16751660 
    16761661    if (SWISH_DEBUG > 2) 
     
    16781663 
    16791664    chars_to_words(parse_data, buffer, size); 
    1680     flush_buffer(parse_data, NULL); 
     1665    flush_buffer(parse_data, (xmlChar*)SWISH_DEFAULT_METANAME, (xmlChar*)SWISH_DEFAULT_METANAME); 
    16811666     
    16821667    if (out != NULL) 
     
    17451730    ) 
    17461731{ 
    1747  
    1748     if (parse_data->analyzer->tokenize == 0) 
    1749         return; 
    17501732 
    17511733    if (len == 0) 
     
    17961778    if (tmplist->nwords == 0) 
    17971779    { 
    1798         swish_free_WordList(tmplist); 
     1780        swish_free_wordlist(tmplist); 
    17991781        return; 
    18001782    } 
     
    19041886add_stack_to_prop_buf(xmlChar * tag, swish_ParseData * parse_data) 
    19051887{ 
    1906     swish_TagStack *s = parse_data->propstack; 
     1888    swish_TagStack *s       = parse_data->propstack; 
     1889    int cleanwsp            = 1; 
     1890    xmlHashTablePtr props   = swish_subconfig_hash( parse_data->config, (xmlChar*)SWISH_PROP ); 
     1891     
     1892    /* should we strip whitespace from this particular property ? */ 
     1893    if( xmlStrEqual(xmlHashLookup(props, tag), (xmlChar*)SWISH_PROP_ASIS) ) 
     1894        cleanwsp = 0; 
     1895         
     1896    //swish_debug_msg(" add_stack_to_prop_buf: '%s'", xmlBufferContent(parse_data->prop_buf)); 
    19071897 
    19081898    if (tag != NULL) 
    1909         add_to_prop_buf(parse_data->prop_buf, parse_data->propHash, tag); 
     1899        swish_add_buf_to_nb(parse_data->properties,  
     1900                            tag, 
     1901                            parse_data->prop_buf,  
     1902                            (xmlChar*)SWISH_PROP_CONNECTOR, 
     1903                            cleanwsp, 0); 
    19101904 
    19111905    for (s->temp = s->head; s->temp != NULL; s->temp = s->temp->next) 
    19121906    { 
    1913         add_to_prop_buf(parse_data->prop_buf, parse_data->propHash, s->temp->name); 
     1907        if (xmlStrEqual(s->temp->name, "_"))    /* top of the stack is just a placeholder */ 
     1908            continue; 
     1909             
     1910        swish_add_buf_to_nb(parse_data->properties, 
     1911                            s->temp->name,  
     1912                            parse_data->prop_buf, 
     1913                            (xmlChar*)SWISH_PROP_CONNECTOR, 
     1914                            cleanwsp, 0); 
    19141915    } 
    19151916 
  • libswish3/trunk/src/libswish3/words.c

    r1927 r1930  
    4242static int      bytes_in_chr(wint_t c); 
    4343static void     make_ascii_tables(); 
    44  
     44static int      add_to_wordlist( 
     45                        swish_WordList * list, 
     46                        xmlChar * word, 
     47                        int len, 
     48                        xmlChar * metaname, 
     49                        xmlChar * context, 
     50                        int word_pos, 
     51                        int offset 
     52                ); 
    4553 
    4654static int initialized = 0; 
     
    6068 
    6169swish_WordList * 
    62 swish_init_WordList() 
     70swish_init_wordlist() 
    6371{ 
    6472    swish_WordList *wl = (swish_WordList *) swish_xmalloc(sizeof(swish_WordList)); 
     
    7280 
    7381void 
    74 swish_free_WordList(swish_WordList * list) 
     82swish_free_wordlist(swish_WordList * list) 
    7583{ 
    7684    swish_Word    *t; 
     
    133141{ 
    134142 
    135     swish_WordList *list = swish_init_WordList(); 
     143    swish_WordList *list = swish_init_wordlist(); 
    136144 
    137145 
     
    270278 
    271279    int byte_count = 0; 
    272     swish_WordList *list = swish_init_WordList(); 
     280    swish_WordList *list = swish_init_wordlist(); 
    273281    xmlChar * utf8_str; 
    274282 
     
    492500    char            c, nextc, in_word; 
    493501    int             i, w, wl, byte_count; 
    494     swish_WordList * list = swish_init_WordList(); 
     502    swish_WordList * list = swish_init_wordlist(); 
    495503    xmlChar        * word = swish_xmalloc(sizeof(xmlChar*) * analyzer->maxwordlen); 
    496504 
     
    824832* 
    825833***********************************************/ 
     834static int 
     835add_to_wordlist( 
     836        swish_WordList * list, 
     837        xmlChar * word, 
     838        int len, 
     839        xmlChar * metaname, 
     840        xmlChar * context, 
     841        int word_pos, 
     842        int offset 
     843) 
     844{ 
     845    swish_Word  *thisword = (swish_Word *) swish_xmalloc(sizeof(swish_Word)); 
     846     
     847    if (SWISH_DEBUG == SWISH_DEBUG_TOKENIZER) 
     848    { 
     849        swish_debug_msg(" >>>>>>>>swish_Word<<<<<<<<:  %s", word); 
     850        swish_debug_msg("     --METANAME--:  %s", metaname); 
     851        swish_debug_msg("     --CONTEXT---:  %s", context); 
     852        swish_debug_msg("     --POSITION--:  %d", word_pos); 
     853        swish_debug_msg("     --OFFSET----:  %d", offset); 
     854        swish_debug_msg("     --WORD LEN--:  %d", len); 
     855    } 
     856 
     857    /* add to wordlist */ 
     858 
     859    thisword->word     = word; 
     860    thisword->position = word_pos; 
     861     
     862    if (metaname != NULL) 
     863        thisword->metaname = swish_xstrdup(metaname); 
     864    else 
     865        thisword->metaname = swish_xstrdup((xmlChar*)SWISH_DEFAULT_METANAME); 
     866         
     867    if (context != NULL) 
     868        thisword->context  = swish_xstrdup(context); 
     869    else 
     870        thisword->context  = swish_xstrdup((xmlChar*)SWISH_DEFAULT_METANAME); 
     871         
     872    thisword->end_offset   = offset; 
     873    thisword->start_offset = offset - len + 1;  /* +1 because want the first byte */ 
     874 
     875    /* add thisword to list */ 
     876    if (list->head == 0) 
     877    { 
     878        list->head = thisword; 
     879        thisword->prev = 0; 
     880    } 
     881    else 
     882    { 
     883        list->tail->next = thisword; 
     884        thisword->prev = list->tail; 
     885    } 
     886 
     887    list->tail = thisword; 
     888    thisword->next = 0; 
     889 
     890    /* increment total count */ 
     891    list->nwords++; 
     892 
     893    return len; 
     894} 
     895 
    826896size_t 
    827897swish_add_to_wordlist( 
     
    835905{ 
    836906 
    837     swish_Word     *thisword = (swish_Word *) swish_xmalloc(sizeof(swish_Word)); 
    838     size_t          len = xmlStrlen(word); 
    839  
    840     if (SWISH_DEBUG == SWISH_DEBUG_TOKENIZER) 
    841     { 
    842         swish_debug_msg(" >>>>>>>>swish_Word<<<<<<<<:  %s", word); 
    843         swish_debug_msg("     --METANAME--:  %s", metaname); 
    844         swish_debug_msg("     --CONTEXT---:  %s", context); 
    845         swish_debug_msg("     --POSITION--:  %d", word_pos); 
    846         swish_debug_msg("     --OFFSET----:  %d", offset); 
    847         swish_debug_msg("     --WORD LEN--:  %d", (int)len); 
    848