-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
198 lines (182 loc) · 7.8 KB
/
Copy pathmain.cpp
File metadata and controls
198 lines (182 loc) · 7.8 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
#include "src/parser.hpp"
#include "src/pretty_print.hpp"
#include <iostream>
#include <string>
#include <iomanip>
#include <unistd.h>
// ---------------------------------------------------------------------------
// JSON helpers
// ---------------------------------------------------------------------------
static std::string json_escape(const std::string& s) {
std::string out;
out.reserve(s.size() + 4);
for (unsigned char c : s) {
if (c == '"') out += "\\\"";
else if (c == '\\') out += "\\\\";
else if (c == '\n') out += "\\n";
else if (c == '\r') out += "\\r";
else if (c == '\t') out += "\\t";
else out += c;
}
return out;
}
static std::string tree_to_json(const DerivTree& node, int indent = 0) {
if (!node) return "null";
std::string pad(indent * 2, ' ');
std::string inner(indent * 2 + 2, ' ');
std::string out = "{\n";
out += inner + "\"rule\": \"" + json_escape(node->rule) + "\",\n";
out += inner + "\"cat\": \"" + json_escape(cat_repr(node->cat)) + "\"";
if (!node->word.empty())
out += ",\n" + inner + "\"word\": \"" + json_escape(node->word) + "\"";
if (!node->children.empty()) {
out += ",\n" + inner + "\"children\": [\n";
for (size_t i = 0; i < node->children.size(); ++i) {
out += inner + " " + tree_to_json(node->children[i], indent + 2);
if (i + 1 < node->children.size()) out += ",";
out += "\n";
}
out += inner + "]";
}
out += "\n" + pad + "}";
return out;
}
// ---------------------------------------------------------------------------
// Derivation tree printer (ASCII box-drawing)
// ---------------------------------------------------------------------------
static void print_tree(const DerivTree& node, const std::string& prefix = "",
bool last = true) {
if (!node) return;
std::string connector = last ? "\u2514\u2500\u2500 " : "\u251c\u2500\u2500 ";
std::string child_prefix = prefix + (last ? " " : "\u2502 ");
std::string label = "[" + node->rule + "] " + cat_repr(node->cat);
if (!node->word.empty()) label += " \"" + node->word + "\"";
std::cout << prefix << connector << label << "\n";
for (size_t i = 0; i < node->children.size(); ++i)
print_tree(node->children[i], child_prefix, i + 1 == node->children.size());
}
// ---------------------------------------------------------------------------
// Process one sentence
// ---------------------------------------------------------------------------
static void process(const std::string& sentence, bool show_steps,
bool show_tree, bool json_out, bool all_parses) {
std::string s = sentence;
size_t a = s.find_first_not_of(" \t\n\r");
if (a == std::string::npos) return;
size_t b = s.find_last_not_of(" \t\n\r");
s = s.substr(a, b - a + 1);
if (s.empty()) return;
// ---- JSON output -------------------------------------------------------
if (json_out) {
std::cout << "{\n \"input\": \"" << json_escape(s) << "\",\n \"parses\": [\n";
try {
auto results = parse(s);
for (size_t i = 0; i < results.size(); ++i) {
bool is_q = cat_equal(results[i].cat, CAT_Q);
std::cout << " {\n"
<< " \"type\": \"" << (is_q ? "Question" : "Statement") << "\",\n"
<< " \"logical_form\": \"" << json_escape(pretty(results[i].sem)) << "\"";
if (results[i].deriv) {
std::cout << ",\n \"tree\": " << tree_to_json(results[i].deriv, 3);
}
std::cout << "\n }";
if (i + 1 < results.size()) std::cout << ",";
std::cout << "\n";
}
} catch (const ParseError& e) {
std::cout << " {\"error\": \"" << json_escape(e.what()) << "\"}\n";
}
std::cout << " ]\n}\n";
return;
}
// ---- Normal / step output ----------------------------------------------
std::cout << "\nInput: " << s << "\n";
std::cout << std::string(s.size() + 8, '-') << "\n";
if (show_steps) {
try {
auto results = parse_steps(s);
for (size_t i = 0; i < results.size(); ++i) {
bool multi = all_parses || results.size() > 1;
std::string label = multi ? "Parse " + std::to_string(i+1)
: "Logical Form";
std::cout << "\n" << label << " \u2014 reduction steps:\n";
for (size_t j = 0; j < results[i].steps.size(); ++j) {
std::cout << " Step " << std::setw(2) << j
<< ": " << pretty(results[i].steps[j]) << "\n";
}
std::cout << " Final: " << pretty(results[i].steps.back()) << "\n";
}
} catch (const ParseError& e) {
std::cerr << "Parse failed: " << e.what() << "\n";
}
return;
}
try {
auto results = parse(s);
for (size_t i = 0; i < results.size(); ++i) {
bool multi = all_parses || results.size() > 1;
bool is_q = cat_equal(results[i].cat, CAT_Q);
std::string label;
if (multi) {
label = "Parse " + std::to_string(i+1)
+ (is_q ? " (Question)" : "");
} else {
label = is_q ? "Question" : "Logical Form";
}
std::cout << "\n" << label << ": " << pretty(results[i].sem) << "\n";
if (show_tree && results[i].deriv) {
std::cout << "\nDerivation:\n";
print_tree(results[i].deriv);
}
}
} catch (const ParseError& e) {
std::cerr << "Parse failed: " << e.what() << "\n";
}
}
// ---------------------------------------------------------------------------
// Interactive REPL
// ---------------------------------------------------------------------------
static void interactive(bool show_steps, bool show_tree, bool json_out, bool all_parses) {
std::cout << "lambda. \u2014 interactive mode\n";
std::cout << "Type a sentence and press Enter. Type 'quit' to exit.\n\n";
std::string line;
while (true) {
std::cout << "> " << std::flush;
if (!std::getline(std::cin, line)) break;
if (line == "quit" || line == "exit" || line == "q") break;
process(line, show_steps, show_tree, json_out, all_parses);
}
}
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
int main(int argc, char* argv[]) {
bool show_steps = false;
bool show_tree = false;
bool json_out = false;
bool all_parses = false;
bool do_interact = false;
std::string sentence;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--show-steps") show_steps = true;
else if (arg == "--show-tree") show_tree = true;
else if (arg == "--json") json_out = true;
else if (arg == "--all-parses") all_parses = true;
else if (arg == "--interactive" || arg == "-i") do_interact = true;
else if (arg[0] != '-') sentence = arg;
}
if (do_interact || (sentence.empty() && isatty(0))) {
interactive(show_steps, show_tree, json_out, all_parses);
return 0;
}
if (!sentence.empty()) {
process(sentence, show_steps, show_tree, json_out, all_parses);
return 0;
}
// Batch: read from stdin
std::string line;
while (std::getline(std::cin, line))
process(line, show_steps, show_tree, json_out, all_parses);
return 0;
}