-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov.cpp
65 lines (62 loc) · 2.09 KB
/
markov.cpp
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
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
class Markov {
public:
map<deque<string>, vector<string>> state;
vector<string> all_tokens;
int state_length;
Markov(string input_text, int n) {
ifstream file(input_text);
deque<string> current;
string buff;
state_length = n + 1; // n previous states are used
srand(time(0));
for (int i = 0; i < state_length; i++) { // placeholder of newlines
addWord(current, "\n");
}
while (file >> buff) { // add the main body of the text
addWord(current, buff);
all_tokens.push_back(buff);
}
addWord(current, "\n"); // mark the end of the text
file.close();
}
void generateText(int to_generate, int temperature) {
deque<string> current;
string buff;
int n = all_tokens.size();
int i = 0;
for (int i = 0; i < state_length; i++) { // placeholder of newlines
addWord(current, "\n");
}
while (i < to_generate) {
vector<string>& next = state[current];
if ((rand() % 100 + 1) <= temperature) { // introduce a little anarchy
cout << all_tokens[rand() % n] << " ";
i += 1;
continue;
}
string buff = next[rand() % next.size()];
if (buff != "\n") {
cout << buff << " ";
current.pop_front();
current.push_back(buff);
i += 1;
}
}
cout << "\n";
}
private:
void addWord(deque<string>& current, string extend) {
if ((int) current.size() == state_length) {
state[current].push_back(extend);
current.pop_front();
}
current.push_back(extend);
}
};