-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathydcv.cpp
More file actions
229 lines (189 loc) · 5.63 KB
/
ydcv.cpp
File metadata and controls
229 lines (189 loc) · 5.63 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <sstream>
#include <cpr/cpr.h>
#include <rapidjson/reader.h>
#include <rapidjson/document.h>
/* boost */
#include <boost/program_options.hpp>
/* readline */
#include "readline.hpp"
#include "cpplog.hpp"
using std::string;
using std::stringstream;
using namespace rapidjson;
using namespace std;
using namespace CPPLOG;
namespace po = boost::program_options;
// API KEY from ydcv
#define API "YouDaoCV"
#define API_KEY "659600698"
#define API_VERSION "1.2"
#define YD_BASE_URL "http://fanyi.youdao.com"
#define YD_API_URL (YD_BASE_URL "/openapi.do?keyfrom=" API "&key=" API_KEY "&type=data&doctype=json&version=" API_VERSION "&q=")
/* runtime configuration */
static struct {
int logmask;
bool out_full;
int color;
bool selection;
bool speech;
vector<string> words;
} cfg;
static cpplog mylog;
void print_explanation(Document& doc);
void query(string &word) {
auto response = cpr::Get(cpr::Url{string(YD_API_URL) + word});
Document doc;
doc.Parse(response.text.c_str());
print_explanation(doc);
}
void print_explanation(Document& doc)
{
int has_result = 0;
ILOG(mylog) << doc["query"].GetString();
if (doc.HasMember("basic")) {
has_result = 1;
Value &basic_dic = doc["basic"];
if (basic_dic.HasMember("uk-phonetic") || basic_dic.HasMember("us-phonetic"))
ILOG(mylog) << " UK: [" << basic_dic["uk-phonetic"].GetString() <<
"], US: [" << basic_dic["us-phonetic"].GetString() << "]" << endl;
else if (basic_dic.HasMember("phonetic"))
ILOG(mylog) << " [" << basic_dic["phonetic"].GetString() << "]" << endl;
else
ILOG(mylog) << endl;
if (cfg.speech) {
if (basic_dic.HasMember("uk-speech") && basic_dic.HasMember("us-speech")) {
ILOG(mylog) << " Text to Speech:" << endl;
ILOG(mylog) << " * UK: " << basic_dic["uk-speech"].GetString() << endl;
ILOG(mylog) << " * US: " << basic_dic["us-speech"].GetString() << endl;
}
else if (basic_dic.HasMember("speech"))
ILOG(mylog) << " * " << basic_dic["speech"].GetString() << endl;
ILOG(mylog) << endl;
}
if (basic_dic.HasMember("explains")) {
ILOG(mylog) << " Word Explanation:" << endl;
Value &explains = basic_dic["explains"];
for (auto itr = explains.Begin(); itr != explains.End(); ++itr)
ILOG(mylog) << " * " << itr->GetString() << endl;
} else
ILOG(mylog) << endl;
} else if (doc.HasMember("translation")) {
has_result = 1;
ILOG(mylog) << endl;
ILOG(mylog) << " Translation:" << endl;
Value &trans = doc["translation"];
for (auto itr = trans.Begin(); itr != trans.End(); ++itr)
ILOG(mylog) << " * " << itr->GetString() << endl;
} else {
ILOG(mylog) << endl;
}
if (doc.HasMember("web")) {
has_result = 1;
ILOG(mylog) << endl;
ILOG(mylog) << " Web Reference:" << endl;
Value &web_dic = doc["web"];
for (auto dic = web_dic.Begin(); dic != web_dic.End(); ++dic) {
ILOG(mylog) << " * " << (*dic)["key"].GetString() << endl;
ILOG(mylog) << " ";
Value &values = (*dic)["value"];
for (auto itr = values.Begin(); itr != values.End(); ++itr)
ILOG(mylog) << " " << itr->GetString() << ";";
ILOG(mylog) << endl;
}
}
if (has_result == 0)
ILOG(mylog) << " -- No result for this query." << endl;
ILOG(mylog) << endl;
}
int parse_options(int argc, char **argv)
{
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "show this help message and exit")
("full,f", "print full web reference, only the first 3 results\n"
"will be printed without this flag.")
("simple,s", "only show explainations. argument \"-f\" will not take\n"
"effect.")
("speech,S", "print URL to speech audio.")
("selection,x", "show explaination of current selection.")
("color", po::value<string>(), "{always,auto,never}"
"colorize the output. Default to 'auto' or can be\n"
"'never' or 'always'.")
("debug", "print debug info.")
("verbose", "print verbose info.")
("words", po::value<vector<string>>()->multitoken(), "words to lookup, or quoted sentences to translate.")
;
po::positional_options_description pos_desc;
pos_desc.add("words", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).positional(pos_desc).run(), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 1;
}
if (vm.count("simple"))
cfg.out_full = 0;
else if (vm.count("full"))
cfg.out_full = 1;
if (vm.count("speech"))
cfg.speech = 1;
if (vm.count("selection")) {
cfg.selection = 1;
cout << "Not implemeted yet." << endl;
}
if (vm.count("color")) {
string color = vm["color"].as<string>();
if (color == string("auto") || color == string("always"))
cfg.color = 1;
else if (color == string("never"))
cfg.color = 0;
else
cout << "color only accept {always,auto,never}" << endl;
}
if (vm.count("debug"))
cfg.logmask |= LOG_DEBUG;
if (vm.count("verbose"))
cfg.logmask |= LOG_DEBUG | LOG_VERBOSE;
if (vm.count("words")) {
const vector<string> &v = vm["words"].as<vector<string>>();
// to_cout(v);
copy(v.begin(), v.end(), back_insert_iterator<vector<string>>(cfg.words));
}
return 0;
}
int main(int argc, char **argv)
{
int ret = 0;
/* initialize config */
cfg.logmask = LOG_ERROR|LOG_WARN|LOG_INFO;
cfg.out_full = 1;
cfg.color = 0;
cfg.selection = 0;
cfg.speech = 0;
ret = parse_options(argc, argv);
if (ret)
{
return ret;
}
mylog.SetLogMask(cfg.logmask);
for (auto word : cfg.words) {
query(word);
}
if (cfg.words.size() == 0) {
while (1) {
yarw rl(string("> "));
string word = rl.getLine();
rl.addHistory(word);
if (word.empty()) {
cout << "\nBye\n";
break;
} else {
query(word);
}
}
}
return 0;
}