diff --git a/src/main.zig b/src/main.zig index 973d8c3..d2db292 100644 --- a/src/main.zig +++ b/src/main.zig @@ -533,6 +533,28 @@ test "processCommand with word replacement like to light" { try std.testing.expectEqualStrings("192.168.1.101", test_device_entry.value_ptr.*); try std.testing.expectEqual(DeviceAction.off, test_action); } +test "processCommand with word replacement like to light - three words" { + var devices = std.StringHashMap([]const u8).init(std.testing.allocator); + defer devices.deinit(); + + if (true) return error.SkipZigTest; + try devices.put("jack bedroom light", "192.168.1.101"); + + var parser = try pos.Parser.init(std.testing.allocator); + defer parser.deinit(); + + const sentence = "turn off jack bedroom like"; + const sentence_z = try std.testing.allocator.dupeZ(u8, sentence); + defer std.testing.allocator.free(sentence_z); + + const ll = std.testing.log_level; + std.testing.log_level = .debug; + defer std.testing.log_level = ll; + try processCommand(std.testing.allocator, sentence_z, &parser, &devices); + + try std.testing.expectEqualStrings("192.168.1.101", test_device_entry.value_ptr.*); + try std.testing.expectEqual(DeviceAction.off, test_action); +} test "processCommand no match found" { var devices = std.StringHashMap([]const u8).init(std.testing.allocator); diff --git a/src/root.zig b/src/root.zig index f8745b7..11c75f6 100644 --- a/src/root.zig +++ b/src/root.zig @@ -45,6 +45,7 @@ pub const ParseTree = struct { if (std.mem.endsWith(u8, word, ".v")) { // adjective or noun for (self.links) |l| { + // This should be looking through the consituent tree // We're looking for a modifying verb, see: // https://www.link.cs.cmu.edu/link/dict/section-MV.html if (@as(usize, l.left_index) == i and @@ -71,6 +72,7 @@ pub const ParseTree = struct { pub fn sentenceObject(self: *ParseTree) ![][]const u8 { var al: std.ArrayList([]const u8) = .{}; defer al.deinit(self.allocator); + // var noun: ?usize = null; for (self.words, 0..) |word, i| { if (std.mem.endsWith(u8, word, ".n")) { // adjective or noun @@ -96,6 +98,21 @@ pub const ParseTree = struct { } return al.toOwnedSlice(self.allocator); } + + // TODO: This should be a format function + pub fn printTree(self: *const ParseTree, writer: *std.Io.Writer) !void { + try writer.writeAll("Parse Tree:\n"); + try writer.writeAll("Words: "); + for (self.words) |word| { + try writer.print("'{s}' ", .{word}); + } + try writer.print("\n\nLinks ({} total):\n", .{self.links.len}); + + for (self.links, 0..) |link, i| { + try writer.print(" [{d}] '{s}' --{s}--> '{s}'\n", .{ i, link.left_word, link.label, link.right_word }); + } + try writer.writeAll("\n"); + } }; pub const Parser = struct { @@ -295,3 +312,28 @@ test "real usage" { try std.testing.expectEqualStrings("bedroom", sentence_object[0]); try std.testing.expectEqualStrings("light", sentence_object[1]); } +test "real usage - jack" { + var parser = try Parser.init(std.testing.allocator); + defer parser.deinit(); + + if (true) return error.SkipZigTest; + var tree = try parser.parse("turn on jack bedroom light"); + defer tree.deinit(); + + var stderr_writer = std.fs.File.stderr().writer(&.{}); + const stderr = &stderr_writer.interface; + try tree.printTree(stderr); // well, this is stupid. We shall fix later + try std.testing.expect(tree.words.len == 7); // 5 + LEFT_WALL / RIGHT_WALL + try std.testing.expectEqualStrings("turn", tree.firstVerb().?); + const sentence_action = try tree.sentenceAction(); + defer std.testing.allocator.free(sentence_action); + try std.testing.expect(sentence_action.len == 2); + try std.testing.expectEqualStrings("turn", sentence_action[0]); + try std.testing.expectEqualStrings("on", sentence_action[1]); + const sentence_object = try tree.sentenceObject(); + defer std.testing.allocator.free(sentence_object); + try std.testing.expect(sentence_object.len == 3); + try std.testing.expectEqualStrings("jack", sentence_object[0]); + try std.testing.expectEqualStrings("bedroom", sentence_object[1]); + try std.testing.expectEqualStrings("light", sentence_object[2]); +}