add .gitignore
[ftsbench.git] / stopfilter.c
1 /*
2  * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *        notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *        notice, this list of conditions and the following disclaimer in the
12  *        documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *        may be used to endorse or promote products derived from this software
15  *        without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34
35 /*
36  * Utility for filtering lex file from stop words
37  */
38
39
40 #define TXTBUFLEN       4096
41
42 typedef struct
43 {
44     int         len;
45         char      **stop;
46 }   StopList;
47
48 static int
49 comparestr(const void *a, const void *b)
50 {
51         return strcasecmp(*(char **) a, *(char **) b);
52 }
53
54 static void
55 readstoplist(char *filename, StopList * s)
56 {
57         char      **stop = NULL;
58         FILE       *hin;
59         char            buf[TXTBUFLEN];
60         int                     reallen = 0;
61
62         s->len = 0;
63         if ((hin = fopen(filename, "r")) == NULL) {
64                 fprintf(stderr,"Can't open %s: %s\n", filename, strerror(errno));
65                 exit(1);
66         }
67
68         while (fgets(buf, TXTBUFLEN, hin))
69         {
70                 buf[strlen(buf) - 1] = '\0';
71                 if (*buf == '\0')
72                         continue;
73
74                 if (s->len >= reallen)
75                 {
76                         char      **tmp;
77
78                         reallen = (reallen) ? reallen * 2 : 16;
79                         tmp = (char **) realloc((void *) stop, sizeof(char *) * reallen);
80                         if (!tmp)
81                         {
82                                 fprintf(stderr,"Not enough memory\n");
83                                 exit(1);
84                         }
85                         stop = tmp;
86                 }
87
88                 stop[s->len] = strdup(buf);
89                 if (!stop[s->len])
90                 {
91                         fprintf(stderr,"Not enough memory\n");
92                         exit(1);
93                 }
94
95                 (s->len)++;
96         }
97         fclose(hin);
98         s->stop = stop;
99
100         if (s->stop && s->len > 1)
101                 qsort(s->stop, s->len, sizeof(char *), comparestr);
102 }
103
104 static int
105 searchstoplist(StopList * s, char *key)
106 {
107         if ( strlen(key) <=4 )
108                 return 1;
109         return (s->stop && s->len > 0 && bsearch(&key, s->stop, s->len, sizeof(char *), comparestr)) ? 1 : 0;
110 }
111
112 int 
113 main(int argn, char *argv[]) {
114         char    buf[TXTBUFLEN];
115         StopList        sl={0,NULL};
116
117         if ( argn != 2 ) {
118                 fprintf(stderr,"Usage: %s stopfile < lex\n", argv[0]);
119                 exit(1);
120         }
121
122         readstoplist(argv[1], &sl);
123
124         while( fgets(buf, TXTBUFLEN, stdin) ) {
125                 char    wrd[TXTBUFLEN];
126                 int             occur;
127
128                 if ( sscanf( buf, "%s %d", wrd, &occur )!= 2)  
129                         continue;
130
131                 if ( searchstoplist(&sl, wrd) || occur <=0 )
132                         continue;
133
134                 printf("%s %d\n", wrd, occur);
135         }
136
137         return 0;
138 }