testsAndMisc-archive/CPP/miscelanious/markovChainGenerator/main.cpp

138 lines
3.6 KiB
C++
Raw Normal View History

#ifndef MAIN_CPP
#define MAIN_CPP
2026-02-20 00:37:32 +01:00
#include "basic.cpp"
#include <iostream>
2026-02-20 00:37:32 +01:00
#include <string>
#include <vector>
2026-02-20 00:37:32 +01:00
struct wordOccurences {
std::string word;
int occurences;
};
2026-02-20 00:37:32 +01:00
struct previousWords {
std::string word;
std::vector<wordOccurences> previousWords;
};
struct wordProbability {
2026-02-20 00:37:32 +01:00
std::string previousWord;
std::string nextWord;
float probability;
};
bool validInput(const std::string userInput) {
2026-02-20 00:37:32 +01:00
if (stringContainsNumbers(userInput))
return 0;
return 1;
}
2026-02-20 00:37:32 +01:00
std::vector<std::string> divideIntoWords(const std::string userInput) {
std::vector<std::string> words;
int inputLength = userInput.length();
int wordLength = 0;
for (int i = 0; i < inputLength; i++) {
if (userInput.at(i) == ' ') {
words.push_back(userInput.substr(i - wordLength, wordLength));
wordLength = 0;
} else
wordLength++;
if (i + 1 == inputLength) {
words.push_back(userInput.substr(i - wordLength + 1, wordLength + 1));
wordLength = 0;
}
}
return words;
}
2026-02-20 00:37:32 +01:00
int wordRepeats(const std::vector<previousWords> wordsList,
const std::string word) {
int wordsSize = wordsList.size();
for (int i = 0; i < wordsSize; i++) {
if (wordsList.at(i).word == word)
return i;
}
return -1;
}
2026-02-20 00:37:32 +01:00
bool alreadyExists(const std::vector<previousWords> wordsList,
const std::string s) {
for (unsigned int i = 0; i < wordsList.size(); i++) {
if (s == wordsList.at(i).word)
return true;
2026-02-20 00:37:32 +01:00
}
return false;
}
2026-02-20 00:37:32 +01:00
std::vector<previousWords>
getWordsAndTheirPrevious(const std::vector<std::string> words) {
std::vector<previousWords> wordsList;
int wordsSize = words.size();
for (int i = 1; i < wordsSize; i++) {
previousWords temp;
temp.word = words.at(i);
wordOccurences tempTwo;
tempTwo.word = words.at(i - 1);
tempTwo.occurences = 1;
temp.previousWords.push_back(tempTwo);
int position = wordRepeats(wordsList, temp.word);
if (position == -1) {
wordsList.push_back(temp);
} else {
wordsList.at(position).previousWords.push_back(temp.previousWords.at(0));
}
2026-02-20 00:37:32 +01:00
}
return wordsList;
}
2026-02-20 00:37:32 +01:00
void printPreviousWord(const previousWords word) {
std::cout << "The word is \"" << word.word
<< "\" Words before it are: " << std::endl;
for (unsigned int i = 0; i < word.previousWords.size(); i++) {
print(word.previousWords.at(i).word);
2026-02-20 00:37:32 +01:00
}
}
2026-02-20 00:37:32 +01:00
void printPreviousWordsVector(const std::vector<previousWords> v) {
for (unsigned int i = 0; i < v.size(); i++) {
printPreviousWord(v.at(i));
}
}
2026-02-20 00:37:32 +01:00
std::vector<wordProbability>
getWordProbability(const std::vector<previousWords> wordsList) {
std::vector<wordProbability> probalityVector;
for (unsigned int i = 0; i < wordsList.size(); i++) {
int total = 0;
for (unsigned int j = 0; j < wordsList.at(i).previousWords.size(); j++)
total += wordsList.at(i).previousWords.at(j).occurences;
for (unsigned int j = 0; j < wordsList.at(i).previousWords.size(); j++) {
wordProbability wp;
wp.previousWord = wordsList.at(i).previousWords.at(j).word;
wp.nextWord = wordsList.at(i).word;
wp.probability =
total > 0
? (float)wordsList.at(i).previousWords.at(j).occurences / total
: 0.0f;
probalityVector.push_back(wp);
}
2026-02-20 00:37:32 +01:00
}
return probalityVector;
}
2026-02-20 00:37:32 +01:00
int main() {
std::string userInput;
do {
getline(std::cin, userInput);
} while (!validInput(userInput));
std::vector<std::string> words = divideIntoWords(userInput);
std::vector<previousWords> prev = getWordsAndTheirPrevious(words);
printPreviousWordsVector(prev);
return 0;
}
#endif