add a --sentence-parse-only option/clean up

This commit is contained in:
Emil Lerch 2025-09-22 16:50:32 -07:00
parent a75b80c5c4
commit a0a162632e
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -230,26 +230,52 @@ pub fn main() !u8 {
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
// Check for --sentence-parse-only option
var sentence_parse_only = false;
var sentence_arg: ?[]const u8 = null;
for (args[1..]) |arg| {
if (std.mem.eql(u8, arg, "--sentence-parse-only")) {
sentence_parse_only = true;
} else if (sentence_arg == null) {
sentence_arg = arg;
}
}
var stdout_writer = std.fs.File.stdout().writer(&.{});
const stdout = &stdout_writer.interface;
_ = stdout;
var stderr_writer = std.fs.File.stderr().writer(&.{});
const stderr = &stderr_writer.interface;
if (args.len < 2) {
stderr.print("Usage: {s} <sentence>\n", .{args[0]}) catch {};
if (sentence_arg == null) {
try stderr.print("Usage: {s} [--sentence-parse-only] <sentence>\n", .{args[0]});
return 1;
}
std.log.debug("loading device config", .{});
// Get binary directory
const bin_path = std.fs.selfExePathAlloc(allocator) catch |err| {
const bin_dir = std.fs.selfExeDirPathAlloc(allocator) catch |err| {
stderr.print("Failed to get binary path: {}\n", .{err}) catch {};
return 1;
};
defer allocator.free(bin_path);
defer allocator.free(bin_dir);
const bin_dir = std.fs.path.dirname(bin_path) orelse ".";
std.log.debug("initializing parser", .{});
var parser = pos.Parser.initWithDataDir(allocator, bin_dir) catch |err| {
std.debug.print("Failed to initialize parser: {}\n", .{err});
return 1;
};
defer parser.deinit();
if (sentence_parse_only) {
const sentence_z = try allocator.dupeZ(u8, sentence_arg.?);
defer allocator.free(sentence_z);
var tree = parser.adaptiveParse(sentence_z, word_replacements) catch |err| {
std.debug.print("Failed to parse sentence: {}\n", .{err});
return 1;
};
defer tree.deinit();
try stdout.print("{f}", .{tree});
} else {
std.log.debug("loading device config", .{});
var devices = loadDeviceConfig(allocator, bin_dir) catch |err| {
stderr.print("Failed to load device configuration: {}\n", .{err}) catch {};
@ -264,13 +290,6 @@ pub fn main() !u8 {
devices.deinit();
}
std.log.debug("initializing parser", .{});
var parser = pos.Parser.initWithDataDir(allocator, bin_dir) catch |err| {
stderr.print("Failed to initialize parser: {}\n", .{err}) catch {};
return 1;
};
defer parser.deinit();
processCommand(allocator, args[1], &parser, &devices) catch |err| {
switch (err) {
error.UnrecognizedSentence => {
@ -280,6 +299,7 @@ pub fn main() !u8 {
else => return err,
}
};
}
return 0;
}