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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
//--------------------------------------------------------------------
//
// TextCollector.cc
//
// 2/6/2002 created for libhtdig
//
// Neal Richter nealr@rightnow.com
//
// TextCollector:
// General Purpose Text Document Indexer.
// Calls appropriate parsers.
// The parser notifies the TextCollector object that it got something
// (got_* functions) and the TextCollector object feed the databases
// and statistics accordingly.
//
//
// Part of the ht://Dig package <https://htdig.sourceforge.net/>
// 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 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: TextCollector.cc,v 1.4 2004/05/28 13:15:29 lha Exp $
//
//--------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include "TextCollector.h"
#include "htdig.h"
#include "HtWordList.h"
#include "WordRecord.h"
#include "URLRef.h"
#include "Server.h"
#include "Parsable.h"
#include "BasicDocument.h"
#include "StringList.h"
#include "WordType.h"
#include "md5.h"
#include "defaults.h"
#include <signal.h>
#include <stdio.h>
#include <sys/timeb.h>
//*****************************************************************************
// TextCollector::TextCollector()
//
TextCollector::TextCollector(TextCollectorLog flags):
words(*(HtConfiguration::config()))
{
HtConfiguration *config = HtConfiguration::config();
//FILE *urls_parsed;
currenthopcount = 0;
//turn on word tracking!
trackWords = 1;
//
// Initialize the flags for the various HTML factors
//
// text_factor
factor[0] = FLAG_TEXT;
// title_factor
factor[1] = FLAG_TITLE;
// heading factor (now generic)
factor[2] = FLAG_HEADING;
factor[3] = FLAG_HEADING;
factor[4] = FLAG_HEADING;
factor[5] = FLAG_HEADING;
factor[6] = FLAG_HEADING;
factor[7] = FLAG_HEADING;
// img alt text
//factor[8] = FLAG_KEYWORDS;
factor[8] = FLAG_TEXT; // treat alt text as plain text, until it has
// its own FLAG and factor.
// keywords factor
factor[9] = FLAG_KEYWORDS;
// META description factor
factor[10] = FLAG_DESCRIPTION;
doc = NULL;
minimumWordLength = config->Value("minimum_word_length", 3);
//TODO put document-index log file stuff here via logs like Retriever
check_unique_md5 = config->Boolean("check_unique_md5", 0);
check_unique_date = config->Boolean("check_unique_date", 0);
d_md5 = 0;
if (check_unique_md5)
{
d_md5 = Database::getDatabaseInstance(DB_HASH);
if (d_md5->OpenReadWrite(config->Find("md5_db"), 0666) != OK)
{
cerr << "DocumentDB::Open: " << config->Find("md5_db") << " " << strerror(errno) << "\n";
}
}
temp_doc_count = 0;
}
//*****************************************************************************
// TextCollector::~TextCollector()
//
TextCollector::~TextCollector()
{
if (d_md5)
d_md5->Close();
//delete doc;
if(temp_doc_count != 0)
{
words.Flush();
temp_doc_count = 0;
}
words.Flush();
words.Close();
}
//*****************************************************************************
// void TextCollector::IndexDoc()
//
//
int
TextCollector::IndexDoc(BasicDocument & a_basicdoc)
{
DocumentRef *ref;
time_t date;
int old_document = 0;
static int index = 0;
//struct timeb tb;
//HtConfiguration *config = HtConfiguration::config();
doc = &a_basicdoc;
ref = docs[doc->Location()]; // It might be nice to have just an Exists() here
if (ref)
{
//
// We already have an entry for this document in our database.
// This means we can get the document ID and last modification
// time from there.
//
current_id = ref->DocID();
date = ref->DocTime();
if (ref->DocAccessed())
old_document = 1;
else // we haven't retrieved it yet, so we only have the first link
old_document = 0;
ref->DocBackLinks(ref->DocBackLinks() + 1); // we had a new link
ref->DocAccessed(time(0));
ref->DocState(Reference_normal);
currenthopcount = ref->DocHopCount();
}
else
{
//
// Never seen this document before. We need to create an
// entry for it. This implies that it gets a new document ID.
//
date = 0;
current_id = docs.NextDocID();
ref = new DocumentRef;
ref->DocID(current_id);
ref->DocURL(doc->Location());
ref->DocState(Reference_normal);
ref->DocAccessed(time(0));
ref->DocHopCount(0);
ref->DocBackLinks(1); // We had to have a link to get here!
old_document = 0;
}
word_context.DocID(ref->DocID());
if (debug > 0)
{
//
// Display progress
//
cout << index++ << ':' << current_id << ':' << currenthopcount << ':' << doc->Location() <<
": ";
cout.flush();
}
//printf("New Doc\n");
//ftime(&tb);
//fprintf(stderr, "[1] TIME: [%s] [%d]\n", ctime(&tb.time), tb.millitm);
RetrievedDocument(ref);
//ftime(&tb);
//fprintf(stderr, "[2] TIME: [%s] [%d]\n", ctime(&tb.time), tb.millitm);
if(temp_doc_count > 250)
{
//words.Flush();
temp_doc_count = 0;
}
else
{
temp_doc_count++;
}
//ftime(&tb);
//fprintf(stderr, "[3] TIME: [%s] [%d]\n", ctime(&tb.time), tb.millitm);
docs.Add(*ref);
//ftime(&tb);
//fprintf(stderr, "[4] TIME: [%s] [%d]\n", ctime(&tb.time), tb.millitm);
delete ref;
words.Flush();
//words.Close();
if (urls_seen)
{
fprintf(urls_seen, "%s|%d|%s|%d|0|1\n",
(const char *) doc->Location(), doc->Length(), doc->ContentType(),
(int) doc->ModTime());
}
return(1);
}
int TextCollector::FlushWordDB()
{
if(temp_doc_count != 0)
{
words.Flush();
temp_doc_count = 0;
}
words.Flush();
words.Close();
return(1);
}
//*****************************************************************************
// void TextCollector::RetrievedDocument(Document &doc, const String &url, DocumentRef *ref)
// We found a document that needs to be parsed. Since we don't know the
// document type, we'll let the Document itself return an appropriate
// Parsable object which we can call upon to parse the document contents.
//
void
TextCollector::RetrievedDocument(DocumentRef * ref)
{
n_links = 0;
current_ref = ref;
current_title = 0;
word_context.Anchor(0);
current_time = 0;
current_head = 0;
current_meta_dsc = 0;
time_t doc_time;
//Check if the Document is self-parseable
//We will pass ourselves as a callback object for all the got_*() routines
if (doc->SelfParseable() == TRUE)
{
doc->internalParser(*this);
}
else
{
// Create a parser object and let it have a go at the document.
// We will pass ourselves as a callback object for all the got_*()
// routines.
// This will generate the Parsable object as a specific parser
/*
Parsable *parsable = doc->getParsable();
if (parsable)
parsable->parse(*this, *base);
else
{ // If we didn't get a parser, then we should get rid of this!
ref->DocState(Reference_noindex);
return;
}
*/
}
// We don't need to dispose of the parsable object since it will
// automatically be reused.
//
// Update the document reference
//
ref->DocTitle((char *) current_title);
ref->DocHead((char *) current_head);
ref->DocMetaDsc((char *) current_meta_dsc);
/* if (current_time == 0)
ref->DocTime(doc->ModTime());
else
ref->DocTime(current_time); */
doc_time = doc->ModTime();
if(doc_time != 0)
ref->DocTime(doc_time);
else
ref->DocTime(time(NULL));
ref->DocSize(doc->Length());
ref->DocAccessed(time(0));
ref->DocLinks(n_links);
}
//*****************************************************************************
// void TextCollector::got_word(char *word, int location, int heading)
// The location is normalized to be in the range 0 - 1000.
//
void
TextCollector::got_word(const char *word, int location, int heading)
{
if (debug > 3)
cout << "word: " << word << '@' << location << endl;
if (heading >= 11 || heading < 0) // Current limits for headings
heading = 0; // Assume it's just normal text
if ((trackWords) && (strlen(word) >= minimumWordLength))
{
String w = word;
HtWordReference wordRef;
wordRef.Location(location);
wordRef.Flags(factor[heading]);
wordRef.Word(w);
words.Replace(WordReference::Merge(wordRef, word_context));
#ifdef DEBUG
cout << "Adding: [" << w << "]"<< endl; //NEALR
#endif
// Check for compound words...
String parts = word;
int added;
int nparts = 1;
do
{
added = 0;
char *start = parts.get();
char *punctp = 0, *nextp = 0, *p;
char punct;
int n;
while (*start)
{
p = start;
for (n = 0; n < nparts; n++)
{
while (HtIsStrictWordChar((unsigned char) *p))
p++;
punctp = p;
if (!*punctp && n + 1 < nparts)
break;
while (*p && !HtIsStrictWordChar((unsigned char) *p))
p++;
if (n == 0)
nextp = p;
}
if (n < nparts)
break;
punct = *punctp;
*punctp = '\0';
if (*start && (*p || start > parts.get()))
{
w = start;
HtStripPunctuation(w);
if (w.length() >= minimumWordLength)
{
wordRef.Word(w);
words.Replace(WordReference::Merge(wordRef, word_context));
if (debug > 3)
cout << "word part: " << start << '@' << location << endl;
#ifdef DEBUG
cout << "Adding: [" << w << "]"<< endl; //NEALR
#endif
}
added++;
}
start = nextp;
*punctp = punct;
}
nparts++;
}
while (added > 2);
}
}
//*****************************************************************************
// void TextCollector::got_title(const char *title)
//
void
TextCollector::got_title(const char *title)
{
if (debug > 1)
cout << "\ntitle: " << title << endl;
current_title = title;
}
//*****************************************************************************
// void TextCollector::got_time(const char *time)
//
void
TextCollector::got_time(const char *time)
{
HtDateTime new_time(current_time);
if (debug > 1)
cout << "\ntime: " << time << endl;
//
// As defined by the Dublin Core, this should be YYYY-MM-DD
// In the future, we'll need to deal with the scheme portion
// in case someone picks a different format.
//
new_time.SetFTime(time, "%Y-%m-%d");
current_time = new_time.GetTime_t();
// If we can't convert it, current_time stays the same and we get
// the default--the date returned by the server...
}
//*****************************************************************************
// void TextCollector::got_head(const char *head)
//
void
TextCollector::got_head(const char *head)
{
if (debug > 4)
cout << "head: " << head << endl;
current_head = head;
}
//*****************************************************************************
// void TextCollector::got_meta_dsc(const char *md)
//
void
TextCollector::got_meta_dsc(const char *md)
{
if (debug > 4)
cout << "meta description: " << md << endl;
current_meta_dsc = md;
}
//*****************************************************************************
// void TextCollector::got_meta_email(const char *e)
//
void
TextCollector::got_meta_email(const char *e)
{
if (debug > 1)
cout << "\nmeta email: " << e << endl;
current_ref->DocEmail(e);
}
//*****************************************************************************
// void TextCollector::got_meta_notification(const char *e)
//
void
TextCollector::got_meta_notification(const char *e)
{
if (debug > 1)
cout << "\nmeta notification date: " << e << endl;
current_ref->DocNotification(e);
}
//*****************************************************************************
// void TextCollector::got_meta_subject(const char *e)
//
void
TextCollector::got_meta_subject(const char *e)
{
if (debug > 1)
cout << "\nmeta subect: " << e << endl;
current_ref->DocSubject(e);
}
//*****************************************************************************
// void TextCollector::got_noindex()
//
void
TextCollector::got_noindex()
{
if (debug > 1)
cout << "\nMETA ROBOT: Noindex " << current_ref->DocURL() << endl;
current_ref->DocState(Reference_noindex);
}
|