How to count sentences from a text file?

Hello, everyone. I am working on a C++ program that would count the number of sentences from a text file. I know you have to use #include <fstream> and open the file by doing the following
1
2
3
4
5
6
7
8
#include <iostream>
#include <fstream>
using namespace std;

int main () {
int total, m;  
ifstream myfile ("example.txt");
}


In this case, the end of a senescence means that you have . (period), ? (Question mark), and ! (exclamation point).

Can you please help me how to incorporate that and implement it into C++?

Thanks
Last edited on
You're on the right track with the checking for a period and so on.

You should read in some amount of text from the file and then check it, one character at a time, for sentence ending punctuation. Every character before that is part of a sentence.

Hope that helps.

use cin.get, that gets a character at a time and you can check for delimiters that way.
Last edited on
I did some research on the internet and I am not quiet getting this. Can you perhaps give me examples?
Which part are you having trouble with?

http://www.cplusplus.com/reference/string/string/getline/
should help with getting the text from the file.

http://www.cplusplus.com/reference/string/string/?kw=string
to see how to acess one character at a time.
char ch;
infile.open(fileName, ios::in);
ch = inFile.get();
while (ch != EOF)
{
cout << ch;
ch = inFile.get();
}

this will read in all the characters until you get to the end of file. you can write if statements if you want it to do something when it encounters a certain character.
I think it's how to access one character at a time. It would check if each character is "." (period), "?" (question), and "!" (exclamation point). If it is one of the followings, it would add counter until the end of the text (there are no more inputs in the text file).
Ok, so in this case, you would add the "if" statement in the while loop?

Would it look like this?

char ch;
int counter;
int total;
infile.open(fileName, ios::in);
ch = inFile.get();
while (ch != EOF)
{
if (ch == "." || ch== "!" || ch"?")
{
total = counter + 1;
}
ch = inFile.get();

cout << "The total number of sentences " << total;

}
Last edited on
If you use getline you get one string at a time. Loop through that string checking one character at a time. If the character is sentence ending punctuation add one to the sentence count.
Can you perhaps give me an example please? Not exactly sure what you are trying to say?
Here's some pseudo code to hopefully help get you started.

1
2
3
4
5
6
7
8
9
string currentString;
int sentCount=0;
while(/*able to read into current string*/)
{
     for(int i=0;i<currentString.size();i++)
     {
          //if character i in currentString is a period add one to sentCount
     }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream infile;
infile.open("example.txt");
string currentString;
int counter = 0;

while (!infile.eof)
{
     infile >> currentString;
     if (currentString == '?' || currentString == '!' currentString == '!')
         counter ++

}



Will this work?
closed account (j3Rz8vqX)
You should probably stick with what you had before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char ch;
//int counter;//Commented this line
int total=0;//Adjusted this line; assigned to 0;
infile.open(fileName, ios::in);
ch = inFile.get();
while (ch != EOF)
{
    if (ch == '.' || ch== '!' || ch=='?')//Fixed this line
    {
        total = total + 1;//Modified this line to correctly use accumulator
    }
    ch = inFile.get();
    cout << "The total number of sentences " << total;
}
Last edited on
If I did Dput's way, then my complier says that at line 8, ch must be a modifiable value
closed account (j3Rz8vqX)
I had a lot of syntax error in my earlier post because it was done on the web browser...

The below would be more ideal:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    char ch;
    //int counter;//Commented this line
    ifstream infile;
    int total=0;//Adjusted this line; assigned to 0;
    infile.open("example.txt", ios::in);
    ch = infile.get();
    while (ch != EOF)
    {
        if (ch == '.' || ch== '!' || ch=='?')//Fixed this line
        {
            total = total + 1;//Modified this line to correctly use accumulator
        }
        ch = infile.get();
        cout << "The total number of sentences " << total;
    }
	return 0;
}


This compiles with no error for me; though it has not been tested.
Looks good enough though >.<
Last edited on
What is the advantage of reading in one character at a time vs a string at a time and then looping through that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	ifstream infile("test.txt",ios::in);
	string current;
	int count=0;
	while(getline(infile, current))
	{
		for(int i=0;i<current.size();i++)
			if(current[i]=='.' || current[i]=='?' || current[i]=='!')
				count++;
	}
	cout<<"Total Sentences: "<<count<<"\n";

  return 0;
}
Topic archived. No new replies allowed.