Hi you all, i have a function that searches paragraphs in a file and compares whether within that particular paragraph, there is word or pair/triple-word. Up to here works well if you pass for example "John", the problem I have is when i give in console, for example "John Doe" or "John Doe Doll". Always find only "John" I whant to find pairs/triples or more words, always taking care the case-sensitive, since it is not the same "John Doe Doll" that "john doe doll". Must be the exact search.. Then, the function i have:
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
|
string findParagraphs( istream &in ) {
string searchWord;
string result, line, resultMATCH;
string::size_type position, found;
const string LF("\n");
const string CR("\r");
const string DOT(".");
const string SPACE(" ");
const string NUL("\0");
string whitespaces ("\r\n\v\f\t");
bool IsParagraph;
int numline=0, paragraph=0, paragraphMATCH=0;
unsigned int place;
searchWord = wordsToFind(); // Here i pass the words
while (getline( in, line )) {
IsParagraph = false;
if (line.empty()) {
line.clear();
}
found=line.find_last_not_of(whitespaces);
if (found!=string::npos)
line.erase(found+1);
else {
line.clear();
}
position = line.find_last_of(DOT);
//cout << "DOT found in position " << position << endl; // Comprobation
if ((position != string::npos && line.substr(position+1)==SPACE && line.substr(position+2)==NUL) ||
(position != string::npos && line.substr(position+1)==SPACE && line.substr(position+2)==CR)) {
paragraph++;
IsParagraph = true;
}
if (IsParagraph){
// Store the Paragraph in new lines "LF"
result += line +LF;
place = result.find(searchWord); // HERE THE PROBLEM!!!...
if (place != string::npos) {
paragraphMATCH++;
//Store only the paragraphs that match the search words
resultMATCH += result;
}
// Clean result to avoid duplicates
result.clear();
} else {
// Concatenate lines that have no end of paragraph
result += line +SPACE;
result.erase( result.length() -1 );
}
numline++;
}
cout << " ===================================== " << endl;
cout << "+Total number of lines analyzed: " << numline << endl;
cout << "+Total number of paragraphs: " << paragraph << endl;
cout << "+Paragraphs that contain the search: " << paragraphMATCH << endl;
cout << " ===================================== " << endl;
return resultMATCH;
}
|
The function plays the role of finding paragraphs but takes only the 1st word entered "John"..
For example:
John - find in 122 paragraphs
John Doe - find in 122 paragraphs
John Doe Doll - find in 122 paragraphs
Doe Doll - find in 233 paragraphs
But the words "John Doe Doll" together, are in 80 paragraphs. :/
Apreciate any help... Thanks in advance!!!....
Mac