1 /* $Id: snippet.c,v 1.11 2006-10-29 17:20:02 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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(void)
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 ord, const char *term)
52 zebra_snippets_append_match(l, seqno, ord, term, 0);
55 void zebra_snippets_append_match(zebra_snippets *l,
56 zint seqno, int ord, const char *term,
59 struct zebra_snippet_word *w = nmem_malloc(l->nmem, sizeof(*w));
70 w->term = nmem_strdup(l->nmem, term);
74 zebra_snippet_word *zebra_snippets_list(zebra_snippets *l)
79 void zebra_snippets_log(zebra_snippets *l, int log_level)
81 zebra_snippet_word *w;
82 for (w = l->front; w; w = w->next)
83 yaz_log(log_level, "term=%s%s seqno=" ZINT_FORMAT " ord=%d",
84 w->term, (w->match ? "*" : ""), w->seqno, w->ord);
87 zebra_snippets *zebra_snippets_window(zebra_snippets *doc, zebra_snippets *hit,
92 zebra_snippets *result = zebra_snippets_create();
94 window_size = 1000000;
99 zint first_seq_no_best_window = 0;
100 zint last_seq_no_best_window = 0;
101 int number_best_window = 0;
102 zebra_snippet_word *hit_w, *doc_w;
103 int min_ord = 0; /* not set yet */
105 for (hit_w = zebra_snippets_list(hit); hit_w; hit_w = hit_w->next)
106 if (hit_w->ord > ord &&
107 (min_ord == 0 || hit_w->ord < min_ord))
109 min_ord = hit_w->ord;
115 for (hit_w = zebra_snippets_list(hit); hit_w; hit_w = hit_w->next)
117 if (hit_w->ord == ord)
119 zebra_snippet_word *look_w = hit_w;
121 zint seq_no_last = 0;
122 while (look_w && look_w->seqno < hit_w->seqno + window_size)
124 if (look_w->ord == ord)
126 seq_no_last = look_w->seqno;
129 look_w = look_w->next;
131 if (number_this > number_best_window)
133 number_best_window = number_this;
134 first_seq_no_best_window = hit_w->seqno;
135 last_seq_no_best_window = seq_no_last;
139 yaz_log(YLOG_DEBUG, "ord=%d", ord);
140 yaz_log(YLOG_DEBUG, "first_seq_no_best_window=" ZINT_FORMAT,
141 first_seq_no_best_window);
142 yaz_log(YLOG_DEBUG, "last_seq_no_best_window=" ZINT_FORMAT,
143 last_seq_no_best_window);
144 yaz_log(YLOG_DEBUG, "number_best_window=%d", number_best_window);
146 window_start = (first_seq_no_best_window + last_seq_no_best_window -
148 for (doc_w = zebra_snippets_list(doc); doc_w; doc_w = doc_w->next)
149 if (doc_w->ord == ord
150 && doc_w->seqno >= window_start
151 && doc_w->seqno < window_start + window_size)
154 for (hit_w = zebra_snippets_list(hit); hit_w;
157 if (hit_w->ord == ord && hit_w->seqno == doc_w->seqno)
164 zebra_snippets_append_match(result, doc_w->seqno,
165 ord, doc_w->term, match);
174 * indent-tabs-mode: nil
176 * vim: shiftwidth=4 tabstop=8 expandtab