1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
6 * \file ccl_stop_words.c
7 * \brief Removes stop words from terms in RPN tree
16 struct ccl_stop_info {
19 struct ccl_stop_info *next;
22 struct ccl_stop_words {
24 NMEM nmem; /* memory for removed items */
25 struct ccl_stop_info *removed_items;
28 static void append_removed_item(ccl_stop_words_t csw,
30 const char *t, size_t len)
32 struct ccl_stop_info *csi = (struct ccl_stop_info *)
33 nmem_malloc(csw->nmem, sizeof(*csi));
34 struct ccl_stop_info **csip = &csw->removed_items;
36 csi->qualname = nmem_strdup(csw->nmem, qname);
40 csi->term = (char *) nmem_malloc(csw->nmem, len+1);
41 memcpy(csi->term, t, len);
42 csi->term[len] = '\0';
46 csip = &(*csip)->next;
51 ccl_stop_words_t ccl_stop_words_create(void)
53 NMEM nmem = nmem_create();
54 ccl_stop_words_t csw = (ccl_stop_words_t) xmalloc(sizeof(*csw));
56 csw->removed_items = 0;
57 csw->blank_chars = xstrdup(" \r\n\t");
61 void ccl_stop_words_destroy(ccl_stop_words_t csw)
65 nmem_destroy(csw->nmem);
66 xfree(csw->blank_chars);
71 struct ccl_rpn_node *ccl_remove_stop_r(ccl_stop_words_t csw,
73 struct ccl_rpn_node *p)
75 struct ccl_rpn_node *left, *right;
82 left = ccl_remove_stop_r(csw, bibset, p->u.p[0]);
83 right = ccl_remove_stop_r(csw, bibset, p->u.p[1]);
86 /* we must delete our binary node and return child (if any) */
104 char *cp = p->u.t.term;
108 while (*cp && strchr(csw->blank_chars, *cp))
115 while (*cp && !strchr(csw->blank_chars, *cp))
119 size_t len = cp - cp0;
120 if (ccl_search_stop(bibset, p->u.t.qual,
123 append_removed_item(csw, p->u.t.qual,
125 while (*cp && strchr(csw->blank_chars, *cp))
127 memmove(cp0, cp, strlen(cp)+1);
136 /* chop right blanks .. and see if term it gets empty */
137 if (p->u.t.term && csw->removed_items)
139 char *cp = p->u.t.term + strlen(p->u.t.term);
142 if (cp == p->u.t.term)
144 /* term is empty / blank */
148 if (!strchr(csw->blank_chars, cp[-1]))
160 int ccl_stop_words_tree(ccl_stop_words_t csw,
161 CCL_bibset bibset, struct ccl_rpn_node **t)
163 struct ccl_rpn_node *r;
165 /* remove list items */
166 nmem_reset(csw->nmem);
167 csw->removed_items = 0;
169 r = ccl_remove_stop_r(csw, bibset, *t);
171 if (csw->removed_items)
176 int ccl_stop_words_info(ccl_stop_words_t csw, int idx,
177 const char **qualname, const char **term)
179 struct ccl_stop_info *csi = csw->removed_items;
181 while (csi && i < idx)
188 *qualname = csi->qualname;
198 * c-file-style: "Stroustrup"
199 * indent-tabs-mode: nil
201 * vim: shiftwidth=4 tabstop=8 expandtab