morfeusz_analyzer.cpp
4.41 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
/*
* File: main.cc
* Author: mlenart
*
* Created on October 8, 2013, 12:41 PM
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include <map>
#include "fsa/fsa.hpp"
#include "Tagset.hpp"
#include "Morfeusz.hpp"
#include "const.hpp"
#include "cli/cli.hpp"
using namespace std;
using namespace ez;
int main(int argc, const char** argv) {
ezOptionParser opt;
opt.overview = "Morfeusz analyzer";
opt.syntax = string(argv[0]) + " [OPTIONS]";
opt.example = string(argv[0]) + " --aggl strict --praet split --input /path/to/file.fsa";
// opt.footer = "Morfeusz Copyright (C) 2014\n";
opt.add(
"", // Default.
0, // Required?
0, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Display usage instructions.", // Help description.
"-h", // Flag token.
"-help", // Flag token.
"--help", // Flag token.
"--usage" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"file with analyzer finite state automaton and data, created with buildfsa.py script.", // Help description.
"-i", // Flag token.
"-input", // Flag token.
"--input" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"aggl option.", // Help description.
"-a", // Flag token.
"-aggl", // Flag token.
"--aggl" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"praet option.", // Help description.
"-p", // Flag token.
"-praet", // Flag token.
"--praet" // Flag token.
);
opt.parse(argc, argv);
if (opt.firstArgs.size() > 1) {
cerr << "Invalid argument (not bound to any flag): " << *opt.firstArgs[1] << endl;
return 1;
}
if (!opt.lastArgs.empty()) {
cerr << "Invalid argument (not bound to any flag): " << *opt.lastArgs[0] << endl;
return 1;
}
if (opt.isSet("-h")) {
printCLIUsage(opt, cout);
return 0;
}
Morfeusz morfeusz;
if (opt.isSet("-i")) {
string analyzerFile;
opt.get("-i")->getString(analyzerFile);
morfeusz.setAnalyzerFile(analyzerFile);
printf("Using dictionary from %s\n", analyzerFile.c_str());
}
if (opt.isSet("-a")) {
string aggl;
opt.get("-a")->getString(aggl);
cerr << "setting aggl option to " << aggl << endl;
morfeusz.setAggl(aggl);
}
if (opt.isSet("-p")) {
string praet;
opt.get("-p")->getString(praet);
cerr << "setting praet option to " << praet << endl;
morfeusz.setPraet(praet);
}
#ifdef _WIN32
morfeusz.setCharset(CP852);
#endif
#ifdef _WIN64
morfeusz.setCharset(CP852);
#endif
string line;
while (getline(cin, line)) {
// printf("%s\n", line.c_str());
vector<MorphInterpretation> res;
morfeusz.analyze(line, res);
int prevStart = -1;
int prevEnd = -1;
printf("[");
for (unsigned int i = 0; i < res.size(); i++) {
MorphInterpretation& mi = res[i];
string lemmaToShow = mi.getLemma().c_str();
if (!mi.getHomonymId().empty()) {
lemmaToShow += ":";
lemmaToShow += mi.getHomonymId();
}
if (prevStart != -1
&& (prevStart != mi.getStartNode() || prevEnd != mi.getEndNode())) {
printf("]\n[");
}
else if (prevStart != -1) {
printf("; ");
}
printf("%s", mi.toString(true).c_str());
// printf("%d,%d,%s,%s,%s,%s",
// mi.getStartNode(), mi.getEndNode(),
// mi.getOrth().c_str(), lemmaToShow.c_str(),
// mi.getTag().c_str(), lemmaToShow.c_str());
prevStart = mi.getStartNode();
prevEnd = mi.getEndNode();
}
printf("]\n");
}
printf("\n");
return 0;
}