r/AskProgramming • • 12d ago
Part two of my Markov Chain Project
Hey there! So i figured how to do the lottery system for my markov chain but im getting stuck on how to initialize it. what im trying to do is im trying to get the first two words and get them to predict the third word coming. But i dont know how to do it so far and this is what i have. I just kinda threw stuff at the wall till it stuck but yeah this is what i have so far. Thanks
I cant figure out the stupid code format on reddit for mobile, sorry in advanced.
void MarkovChain::initializeChains(const vector<string>& words) {
//(void)words;
// fill me in
map<list<string>, map<string, int>> token;
map<string, int> ngram;
for (const auto& word : words) {
list<string> key(words.begin(), words.end());
token\[key\]\[word\]++;
}
}
3
Upvotes
1
u/LaughingIshikawa 12d ago edited 12d ago
To format code properly, type four spaces in front of each line you want to appear as code:Nevermind, apparently I can't figure it out either. 🤷🙄As far as your problem... A Markov Chain doesn't use prediction algorithms, so I think you're confused about what you're trying to make / what these terms mean? Like this
Do you mean a "Markov decision process?"
I'm not an expert, but from what I understand the goal with a Markov Decision Process is to define a function that will take in state information (the two previous words) and use that information to predict the most likely next word, as a way to maximize the "reward" function (ie correctly predicting the next word.)
I'm... Not sure why you would being using a Markov Decision process in this case, because words in a sentence depend on a lot more than just the two previous words, but perhaps you're trying to built a simple LLM or text completion, and you're starting with a super small "context window" of only two words? (If so that's not a terrible way to approach the problem.)
There isn't really a "lottery system" at all, you just need to define a probability of each word in your training set appearing after the two input words, usually represented by assigning each word to a certain interval on the number line between 0 and 1. (The length of each segment is longer or shorter in proportion to a larger or smaller probability, and all the intervals add up to 1 because it's certain that one of the words will be chosen as the next word. A Markov Decision Process is then going to pick the longest segment each time, because that's all it's concerned with.)
If you're talking about how modern LLMs "pick" a prediction for the next word randomly from a set of possibilities... They do, but if you're modeling that you have completely left the realm of anything that can be modeled with the Markov Principle, at least as I understand it? 😅😅
1
u/PLBjt 12d ago
The loop uses the entire words vector as the key on every iteration, then counts the current word under that one giant key, so you get word frequencies instead of a chain. For a two-word context, take words[i] and words[i+1] as the key and words[i+2] as the next token, and only increment that pair. Start i at 0 and stop at words.size() - 3 so you don't read off the end. A list works as a map key but it's slower and easier to mutate by accident than a pair or a string joined with a separator. A longer context needs a lot more text before any key shows up more than once, otherwise every lookup is unique and the next word is just whatever followed it once in training.
1
u/Lunar_Panda_72987 8d ago
Right now token is declared as a local variable inside initializeChains, so it gets destroyed as soon as the function finishes. You probably want that to be a member variable on your class instead. Also, using a pair of strings as your map key might be a lot simpler than a list.