| 1 | # Copyright (c) 2025-2026 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module to interact with the famibeib words | ||
| 6 | |||
| 7 | |||
| 8 | package Lingua::famibeib::Word; | ||
| 9 | |||
| 10 | use v5.16; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | |||
| 16 | our $VERSION = v0.03; | ||
| 17 | |||
| 18 | use parent qw(Data::Identifier::Interface::Simple Data::Identifier::Interface::Subobjects); | ||
| 19 | |||
| 20 | use constant { | ||
| 21 | _GENERATOR => Data::Identifier->new(uuid => 'e2afa39e-fd57-45f8-89fd-8662b275cc68')->register, | ||
| 22 | _GENERATOR_UINTEGER => Data::Identifier->new(uuid => '53863a15-68d4-448d-bd69-a9b19289a191')->register, | ||
| 23 | _GENERATOR_SINTEGER => Data::Identifier->new(uuid => 'e8aa9e01-8d37-4b4b-8899-42ca0a2a906f')->register, | ||
| 24 | }; | ||
| 25 | |||
| 26 | use overload ( | ||
| 27 | '""' => \&as_string, | ||
| 28 | 'eq' => sub { $_[0]->eq($_[1]) }, | ||
| 29 | 'ne' => sub { !$_[0]->eq($_[1]) }, | ||
| 30 | 'cmp' => sub { $_[0]->cmp($_[1]) }, | ||
| 31 | ); | ||
| 32 | |||
| 33 | my @_word_mora = qw( | ||
| 34 | ba be bi bo bu | ||
| 35 | fa fe fi fo fu | ||
| 36 | ka ke ki ko ku | ||
| 37 | la le li lo lu | ||
| 38 | ma me mi mo mu | ||
| 39 | sa se si so su | ||
| 40 | ta te ti to tu | ||
| 41 | ); | ||
| 42 | |||
| 43 | my %_word_mora; | ||
| 44 | { | ||
| 45 | my $i = 0; | ||
| 46 | %_word_mora = map {$_ => $i++} @_word_mora; | ||
| 47 | } | ||
| 48 | |||
| 49 | my %_modifier_resets = ( | ||
| 50 | eb => [qw(ib)], | ||
| 51 | ib => [qw(eb)], | ||
| 52 | ak => [qw(ek)], | ||
| 53 | ek => [qw(ak)], | ||
| 54 | ok => [qw(uk)], | ||
| 55 | uk => [qw(ok)], | ||
| 56 | al => [qw(il ol am em im)], | ||
| 57 | il => [qw(al ol am em im)], | ||
| 58 | ol => [qw(al il am em im)], | ||
| 59 | am => [qw(al il ol em im)], | ||
| 60 | em => [qw(al il ol am im)], | ||
| 61 | im => [qw(al il ol am em)], | ||
| 62 | ); | ||
| 63 | |||
| 64 | my %_registered_by_string; | ||
| 65 | my %_registered_by_uuid; | ||
| 66 | |||
| 67 | |||
| 68 | sub new { | ||
| 69 | my ($pkg, $type, $value, @opts) = @_; | ||
| 70 | my $self = bless {}, $pkg; | ||
| 71 | |||
| 72 | croak 'Stray options passed' if scalar @opts; | ||
| 73 | croak 'No type given' unless defined $type; | ||
| 74 | croak 'No value given' unless defined $value; | ||
| 75 | |||
| 76 | if ($type eq 'from') { | ||
| 77 | if (ref $value) { | ||
| 78 | if ($value->isa(__PACKAGE__)) { | ||
| 79 | # TODO: handle this when @opts are non-empty | ||
| 80 | return $value; | ||
| 81 | } elsif ($value->isa('Data::Identifier') || $value->isa('Data::Identifier::Interface::Simple') || $value->isa('Data::URIID::Base')) { | ||
| 82 | my $id = $value->as('Data::Identifier'); | ||
| 83 | my $generator = eval {$id->generator}; | ||
| 84 | my $request; | ||
| 85 | |||
| 86 | if (defined($generator) && $generator->eq(_GENERATOR) && defined($request = $id->request(default => undef, no_defaults => 1))) { | ||
| 87 | $type = 'string'; | ||
| 88 | $value = $request; | ||
| 89 | } elsif (defined($generator) && ($generator->eq(_GENERATOR_UINTEGER) || $generator->eq(_GENERATOR_SINTEGER)) && defined($request = $id->request(default => undef, no_defaults => 1))) { | ||
| 90 | $type = 'number'; | ||
| 91 | $value = $request; | ||
| 92 | } else { | ||
| 93 | if (defined(my $o = $_registered_by_uuid{$id->uuid})) { | ||
| 94 | if (scalar @opts) { | ||
| 95 | $type = 'string'; | ||
| 96 | $value = $o>as_string; | ||
| 97 | $self = $o; | ||
| 98 | } else { | ||
| 99 | return $o; | ||
| 100 | } | ||
| 101 | } else { | ||
| 102 | croak 'Unknown word (did you register it or a dictionary?): '.$value; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } elsif ($value =~ /^(-?)(0|[1-9][0-9]*)\z/) { | ||
| 107 | $type = 'number'; | ||
| 108 | } else { | ||
| 109 | $type = 'string'; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | if ($type eq 'number') { | ||
| 114 | if ($value =~ /^(-?)(0|[1-9][0-9]*)\z/) { | ||
| 115 | my ($neg, $val) = ($1, int($2)); | ||
| 116 | |||
| 117 | if ($val == 0) { | ||
| 118 | $value = 'beba'; | ||
| 119 | } else { | ||
| 120 | my @parts; | ||
| 121 | |||
| 122 | while ($val) { | ||
| 123 | my $p = $val % 35; | ||
| 124 | $val = int($val / 35); | ||
| 125 | |||
| 126 | push(@parts, $_word_mora[$p]); | ||
| 127 | } | ||
| 128 | |||
| 129 | $value = 'be'.join('', reverse @parts); | ||
| 130 | $value .= 'ub' if length $neg; | ||
| 131 | } | ||
| 132 | |||
| 133 | $type = 'string'; | ||
| 134 | } else { | ||
| 135 | croak 'Bad number: '.$value; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | if ($type eq 'string') { | ||
| 140 | $value = lc($value); | ||
| 141 | |||
| 142 | $value =~ s/^([bfklmst][aeiou])$/to$1al/; | ||
| 143 | |||
| 144 | if ($value =~ /^[bfklmst][aeiou](?:[bfklmst][aeiou]|[aeiou][bfklmst])+\z/m) { | ||
| 145 | my ($stem, $modifiers) = $value =~ /^((?:[bfklmst][aeiou])+)([aeiou][bfklmst].*)?\z/; | ||
| 146 | |||
| 147 | if ($stem eq $value) { | ||
| 148 | # This is a stem | ||
| 149 | $self->{stem} = ':self'; | ||
| 150 | } else { | ||
| 151 | $self->{stem} = $stem; | ||
| 152 | } | ||
| 153 | |||
| 154 | if (defined($modifiers)) { | ||
| 155 | my %mod; | ||
| 156 | my $plural; | ||
| 157 | |||
| 158 | while (length($modifiers)) { | ||
| 159 | my $mod; | ||
| 160 | |||
| 161 | croak 'Invalid word: '.$value unless $modifiers =~ s/^([aeiou][bfklmst])//; | ||
| 162 | $mod = $1; | ||
| 163 | |||
| 164 | if ($mod eq 'ab') { | ||
| 165 | # plural | ||
| 166 | if ($modifiers =~ /^(?:[bfklmst][aeiou])*\z/) { | ||
| 167 | $plural = $mod.$modifiers; | ||
| 168 | $modifiers = ''; | ||
| 169 | } else { | ||
| 170 | croak 'Unsupported plural: -ab'.$modifiers; | ||
| 171 | } | ||
| 172 | } elsif ($mod eq 'af' || $mod eq 'ef') { | ||
| 173 | croak 'Modifiers af/ef not supported: '.$value; | ||
| 174 | } else { | ||
| 175 | $mod{$mod} //= 0; | ||
| 176 | $mod{$mod}++; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | $modifiers = ''; | ||
| 181 | $self->{modifiers} = {}; | ||
| 182 | foreach my $mod (sort keys %mod) { | ||
| 183 | $mod = $mod x $mod{$mod}; | ||
| 184 | $self->{modifiers}{$mod} = undef; | ||
| 185 | $modifiers .= $mod; | ||
| 186 | } | ||
| 187 | |||
| 188 | if (defined($plural) && length($plural)) { | ||
| 189 | $self->{modifiers}{$plural} = undef; | ||
| 190 | $modifiers .= $plural; | ||
| 191 | } | ||
| 192 | |||
| 193 | $value = $stem.$modifiers; | ||
| 194 | } | ||
| 195 | |||
| 196 | $self = $_registered_by_string{$value} // $self; | ||
| 197 | $self->{string} = $value; | ||
| 198 | } else { | ||
| 199 | croak 'Bad string: '.$value; | ||
| 200 | } | ||
| 201 | } else { | ||
| 202 | croak 'Bad type: '.$type; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | return $self; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | sub combine { | ||
| 211 | my ($pkg, $word, @parts) = @_; | ||
| 212 | |||
| 213 | require Lingua::famibeib::Modifier; | ||
| 214 | |||
| 215 | if (ref($pkg)) { | ||
| 216 | unshift(@parts, $word); | ||
| 217 | $word = $pkg; | ||
| 218 | $pkg = __PACKAGE__; | ||
| 219 | } | ||
| 220 | |||
| 221 | $word = $pkg->new(from => $word); | ||
| 222 | |||
| 223 | $_ = Lingua::famibeib::Modifier->new(from => $_) foreach @parts; | ||
| 224 | |||
| 225 | unshift(@parts, $word->modifiers); | ||
| 226 | $word = $word->stem; | ||
| 227 | |||
| 228 | { | ||
| 229 | my $plural; | ||
| 230 | my %mod; | ||
| 231 | |||
| 232 | foreach my $mod (@parts) { | ||
| 233 | my $master_mora = $mod->master_mora; | ||
| 234 | |||
| 235 | if ($master_mora eq 'ab') { | ||
| 236 | $plural = $mod; | ||
| 237 | next; | ||
| 238 | } | ||
| 239 | |||
| 240 | if (defined(my $reset = $_modifier_resets{$master_mora})) { | ||
| 241 | delete $mod{$_} foreach @{$reset}; | ||
| 242 | } | ||
| 243 | |||
| 244 | $mod{$master_mora} = $mod; | ||
| 245 | } | ||
| 246 | |||
| 247 | @parts = map {$mod{$_}->as_string} sort keys %mod; | ||
| 248 | |||
| 249 | push(@parts, $plural->as_string) if defined $plural; | ||
| 250 | } | ||
| 251 | |||
| 252 | return $pkg->new(string => sprintf('%s%s', $word->as_string, join('', @parts))); | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | sub as_string { | ||
| 257 | my ($self) = @_; | ||
| 258 | return $self->{string}; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | sub as_number { | ||
| 263 | my ($self) = @_; | ||
| 264 | |||
| 265 | if ($self->as_string =~ /^be((?:[bfklmst][aeiou])+)((?:ub)?)\z/) { | ||
| 266 | my ($mora, $neg) = ($1, $2); | ||
| 267 | my $value = 0; | ||
| 268 | |||
| 269 | while ($mora =~ /([bfklmst][aeiou])/g) { | ||
| 270 | $value *= 35; | ||
| 271 | $value += $_word_mora{$1}; | ||
| 272 | } | ||
| 273 | |||
| 274 | return length($neg) ? -$value : $value; | ||
| 275 | } elsif ($self->as_string =~ /^be/) { | ||
| 276 | croak 'Not a simple number'; | ||
| 277 | } else { | ||
| 278 | croak 'Not a number'; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | sub stem { | ||
| 284 | my ($self, @opts) = @_; | ||
| 285 | my $stem = $self->{stem}; | ||
| 286 | |||
| 287 | croak 'Stray options passed' if scalar @opts; | ||
| 288 | |||
| 289 | return $stem if ref $stem; | ||
| 290 | return $self if $stem eq ':self'; | ||
| 291 | return $self->{stem} = __PACKAGE__->new(string => $stem); | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | sub modifiers { | ||
| 296 | my ($self, @opts) = @_; | ||
| 297 | my $modifiers = $self->{modifiers}; | ||
| 298 | |||
| 299 | croak 'Stray options passed' if scalar @opts; | ||
| 300 | |||
| 301 | require Lingua::famibeib::Modifier; | ||
| 302 | |||
| 303 | return map {$modifiers->{$_} //= Lingua::famibeib::Modifier->new(string => $_)} keys %{$modifiers}; | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | sub get_modifier_by_master_mora { | ||
| 308 | my ($self, $mora, %opts) = @_; | ||
| 309 | my $has_default = exists $opts{default}; | ||
| 310 | my $default = delete $opts{default}; | ||
| 311 | |||
| 312 | croak 'Stray options passed' if scalar keys %opts; | ||
| 313 | |||
| 314 | require Lingua::famibeib::Modifier; | ||
| 315 | |||
| 316 | $mora = Lingua::famibeib::Modifier->new(from => $mora)->master_mora; | ||
| 317 | |||
| 318 | foreach my $modifier ($self->modifiers) { | ||
| 319 | return $modifier if $mora eq $modifier->master_mora; | ||
| 320 | } | ||
| 321 | |||
| 322 | return $default if $has_default; | ||
| 323 | |||
| 324 | croak 'No such modifier'; | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | sub eq { | ||
| 329 | my ($self, $other, @opts) = @_; | ||
| 330 | |||
| 331 | croak 'Stray options passed' if scalar @opts; | ||
| 332 | |||
| 333 | return 1 if !defined($self) && !defined($other); | ||
| 334 | return undef unless defined($self) && defined($other); | ||
| 335 | |||
| 336 | $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)}; | ||
| 337 | $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)}; | ||
| 338 | |||
| 339 | return $self->as_string eq $other->as_string; | ||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | sub cmp { | ||
| 344 | my ($self, $other, @opts) = @_; | ||
| 345 | |||
| 346 | croak 'Stray options passed' if scalar @opts; | ||
| 347 | |||
| 348 | return 1 if !defined($self) && !defined($other); | ||
| 349 | return undef unless defined($self) && defined($other); | ||
| 350 | |||
| 351 | $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)}; | ||
| 352 | $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)}; | ||
| 353 | |||
| 354 | { | ||
| 355 | my $stem_self = $self->stem; | ||
| 356 | my $stem_other = $other->stem; | ||
| 357 | |||
| 358 | if (!$self->eq($stem_self) || !$other->eq($stem_other)) { | ||
| 359 | my $res = $stem_self->cmp($stem_other); | ||
| 360 | |||
| 361 | return $res if $res != 0; | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | { | ||
| 366 | my $str_self = $self->as_string; | ||
| 367 | my $str_other = $other->as_string; | ||
| 368 | |||
| 369 | return $str_self cmp $str_other; | ||
| 370 | } | ||
| 371 | |||
| 372 | croak 'BUG!'; | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | sub is_verb { | ||
| 377 | my ($self, @opts) = @_; | ||
| 378 | |||
| 379 | croak 'Stray options passed' if scalar @opts; | ||
| 380 | |||
| 381 | return !!$self->get_modifier_by_master_mora('ol', default => undef); | ||
| 382 | } | ||
| 383 | |||
| 384 | |||
| 385 | sub is_negative { | ||
| 386 | my ($self, @opts) = @_; | ||
| 387 | |||
| 388 | croak 'Stray options passed' if scalar @opts; | ||
| 389 | |||
| 390 | return !!$self->get_modifier_by_master_mora('ub', default => undef); | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | sub is_application { | ||
| 395 | my ($self, @opts) = @_; | ||
| 396 | |||
| 397 | croak 'Stray options passed' if scalar @opts; | ||
| 398 | |||
| 399 | return !!$self->get_modifier_by_master_mora('al', default => undef); | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | sub register { | ||
| 404 | my ($self) = @_; | ||
| 405 | |||
| 406 | $_registered_by_string{$self->as_string} //= $self; | ||
| 407 | $_registered_by_uuid{$self->as('uuid')} //= $self; | ||
| 408 | |||
| 409 | if (ref($self->{stem}) || $self->{stem} ne ':self') { | ||
| 410 | $self->stem->register; | ||
| 411 | } | ||
| 412 | |||
| 413 | return $self; | ||
| 414 | } | ||
| 415 | |||
| 416 | # ---- Private helpers ---- | ||
| 417 | |||
| 418 | sub as { | ||
| 419 | my ($self, $as, @opts) = @_; | ||
| 420 | my $id = $self->{id} //= do { | ||
| 421 | require Data::Identifier::Generate; | ||
| 422 | my $str = $self->as_string; | ||
| 423 | |||
| 424 | Data::Identifier::Generate->generic( | ||
| 425 | request => $str, | ||
| 426 | displayname => $str, | ||
| 427 | style => 'id-based', | ||
| 428 | namespace => '10ce38bf-6238-4ed7-96ef-98ea9642a4c6', | ||
| 429 | generator => _GENERATOR, | ||
| 430 | ) | ||
| 431 | }; | ||
| 432 | |||
| 433 | return $id if $as eq 'Data::Identifier' && scalar(@opts) == 0; | ||
| 434 | |||
| 435 | return $id->as($as, @opts); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub displayname { | ||
| 439 | my ($self, @opts) = @_; | ||
| 440 | return $self->as_string if scalar(@opts) == 0; | ||
| 441 | return $self->as('Data::Identifier')->displayname(@opts); | ||
| 442 | } | ||
| 443 | |||
| 444 | 1; | ||
| 445 | |||
| 446 | __END__ |