81c363009acef20554311a922f009a8913e2e62e
[sheet.git] / tools / mkwordlist
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4
5 BEGIN { push @INC, '.' }
6 use Shiar_Sheet::DB;
7 use open ':std' => ':encoding(utf-8)';
8 my $db = Shiar_Sheet::DB->connect;
9
10 say 'use utf8;';
11
12 use Data::Dump 'pp';
13 my %rows;
14 if (my $lang = shift @ARGV) {
15         my %filter = (lang => $lang);
16         my $cols = "ref, array_to_string(form || alt, '/'), prio, id, sub";
17         %rows = $db->select(_word => $cols, \%filter)->map_arrays;
18         defined $_->[-1] or pop @$_ for values %rows;
19         $rows{''} = [
20                 (undef) x 3,
21                 [$db->select(word => 'id', {cat => undef, ref => undef})->flat]
22         ];
23         say pp \%rows
24                 =~ s/\\x\{([0-9A-F]+)\}/chr hex $1/ger;
25         exit;
26 }
27 else {
28         %rows = $db->select(_cat_words => "coalesce(id::text, ''), forms")->map;
29         $_ = [ map { [split /:/, $_, 3] } @{$_} ] for values %rows;
30         say pp \%rows;
31 }