-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarkov_chain_generator.cpp
160 lines (127 loc) · 4.32 KB
/
markov_chain_generator.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
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
/****************************************************************************
**
** MIT License
**
** Copyright (c) 2018 Dmitry Bravikov
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
** OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
** THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <locale>
#include "string_util.h"
#include "markov_chain.h"
struct Options
{
unsigned markovChainOrder;
};
Options parseOptions(int argc, char* argv[]);
int main(int argc, char* argv[])
{
StringUtil::setLocale();
const Options options = parseOptions(argc, argv);
/* Принять список URL из стандартного потока. */
std::vector<std::string> urls;
while (true)
{
std::string url;
std::cin >> url;
if (std::cin.fail())
{
if (std::cin.eof())
{
break;
}
else
{
std::cerr << "Не удалось прочитать URL из потока" << std::endl;
exit(EXIT_FAILURE);
}
}
urls.push_back(url);
}
/* Создать цепь Маркова */
MarkovChain markovChain{options.markovChainOrder};
for (auto url: urls)
{
/* Получить содержимое файла, к которому ведет URL. */
std::string command{"curl --silent " + url};
static const char* mode = "r";
FILE* pipe = popen(command.c_str(), mode);
if (pipe == nullptr)
{
std::cerr << "Не удалось запустить curl." << std::endl;
exit(EXIT_FAILURE);
}
static const size_t bufferSize = 1000; /* Необоснованное значение */
std::vector<char> buffer(bufferSize);
std::string string;
while (fgets(buffer.data(), bufferSize, pipe) != nullptr)
{
string += buffer.data();
}
/* Добавить последовательность слов к цепи Маркова */
auto words = StringUtil::split(string);
markovChain.append(words);
}
std::cout << markovChain.toText() << std::endl;
return EXIT_SUCCESS;
}
Options parseOptions(int argc, char* argv[])
{
std::string helpMessage{
"Вызов:\n"
"\n"
" cat url_list | ./markov_chain_generator [order] | tee markov_chain\n"
"\n"
"Аргументы: \n"
"\n"
" order — порядок цепи Маркова, целое положительное число или ноль.\n"
};
unsigned markovChainOrder = 0;
if (argc == 2)
{
std::stringstream ss;
ss.str(argv[1]);
int order;
ss >> order;
if (!ss.eof() || ss.fail() || order < 0)
{
std::cerr
<< "Ошибка: неверный порядок цепи Маркова."
<< "\n"
<< std::endl
<< helpMessage
<< std::endl
;
exit(EXIT_FAILURE);
}
markovChainOrder = order;
}
else
{
std::cerr << helpMessage << std::endl;
exit(EXIT_FAILURE);
}
return {markovChainOrder};
}