LCOV - code coverage report
Current view: top level - languages - steminternal.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core r Lines: 24 24 100.0 %
Date: 2011-08-21 Functions: 9 9 100.0 %
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file steminternal.h
       2                 :            :  *  @brief Base class for implementations of stemming algorithms
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2009,2010 Olly Betts
       5                 :            :  * Copyright (C) 2010 Evgeny Sizikov
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU General Public License as
       9                 :            :  * published by the Free Software Foundation; either version 2 of the
      10                 :            :  * License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef XAPIAN_INCLUDED_STEMINTERNAL_H
      23                 :            : #define XAPIAN_INCLUDED_STEMINTERNAL_H
      24                 :            : 
      25                 :            : #include <xapian/base.h>
      26                 :            : #include <xapian/stem.h>
      27                 :            : 
      28                 :            : #include <cstdlib>
      29                 :            : #include <string>
      30                 :            : 
      31                 :            : typedef unsigned char symbol;
      32                 :            : 
      33                 :            : #define HEAD (2*sizeof(int))
      34                 :            : 
      35                 :            : // Cast via (void*) to avoid warnings about alignment (the pointers *are*
      36                 :            : // appropriately aligned).
      37                 :            : 
      38                 :            : inline int
      39                 :    5192450 : SIZE(const symbol* p)
      40                 :            : {
      41                 :    5192450 :     const void * void_p = reinterpret_cast<const void *>(p);
      42                 :    5192450 :     return reinterpret_cast<const int *>(void_p)[-1];
      43                 :            : }
      44                 :            : 
      45                 :            : inline void
      46                 :    4516409 : SET_SIZE(symbol* p, int n)
      47                 :            : {
      48                 :    4516409 :     void * void_p = reinterpret_cast<void *>(p);
      49                 :    4516409 :     reinterpret_cast<int *>(void_p)[-1] = n;
      50                 :    4516409 : }
      51                 :            : 
      52                 :            : inline int
      53                 :    4483458 : CAPACITY(const symbol* p)
      54                 :            : {
      55                 :    4483458 :     const void * void_p = reinterpret_cast<const void *>(p);
      56                 :    4483458 :     return reinterpret_cast<const int *>(void_p)[-2];
      57                 :            : }
      58                 :            : 
      59                 :            : inline void
      60                 :      64723 : SET_CAPACITY(symbol* p, int n)
      61                 :            : {
      62                 :      64723 :     void * void_p = reinterpret_cast<void *>(p);
      63                 :      64723 :     reinterpret_cast<int *>(void_p)[-2] = n;
      64                 :      64723 : }
      65                 :            : 
      66                 :            : typedef int (*among_function)(Xapian::StemImplementation *);
      67                 :            : 
      68                 :            : struct among {
      69                 :            :     int s_size;         /* length of search string (in symbols) */
      70                 :            :     unsigned s;         /* offset in pool to search string */
      71                 :            :     int substring_i;    /* index to longest matching substring */
      72                 :            :     int result;         /* result of the lookup */
      73                 :            : };
      74                 :            : 
      75                 :            : extern symbol * create_s();
      76                 :            : 
      77                 :      32951 : inline void lose_s(symbol * p) {
      78         [ +  - ]:      32951 :     if (p) std::free(reinterpret_cast<char *>(p) - HEAD);
      79                 :      32951 : }
      80                 :            : 
      81                 :            : extern int skip_utf8(const symbol * p, int c, int lb, int l, int n);
      82                 :            : 
      83                 :            : namespace Xapian {
      84                 :            : 
      85                 :            : class SnowballStemImplementation : public StemImplementation {
      86                 :            :     int slice_check();
      87                 :            : 
      88                 :            :   protected:
      89                 :            :     symbol * p;
      90                 :            :     int c, l, lb, bra, ket;
      91                 :            : 
      92                 :            :     int get_utf8(int * slot);
      93                 :            :     int get_b_utf8(int * slot);
      94                 :            : 
      95                 :            :     int in_grouping_U(const unsigned char * s, int min, int max, int repeat);
      96                 :            :     int in_grouping_b_U(const unsigned char * s, int min, int max, int repeat);
      97                 :            :     int out_grouping_U(const unsigned char * s, int min, int max, int repeat);
      98                 :            :     int out_grouping_b_U(const unsigned char * s, int min, int max, int repeat);
      99                 :            : 
     100                 :            :     int eq_s(int s_size, const symbol * s);
     101                 :            :     int eq_s_b(int s_size, const symbol * s);
     102                 :            :     int eq_v(const symbol * v) { return eq_s(SIZE(v), v); }
     103                 :     187410 :     int eq_v_b(const symbol * v) { return eq_s_b(SIZE(v), v); }
     104                 :            : 
     105                 :            :     int find_among(const symbol *pool, const struct among * v, int v_size,
     106                 :            :                    const unsigned char * fnum, const among_function * f);
     107                 :            :     int find_among_b(const symbol *pool, const struct among * v, int v_size,
     108                 :            :                      const unsigned char * fnum, const among_function * f);
     109                 :            : 
     110                 :            :     int replace_s(int c_bra, int c_ket, int s_size, const symbol * s);
     111                 :            :     int slice_from_s(int s_size, const symbol * s);
     112                 :            :     int slice_from_v(const symbol * v) { return slice_from_s(SIZE(v), v); }
     113                 :            : 
     114                 :    1064682 :     int slice_del() { return slice_from_s(0, 0); }
     115                 :            : 
     116                 :            :     void insert_s(int c_bra, int c_ket, int s_size, const symbol * s);
     117                 :       4190 :     void insert_v(int c_bra, int c_ket, const symbol * v) {
     118                 :       4190 :         insert_s(c_bra, c_ket, SIZE(v), v);
     119                 :       4190 :     }
     120                 :            : 
     121                 :            :     symbol * slice_to(symbol * v);
     122                 :            :     symbol * assign_to(symbol * v);
     123                 :            : 
     124                 :            : #if 0
     125                 :            :     void debug(int number, int line_count);
     126                 :            : #endif
     127                 :            : 
     128                 :            :   public:
     129                 :            :     /// Perform initialisation common to all Snowball stemmers.
     130                 :      32943 :     SnowballStemImplementation()
     131                 :      32943 :         : p(create_s()), c(0), l(0), lb(0), bra(0), ket(0) { }
     132                 :            : 
     133                 :            :     /// Perform cleanup common to all Snowball stemmers.
     134                 :            :     virtual ~SnowballStemImplementation();
     135                 :            : 
     136                 :            :     /// Stem the specified word.
     137                 :            :     virtual std::string operator()(const std::string & word);
     138                 :            : 
     139                 :            :     /// Virtual method implemented by the subclass to actually do the work.
     140                 :            :     virtual int stem() = 0;
     141                 :            : };
     142                 :            : 
     143                 :            : }
     144                 :            : 
     145                 :            : #endif // XAPIAN_INCLUDED_STEMINTERNAL_H

Generated by: LCOV version 1.8