Detecting a sentence ending with "?", "!",

Pages: 12
> do need to add another code to do the searching seperately?

Yes. you have vector<string> sentence which holds the sentences, and vector<string> curr_word which holds the words to search for.
(Though, I can't fathom why anyone would want to call a vector of words curr_word).

Now, for each word find out the sentences that have the word in them.
Tip: ignore case, ignore punctuation, a word must be delimited by white-space on either side.

If the sentence is:
The woods are lovely, dark, and deep, But I have promises to keep, And miles to go before I sleep

You should find: the, dark, Dark and DEEP

But not find: wood, peek, fore, be
@JLBorges... Thanks for your effort.. maybe i seem to be the one confusing myself. its not letting me search for words that is contained in the sentence and then display the sentences which the words are found.

i understand that vector<string> sentence now contains the sentences that i need to search in.

i don't understand vector<string> curr_word, because in last code i posted and in yours, curr_word is not in a vector its just a string. the vector that contains the word to search for is vector <string> word

To search for the words entered by the user at the beginning i used the code below..

first i created a search function to search for words

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool searchWord(string line, string topic)
{
	bool found = false;

	size_t location;

	location = line.find(topic);

	if (location != string::npos)
	{
		found = true;		
	}

	return found;

}


then i used a loop to search through the vector<string> sentence and calling the function in it based on the number of words entered by the user.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int j = 0; j < word.size(); j++)
	{
	
		for (std::size_t  n = 0; n < sentence.size(); n++)
		{
			if (searchWord(sentence[n], word[j]))
			{
				std::cout << ' ' << sentence[n];
				std::cout << '\n'; std::cout << '\n';
				sentence.erase(sentence.begin() + n);
			}		
		}

	}



but am just still hooked... please help me check it
Last edited on
> maybe i seem to be the one confusing myself.

Yes. Why are you modifying the sentences with sentence.erase(sentence.begin() + n); during the search?

The search would be something like this:

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
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <iostream>

// replace punctuation with space, convert to lower case
std::string sanitize( const std::string& str )
{
    std::string result ;
    for( char c : str )
    {
        if( std::ispunct(c) ) result += ' ' ;
        else result += std::tolower(c) ;
    }
    return result ;
}

// remove leading and trailing whitespace
std::string trim( std::string str )
{
    std::stringstream stm ;
    stm << str ;
    stm >> str ;
    return str ;
}

bool sentence_has_word( std::string sentence, std::string word )
{
    sentence = sanitize(sentence) ;

    word = trim( sanitize(word) ) ;
    if( word.size() > sentence.size() ) return false ;
    if( word.size() == sentence.size() ) return word == sentence ;

    auto pos = sentence.find( word + ' ' ) ; // check for word at the beginning
    if( pos == 0 ) return true ;
    
    pos = sentence.find( ' ' + word + ' ' ) ; // check for word in the middle
    if( pos != std::string::npos ) return true ; 
    
    word = ' ' + word ;
    return sentence.substr( sentence.size() - word.size() ) == word  ; // check for word at the end
}

