counting syllables

Started by
3 comments, last by Krohm 10 years, 6 months ago

I want to count the number of syllables in the words in a text file. In my code I am getting the count of words contained in the text file but am getting one more than present.

#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
using namespace std;
int main()
{
double index=0;
int syllabus,words=0,sentences;
string filename,word;
cout << "Enter the data file name: ";
cin >> filename;
ifstream infile;
infile.open(filename.c_str());
if(infile.fail())
{
cout << "Error opening " << filename << endl;
system("pause");
return 1;
}
while(!infile.eof())
{
cout << word << endl;
infile >> word;
words++;
}
cout << words << endl;
infile.close();
system("pause");
return 0;
}

I think my problem is at the !infile.eof line of code.

Advertisement

How do you define a syllable via programming? How does a program know what a syllable is? It's virtually impossible. For example, the word "deliberate" has 2 pronunciations with different syllables and 2 meanings:

DE-lib-ERIT (3 syllables)

DE-lib-ER-ate - (4 syllables)

You should abandon this path of finding syllables, it's not really possible for you and has no real benefit. Stick to words (defined as separated by spaces).

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

I am working on a text book problem that involves the Flesch Readability Index.

while(!infile.eof())
{
cout << word << endl;
infile >> word;
words++;
}

Why do you output the previous word instead of outputting the word that came from the file?

I am not very familiar with the C++ stream classes, but it looks like, when you are at the end of the file, it needs to try to read a word before finally setting the "eof" flag to true. So, you have to account for that one failure at the end. You could try to make sure the contents of "word" look like a real word before increasing your count. The fanciness of your filter is up to you. I'll start with size.

while(!infile.eof())
{
    std::string word;
    infile >> word;
    cout << word << endl;
    if (word.size() > 0)
        words++;
}

It's worth noticing browsers have given up this problem. They do only word-wrap by default, unless they find the soft-hypen (U+00AD, also "shy") character and only when appropriate CSS property is set. I expect you to go uphill.

Previously "Krohm"

This topic is closed to new replies.

Advertisement