be clear that adaptive parse assumes this is a command
This commit is contained in:
parent
46fd8f585c
commit
a41c94b2c9
2 changed files with 31 additions and 32 deletions
31
src/main.zig
31
src/main.zig
|
@ -231,15 +231,16 @@ pub fn main() !u8 {
|
|||
defer std.process.argsFree(allocator, args);
|
||||
|
||||
// Check for --sentence-parse-only option
|
||||
var sentence_parse_only = false;
|
||||
var sentence_parse_only: enum { sentence, command, none } = .none;
|
||||
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) {
|
||||
if (std.mem.eql(u8, arg, "--sentence-parse-only"))
|
||||
sentence_parse_only = .sentence
|
||||
else if (std.mem.eql(u8, arg, "--command-parse-only"))
|
||||
sentence_parse_only = .command
|
||||
else if (sentence_arg == null)
|
||||
sentence_arg = arg;
|
||||
}
|
||||
}
|
||||
|
||||
var stdout_writer = std.fs.File.stdout().writer(&.{});
|
||||
|
@ -248,7 +249,7 @@ pub fn main() !u8 {
|
|||
const stderr = &stderr_writer.interface;
|
||||
|
||||
if (sentence_arg == null) {
|
||||
try stderr.print("Usage: {s} [--sentence-parse-only] <sentence>\n", .{args[0]});
|
||||
try stderr.print("Usage: {s} [--sentence-parse-only] [--command-parse-only] <sentence>\n", .{args[0]});
|
||||
return 1;
|
||||
}
|
||||
const bin_dir = std.fs.selfExeDirPathAlloc(allocator) catch |err| {
|
||||
|
@ -263,14 +264,20 @@ pub fn main() !u8 {
|
|||
return 1;
|
||||
};
|
||||
defer parser.deinit();
|
||||
if (sentence_parse_only) {
|
||||
if (sentence_parse_only != .none) {
|
||||
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;
|
||||
};
|
||||
var tree = if (sentence_parse_only == .command)
|
||||
parser.adaptiveCommandParse(sentence_z, word_replacements) catch {
|
||||
std.log.err("Failed to parse sentence: {s}", .{sentence_z});
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
parser.parse(sentence_z) catch {
|
||||
std.log.err("Failed to parse sentence: {s}", .{sentence_z});
|
||||
return 1;
|
||||
};
|
||||
defer tree.deinit();
|
||||
|
||||
try stdout.print("{f}", .{tree});
|
||||
|
@ -304,7 +311,7 @@ pub fn main() !u8 {
|
|||
}
|
||||
|
||||
fn processCommand(allocator: std.mem.Allocator, sentence: [:0]const u8, parser: *pos.Parser, devices: *std.StringHashMap([]const u8)) !void {
|
||||
var tree = parser.adaptiveParse(sentence, word_replacements) catch |err| {
|
||||
var tree = parser.adaptiveCommandParse(sentence, word_replacements) catch |err| {
|
||||
std.log.err("Failed to parse sentence with all replacements: {}\n", .{err});
|
||||
return error.UnrecognizedSentence;
|
||||
};
|
||||
|
|
32
src/root.zig
32
src/root.zig
|
@ -360,11 +360,12 @@ pub const Parser = struct {
|
|||
_ = c.dictionary_delete(self.dict);
|
||||
}
|
||||
|
||||
/// Parses a sentence with an attempt to "fix" the sentence. If a valid
|
||||
/// Parses a sentence with an attempt to "fix" the sentence, assuming
|
||||
/// the sentence is a command with an action and an object. If a valid
|
||||
/// sentence is found, it will be returned, with the guarantee that
|
||||
/// sentenceObject and sentenceAction will return non-zero results. If that
|
||||
/// condition cannot be satisfied, error.NoValidParse will be returned
|
||||
pub fn adaptiveParse(self: *Parser, sentence: [:0]const u8, replacements: std.StaticStringMap([]const u8)) !ParseTree {
|
||||
pub fn adaptiveCommandParse(self: *Parser, sentence: []const u8, replacements: std.StaticStringMap([]const u8)) !ParseTree {
|
||||
var final_buf: [1024]u8 = undefined;
|
||||
|
||||
const replacement_keys = replacements.keys();
|
||||
|
@ -690,7 +691,7 @@ test "real usage - jack" {
|
|||
try std.testing.expectEqualStrings("bedroom", sentence_object[1]);
|
||||
try std.testing.expectEqualStrings("light", sentence_object[2]);
|
||||
}
|
||||
test "adaptiveParse successful without replacements" {
|
||||
test "adaptiveCommandParse successful without replacements" {
|
||||
var parser = try Parser.init(std.testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
|
@ -700,10 +701,7 @@ test "adaptiveParse successful without replacements" {
|
|||
});
|
||||
|
||||
const sentence = "turn on the kitchen light";
|
||||
const sentence_z = try std.testing.allocator.dupeZ(u8, sentence);
|
||||
defer std.testing.allocator.free(sentence_z);
|
||||
|
||||
var tree = try parser.adaptiveParse(sentence_z, replacements);
|
||||
var tree = try parser.adaptiveCommandParse(sentence, replacements);
|
||||
defer tree.deinit();
|
||||
|
||||
const action_words = try tree.sentenceAction();
|
||||
|
@ -719,7 +717,7 @@ test "adaptiveParse successful without replacements" {
|
|||
try std.testing.expectEqualStrings("light", object_words[1]);
|
||||
}
|
||||
|
||||
test "adaptiveParse with word replacement" {
|
||||
test "adaptiveCommandParse with word replacement" {
|
||||
var parser = try Parser.init(std.testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
|
@ -729,10 +727,8 @@ test "adaptiveParse with word replacement" {
|
|||
});
|
||||
|
||||
const sentence = "turn on the kitchen lake";
|
||||
const sentence_z = try std.testing.allocator.dupeZ(u8, sentence);
|
||||
defer std.testing.allocator.free(sentence_z);
|
||||
|
||||
var tree = try parser.adaptiveParse(sentence_z, replacements);
|
||||
var tree = try parser.adaptiveCommandParse(sentence, replacements);
|
||||
defer tree.deinit();
|
||||
|
||||
const action_words = try tree.sentenceAction();
|
||||
|
@ -748,7 +744,7 @@ test "adaptiveParse with word replacement" {
|
|||
try std.testing.expectEqualStrings("light", object_words[1]);
|
||||
}
|
||||
|
||||
test "adaptiveParse no valid parse" {
|
||||
test "adaptiveCommandParse no valid parse" {
|
||||
var parser = try Parser.init(std.testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
|
@ -758,22 +754,20 @@ test "adaptiveParse no valid parse" {
|
|||
});
|
||||
|
||||
const sentence = "xyz abc def";
|
||||
const sentence_z = try std.testing.allocator.dupeZ(u8, sentence);
|
||||
defer std.testing.allocator.free(sentence_z);
|
||||
|
||||
// const ll = std.testing.log_level;
|
||||
// defer std.testing.log_level = ll;
|
||||
// std.testing.log_level = .debug;
|
||||
try std.testing.expectError(
|
||||
error.SentenceCreationFailed,
|
||||
parser.adaptiveParse(
|
||||
sentence_z,
|
||||
parser.adaptiveCommandParse(
|
||||
sentence,
|
||||
replacements,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
test "adaptiveParse with word replacement and null removal" {
|
||||
test "adaptiveCommandParse with word replacement and null removal" {
|
||||
var parser = try Parser.init(std.testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
|
@ -783,10 +777,8 @@ test "adaptiveParse with word replacement and null removal" {
|
|||
});
|
||||
|
||||
const sentence = "alexa turn on the kitchen lake";
|
||||
const sentence_z = try std.testing.allocator.dupeZ(u8, sentence);
|
||||
defer std.testing.allocator.free(sentence_z);
|
||||
|
||||
var tree = try parser.adaptiveParse(sentence_z, replacements);
|
||||
var tree = try parser.adaptiveCommandParse(sentence, replacements);
|
||||
defer tree.deinit();
|
||||
|
||||
const action_words = try tree.sentenceAction();
|
||||
|
|
Loading…
Add table
Reference in a new issue