diff options
| author | 2025-05-11 16:30:47 -0400 | |
|---|---|---|
| committer | 2025-05-15 15:31:15 -0400 | |
| commit | e3dbcc70688321e48ac31599105c51edac2736af (patch) | |
| tree | 49ce6f507d9f4c846cccc07f9ed2d942a0930a7a | |
| parent | Various small iterator improvements (diff) | |
| download | zg-e3dbcc70688321e48ac31599105c51edac2736af.tar.gz zg-e3dbcc70688321e48ac31599105c51edac2736af.tar.xz zg-e3dbcc70688321e48ac31599105c51edac2736af.zip | |
Add WordBreakPropertyData
Passes some simple lookup tests.
| -rw-r--r-- | build.zig | 28 | ||||
| -rw-r--r-- | codegen/wbp.zig | 146 | ||||
| -rw-r--r-- | src/WordBreak.zig | 102 |
3 files changed, 276 insertions, 0 deletions
| @@ -22,6 +22,15 @@ pub fn build(b: *std.Build) void { | |||
| 22 | const run_gbp_gen_exe = b.addRunArtifact(gbp_gen_exe); | 22 | const run_gbp_gen_exe = b.addRunArtifact(gbp_gen_exe); |
| 23 | const gbp_gen_out = run_gbp_gen_exe.addOutputFileArg("gbp.bin.z"); | 23 | const gbp_gen_out = run_gbp_gen_exe.addOutputFileArg("gbp.bin.z"); |
| 24 | 24 | ||
| 25 | const wbp_gen_exe = b.addExecutable(.{ | ||
| 26 | .name = "wbp", | ||
| 27 | .root_source_file = b.path("codegen/wbp.zig"), | ||
| 28 | .target = b.graph.host, | ||
| 29 | .optimize = .Debug, | ||
| 30 | }); | ||
| 31 | const run_wbp_gen_exe = b.addRunArtifact(wbp_gen_exe); | ||
| 32 | const wbp_gen_out = run_wbp_gen_exe.addOutputFileArg("wbp.bin.z"); | ||
| 33 | |||
| 25 | // Display width | 34 | // Display width |
| 26 | const cjk = b.option(bool, "cjk", "Ambiguous code points are wide (display width: 2).") orelse false; | 35 | const cjk = b.option(bool, "cjk", "Ambiguous code points are wide (display width: 2).") orelse false; |
| 27 | const options = b.addOptions(); | 36 | const options = b.addOptions(); |
| @@ -183,6 +192,7 @@ pub fn build(b: *std.Build) void { | |||
| 183 | const props_gen_out = run_props_gen_exe.addOutputFileArg("props.bin.z"); | 192 | const props_gen_out = run_props_gen_exe.addOutputFileArg("props.bin.z"); |
| 184 | 193 | ||
| 185 | // Modules we provide | 194 | // Modules we provide |
| 195 | |||
| 186 | // Code points | 196 | // Code points |
| 187 | const code_point = b.addModule("code_point", .{ | 197 | const code_point = b.addModule("code_point", .{ |
| 188 | .root_source_file = b.path("src/code_point.zig"), | 198 | .root_source_file = b.path("src/code_point.zig"), |
| @@ -215,6 +225,23 @@ pub fn build(b: *std.Build) void { | |||
| 215 | }); | 225 | }); |
| 216 | const grapheme_tr = b.addRunArtifact(grapheme_t); | 226 | const grapheme_tr = b.addRunArtifact(grapheme_t); |
| 217 | 227 | ||
| 228 | // Word Breaking | ||
| 229 | const word_break = b.addModule("WordBreak", .{ | ||
| 230 | .root_source_file = b.path("src/WordBreak.zig"), | ||
| 231 | .target = target, | ||
| 232 | .optimize = optimize, | ||
| 233 | }); | ||
| 234 | word_break.addAnonymousImport("wbp", .{ .root_source_file = wbp_gen_out }); | ||
| 235 | word_break.addImport("code_point", code_point); | ||
| 236 | |||
| 237 | const word_break_t = b.addTest(.{ | ||
| 238 | .name = "WordBreak", | ||
| 239 | .root_module = word_break, | ||
| 240 | .target = target, | ||
| 241 | .optimize = optimize, | ||
| 242 | }); | ||
| 243 | const word_break_tr = b.addRunArtifact(word_break_t); | ||
| 244 | |||
| 218 | // ASCII utilities | 245 | // ASCII utilities |
| 219 | const ascii = b.addModule("ascii", .{ | 246 | const ascii = b.addModule("ascii", .{ |
| 220 | .root_source_file = b.path("src/ascii.zig"), | 247 | .root_source_file = b.path("src/ascii.zig"), |
| @@ -452,6 +479,7 @@ pub fn build(b: *std.Build) void { | |||
| 452 | test_step.dependOn(&code_point_tr.step); | 479 | test_step.dependOn(&code_point_tr.step); |
| 453 | test_step.dependOn(&display_width_tr.step); | 480 | test_step.dependOn(&display_width_tr.step); |
| 454 | test_step.dependOn(&grapheme_tr.step); | 481 | test_step.dependOn(&grapheme_tr.step); |
| 482 | test_step.dependOn(&word_break_tr.step); | ||
| 455 | test_step.dependOn(&ascii_tr.step); | 483 | test_step.dependOn(&ascii_tr.step); |
| 456 | test_step.dependOn(&ccc_data_tr.step); | 484 | test_step.dependOn(&ccc_data_tr.step); |
| 457 | test_step.dependOn(&canon_data_tr.step); | 485 | test_step.dependOn(&canon_data_tr.step); |
diff --git a/codegen/wbp.zig b/codegen/wbp.zig new file mode 100644 index 0000000..741103e --- /dev/null +++ b/codegen/wbp.zig | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | |||
| 4 | const WordBreakProperty = enum(u5) { | ||
| 5 | none, | ||
| 6 | Double_Quote, | ||
| 7 | Single_Quote, | ||
| 8 | Hebrew_Letter, | ||
| 9 | CR, | ||
| 10 | LF, | ||
| 11 | Newline, | ||
| 12 | Extend, | ||
| 13 | Regional_Indicator, | ||
| 14 | Format, | ||
| 15 | Katakana, | ||
| 16 | ALetter, | ||
| 17 | MidLetter, | ||
| 18 | MidNum, | ||
| 19 | MidNumLet, | ||
| 20 | Numeric, | ||
| 21 | ExtendNumLet, | ||
| 22 | ZWJ, | ||
| 23 | WSegSpace, | ||
| 24 | }; | ||
| 25 | |||
| 26 | const block_size = 256; | ||
| 27 | const Block = [block_size]u5; | ||
| 28 | |||
| 29 | const BlockMap = std.HashMap( | ||
| 30 | Block, | ||
| 31 | u16, | ||
| 32 | struct { | ||
| 33 | pub fn hash(_: @This(), k: Block) u64 { | ||
| 34 | var hasher = std.hash.Wyhash.init(0); | ||
| 35 | std.hash.autoHashStrat(&hasher, k, .DeepRecursive); | ||
| 36 | return hasher.final(); | ||
| 37 | } | ||
| 38 | |||
| 39 | pub fn eql(_: @This(), a: Block, b: Block) bool { | ||
| 40 | return std.mem.eql(u5, &a, &b); | ||
| 41 | } | ||
| 42 | }, | ||
| 43 | std.hash_map.default_max_load_percentage, | ||
| 44 | ); | ||
| 45 | |||
| 46 | pub fn main() !void { | ||
| 47 | var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
| 48 | defer arena.deinit(); | ||
| 49 | const allocator = arena.allocator(); | ||
| 50 | |||
| 51 | var flat_map = std.AutoHashMap(u21, u5).init(allocator); | ||
| 52 | defer flat_map.deinit(); | ||
| 53 | |||
| 54 | var line_buf: [4096]u8 = undefined; | ||
| 55 | |||
| 56 | // Process HangulSyllableType.txt | ||
| 57 | var in_file = try std.fs.cwd().openFile("data/unicode/auxiliary/WordBreakProperty.txt", .{}); | ||
| 58 | defer in_file.close(); | ||
| 59 | var in_buf = std.io.bufferedReader(in_file.reader()); | ||
| 60 | const in_reader = in_buf.reader(); | ||
| 61 | |||
| 62 | while (try in_reader.readUntilDelimiterOrEof(&line_buf, '\n')) |line| { | ||
| 63 | if (line.len == 0 or line[0] == '#') continue; | ||
| 64 | |||
| 65 | const no_comment = if (std.mem.indexOfScalar(u8, line, '#')) |octo| line[0..octo] else line; | ||
| 66 | |||
| 67 | var field_iter = std.mem.tokenizeAny(u8, no_comment, "; "); | ||
| 68 | var current_code: [2]u21 = undefined; | ||
| 69 | |||
| 70 | var i: usize = 0; | ||
| 71 | while (field_iter.next()) |field| : (i += 1) { | ||
| 72 | switch (i) { | ||
| 73 | 0 => { | ||
| 74 | // Code point(s) | ||
| 75 | if (std.mem.indexOf(u8, field, "..")) |dots| { | ||
| 76 | current_code = .{ | ||
| 77 | try std.fmt.parseInt(u21, field[0..dots], 16), | ||
| 78 | try std.fmt.parseInt(u21, field[dots + 2 ..], 16), | ||
| 79 | }; | ||
| 80 | } else { | ||
| 81 | const code = try std.fmt.parseInt(u21, field, 16); | ||
| 82 | current_code = .{ code, code }; | ||
| 83 | } | ||
| 84 | }, | ||
| 85 | 1 => { | ||
| 86 | // WordBreak type | ||
| 87 | const st: WordBreakProperty = std.meta.stringToEnum(WordBreakProperty, field) orelse .none; | ||
| 88 | for (current_code[0]..current_code[1] + 1) |cp| try flat_map.put(@intCast(cp), @intFromEnum(st)); | ||
| 89 | }, | ||
| 90 | else => {}, | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | var blocks_map = BlockMap.init(allocator); | ||
| 96 | defer blocks_map.deinit(); | ||
| 97 | |||
| 98 | var stage1 = std.ArrayList(u16).init(allocator); | ||
| 99 | defer stage1.deinit(); | ||
| 100 | |||
| 101 | var stage2 = std.ArrayList(u5).init(allocator); | ||
| 102 | defer stage2.deinit(); | ||
| 103 | |||
| 104 | var block: Block = [_]u5{0} ** block_size; | ||
| 105 | var block_len: u16 = 0; | ||
| 106 | |||
| 107 | for (0..0x110000) |i| { | ||
| 108 | const cp: u21 = @intCast(i); | ||
| 109 | const st = flat_map.get(cp) orelse 0; | ||
| 110 | |||
| 111 | // Process block | ||
| 112 | block[block_len] = st; | ||
| 113 | block_len += 1; | ||
| 114 | |||
| 115 | if (block_len < block_size and cp != 0x10ffff) continue; | ||
| 116 | |||
| 117 | const gop = try blocks_map.getOrPut(block); | ||
| 118 | if (!gop.found_existing) { | ||
| 119 | gop.value_ptr.* = @intCast(stage2.items.len); | ||
| 120 | try stage2.appendSlice(&block); | ||
| 121 | } | ||
| 122 | |||
| 123 | try stage1.append(gop.value_ptr.*); | ||
| 124 | block_len = 0; | ||
| 125 | } | ||
| 126 | |||
| 127 | var args_iter = try std.process.argsWithAllocator(allocator); | ||
| 128 | defer args_iter.deinit(); | ||
| 129 | _ = args_iter.skip(); | ||
| 130 | const output_path = args_iter.next() orelse @panic("No output file arg!"); | ||
| 131 | |||
| 132 | const compressor = std.compress.flate.deflate.compressor; | ||
| 133 | var out_file = try std.fs.cwd().createFile(output_path, .{}); | ||
| 134 | defer out_file.close(); | ||
| 135 | var out_comp = try compressor(.raw, out_file.writer(), .{ .level = .best }); | ||
| 136 | const writer = out_comp.writer(); | ||
| 137 | |||
| 138 | const endian = builtin.cpu.arch.endian(); | ||
| 139 | try writer.writeInt(u16, @intCast(stage1.items.len), endian); | ||
| 140 | for (stage1.items) |i| try writer.writeInt(u16, i, endian); | ||
| 141 | |||
| 142 | try writer.writeInt(u16, @intCast(stage2.items.len), endian); | ||
| 143 | for (stage2.items) |i| try writer.writeInt(u8, i, endian); | ||
| 144 | |||
| 145 | try out_comp.flush(); | ||
| 146 | } | ||
diff --git a/src/WordBreak.zig b/src/WordBreak.zig new file mode 100644 index 0000000..9044740 --- /dev/null +++ b/src/WordBreak.zig | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | //! Word Breaking Algorithm. | ||
| 2 | |||
| 3 | const WordBreakProperty = enum(u5) { | ||
| 4 | none, | ||
| 5 | Double_Quote, | ||
| 6 | Single_Quote, | ||
| 7 | Hebrew_Letter, | ||
| 8 | CR, | ||
| 9 | LF, | ||
| 10 | Newline, | ||
| 11 | Extend, | ||
| 12 | Regional_Indicator, | ||
| 13 | Format, | ||
| 14 | Katakana, | ||
| 15 | ALetter, | ||
| 16 | MidLetter, | ||
| 17 | MidNum, | ||
| 18 | MidNumLet, | ||
| 19 | Numeric, | ||
| 20 | ExtendNumLet, | ||
| 21 | ZWJ, | ||
| 22 | WSegSpace, | ||
| 23 | }; | ||
| 24 | |||
| 25 | s1: []u16 = undefined, | ||
| 26 | s2: []u5 = undefined, | ||
| 27 | |||
| 28 | const WordBreak = @This(); | ||
| 29 | |||
| 30 | pub fn init(allocator: Allocator) Allocator.Error!WordBreak { | ||
| 31 | var wb: WordBreak = undefined; | ||
| 32 | try wb.setup(allocator); | ||
| 33 | return wb; | ||
| 34 | } | ||
| 35 | |||
| 36 | pub fn setup(wb: *WordBreak, allocator: Allocator) Allocator.Error!void { | ||
| 37 | wb.setupImpl(allocator) catch |err| { | ||
| 38 | switch (err) { | ||
| 39 | error.OutOfMemory => |e| return e, | ||
| 40 | else => unreachable, | ||
| 41 | } | ||
| 42 | }; | ||
| 43 | } | ||
| 44 | |||
| 45 | inline fn setupImpl(wb: *WordBreak, allocator: Allocator) !void { | ||
| 46 | const decompressor = compress.flate.inflate.decompressor; | ||
| 47 | const in_bytes = @embedFile("wbp"); | ||
| 48 | var in_fbs = std.io.fixedBufferStream(in_bytes); | ||
| 49 | var in_decomp = decompressor(.raw, in_fbs.reader()); | ||
| 50 | var reader = in_decomp.reader(); | ||
| 51 | |||
| 52 | const endian = builtin.cpu.arch.endian(); | ||
| 53 | |||
| 54 | const stage_1_len: u16 = try reader.readInt(u16, endian); | ||
| 55 | wb.s1 = try allocator.alloc(u16, stage_1_len); | ||
| 56 | errdefer allocator.free(wb.s1); | ||
| 57 | for (0..stage_1_len) |i| wb.s1[i] = try reader.readInt(u16, endian); | ||
| 58 | |||
| 59 | const stage_2_len: u16 = try reader.readInt(u16, endian); | ||
| 60 | wb.s2 = try allocator.alloc(u5, stage_2_len); | ||
| 61 | errdefer allocator.free(wb.s2); | ||
| 62 | for (0..stage_2_len) |i| wb.s2[i] = @intCast(try reader.readInt(u8, endian)); | ||
| 63 | var count_0: usize = 0; | ||
| 64 | for (wb.s2) |nyb| { | ||
| 65 | if (nyb == 0) count_0 += 1; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | pub fn deinit(wordbreak: *const WordBreak, allocator: mem.Allocator) void { | ||
| 70 | allocator.free(wordbreak.s1); | ||
| 71 | allocator.free(wordbreak.s2); | ||
| 72 | } | ||
| 73 | |||
| 74 | /// Returns the word break property type for `cp`. | ||
| 75 | pub fn breakProperty(wordbreak: *const WordBreak, cp: u21) WordBreakProperty { | ||
| 76 | return @enumFromInt(wordbreak.s2[wordbreak.s1[cp >> 8] + (cp & 0xff)]); | ||
| 77 | } | ||
| 78 | |||
| 79 | test "Word Break Properties" { | ||
| 80 | const wb = try WordBreak.init(testing.allocator); | ||
| 81 | defer wb.deinit(testing.allocator); | ||
| 82 | try testing.expectEqual(.CR, wb.breakProperty('\r')); | ||
| 83 | try testing.expectEqual(.LF, wb.breakProperty('\n')); | ||
| 84 | try testing.expectEqual(.Hebrew_Letter, wb.breakProperty('ש')); | ||
| 85 | try testing.expectEqual(.Katakana, wb.breakProperty('\u{30ff}')); | ||
| 86 | } | ||
| 87 | |||
| 88 | fn testAllocations(allocator: Allocator) !void { | ||
| 89 | const wb = try WordBreak.init(allocator); | ||
| 90 | wb.deinit(allocator); | ||
| 91 | } | ||
| 92 | |||
| 93 | test "allocation safety" { | ||
| 94 | try testing.checkAllAllocationFailures(testing.allocator, testAllocations, .{}); | ||
| 95 | } | ||
| 96 | |||
| 97 | const std = @import("std"); | ||
| 98 | const builtin = @import("builtin"); | ||
| 99 | const compress = std.compress; | ||
| 100 | const mem = std.mem; | ||
| 101 | const Allocator = mem.Allocator; | ||
| 102 | const testing = std.testing; | ||