summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htsearch/DocMatch.cc
blob: 575c82aa14cfa88b2b39870479bf496428f5669a (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// DocMatch.cc
//
// DocMatch: Data object only. Contains information related to a given
//           document that was matched by a search. For instance, the
//           score of the document for this search.
//           
// 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: DocMatch.cc,v 1.8 2004/05/28 13:15:24 lha Exp $
//

#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */

#include "DocMatch.h"
#include "HtConfiguration.h"
#include "HtWordReference.h"

#ifdef HAVE_STD
#include <iostream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <iostream.h>
#endif /* HAVE_STD */

//*******************************************************************************
// DocMatch::DocMatch()
//


//*******************************************************************************
// DocMatch::~DocMatch()
//
DocMatch::~DocMatch()
{
}

//
// merge with another match
// sets anchor to the lower value
// merges location lists
//
void
DocMatch::Merge(const DocMatch &match)
{
        if(match.anchor < anchor)
        {
		anchor = match.anchor;
        }
        AddLocations(match.GetLocations());
}

//
// adds locations to an existing list
// avoiding duplicates, in location order
//
void
DocMatch::AddLocations(const List *locs)
{
	List *merge = new List;
	ListCursor c;

	locations->Start_Get();
	locs->Start_Get(c);
	Location *a = (Location *)locations->Get_Next();
	Location *b = (Location *)locs->Get_Next(c);
	while(a && b)
	{
		if(a->from < b->from)
		{
			merge->Add(a);
			a = (Location *)locations->Get_Next();
		}
		else if(a->from > b->from)
		{
			merge->Add(new Location(*b));
			b = (Location *)locs->Get_Next(c);
		}
		else // (a->from == b->from)
		{
			if(a->to < b->to)
			{
				merge->Add(new Location(*a));
				merge->Add(new Location(*b));
			}
			else if(a->to > b->to)
			{
				merge->Add(new Location(*b));
				merge->Add(new Location(*a));
			}
			else // (a->to == b->to)
			{
				merge->Add(new Location(
						a->from,
						a->to,
						a->flags,
						a->weight + b->weight));
			}
			a = (Location *)locations->Get_Next();
			b = (Location *)locs->Get_Next(c);
		}
	}
	while(a)
	{
		merge->Add(a);
		a = (Location *)locations->Get_Next();
	}
	while(b)
	{
		merge->Add(new Location(*b));
		b = (Location *)locs->Get_Next(c);
	}
	locations->Release();
	delete locations;
	locations = merge;
}

//
// set the location list
//
void
DocMatch::SetLocations(List *locs)
{
	delete locations;
	locations = locs;
}

//
// copy constructor, copies locations
//
DocMatch::DocMatch(const DocMatch &other)
{
	score = -1.0;
	//score = other.score;
	id = other.id;
	anchor = other.anchor;
	locations = new List;
	AddLocations(other.GetLocations());
}

//
// set weight of all locations
//
void
DocMatch::SetWeight(double weight)
{
	locations->Start_Get();
	for(int i = 0; i < locations->Count(); i++)
	{
		Location *loc = (Location *)locations->Get_Next();
		loc->weight = weight;
	}
}

//
// debug dump
//
void
DocMatch::Dump()
{
	cerr << "DocMatch id: " <<  id << " {" << endl;
	locations->Start_Get();
	for(int i = 0; i < locations->Count(); i++)
	{
		Location *loc = (Location *)locations->Get_Next();
		cerr << "location [" << loc->from;
		cerr << ", " << loc->to << "] ";
		cerr << "weight " << loc->weight;
		cerr << " flags " << loc->flags;
		cerr << endl;
	}
	cerr << "score: " << GetScore() << endl << "}" << endl;
}

double
DocMatch::GetScore()
{
  HtConfiguration* config= HtConfiguration::config();
  static double text_factor = config->Double("text_factor", 1);
  static double caps_factor = config->Double("caps_factor", 1);
  static double title_factor = config->Double("title_factor", 1);
  static double heading_factor = config->Double("heading_factor", 1);
  static double keywords_factor = config->Double("keywords_factor", 1);
  static double meta_desc_factor = config->Double("meta_description_factor", 1);
  static double author_factor = config->Double("author_factor", 1);
  static double description_factor = config->Double("description_factor", 1);
  static double url_text_factor = config->Double("url_text_factor", 1);

  if (score == -1.0)
    {
      score = 0.0;

      double locresult = 0.0;
      ListCursor c;
      locations->Start_Get(c);
      Location *loc = (Location *)locations->Get_Next(c);
      while(loc)
	{
	  locresult = 0.0;
	  if (loc->flags == FLAG_TEXT)		locresult += text_factor;
	  if (loc->flags & FLAG_CAPITAL)		locresult += caps_factor;
	  if (loc->flags & FLAG_TITLE)		locresult += title_factor;
	  if (loc->flags & FLAG_HEADING)		locresult += heading_factor;
	  if (loc->flags & FLAG_KEYWORDS)		locresult += keywords_factor;
	  if (loc->flags & FLAG_DESCRIPTION)	locresult += meta_desc_factor;
	  if (loc->flags & FLAG_AUTHOR)		locresult += author_factor;
	  if (loc->flags & FLAG_LINK_TEXT)	locresult += description_factor;
	  if (loc->flags & FLAG_URL)		locresult += url_text_factor;
	  
	  score += loc->weight * locresult;
	  loc = (Location *)locations->Get_Next(c);
	}
    }
  return score;
}