Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (111 loc) · 3.6 KB
/
Copy pathmain.cpp
File metadata and controls
131 lines (111 loc) · 3.6 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
/**
* @author Jamie (jamie.lim@kakaocorp.com)
* @copyright Copyright (C) 2018-, Kakao Corp. All rights reserved.
*/
//////////////
// includes //
//////////////
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include "cxxopts.hpp"
#include "fmt/printf.h"
#ifdef PROFILER
#include "gperftools/profiler.h"
#endif
#include "spdlog/spdlog.h"
#include "khaiii/KhaiiiApi.hpp"
#include "khaiii/khaiii_dev.h"
using std::cerr;
using std::cin;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using khaiii::KhaiiiApi;
///////////////
// functions //
///////////////
int run(const cxxopts::ParseResult& opts) {
auto _log = spdlog::get("console");
khaiii_set_log_levels(opts["set-log"].as<string>().c_str());
auto khaiii_api = KhaiiiApi::create();
try {
khaiii_api->open(opts["rsc-dir"].as<string>(), opts["opt-str"].as<string>());
} catch (const khaiii::Except& exc) {
_log->error("fail to open dir: '{}', opt: '{}'", opts["rsc-dir"].as<string>(),
opts["opt-str"].as<string>());
_log->error(exc.what());
return 1;
}
for (string line; getline(cin, line); ) {
_log->debug("sent: {}", line);
const khaiii_word_t* results = nullptr;
try {
results = khaiii_api->analyze(line.c_str(), "");
} catch (const khaiii::Except& exc) {
_log->warn("{}: {}", exc.what(), line);
continue;
}
for (auto word = results; word != nullptr; word = word->next) {
fmt::print("{}\t", line.substr(word->begin, word->length));
const khaiii_morph_t* morphs = word->morphs;
for (auto morph = morphs; morph != nullptr; morph = morph->next) {
if (morph != morphs) fmt::print(" + ");
fmt::print("{}/{}", morph->lex, morph->tag);
}
fmt::print("\n");
}
fmt::print("\n");
khaiii_api->free_results(results);
}
return 0;
}
//////////
// main //
//////////
int main(int argc, char** argv) {
auto _log = spdlog::stderr_color_mt("console");
spdlog::set_level(spdlog::level::warn);
cxxopts::Options options("khaiii", "analyze with khaiii");
options.add_options()
("h,help", "print this help")
("rsc-dir", "resource directory", cxxopts::value<string>()->default_value(""))
("opt-str", "option (JSON format)", cxxopts::value<string>()->default_value(""))
("input", "input file (default: stdin)", cxxopts::value<string>())
("output", "output file (default: stdout)", cxxopts::value<string>())
("set-log", "set log level", cxxopts::value<string>()->default_value("all:info"));
auto opts = options.parse(argc, argv);
if (opts.count("help")) {
fmt::fprintf(cerr, "%s\n", options.help());
return 0;
}
if (opts.count("input")) {
string path = opts["input"].as<string>();
ifstream fin(path);
if (!fin.good()) {
_log->error("input file not found: {}", path);
return 1;
}
if (freopen(path.c_str(), "r", stdin) == nullptr) {
_log->error("fail to open input file: {}", path);
return 2;
}
}
if (opts.count("output")) {
string path = opts["output"].as<string>();
if (freopen(path.c_str(), "w", stdout) == nullptr) {
_log->error("fail to open output file: {}", path);
return 3;
}
}
#ifdef PROFILER
ProfilerStart("/tmp/bin_khaiii.prof");
#endif
int ret = run(opts);
#ifdef PROFILER
ProfilerStop();
#endif
return ret;
}