int main()
{
    const std::vector<std::string> sentences =
    {
        "The woods are lovely, dark, and deep, "
        "But I have promises to keep, "
        "And miles to go before I sleep.",

        "You linger your little hour and are gone, "
        "And still the woods sweep leafily on.",

        "Two roads diverged in a wood, and I, "
        "I took the one less traveled by, "
        "And that has made all the difference."
    };

    const std::vector<std::string> words =
    { "wood", "Woods", "DARK", "be", "fore", " traveled ", " linger ", "two" };

    for( std::string word : words )
    {
        std::cout << '"' << word << "\"\n" ;
        for( std::string sentence : sentences )
            if( sentence_has_word(sentence,word) ) std::cout << sentence << '\n' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/adb2914873d621c6
@JLBorges, in you last code, my compiler is not allowing me to initialze the const std::vector<std::string> sentences and const std::vector<std::string> words .its saying you cant use the braises
Use your own code to create those two vectors.

std::vector<std::string> sentences should be populated with the sentences read from the input file
and std::vector<std::string> words should contain the words typed in by the user of the program.
@JLBorges I cannot Thank you enough. I finally got it right. i used your code and mine to get the work done.. Am very grateful. i modified it to even search for word phrases like " Thank You".
@Cire... Thanks for your words it really gave me the push.

here is how i got it done.


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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
using namespace std;

vector<string> split(const string &s, char delim);
int main()
{
	char question = '?';
	
	char exclamation = '!';
	char TempAssign = 0;
	ifstream inFile;
	string filePath;
	string tempString;
	stringstream ss;
	vector<string> words;
	vector<string> sentence;
	vector<string> sentences;
	string curr_sentence ; // current sentence being built
    string curr_word ; // the word just read
    const string sentence_enders = "?!." ;



	cout << " User-Query Based Automatic summarizer" << endl << endl;

	cout << "Please enter the file path that contains the text to be summarized: ";
	getline(cin, filePath);
	cout << endl;

	//opens the input file
	inFile.open(filePath);

	//makes sure the input file opened successfully
	if (!inFile)
	{
		cout << "Error unable to open file " << filePath << " Program terminated";
		cin.ignore();
		exit(1);
	}
	else { cout << "      File Found !" << endl<<endl; }

	cout << "Please enter the words to be used in generating your summary (Please seperate each word with spaces )  then press enter when done: ";
	getline(cin, tempString);
	cout << endl;
	cout << endl;
	ss << tempString;

	//extracts individual words into a vector
	while (ss >> tempString)
	{
		words.push_back(tempString);
	}

	vector <string> word_delim = split(tempString, ', ');
  

    // read word by word from the file until input fails
    while( inFile >> curr_word ) // for each word read
    {
        curr_sentence += curr_word ; // add word to current sentence

        // if the current word ends with a sentence ender
        if( sentence_enders.find( curr_word[ curr_word.size() - 1 ] ) != string::npos )
        {
            // this is the end of the sentence.
            sentence.push_back( curr_sentence ) ; // add current sentence to sentence vector
            curr_sentence= "" ; // and begin a new empty sentence
        }

        else curr_sentence += ' ' ; // put a space between this and the next word
    }

    // add the last sentence even if no sentence ender was found
    if( !curr_sentence.empty() ) sentence.push_back( curr_sentence ) ;



	for (size_t i = 0; i < word_delim.size(); i++)
			{ 
				for ( size_t x =0; x < sentence.size (); x++)
				{
				//checks to see if a word can be found in tempString
					if (sentence[x].find(word_delim[i]) != std::string::npos)
					{
						//removes any newlines in the string
						while (sentence[x].front() == '\n' || sentence[x].front() == ' ')
							sentence[x] = sentence[x].substr(1, sentence[x].size() - 1);
						while (sentence[x].back() == '\n' || sentence[x].back() == ' ')
							sentence[x] = sentence[x].substr(0, sentence[x].size() - 2);
						/*tempString += "." ;*/
						/*tempString += "?";*/

						sentences.push_back(sentence[x]);
						break;
					}
				}
			}
	
	

	
	//prints a vector
	for (unsigned i = 0; i < sentences.size(); i++)
	{
		cout << sentence[i]  << endl;
	}

	cin.ignore();
		return 0;
}


vector<string> split(const string &s, char delim)
{
	vector<string> elems;	
	stringstream ss(s);
	string item;
	while (getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;

	
}
Last edited on
@JLBorges I actually thought i got the word phrase part right but i did not. is there any way i can make the compiler understand that a complete word input by the user?
for instance i input the following to search : actual, compiler, word input. where word input is a phrase not just a word. i tried using a delimiter as ', ' ( comma with a space) to seperate complete words but its only getting the last word of the input ( input). i want to know i how can getline a list of words into a vector and some of the words are phrases.
thanks
Newline separated:
1
2
3
4
5
6
7
8
9
10
11
12
std::vector<std::string> read_phrases()
{
    std::cout << "enter words or phrases, end each word or phrase with the enter key\n"
              << "enter an empty phrase (just the enter key) to end input\n" ;

    std::vector<std::string> result ;
    std::string phrase ;
    while( std::cout << "? " && std::getline( std::cin, phrase ) && !phrase.empty() )
        result.push_back(phrase) ;

    return result ;
}


Comma separated (phrase does not contain commas):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::vector<std::string> read_phrases()
{
    std::cout << "type in words or phrases, end each word or phrase with a comma\n"
              << "and enter when done\n" ;

    std::string line ;
    std::getline( std::cin, line ) ;

    std::vector<std::string> result ;

    std::string phrase ;
    char c ;
    std::istringstream stm(line) ;

    // stm >> c && stm.putback(c) to skip leading white-space
    while( stm >> c && stm.putback(c) && std::getline( stm, phrase, ',' ) )
        result.push_back(phrase) ;

    return result ;
}
@JLBorges thanks for your effort but as much as i want to learn but i think i will have to pass on this. Your code seem so hard for me to understand. i dont just wanna copy and paste peoples code. i want to understand it but this one is really a hard one for me. Thanks anyway.
@JLBorges Thanks again. i had it done in a much easier way and less code. thanks again
@JLBorges... Please How do i use an input file that has been opened in a friend class. i.e.

File A has been opened and tested if file exist in class inputfile. i need to use the content of the file in another class without having to re-open it again.
Hand it over ( move it to the function, not available in legacy C++)
Or lend it (pass by reference).

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
#include <iostream>
#include <fstream>
#include <utility>

void foo( std::ifstream&& stm )
{
    std::cout << stm.tellg() << '\n' ;
    // ...
}

void bar( const std::string& file_name )
{
    std::ifstream file(file_name) ;
    foo( std::move(file) ) ;
}

void foo_legacy( std::ifstream& stm ) // c++98
{
    std::cout << stm.tellg() << '\n' ;
    // ...
}

void bar_legacy( const char* file_name ) // c++98
{
    std::ifstream file(file_name) ;
    foo_legacy(file) ;
}

int main()
{
    bar(__FILE__) ;

    bar_legacy(__FILE__) ;
}

http://coliru.stacked-crooked.com/a/2327e86fd3e440a6
@JLBorges Please do you by any chance know which site or book that i could use to learn windows application using c++ ?
Windows System Programming (4th Edition) by Johnson M. Hart
http://www.amazon.com/Windows-Programming-Addison-Wesley-Microsoft-Technology/dp/0321657748/

Note: About operating system services (ie. not about programming the GUI)
Topic archived. No new replies allowed.
Pages: 12