A3/OutputProcessor.cpp
2024-10-09 17:20:26 -06:00

214 lines
No EOL
7 KiB
C++

#include "OutputProcessor.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <cstdint>
OutputProcessor::OutputProcessor() {
_fileOut = std::ofstream();
_allWords = std::vector<std::string>();
_uniqueWords = std::vector<std::string>();
_letterCounts = std::vector<unsigned int>(26, 0);
_wordCounts = std::vector<unsigned int>();
_totalLetterCount = 0;
_totalWordCount = 0;
}
void OutputProcessor::analyzeWords(std::vector<std::string> allWords,
const std::string PUNCTUATION) {
// Iterate over all words, processing incrementally
for (size_t wordIdx = 0; wordIdx < allWords.size(); wordIdx++) {
std::string &word = allWords.at(wordIdx);
// Remove punctuation from word
size_t punctuationIdx = 0;
while ((punctuationIdx = word.find_first_of(PUNCTUATION)) !=
std::string::npos) {
word.erase(punctuationIdx, 1);
}
// Save word internally
_allWords.push_back(word);
// Check all unique words for a match, and if so increment the count
bool foundUnique = false;
size_t uniqueWordIdx;
for (uniqueWordIdx = 0; uniqueWordIdx < _uniqueWords.size();
uniqueWordIdx++) {
if (_uniqueWords.at(uniqueWordIdx) == word) {
foundUnique = true;
break;
}
}
// If no unique word exists, add it to both vectors
if (!foundUnique) {
_uniqueWords.push_back(word);
_wordCounts.push_back(1);
} else {
_wordCounts.at(uniqueWordIdx)++;
}
// Add letter count for each letter in the word
for (size_t letterIdx = 0; letterIdx < word.length(); letterIdx++) {
char letter = word.at(letterIdx);
// Normalize to uppercase
if (letter >= 'a' && letter <= 'z') {
letter -= 97;
} else {
if (letter >= 'A' && letter <= 'Z') {
letter -= 65;
} else {
continue;
}
}
// Subtracting an uppercase letter by 65 creates its alphabetical
// index
_letterCounts.at(letter)++;
}
// Sum total letter count
_totalLetterCount += word.length();
// Increment total word count
_totalWordCount++;
}
}
bool OutputProcessor::openStream() {
std::string file;
std::cout << "What is the name of the file you would like to write to? ";
std::cin >> file;
if (std::cin.fail()) {
std::cerr << "Invalid file input" << std::endl;
return false;
}
_fileOut.open(file);
if (_fileOut.fail()) {
std::cerr << "Unable to open file, does it exist?" << std::endl;
return false;
}
return true;
}
void OutputProcessor::closeStream() { _fileOut.close(); }
void OutputProcessor::write() {
// 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;
size_t mostCommonWordIdx = 0;
size_t leastCommonWordIdx = 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 < _wordCounts.at(leastCommonWordIdx)) {
leastCommonWordIdx = uniqueWordIdx;
} else {
if (wordCount > _wordCounts.at(mostCommonWordIdx)) {
mostCommonWordIdx = uniqueWordIdx;
}
}
}
_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
const size_t MOST_COMMON_WORD_COUNT_LENGTH =
std::to_string(_wordCounts.at(mostCommonWordIdx)).length();
for (size_t uniqueWordIdx = 0; uniqueWordIdx < _uniqueWords.size();
uniqueWordIdx++) {
_fileOut << std::setw(longestWordLength) << std::left
<< _uniqueWords.at(uniqueWordIdx) << " : "
<< std::setw(MOST_COMMON_WORD_COUNT_LENGTH) << std::right
<< _wordCounts.at(uniqueWordIdx) << std::endl;
}
// Print the most and least common word
const std::string &MOST_COMMON_WORD = _uniqueWords.at(mostCommonWordIdx);
const std::string &LEAST_COMMON_WORD = _uniqueWords.at(leastCommonWordIdx);
size_t longerFrequentWordLength =
MOST_COMMON_WORD.length() > LEAST_COMMON_WORD.length()
? MOST_COMMON_WORD.length()
: LEAST_COMMON_WORD.length();
size_t mostFrequentWordCountLength =
std::to_string(_wordCounts.at(mostCommonWordIdx)).length();
_fileOut << " Most Frequent Word: " << std::setw(longerFrequentWordLength)
<< std::left << MOST_COMMON_WORD << " " << std::right
<< std::setw(mostFrequentWordCountLength)
<< _wordCounts.at(mostCommonWordIdx) << " (" << std::setw(7)
<< std::fixed << std::setprecision(3) << std::right
<< (float)_wordCounts.at(mostCommonWordIdx) / _totalWordCount * 100
<< "%)" << std::endl;
_fileOut << "Least Frequent Word: " << std::setw(longerFrequentWordLength)
<< std::left << LEAST_COMMON_WORD << " " << std::right
<< std::setw(mostFrequentWordCountLength)
<< _wordCounts.at(leastCommonWordIdx) << " (" << std::setw(7)
<< std::fixed << std::setprecision(3) << std::right
<< (float)_wordCounts.at(leastCommonWordIdx) / _totalWordCount *
100
<< "%)" << std::endl;
// Calculate the most and least common letters to display
uint8_t mostCommonLetterIdx = 0;
uint8_t leastCommonLetterIdx = 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(leastCommonLetterIdx)) {
leastCommonLetterIdx = letterIdx;
} else {
if (_letterCounts.at(letterIdx) >
_letterCounts.at(mostCommonLetterIdx)) {
mostCommonLetterIdx = letterIdx;
}
}
}
// Print out each letter along with the amount of times it occurs
const size_t MOST_COMMON_LETTER_COUNT_LENGTH =
std::to_string(_letterCounts.at(mostCommonLetterIdx)).length();
for (size_t letterIdx = 0; letterIdx < 26; letterIdx++) {
_fileOut << (char)(letterIdx + 65) << ": "
<< std::setw(MOST_COMMON_LETTER_COUNT_LENGTH) << std::right
<< _letterCounts.at(letterIdx) << std::endl;
}
// Print out the most and least common letters in total
_fileOut << " Most Frequent Letter: " << (char)(mostCommonLetterIdx + 65)
<< " " << std::setw(MOST_COMMON_LETTER_COUNT_LENGTH) << std::right
<< _letterCounts.at(mostCommonLetterIdx) << " (" << std::setw(7)
<< std::fixed << std::setprecision(3)
<< ((float)_letterCounts.at(mostCommonLetterIdx) /
_totalLetterCount * 100)
<< "%)" << std::endl;
_fileOut << "Least Frequent Letter: " << (char)(leastCommonLetterIdx + 65)
<< " " << std::setw(MOST_COMMON_LETTER_COUNT_LENGTH) << std::right
<< _letterCounts.at(leastCommonLetterIdx) << " (" << std::setw(7)
<< std::fixed << std::setprecision(3)
<< ((float)_letterCounts.at(leastCommonLetterIdx) /
_totalLetterCount * 100)
<< "%)" << std::endl;
}