-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
134 lines (120 loc) · 4.26 KB
/
main.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
/***************************************************************************
* Copyright (C) 2008 by Guy Rutenberg *
* guyrutenberg@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#include "text_generator.h"
#include "config.h"
namespace po = boost::program_options;
using namespace std;
string usage();
int main (int argc, char **argv)
{
po::options_description desc("Options");
desc.add_options()
("help,h", "produce this help message")
("version", "prints version string")
("num-of-words,w", po::value<int>()->default_value(40), "how many words should be generated")
("steps,s", po::value<int>()->default_value(2), "the number of steps used in the Markov chain.")
;
po::options_description hidden("Hidden options");
hidden.add_options()
("input-file", po::value<string>()->default_value("-"), "sample text input file")
;
po::options_description cmdline_options;
cmdline_options.add(desc).add(hidden);
po::positional_options_description p;
p.add("input-file", 1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).
options(cmdline_options).positional(p).run(), vm);
}
catch(po::too_many_positional_options_error &e)
{
cerr<<e.what()<<endl;
cerr<<usage();
return 1;
}
catch(po::unknown_option &e)
{
cerr<<PACKAGE<<": "<<e.what()<<endl;
cerr<<usage();
cerr<<"Try `"<<PACKAGE<<" --help' for more information."<<endl;
return 1;
}
po::notify(vm);
if (vm.count("help")) {
cout<<usage();
cout<<"Reads a sample text from file and generate new text based on it using Markov chains.\n\n";
cout << desc << "\n";
cout <<"With no FILE, or when FILE is -, read standard input.\n"<<endl;
cout <<"Report bugs to <"<< PACKAGE_BUGREPORT <<">"<<endl;
return 0;
}
if (vm.count("version")) {
cout << PACKAGE_STRING << "\n";
cout << "Copyright (C) 2008 Guy Rutenberg.\n";
cout << "This is free software; see the source for copying "
"conditions. There is NO\nwarranty; not even for "
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
return 0;
}
if (vm["steps"].as<int>()<1) {
cerr<<"The number of steps must be positive value"<<endl;
cerr<<usage();
return 1;
}
TextGenerator *gen = new DefaultTextGenerator();
// read file (or stdin) and generate text
if (vm["input-file"].as<string>() == "-") {
string line;
while (!cin.eof()) {
getline(cin, line);
gen->addWords(line);
}
} else {
ifstream file (vm["input-file"].as<string>().c_str());
if (!file.is_open()) {
cerr<<"Error openning file: "<<vm["input-file"].as<string>()<<endl;
return 1;
}
string line;
while (!file.eof()) {
getline(file, line);
gen->addWords(line);
}
file.close();
}
int num_words = vm["num-of-words"].as<int>();
int num_steps = vm["steps"].as<int>();
cout<<gen->generateWords(num_words, num_steps)<<endl;
return 0;
}
/**
* Prints usage information.
*/
string usage()
{
string out = "Usage: ";
out += PACKAGE;
out += " [options] FILE\n";
return out;
}