summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htsearch/PhraseQuery.cc
blob: a42d97b37fc9c0918e83ad2da676831bed25b88d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 
// PhraseQuery.cc
//
// PhraseQuery: an operator query that filters sequenced word matches
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 1995-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
// 
// $Id: PhraseQuery.cc,v 1.4 2004/05/28 13:15:24 lha Exp $
// 

#include "PhraseQuery.h"

//
// evaluate operands and make a result with matches if some.
//
ResultList *
PhraseQuery::Evaluate()
{
	ResultList *result = 0;

	operands.Start_Get();
	Query *next = (Query *)operands.Get_Next();
	if(next)
	{
		result = (ResultList *)next->GetResults();
		next = (Query *)operands.Get_Next();
	}
	if(result)
	{
		result = new ResultList(*result);
	}
	while(result && next)
	{
		ResultList *r = next->GetResults();
		if(r)
		{
			if(result->IsIgnore())
			{
				delete result;
				result = new ResultList(*r);
			}
			else if(!r->IsIgnore())
			{
				ResultList *tmp = result;
				result = Near(*tmp, *r);
				delete tmp;
			}
			next = (Query *)operands.Get_Next();
		}
		else
		{
			delete result;
			result = 0;
		}
	}
	return result;
}

String
PhraseQuery::GetLogicalWords() const
{
	ListCursor c;
	String out;
	out << "\"";
	if(operands.Count())
	{
		operands.Start_Get(c);
		out << ((Query *) operands.Get_Next(c))->GetLogicalWords();
		Query *next = (Query *) operands.Get_Next(c);
		while(next)
		{
			out << " ";
			if(next)
			{
				out << next->GetLogicalWords();
			}
			else
			{
				out << "*nothing*";
			}
			next = (Query *) operands.Get_Next(c);
		}
	}
	out << "\"";
	return out;
}

//
// return a resultlist containing matches that are contiguous
//
ResultList *
PhraseQuery::Near(const ResultList &l, const ResultList &r)
{
	ResultList *result = 0;
	DictionaryCursor c;
	l.Start_Get(c);
	DocMatch *match = (DocMatch *)l.Get_NextElement(c);
	while(match)
	{
		DocMatch *confirm = r.find(match->GetId());
		if(confirm)
		{
			List *locations = MergeLocations(
						*match->GetLocations(),
						*confirm->GetLocations());
			if(locations)
			{
				if(!result)
				{
					result = new ResultList;
				}
				DocMatch *copy = new DocMatch(*match);
				copy->SetLocations(locations);
				result->add(copy);
			}
		}
		match = (DocMatch *)l.Get_NextElement(c);
	}
	return result;
}


//
//: merge match positions in a 'next' operation
// each position of left operand match is tested against right operand positions
// if two contiguous positions are found, they are merged into a single one
// beginning at the begin of the left operand
// and ending and the end of the right operand
// 
List *
PhraseQuery::MergeLocations(const List &p, const List &q)
{
	List *result = 0;
	ListCursor pc;
	p.Start_Get(pc);
	const Location *left = (const Location *)p.Get_Next(pc);
	while(left)
	{
		ListCursor qc;
		q.Start_Get(qc);
		const Location *right = (const Location *)q.Get_Next(qc);
		while(right)
		{
			if(left->to + 1 == right->from)
			{
				double prevsize = left->to - left->from + 1.0;
				double addsize = right->to - right->from + 1.0;
				double weight = 
					((left->weight * prevsize) +
					(right->weight * addsize)) / 
					(right->to - left->from + 1.0);

				if(!result)
				{
					result = new List;
				}

				result->Add(new Location(
						left->from,
						right->to,
						left->flags & right->flags,
						weight));
				break;
			}
			right = (const Location *)q.Get_Next(qc);
		}
		left = (const Location *)p.Get_Next(pc);
	}
	return result;
}