1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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
19 struct ccl_stop_info {
22 struct ccl_stop_info *next;
25 struct ccl_stop_words {
27 NMEM nmem; /* memory for removed items */
28 struct ccl_stop_info *removed_items;
31 static void append_removed_item(ccl_stop_words_t csw,
33 const char *t, size_t len)
35 struct ccl_stop_info *csi = (struct ccl_stop_info *)
36 nmem_malloc(csw->nmem, sizeof(*csi));
37 struct ccl_stop_info **csip = &csw->removed_items;
39 csi->qualname = nmem_strdup(csw->nmem, qname);
43 csi->term = (char *) nmem_malloc(csw->nmem, len+1);
44 memcpy(csi->term, t, len);
45 csi->term[len] = '\0';
49 csip = &(*csip)->next;
54 ccl_stop_words_t ccl_stop_words_create(void)
56 NMEM nmem = nmem_create();
57 ccl_stop_words_t csw = (ccl_stop_words_t) xmalloc(sizeof(*csw));
59 csw->removed_items = 0;
60 csw->blank_chars = xstrdup(" \r\n\t");
64 void ccl_stop_words_destroy(ccl_stop_words_t csw)
68 nmem_destroy(csw->nmem);
69 xfree(csw->blank_chars);
74 struct ccl_rpn_node *ccl_remove_stop_r(ccl_stop_words_t csw,
76 struct ccl_rpn_node *p)
78 struct ccl_rpn_node *left, *right;
85 left = ccl_remove_stop_r(csw, bibset, p->u.p[0]);
86 right = ccl_remove_stop_r(csw, bibset, p->u.p[1]);
89 /* we must delete our binary node and return child (if any) */
107 char *cp = p->u.t.term;
111 while (*cp && strchr(csw->blank_chars, *cp))
118 while (*cp && !strchr(csw->blank_chars, *cp))
122 size_t len = cp - cp0;
123 if (ccl_search_stop(bibset, p->u.t.qual,
126 append_removed_item(csw, p->u.t.qual,
128 while (*cp && strchr(csw->blank_chars, *cp))
130 memmove(cp0, cp, strlen(cp)+1);
139 /* chop right blanks .. and see if term it gets empty */
140 if (p->u.t.term && csw->removed_items)
142 char *cp = p->u.t.term + strlen(p->u.t.term);
145 if (cp == p->u.t.term)
147 /* term is empty / blank */
151 if (!strchr(csw->blank_chars, cp[-1]))
163 int ccl_stop_words_tree(ccl_stop_words_t csw,
164 CCL_bibset bibset, struct ccl_rpn_node **t)
166 struct ccl_rpn_node *r;
168 /* remove list items */
169 nmem_reset(csw->nmem);
170 csw->removed_items = 0;
172 r = ccl_remove_stop_r(csw, bibset, *t);
174 if (csw->removed_items)
179 int ccl_stop_words_info(ccl_stop_words_t csw, int idx,
180 const char **qualname, const char **term)
182 struct ccl_stop_info *csi = csw->removed_items;
184 while (csi && i < idx)
191 *qualname = csi->qualname;
201 * c-file-style: "Stroustrup"
202 * indent-tabs-mode: nil
204 * vim: shiftwidth=4 tabstop=8 expandtab