-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMarkovChain.h
162 lines (143 loc) · 3.8 KB
/
MarkovChain.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// MarkovChain.h
// Core
//
// Created by Rasmus Anthin on 2022-11-15.
//
#pragma once
#include <map>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include "Rand.h"
namespace markov_chain
{
template<typename T>
class MarkovChain
{
public:
using MarkovChainTransitionTable = std::map<T, std::vector<std::pair<T, float>>>;
MarkovChainTransitionTable mctt;
T empty_item;
MarkovChain(T empty_val) : empty_item(empty_val) {}
void add_transition(T from, T to)
{
auto& targets = mctt[from];
auto it = std::find_if(targets.begin(), targets.end(), [&to](const auto& ipair) { return ipair.first == to; });
if (it != targets.end())
it->second++;
else
targets.emplace_back(to, 1);
}
void add_transitions(const std::vector<T>& items)
{
auto N = static_cast<int>(items.size());
if (N >= 2)
{
for (int i = 0; i < N - 1; ++i)
{
T from = items[i];
T to = items[i+1];
add_transition(from, to);
}
}
if (N > 0)
add_transition(items[N-1], empty_item);
}
int import_transitions(const std::string& filename)
{
std::ifstream input(filename);
if (input.fail())
{
std::cerr << "Error opening file \"" + filename + "\"!" << std::endl;
return EXIT_FAILURE;
}
std::stringstream ss;
ss << input.rdbuf();
input.close();
std::string line;
while (std::getline(ss, line))
{
// Break line into words.
std::istringstream iss(line);
std::vector<std::string> word_vec;
std::string word;
while (iss >> word)
word_vec.emplace_back(word);
add_transitions(word_vec);
}
return EXIT_SUCCESS;
}
void print() const
{
for (const auto& state : mctt)
{
std::cout << " [" << state.first << "] :" << std::endl;
for (const auto& trg : state.second)
std::cout << " => " << trg.first << " : " << trg.second << std::endl;
}
}
void normalize_transition_weights()
{
for (auto& targets : mctt)
{
int tot = 0;
for (const auto& trg : targets.second)
tot += trg.second;
if (tot > 0)
{
for (auto& trg : targets.second)
trg.second /= tot;
}
std::sort(targets.second.begin(), targets.second.end(),
[](const auto& A, const auto& B) { return A.second < B.second; });
}
}
T generate(int min_num_items = -1, int max_num_items = -1) const
{
auto N = mctt.size();
if (N == 0)
return empty_item;
size_t num_items = 1;
T ret = empty_item;
do
{
num_items = 1;
auto rnd_idx = rnd::rand_int(0, static_cast<int>(N) - 1);
int ctr = 0;
const T* start_item_ptr = nullptr;
for (auto& item : mctt)
{
if (rnd_idx == ctr++)
{
start_item_ptr = &item.first;
break;
}
}
if (start_item_ptr == nullptr)
return empty_item;
T item = *start_item_ptr;
ret = item;
while (item != "")
{
auto t = rnd::rand();
const auto& entry = mctt.at(item);
for (const auto& trg : entry)
{
if (t < trg.second)
{
item = trg.first;
ret += item;
if (item != "")
num_items++;
break;
}
t -= trg.second;
}
}
} while ((min_num_items != -1 && num_items < min_num_items) || (max_num_items != -1 && num_items > max_num_items));
return ret;
}
};
}