File: WebEngine.cpp

package info (click to toggle)
pinot 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,308 kB
  • sloc: cpp: 39,057; sh: 10,481; ansic: 4,174; makefile: 612; xml: 372
file content (297 lines) | stat: -rw-r--r-- 6,478 bytes parent folder | download | duplicates (7)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 *  Copyright 2005-2011 Fabrice Colin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include <sys/types.h>
#include <glib.h>
#include <string>
#include <vector>
#include <iostream>

#include "StringManip.h"
#include "Url.h"
#include "DownloaderFactory.h"
#include "FilterUtils.h"
#include "CJKVTokenizer.h"
#include "WebEngine.h"

using std::clog;
using std::clog;
using std::endl;
using std::string;
using std::set;
using std::map;
using std::vector;

class TermHighlighter : public Dijon::CJKVTokenizer::TokensHandler
{
        public:
                TermHighlighter(string &extract, set<string> &queryTerms,
			unsigned int nGramSize) :
			Dijon::CJKVTokenizer::TokensHandler(),
			m_extract(extract),
			m_nGramSize(nGramSize),
			m_nGramCount(0),
			m_queryTerms(queryTerms)
		{
		}

		virtual ~TermHighlighter()
		{
		}

		virtual bool handle_token(const string &tok, bool is_cjkv)
		{
			gchar *pEscToken = NULL;
			gchar *pUTF8Token = NULL;
			gsize bytesWritten = 0;

			if (tok.empty() == true)
			{
				return false;
			}

			if (is_cjkv == false)
			{
				m_nGramCount = 0;
			}
			else
			{
				++m_nGramCount;
				if (tok.length() > 4)
				{
					// Skip multi-character tokens
#ifdef DEBUG
					clog << "WebEngine::processResult: skipping " << tok << endl;
#endif
					return true;
				}
			}

			pUTF8Token = g_locale_to_utf8(tok.c_str(), tok.length(),
				NULL, &bytesWritten, NULL);
			if (pUTF8Token != NULL)
			{
				pEscToken = g_markup_escape_text(pUTF8Token, -1);
				g_free(pUTF8Token);
			}
			if (pEscToken == NULL)
			{
				return true;
			}

			if ((m_extract.empty() == false) &&
				(m_nGramCount <= 1))
			{
				m_extract += " ";
			}
			// Is this a query term ?
			if (m_queryTerms.find(StringManip::toLowerCase(tok)) == m_queryTerms.end())
			{
				m_extract += pEscToken;
			}
			else
			{
				m_extract += "<b>";
				m_extract += pEscToken;
				m_extract += "</b>";
			}

			g_free(pEscToken);

			return true;
		}

	protected:
		string &m_extract;
		unsigned int m_nGramSize;
		unsigned int m_nGramCount;
		set<string> &m_queryTerms;

};

WebEngine::WebEngine() :
	SearchEngineInterface(),
	m_pDownloader(DownloaderFactory::getDownloader("http"))
{
}

WebEngine::~WebEngine()
{
	if (m_pDownloader != NULL)
	{
		delete m_pDownloader;
	}
}

Document *WebEngine::downloadPage(const DocumentInfo &docInfo)
{
	m_charset.clear();

	if (m_pDownloader == NULL)
	{
		return NULL;
	}

	Document *pDoc = m_pDownloader->retrieveUrl(docInfo);
	if (pDoc != NULL)
	{
		string contentType(pDoc->getType());

		// Is a charset specified ?
		string::size_type pos = contentType.find("charset=");
		if (pos != string::npos)
		{
			m_charset = StringManip::removeQuotes(contentType.substr(pos + 8));
#ifdef DEBUG
			clog << "WebEngine::downloadPage: page charset is " << m_charset << endl;
#endif
		}
	}

	return pDoc;
}

void WebEngine::setQuery(const QueryProperties &queryProps)
{
	queryProps.getTerms(m_queryTerms);
}

bool WebEngine::processResult(const string &queryUrl, DocumentInfo &result)
{
	Url queryUrlObj(queryUrl);
	string resultUrl(result.getLocation());
	string queryHost(Url::reduceHost(queryUrlObj.getHost(), 2));

	if (resultUrl.empty() == true)
	{
		return false;
	}

	// Is this URL relative to the search engine's domain ?
	if ((resultUrl[0] == '/') ||
		((resultUrl.length() > 1) &&
		(resultUrl[0] == '.') &&
		(resultUrl[1] == '/')))
	{
		string fullResultUrl(queryUrlObj.getProtocol());

		fullResultUrl += "://";
		fullResultUrl += queryUrlObj.getHost();
		if (resultUrl[0] == '.')
		{
			fullResultUrl += resultUrl.substr(1);
		}
		else
		{
			fullResultUrl += resultUrl;
		}

		resultUrl = fullResultUrl;
	}
	else
	{
		Url resultUrlObj(resultUrl);

		if ((resultUrlObj.getHost().empty() == true) ||
			(resultUrlObj.getHost() == "localhost"))
		{
			string fullResultUrl(queryUrlObj.getProtocol());

			fullResultUrl += "://";
			fullResultUrl += queryUrlObj.getHost();
			fullResultUrl += "/";
			fullResultUrl += resultUrl;
			resultUrl = fullResultUrl;
		}
	}


	Url resultUrlObj(resultUrl);

	// Is the result's host name the same as the search engine's ?
	// FIXME: not all TLDs have leafs at level 2
	if (queryHost == Url::reduceHost(resultUrlObj.getHost(), 2))
	{
		string protocol(resultUrlObj.getProtocol());

		if (protocol.empty() == false)
		{
			string embeddedUrl;

			string::size_type startPos = resultUrl.find(protocol, protocol.length());
			if (startPos != string::npos)
			{
				string::size_type endPos = resultUrl.find("&amp;", startPos);
				if (endPos != string::npos)
				{
					embeddedUrl = resultUrl.substr(startPos, endPos - startPos);
				}
				else
				{
					embeddedUrl = resultUrl.substr(startPos);
				}

				resultUrl = Url::unescapeUrl(embeddedUrl);
			}
#ifdef DEBUG
			else clog << "WebEngine::processResult: no embedded URL" << endl;
#endif
		}
#ifdef DEBUG
		else clog << "WebEngine::processResult: no protocol" << endl;
#endif
	}

	// Trim spaces
	string trimmedUrl(resultUrl);
	StringManip::trimSpaces(trimmedUrl);

	// Make the URL canonical
	result.setLocation(Url::canonicalizeUrl(trimmedUrl));

	// Scan the extract for query terms
	string extract(result.getExtract());
	if (extract.empty() == true)
	{
		return true;
	}

	Dijon::CJKVTokenizer tokenizer;
	TermHighlighter handler(extract, m_queryTerms, tokenizer.get_ngram_size());

	// Highlight query terms in the extract
	extract.clear();
	tokenizer.tokenize(result.getExtract(), handler);
	result.setExtract(extract);

	return true;
}

/// Returns the downloader used if any.
DownloaderInterface *WebEngine::getDownloader(void)
{
	return m_pDownloader;
}

/// Specifies values for editable parameters.
void WebEngine::setEditableValues(const map<string, string> &editableValues)
{
	m_editableValues = editableValues;
}