Show
Ignore:
Timestamp:
20.09.2010 10:52:00 (9 years ago)
Author:
davidb
Message:

Main change is to use reverse iterator (see comment in code), but also added a bit more white space, and { } on if-statements etc.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/runtime-src/src/recpt/summarise.cpp

    r20805 r22923  
    3333#include <string.h> 
    3434 
     35#include <iostream> 
     36using namespace std; 
    3537 
    3638/* **************** LOCAL PROTOTYPES **************** */ 
     
    99101*****************************************************/ 
    100102 
    101 text_t summarise_keywords(text_t &htmlstr, text_t &query, int summaryLength) { 
     103text_t summarise_keywords(text_t &htmlstr, text_t &query, int summaryLength)  
     104{ 
    102105 
    103106  if ((query.size()==0) || (htmlstr.size()==0)) { 
     
    109112 
    110113  // consider only non-empty terms 
    111   for(text_tarray::iterator term = allterms.begin(); 
     114  for (text_tarray::iterator term = allterms.begin(); 
    112115      term < allterms.end(); ++term) { 
    113     if(!(*term).empty()) 
     116    if (!(*term).empty()) 
    114117      terms.push_back(*term); 
    115118  } 
     
    130133    //   answers[0] contains sentences with 1 keyword 
    131134    //   answers[1] contains sentences with 2 keywords, etc. 
     135 
    132136  vector<int> answersSize(terms.size()); 
    133137    // answersSize[0] is the combined size of sentences with 1 keyword, etc. 
    134138  for(vector<int>::iterator size = answersSize.begin(); 
    135       size<answersSize.end(); ++size) 
     139      size<answersSize.end(); ++size) { 
    136140    *size = 0; // initialise sentence size 
     141  } 
    137142 
    138143  int totfound  = 0; 
    139144  text_t::iterator str_current = str_start; 
    140   while(str_current<str_end && answersSize[terms.size()-1]<summaryLength) { 
     145  while (str_current<str_end && answersSize[terms.size()-1]<summaryLength) { 
    141146    // if the size of best sentences is greater than summary, that's enough! 
    142147    text_t sentence = next_sentence(str_current,str_end); 
     
    144149    text_tarray::iterator terms_current = terms_start; 
    145150    int nFound = 0; 
    146     while(terms_current!=terms_end) { 
     151    while (terms_current!=terms_end) { 
    147152      text_t::iterator word = findword(sentence.begin(),sentence.end(), 
    148153                       *terms_current); 
    149       if(word!=sentence.end()) 
    150     {        ++nFound; ++totfound; } 
     154      if (word!=sentence.end()) { 
     155        ++nFound;  
     156    ++totfound;  
     157      } 
    151158      ++terms_current; 
    152159    } 
    153160 
    154     if(nFound>0 && answersSize[nFound-1]<summaryLength) { 
     161    if (nFound>0 && answersSize[nFound-1]<summaryLength) { 
    155162      answers[nFound-1].push_back(sentence); 
    156163      answersSize[nFound-1] += sentence.size(); 
     
    159166 
    160167  text_t answer; 
    161   for(vector<text_tarray>::iterator sentarray = answers.end()-1; 
    162       sentarray>=answers.begin(); --sentarray) 
    163     for(text_tarray::iterator sentence = (*sentarray).begin();  
     168 
     169  // Changed to using reverse iterator, as there is some concern as to 
     170  // whether the operations encoded with the usual iterator -- e.g.  
     171  // answers.end()-1 and so forth -- are safe.  Certainly the code 
     172  // works out tidier using the reverse iterator and the segmentation 
     173  // fault that was occurring in this block went away 
     174 
     175  for (vector<text_tarray>::reverse_iterator sentarray = answers.rbegin(); 
     176      sentarray<answers.rend(); ++sentarray) { 
     177    for (text_tarray::iterator sentence = (*sentarray).begin();  
    164178        sentence < (*sentarray).end(); ++sentence) { 
    165179      answer.append(*sentence); 
    166       if(answer.size()>=summaryLength) 
     180 
     181      if(answer.size()>=summaryLength) { 
    167182        return answer; 
    168     } 
    169  
    170   if(!answer.empty()) 
     183      } 
     184    } 
     185  } 
     186 
     187  if (!answer.empty()) { 
    171188    return answer; 
     189  } 
    172190 
    173191  return summarise_startend(htmlstr,summaryLength);