/* Average Number Of Words - This program processes the contents of a text-
file. The text is stored as one sentence per line. It reads the file's
contents and calculates the average number of words per sentence. */
#include "Utility.h"
int readFile();
void erasePunct(string &);
void analyzeText(const string);
void displaySentence(string);
int main()
{
int fOpenErr = 0;
cout << "TEXT ANALYSIS\n";
fOpenErr = readFile();
if (fOpenErr != -1)
{
cout << "Thank you for using this program, have a nice day!\n";
}
pauseSystem();
return 0;
}
/* **********************************************************
Definition: readFile
This function attempts to open a file containing a text.
The user is asked to enter the name of this file. Upon
success, the file contents is read in and processed. If an
error occurs, the function exits with an error message.
********************************************************** */
int readFile()
{
string tmpString = "";
string fileName = "";
cout << "\nPlease enter a filename: ";
cin >> fileName;
cout << "\nTEXT ANALYSIS - AVERAGE NUMBER OF WORDS IN SENTENCE\n\n";
fstream getText(fileName, ios::in);
if (!getText.fail())
{
while (getline(getText, tmpString))
{
displaySentence(tmpString);
erasePunct(tmpString);
analyzeText(tmpString);
}
getText.clear();
getText.seekg(0L, ios::beg);
cout << "\nTEXT ANALYSIS - FULL TEXT ANALYSIS\n\n";
while (getline(getText, tmpString, '\0'))
{
displaySentence(tmpString);
erasePunct(tmpString);
analyzeText(tmpString);
}
}
else
{
cout << "\nThis file could not be opened or processed.\n"
<< "Aborting the program now ...";
return -1;
}
getText.close();
return 0;
}
/* **********************************************************
Definition: erasePunct
This function accepts a reference to a string object as
its parameter. It strips all punctuation and asterisk
characters from the sentences. It also replaces hyphens
with whitespace characters.
********************************************************** */
void erasePunct(string &sentence)
{
for (size_t index = 0; index < sentence.length(); index++)
{
if (sentence[index] == '-' && isalnum(sentence[index + 1]))
{
sentence[index] = ' ';
}
}
sentence.erase(remove(sentence.begin(), sentence.end(), '*'), sentence.end());
sentence.erase(remove_if(sentence.begin(), sentence.end(), ispunct), sentence.end());
}
/* **********************************************************
Definition: analyzeText
This function accepts a const string object as parameter.
It calculates the average sentence length, then displays
the following items:
* The number of characters in the sentence
* The number of words in the sentence
* The average sentence length in percent
********************************************************** */
void analyzeText(const string sentence)
{
bool isDelim = true;
double avgNumWords = 0.0;
size_t wordCount = 0;
size_t numChars = 0;
size_t wordLength = 0;
for (size_t index = 0; index < sentence.length(); ++index)
{
if (isspace(sentence[index]))
{
++numChars;
isDelim = true;
}
else if (isDelim)
{
wordLength = (sentence.length() - numChars);
++wordCount;
isDelim = false;
}
avgNumWords = (static_cast<double>(wordLength) / wordCount);
}
cout << showpoint << fixed << setprecision(2);
cout << "Characters: " << "\t" << "Word Count: " << "\t" << "Average Word Number:\n";
cout << wordLength << " \t\t";
cout << wordCount << " \t\t";
cout << "%" << avgNumWords << "\t\n\n";
}
/* **********************************************************
Definition: displaySentence
This function accepts a string object as its parameter. It
displays the sentences.
********************************************************** */
void displaySentence(string sentence)
{
for (size_t index = 0; index < sentence.length(); index++)
{
if (sentence[index] == '*')
{
sentence[index] = '\n';
}
}
cout << sentence << "\n\n";
}
No comments:
Post a Comment