Almost done with printing, need to fix most & least common words
This commit is contained in:
parent
b2e3fbc645
commit
3246f345b9
5 changed files with 369 additions and 253 deletions
|
@ -5,52 +5,59 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
InputProcessor::InputProcessor() {
|
InputProcessor::InputProcessor() {
|
||||||
_fileIn = std::ifstream();
|
_fileIn = std::ifstream();
|
||||||
_allWords = std::vector<std::string>();
|
_allWords = std::vector<std::string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InputProcessor::openStream() {
|
bool InputProcessor::openStream() {
|
||||||
std::string file;
|
std::string file;
|
||||||
std::cout << "What is the name of the file you would like to read? ";
|
std::cout << "What is the name of the file you would like to read? ";
|
||||||
std::cin >> file;
|
std::cin >> file;
|
||||||
|
|
||||||
if (std::cin.fail()) {
|
if (std::cin.fail()) {
|
||||||
std::cout << "Invalid file input";
|
std::cerr << "Invalid file input" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_fileIn.open(file);
|
_fileIn.open(file);
|
||||||
if (_fileIn.fail()) {
|
if (_fileIn.fail()) {
|
||||||
std::cout << "Unable to open file, does it exist?" << std::endl;
|
std::cerr << "Unable to open file, does it exist?" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputProcessor::closeStream() { _fileIn.close(); }
|
void InputProcessor::closeStream() { _fileIn.close(); }
|
||||||
|
|
||||||
void InputProcessor::read() {
|
void InputProcessor::read() {
|
||||||
std::string characterBuffer = "";
|
std::string characterBuffer = "";
|
||||||
char currentChar;
|
char currentChar;
|
||||||
while (_fileIn.get(currentChar)) {
|
while (_fileIn.get(currentChar)) {
|
||||||
switch (currentChar) {
|
switch (currentChar) {
|
||||||
case ' ':
|
case ' ':
|
||||||
case '\n':
|
case '\n':
|
||||||
_allWords.push_back(characterBuffer);
|
case '\r':
|
||||||
characterBuffer.clear();
|
if (!characterBuffer.empty()) {
|
||||||
break;
|
_allWords.push_back(characterBuffer);
|
||||||
default:
|
characterBuffer.clear();
|
||||||
characterBuffer += currentChar;
|
}
|
||||||
break;
|
break;
|
||||||
}
|
default:
|
||||||
}
|
// Normalize to uppercase
|
||||||
|
if (currentChar >= 'a' && currentChar <= 'z') {
|
||||||
|
currentChar -= 32;
|
||||||
|
}
|
||||||
|
characterBuffer += currentChar;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Flush the rest of the buffer if the file doesn't end with a space or
|
// Flush the rest of the buffer if the file doesn't end with a space or
|
||||||
// newline
|
// newline
|
||||||
if (!characterBuffer.empty()) {
|
if (!characterBuffer.empty()) {
|
||||||
_allWords.push_back(characterBuffer);
|
_allWords.push_back(characterBuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> InputProcessor::getAllWords() { return _allWords; }
|
std::vector<std::string> InputProcessor::getAllWords() { return _allWords; }
|
|
@ -6,49 +6,49 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class InputProcessor {
|
class InputProcessor {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Constructs a new InputProcessor, initializing internal fields to
|
* @brief Constructs a new InputProcessor, initializing internal fields to
|
||||||
* defaults
|
* defaults
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
InputProcessor();
|
InputProcessor();
|
||||||
/**
|
/**
|
||||||
* @brief Prompts the user for the file to open, and opens it as an ifstream
|
* @brief Prompts the user for the file to open, and opens it as an ifstream
|
||||||
*
|
*
|
||||||
* @return true The stream was opened successfully
|
* @return true The stream was opened successfully
|
||||||
* @return false The stream was unable to be opened successfully
|
* @return false The stream was unable to be opened successfully
|
||||||
*/
|
*/
|
||||||
bool openStream();
|
bool openStream();
|
||||||
/**
|
/**
|
||||||
* @brief Closes the open file stream
|
* @brief Closes the open file stream
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void closeStream();
|
void closeStream();
|
||||||
/**
|
/**
|
||||||
* @brief Reads all words from the currently open stream, and stores them
|
* @brief Reads all words from the currently open stream, and stores them
|
||||||
* internally in a vector of all words
|
* internally in a vector of all words
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void read();
|
void read();
|
||||||
/**
|
/**
|
||||||
* @brief Returns all the words parsed by this InputProcessor
|
* @brief Returns all the words parsed by this InputProcessor
|
||||||
*
|
*
|
||||||
* @return std::vector<std::string> The vector containing all words
|
* @return std::vector<std::string> The vector containing all words
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> getAllWords();
|
std::vector<std::string> getAllWords();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* @brief The raw file input stream to read from
|
* @brief The raw file input stream to read from
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::ifstream _fileIn;
|
std::ifstream _fileIn;
|
||||||
/**
|
/**
|
||||||
* @brief The vector containing all parsed words from the input stream
|
* @brief The vector containing all parsed words from the input stream
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> _allWords;
|
std::vector<std::string> _allWords;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INPUTPROCESSOR_H
|
#endif // INPUTPROCESSOR_H
|
||||||
|
|
|
@ -1,92 +1,200 @@
|
||||||
#include "OutputProcessor.h"
|
#include "OutputProcessor.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <ostream>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
OutputProcessor::OutputProcessor() {
|
OutputProcessor::OutputProcessor() {
|
||||||
_fileOut = std::ofstream();
|
_fileOut = std::ofstream();
|
||||||
_allWords = std::vector<std::string>();
|
_allWords = std::vector<std::string>();
|
||||||
_uniqueWords = std::vector<std::string>();
|
_uniqueWords = std::vector<std::string>();
|
||||||
_letterCounts = std::vector<unsigned int>(26, 0);
|
_letterCounts = std::vector<unsigned int>(26, 0);
|
||||||
_wordCounts = std::vector<unsigned int>();
|
_wordCounts = std::vector<unsigned int>();
|
||||||
_totalLetterCount = 0;
|
_totalLetterCount = 0;
|
||||||
_totalWordCount = 0;
|
_totalWordCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputProcessor::analyzeWords(std::vector<std::string> allWords,
|
void OutputProcessor::analyzeWords(std::vector<std::string> allWords,
|
||||||
std::string punctuation) {
|
std::string punctuation) {
|
||||||
// Iterate over all words, processing incrementally
|
// Iterate over all words, processing incrementally
|
||||||
for (size_t wordIdx = 0; wordIdx < allWords.size(); wordIdx++) {
|
for (size_t wordIdx = 0; wordIdx < allWords.size(); wordIdx++) {
|
||||||
std::string& word = allWords.at(wordIdx);
|
std::string &word = allWords.at(wordIdx);
|
||||||
|
|
||||||
// Remove punctuation from word
|
// Remove punctuation from word
|
||||||
size_t punctuationIdx = 0;
|
size_t punctuationIdx = 0;
|
||||||
while ((punctuationIdx = word.find_first_of(punctuation)) !=
|
while ((punctuationIdx = word.find_first_of(punctuation)) !=
|
||||||
std::string::npos) {
|
std::string::npos) {
|
||||||
word.erase(punctuationIdx, 1);
|
word.erase(punctuationIdx, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save word internally
|
// Save word internally
|
||||||
_allWords.push_back(word);
|
_allWords.push_back(word);
|
||||||
|
|
||||||
// Check all unique words for a match, and if so increment the count
|
// Check all unique words for a match, and if so increment the count
|
||||||
bool foundUnique = false;
|
bool foundUnique = false;
|
||||||
for (size_t uniqueWordIdx = 0; uniqueWordIdx < _uniqueWords.size();
|
size_t uniqueWordIdx;
|
||||||
uniqueWordIdx++) {
|
for (uniqueWordIdx = 0; uniqueWordIdx < _uniqueWords.size();
|
||||||
if (_uniqueWords.at(uniqueWordIdx) == word) {
|
uniqueWordIdx++) {
|
||||||
_wordCounts.at(uniqueWordIdx)++;
|
if (_uniqueWords.at(uniqueWordIdx) == word) {
|
||||||
foundUnique = true;
|
foundUnique = true;
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
// If no unique word exists, add it to both vectors
|
}
|
||||||
if (!foundUnique) {
|
// If no unique word exists, add it to both vectors
|
||||||
_uniqueWords.push_back(word);
|
if (!foundUnique) {
|
||||||
_wordCounts.push_back(1);
|
_uniqueWords.push_back(word);
|
||||||
}
|
_wordCounts.push_back(1);
|
||||||
|
} else {
|
||||||
|
_wordCounts.at(uniqueWordIdx)++;
|
||||||
|
}
|
||||||
|
|
||||||
// Add letter count for each letter in the word
|
// Add letter count for each letter in the word
|
||||||
for (size_t letterIdx = 0; letterIdx < word.length(); letterIdx++) {
|
for (size_t letterIdx = 0; letterIdx < word.length(); letterIdx++) {
|
||||||
char letter = word.at(letterIdx);
|
char letter = word.at(letterIdx);
|
||||||
// Normalize to uppercase
|
// Normalize to uppercase
|
||||||
if (letter >= 'a' && letter <= 'z') {
|
if (letter >= 'a' && letter <= 'z') {
|
||||||
letter -= 32;
|
letter -= 32;
|
||||||
}
|
}
|
||||||
// Subtracting an uppercase letter by 65 creates its alphabetical
|
// Subtracting an uppercase letter by 65 creates its alphabetical
|
||||||
// index
|
// index
|
||||||
letter -= 65;
|
letter -= 65;
|
||||||
_letterCounts.at(letter)++;
|
_letterCounts.at(letter)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sum total letter count
|
// Sum total letter count
|
||||||
_totalLetterCount += word.length();
|
_totalLetterCount += word.length();
|
||||||
|
|
||||||
// Increment total word count
|
// Increment total word count
|
||||||
_totalWordCount++;
|
_totalWordCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OutputProcessor::openStream() {
|
bool OutputProcessor::openStream() {
|
||||||
std::string file;
|
std::string file;
|
||||||
std::cout << "What is the name of the file you would like to write to? ";
|
std::cout << "What is the name of the file you would like to write to? ";
|
||||||
std::cin >> file;
|
std::cin >> file;
|
||||||
|
|
||||||
if (std::cin.fail()) {
|
if (std::cin.fail()) {
|
||||||
std::cout << "Invalid file input";
|
std::cerr << "Invalid file input" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_fileOut.open(file);
|
_fileOut.open(file);
|
||||||
if (_fileOut.fail()) {
|
if (_fileOut.fail()) {
|
||||||
std::cout << "Unable to open file, does it exist?" << std::endl;
|
std::cerr << "Unable to open file, does it exist?" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputProcessor::closeStream() { _fileOut.close(); }
|
void OutputProcessor::closeStream() { _fileOut.close(); }
|
||||||
|
|
||||||
void OutputProcessor::write() {
|
void OutputProcessor::write() {
|
||||||
// TODO
|
// Calculate longest word length, longest number length, most common word,
|
||||||
|
// and least common word for later use in one pass for efficiency
|
||||||
|
size_t longestWordLength = 0;
|
||||||
|
|
||||||
|
std::string *mostCommonWord = &_uniqueWords.at(0);
|
||||||
|
unsigned long mostCommonWordOccurrences = _wordCounts.at(0);
|
||||||
|
|
||||||
|
std::string *leastCommonWord = &_uniqueWords.at(0);
|
||||||
|
unsigned long leastCommonWordOccurrences = _wordCounts.at(0);
|
||||||
|
|
||||||
|
for (size_t uniqueWordIdx = 0; uniqueWordIdx < _uniqueWords.size();
|
||||||
|
uniqueWordIdx++) {
|
||||||
|
std::string &uniqueWord = _uniqueWords.at(uniqueWordIdx);
|
||||||
|
unsigned long wordCount = _wordCounts.at(uniqueWordIdx);
|
||||||
|
|
||||||
|
if (uniqueWord.length() > longestWordLength) {
|
||||||
|
longestWordLength = uniqueWord.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equality can be ignored here because we want the word that was
|
||||||
|
// encountered first, so any subsequent extremes can be ignored
|
||||||
|
if (wordCount < leastCommonWordOccurrences) {
|
||||||
|
leastCommonWordOccurrences = wordCount;
|
||||||
|
leastCommonWord = &uniqueWord;
|
||||||
|
} else {
|
||||||
|
if (wordCount > mostCommonWordOccurrences) {
|
||||||
|
mostCommonWordOccurrences = wordCount;
|
||||||
|
mostCommonWord = &uniqueWord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size_t longestWordLengthDigits = std::to_string(longestWordLength).length();
|
||||||
|
|
||||||
|
_fileOut << "Read in " << _totalWordCount << " words" << std::endl;
|
||||||
|
_fileOut << "Encountered " << _uniqueWords.size() << " unique words"
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
// Print out each unique word and how often it happened
|
||||||
|
for (size_t uniqueWordIdx = 0; uniqueWordIdx < _uniqueWords.size();
|
||||||
|
uniqueWordIdx++) {
|
||||||
|
_fileOut << std::setw(longestWordLength) << std::left
|
||||||
|
<< _uniqueWords.at(uniqueWordIdx) << std::right << " : "
|
||||||
|
<< std::setw(longestWordLengthDigits + 1)
|
||||||
|
<< _wordCounts.at(uniqueWordIdx) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print the most and least common word
|
||||||
|
size_t longerFrequentWordLength =
|
||||||
|
mostCommonWord->length() > leastCommonWord->length()
|
||||||
|
? mostCommonWord->length()
|
||||||
|
: leastCommonWord->length();
|
||||||
|
size_t mostCommonWordOccurrencesDigits =
|
||||||
|
std::to_string(mostCommonWordOccurrences).length();
|
||||||
|
|
||||||
|
_fileOut << " Most Frequent Word: " << std::setw(longerFrequentWordLength)
|
||||||
|
<< std::left << *mostCommonWord << " "
|
||||||
|
<< std::setw(mostCommonWordOccurrencesDigits) << std::right
|
||||||
|
<< mostCommonWordOccurrences << std::endl;
|
||||||
|
|
||||||
|
// Calculate the most and least common letters to display, along with their
|
||||||
|
// occurrences for formatting purposes
|
||||||
|
char mostCommonLetter = 'A';
|
||||||
|
unsigned long mostCommonLetterOccurrences = _letterCounts.at(0);
|
||||||
|
char leastCommonLetter = 'A';
|
||||||
|
unsigned long leastCommonLetterOccurrences = _letterCounts.at(0);
|
||||||
|
|
||||||
|
for (size_t letterIdx = 0; letterIdx < 26; letterIdx++) {
|
||||||
|
// Here not using "or equals" means the letters later alphabetically get
|
||||||
|
// ignored if they occur the same amount
|
||||||
|
if (_letterCounts.at(letterIdx) <
|
||||||
|
_letterCounts.at(leastCommonLetter - 65)) {
|
||||||
|
leastCommonLetter = letterIdx + 65;
|
||||||
|
leastCommonLetterOccurrences = _letterCounts.at(letterIdx);
|
||||||
|
} else {
|
||||||
|
if (_letterCounts.at(letterIdx) >
|
||||||
|
_letterCounts.at(mostCommonLetter - 65)) {
|
||||||
|
mostCommonLetter = letterIdx + 65;
|
||||||
|
mostCommonLetterOccurrences = _letterCounts.at(letterIdx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print out each letter along with the amount of times it occurs
|
||||||
|
size_t mostCommonLetterOccurrencesDigits =
|
||||||
|
std::to_string(mostCommonLetterOccurrences).length();
|
||||||
|
for (size_t letterIdx = 0; letterIdx < 26; letterIdx++) {
|
||||||
|
_fileOut << (char)(letterIdx + 65) << ": "
|
||||||
|
<< std::setw(mostCommonLetterOccurrencesDigits) << std::right
|
||||||
|
<< _letterCounts.at(letterIdx) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print out the most and least common letters in total
|
||||||
|
_fileOut << " Most Frequent Letter: " << mostCommonLetter << " "
|
||||||
|
<< mostCommonLetterOccurrences << " (" << std::setw(7)
|
||||||
|
<< std::fixed << std::setprecision(3)
|
||||||
|
<< ((float)mostCommonLetterOccurrences / _totalLetterCount * 100)
|
||||||
|
<< "%)" << std::endl;
|
||||||
|
_fileOut << "Least Frequent Letter: " << leastCommonLetter << " "
|
||||||
|
<< std::setw(mostCommonLetterOccurrencesDigits) << std::right
|
||||||
|
<< leastCommonLetterOccurrences << " (" << std::setw(7)
|
||||||
|
<< std::fixed << std::setprecision(3)
|
||||||
|
<< ((float)leastCommonLetterOccurrences / _totalLetterCount * 100)
|
||||||
|
<< "%)" << std::endl;
|
||||||
}
|
}
|
|
@ -6,84 +6,85 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class OutputProcessor {
|
class OutputProcessor {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Constructs a new OutputProcessor, setting internal fields to their
|
* @brief Constructs a new OutputProcessor, setting internal fields to their
|
||||||
* initial state
|
* initial state
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
OutputProcessor();
|
OutputProcessor();
|
||||||
/**
|
/**
|
||||||
* @brief Removes punctuation from the list of allWords, stores this
|
* @brief Removes punctuation from the list of allWords, stores this
|
||||||
* internally, and then computes the list of all unique words in the
|
* internally, and then computes the list of all unique words in the
|
||||||
* original vector. In addition, it will compute the amount of occurrences
|
* original vector. In addition, it will compute the amount of occurrences
|
||||||
* of all words in the text, and the amounts of letters in each word in the
|
* of all words in the text, and the amounts of letters in each word in the
|
||||||
* text.
|
* text.
|
||||||
*
|
*
|
||||||
* @param allWords The vector containing all read words from the text
|
* @param allWords The vector containing all read words from the text
|
||||||
* @param punctuation A string containing punctuation to remove from the
|
* @param punctuation A string containing punctuation to remove from the
|
||||||
* original vector of words
|
* original vector of words
|
||||||
*/
|
*/
|
||||||
void analyzeWords(std::vector<std::string> allWords, std::string punctuation);
|
void analyzeWords(std::vector<std::string> allWords,
|
||||||
/**
|
std::string punctuation);
|
||||||
* @brief Prompts the user for the filename of the file they wish to open
|
/**
|
||||||
* for outputting to, and then opens an output stream to that file
|
* @brief Prompts the user for the filename of the file they wish to open
|
||||||
*
|
* for outputting to, and then opens an output stream to that file
|
||||||
* @return true The stream was opened successfully
|
*
|
||||||
* @return false The stream was unable to be opened successfully
|
* @return true The stream was opened successfully
|
||||||
*/
|
* @return false The stream was unable to be opened successfully
|
||||||
bool openStream();
|
*/
|
||||||
/**
|
bool openStream();
|
||||||
* @brief Closes the open output stream
|
/**
|
||||||
*
|
* @brief Closes the open output stream
|
||||||
*/
|
*
|
||||||
void closeStream();
|
*/
|
||||||
/**
|
void closeStream();
|
||||||
* @brief Nicely prints the computed data to the output stream as specified
|
/**
|
||||||
*
|
* @brief Nicely prints the computed data to the output stream as specified
|
||||||
*/
|
*
|
||||||
void write();
|
*/
|
||||||
|
void write();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* @brief The output stream to write to
|
* @brief The output stream to write to
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::ofstream _fileOut;
|
std::ofstream _fileOut;
|
||||||
/**
|
/**
|
||||||
* @brief The list of all words with punctuation removed
|
* @brief The list of all words with punctuation removed
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> _allWords;
|
std::vector<std::string> _allWords;
|
||||||
/**
|
/**
|
||||||
* @brief The list of all unique words, parsed from the full set
|
* @brief The list of all unique words, parsed from the full set
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> _uniqueWords;
|
std::vector<std::string> _uniqueWords;
|
||||||
/**
|
/**
|
||||||
* @brief A vector containing information on how often each letter occurs in
|
* @brief A vector containing information on how often each letter occurs in
|
||||||
* the text. The index corresponds to the alphabetical value minus one (A is
|
* the text. The index corresponds to the alphabetical value minus one (A is
|
||||||
* 0, B is 1, C is 2, etc)
|
* 0, B is 1, C is 2, etc)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned int> _letterCounts;
|
std::vector<unsigned int> _letterCounts;
|
||||||
/**
|
/**
|
||||||
* @brief A vector containing information on how common each unique words is
|
* @brief A vector containing information on how common each unique words is
|
||||||
* in the list of all words. The index for each word in _uniqueWords is the
|
* in the list of all words. The index for each word in _uniqueWords is the
|
||||||
* same as the index for the same word in this vector.
|
* same as the index for the same word in this vector.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned int> _wordCounts;
|
std::vector<unsigned int> _wordCounts;
|
||||||
/**
|
/**
|
||||||
* @brief The total amount of letters in the text
|
* @brief The total amount of letters in the text
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
unsigned int _totalLetterCount;
|
unsigned int _totalLetterCount;
|
||||||
/**
|
/**
|
||||||
* @brief The total amount of words in the text
|
* @brief The total amount of words in the text
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
unsigned int _totalWordCount;
|
unsigned int _totalWordCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // OUTPUTPROCESSOR_H
|
#endif // OUTPUTPROCESSOR_H
|
||||||
|
|
72
main.cpp
72
main.cpp
|
@ -1,47 +1,47 @@
|
||||||
#include "InputProcessor.h" // our custom InputProcessor class
|
#include "InputProcessor.h" // our custom InputProcessor class
|
||||||
#include "OutputProcessor.h" // our custom OutputProcessor class
|
#include "OutputProcessor.h" // our custom OutputProcessor class
|
||||||
|
|
||||||
#include <iostream> // for cout, endl
|
#include <iostream> // for cout, endl
|
||||||
#include <string> // for string
|
#include <string> // for string
|
||||||
#include <vector> // for vector
|
#include <vector> // for vector
|
||||||
using namespace std; // so we don't have to type std:: every time
|
using namespace std; // so we don't have to type std:: every time
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// create an input processor object
|
// create an input processor object
|
||||||
InputProcessor iProcessor;
|
InputProcessor iProcessor;
|
||||||
|
|
||||||
// open a stream to input from
|
// open a stream to input from
|
||||||
if (!iProcessor.openStream()) {
|
if (!iProcessor.openStream()) {
|
||||||
// if stream failed to open, quit the program
|
// if stream failed to open, quit the program
|
||||||
cerr << "Shutting down..." << endl;
|
cerr << "Shutting down..." << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
// read the data on the stream
|
// read the data on the stream
|
||||||
iProcessor.read();
|
iProcessor.read();
|
||||||
// close the input stream
|
// close the input stream
|
||||||
iProcessor.closeStream();
|
iProcessor.closeStream();
|
||||||
|
|
||||||
// retrieve all the words read from the stream
|
// retrieve all the words read from the stream
|
||||||
std::vector<std::string> inputWords = iProcessor.getAllWords();
|
std::vector<std::string> inputWords = iProcessor.getAllWords();
|
||||||
|
|
||||||
// create an output processor object
|
// create an output processor object
|
||||||
OutputProcessor oProcessor;
|
OutputProcessor oProcessor;
|
||||||
// analyze the words and ignore the specified punctuation
|
// analyze the words and ignore the specified punctuation
|
||||||
oProcessor.analyzeWords(inputWords, "?!.,;:\"()_-'&[]");
|
oProcessor.analyzeWords(inputWords, "?!.,;:\"()_-'&[]");
|
||||||
// open a stream to output to
|
// open a stream to output to
|
||||||
if (!oProcessor.openStream()) {
|
if (!oProcessor.openStream()) {
|
||||||
// if stream failed to open, quit the program
|
// if stream failed to open, quit the program
|
||||||
cerr << "Shutting down..." << endl;
|
cerr << "Shutting down..." << endl;
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
// write the data to the stream
|
// write the data to the stream
|
||||||
oProcessor.write();
|
oProcessor.write();
|
||||||
// close the output stream
|
// close the output stream
|
||||||
oProcessor.closeStream();
|
oProcessor.closeStream();
|
||||||
|
|
||||||
// signal to user program has completed
|
// signal to user program has completed
|
||||||
cout << "Analysis complete, check file for results" << endl;
|
cout << "Analysis complete, check file for results" << endl;
|
||||||
|
|
||||||
// end our program!
|
// end our program!
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue