1 /* $Id: snippet.c,v 1.4 2005-06-07 15:12:39 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include <idzebra/snippet.h>
28 struct zebra_snippets {
30 zebra_snippet_word *front;
31 zebra_snippet_word *tail;
34 zebra_snippets *zebra_snippets_create()
36 NMEM nmem = nmem_create();
37 zebra_snippets *l = nmem_malloc(nmem, sizeof(*l));
39 l->front = l->tail = 0;
43 void zebra_snippets_destroy(zebra_snippets *l)
46 nmem_destroy(l->nmem);
49 void zebra_snippets_append(zebra_snippets *l,
50 zint seqno, int reg_type, int ord, const char *term)
52 zebra_snippets_append_match(l, seqno, reg_type, ord, term, 0);
55 void zebra_snippets_append_match(zebra_snippets *l,
56 zint seqno, int reg_type,
57 int ord, const char *term, int match)
59 struct zebra_snippet_word *w = nmem_malloc(l->nmem, sizeof(*w));
69 w->reg_type = reg_type;
71 w->term = nmem_strdup(l->nmem, term);
75 zebra_snippet_word *zebra_snippets_list(zebra_snippets *l)
80 void zebra_snippets_log(zebra_snippets *l, int log_level)
82 zebra_snippet_word *w;
83 for (w = l->front; w; w = w->next)
84 yaz_log(log_level, "term=%s%s seqno=" ZINT_FORMAT " reg_type=%c "
86 w->term, (w->match ? "*" : ""), w->seqno, w->reg_type, w->ord);
89 zebra_snippets *zebra_snippets_window(zebra_snippets *doc, zebra_snippets *hit,
94 zebra_snippets *result = zebra_snippets_create();
96 window_size = 1000000;
102 int first_seq_no_best_window = 0;
103 int last_seq_no_best_window = 0;
104 int number_best_window = 0;
105 zebra_snippet_word *hit_w, *doc_w;
106 int min_ord = 0; /* not set yet */
108 for (hit_w = zebra_snippets_list(hit); hit_w; hit_w = hit_w->next)
109 if (hit_w->ord > ord &&
111 (hit_w->ord < min_ord && hit_w->reg_type == reg_type)))
113 min_ord = hit_w->ord;
114 reg_type = hit_w->reg_type;
120 for (hit_w = zebra_snippets_list(hit); hit_w; hit_w = hit_w->next)
122 if (hit_w->ord == ord)
124 zebra_snippet_word *look_w = hit_w;
127 while (look_w && look_w->seqno < hit_w->seqno + window_size)
129 if (look_w->ord == ord && look_w->reg_type == reg_type)
131 seq_no_last = look_w->seqno;
134 look_w = look_w->next;
136 if (number_this > number_best_window)
138 number_best_window = number_this;
139 first_seq_no_best_window = hit_w->seqno;
140 last_seq_no_best_window = seq_no_last;
144 yaz_log(YLOG_DEBUG, "ord=%d", ord);
145 yaz_log(YLOG_DEBUG, "first_seq_no_best_window=%d", first_seq_no_best_window);
146 yaz_log(YLOG_DEBUG, "last_seq_no_best_window=%d", last_seq_no_best_window);
147 yaz_log(YLOG_DEBUG, "number_best_window=%d", number_best_window);
149 window_start = (first_seq_no_best_window + last_seq_no_best_window -
151 for (doc_w = zebra_snippets_list(doc); doc_w; doc_w = doc_w->next)
152 if (doc_w->ord == ord && doc_w->reg_type == reg_type
153 && doc_w->seqno >= window_start
154 && doc_w->seqno < window_start + window_size)
157 for (hit_w = zebra_snippets_list(hit); hit_w; hit_w = hit_w->next)
159 if (hit_w->ord == ord && hit_w->reg_type == reg_type &&
160 hit_w->seqno == doc_w->seqno)
167 zebra_snippets_append_match(result, doc_w->seqno,
168 doc_w->reg_type, ord,