this repo has no description
at main 48 lines 1.5 kB view raw
1//! demo: token attribute extraction and parser state machine transitions. 2 3const std = @import("std"); 4const spacez = @import("spacez"); 5 6pub fn main() !void { 7 const print = std.debug.print; 8 9 // demo: extract token attributes 10 const tokens = [_][]const u8{ "Barack", "Obama", "visited", "Paris" }; 11 12 print("spacez token attributes:\n", .{}); 13 for (tokens) |token| { 14 const attrs = spacez.extractAttrs(token); 15 var shape_buf: [64]u8 = undefined; 16 const shape = spacez.computeShape(token, &shape_buf); 17 print(" {s:>10} norm={x:0>16} shape={s}\n", .{ 18 token, attrs.norm, shape, 19 }); 20 } 21 22 // demo: parser state machine (without model weights, just show transitions) 23 print("\nparser state machine demo:\n", .{}); 24 var state = spacez.parser.State.init(4); 25 26 const actions = [_]struct { a: spacez.parser.Action, l: ?spacez.Label }{ 27 .{ .a = .BEGIN, .l = .PERSON }, 28 .{ .a = .LAST, .l = .PERSON }, 29 .{ .a = .OUT, .l = null }, 30 .{ .a = .UNIT, .l = .GPE }, 31 }; 32 33 for (actions, 0..) |act, i| { 34 const valid = state.isValid(act.a, act.l); 35 print(" step {d}: {s}-{s} valid={}\n", .{ 36 i, 37 @tagName(act.a), 38 if (act.l) |l| @tagName(l) else "(none)", 39 valid, 40 }); 41 if (valid) state.apply(act.a, act.l); 42 } 43 44 print("\nentities found:\n", .{}); 45 for (state.entities()) |e| { 46 print(" [{d}..{d}) {s}\n", .{ e.start, e.end, @tagName(e.label) }); 47 } 48}