-
Notifications
You must be signed in to change notification settings - Fork 1
/
jumanpp_t9.cc
82 lines (62 loc) · 1.88 KB
/
jumanpp_t9.cc
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
//
// Created by Arseny Tolmachev on 2018/06/11.
//
#include "core/env.h"
#include "core/analysis/analysis_result.h"
#include <iostream>
#include <string>
using namespace jumanpp;
struct ResultOutputter {
core::analysis::StringField surface;
core::analysis::StringField english;
core::analysis::AnalysisResult resultFiller;
core::analysis::AnalysisPath top1;
Status initialize(const core::analysis::Analyzer& ana) {
auto& output = ana.output();
JPP_RETURN_IF_ERROR(output.stringField("surface", &surface));
JPP_RETURN_IF_ERROR(output.stringField("english", &english));
return Status::Ok();
}
bool outputResult(const core::analysis::Analyzer& ana, std::ostream& os) {
if (!resultFiller.reset(ana)) return false;
if (!resultFiller.fillTop1(&top1)) return false;
auto& output = ana.output();
core::analysis::NodeWalker walker;
core::analysis::ConnectionPtr cptr{};
while (top1.nextBoundary()) {
if (!top1.nextNode(&cptr) || !output.locate(cptr.latticeNodePtr(), &walker) || !walker.next()) {
return false;
}
os << surface[walker] << "\t" << english[walker] << "\n";
}
os << std::endl;
return true;
}
};
void dieOnError(Status s) {
if (!s) {
std::cerr << s;
exit(1);
}
}
int main(int argc, const char* argv[]) {
core::JumanppEnv env;
dieOnError(env.loadModel(StringPiece::fromCString(argv[1])));
env.setBeamSize(5);
env.setGlobalBeam(6, 1, 5);
dieOnError(env.initFeatures(nullptr));
core::analysis::Analyzer analyzer;
dieOnError(env.makeAnalyzer(&analyzer));
ResultOutputter out;
dieOnError(out.initialize(analyzer));
std::string input;
while (std::getline(std::cin, input)) {
Status s = analyzer.analyze(input);
if (!s) {
std::cerr << "Failed to analyze [" << input << "]: " << s;
continue;
}
out.outputResult(analyzer, std::cout);
}
return 0;
}