| 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.08; | ||
| 17 | |||
| 18 | use parent qw(Lingua::Generic::Interface::Word); | ||
| 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 | my @_word_mora = qw( | ||
| 27 | ba be bi bo bu | ||
| 28 | fa fe fi fo fu | ||
| 29 | ka ke ki ko ku | ||
| 30 | la le li lo lu | ||
| 31 | ma me mi mo mu | ||
| 32 | sa se si so su | ||
| 33 | ta te ti to tu | ||
| 34 | ); | ||
| 35 | |||
| 36 | my %_word_mora; | ||
| 37 | { | ||
| 38 | my $i = 0; | ||
| 39 | %_word_mora = map {$_ => $i++} @_word_mora; | ||
| 40 | } | ||
| 41 | |||
| 42 | my %_modifier_resets = ( | ||
| 43 | eb => [qw(ib)], | ||
| 44 | ib => [qw(eb)], | ||
| 45 | ak => [qw(ek)], | ||
| 46 | ek => [qw(ak)], | ||
| 47 | ok => [qw(uk)], | ||
| 48 | uk => [qw(ok)], | ||
| 49 | al => [qw(il ol am em im)], | ||
| 50 | il => [qw(al ol am em im)], | ||
| 51 | ol => [qw(al il am em im)], | ||
| 52 | am => [qw(al il ol em im)], | ||
| 53 | em => [qw(al il ol am im)], | ||
| 54 | im => [qw(al il ol am em)], | ||
| 55 | ); | ||
| 56 | |||
| 57 | my %_registered_by_string; | ||
| 58 | my %_registered_by_uuid; | ||
| 59 | |||
| 60 | |||
| 61 | sub new { | ||
| 62 | my ($pkg, $type, $value, @opts) = @_; | ||
| 63 | my $self = bless {}, $pkg; | ||
| 64 | |||
| 65 | croak 'Stray options passed' if scalar @opts; | ||
| 66 | croak 'No type given' unless defined $type; | ||
| 67 | croak 'No value given' unless defined $value; | ||
| 68 | |||
| 69 | if ($type eq 'from') { | ||
| 70 | if (ref $value) { | ||
| 71 | if ($value->isa(__PACKAGE__)) { | ||
| 72 | # TODO: handle this when @opts are non-empty | ||
| 73 | return $value; | ||
| 74 | } elsif ($value->isa('Lingua::TokiPona::Word')) { | ||
| 75 | $type = 'string'; | ||
| 76 | $value = 'to'.$value->as_string; | ||
| 77 | } elsif ($value->isa('Data::Identifier') || $value->isa('Data::Identifier::Interface::Simple') || $value->isa('Data::URIID::Base')) { | ||
| 78 | my $id = $value->as('Data::Identifier'); | ||
| 79 | my $generator = eval {$id->generator}; | ||
| 80 | my $request; | ||
| 81 | |||
| 82 | if (defined($generator) && $generator->eq(_GENERATOR) && defined($request = $id->request(default => undef, no_defaults => 1))) { | ||
| 83 | $type = 'string'; | ||
| 84 | $value = $request; | ||
| 85 | } elsif (defined($generator) && ($generator->eq(_GENERATOR_UINTEGER) || $generator->eq(_GENERATOR_SINTEGER)) && defined($request = $id->request(default => undef, no_defaults => 1))) { | ||
| 86 | $type = 'number'; | ||
| 87 | $value = $request; | ||
| 88 | } else { | ||
| 89 | if (defined(my $o = $_registered_by_uuid{$id->uuid})) { | ||
| 90 | if (scalar @opts) { | ||
| 91 | $type = 'string'; | ||
| 92 | $value = $o->as_string; | ||
| 93 | $self = $o; | ||
| 94 | } else { | ||
| 95 | return $o; | ||
| 96 | } | ||
| 97 | } else { | ||
| 98 | croak 'Unknown word (did you register it or a dictionary?): '.$value; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } elsif ($value =~ /^(-?)(0|[1-9][0-9]*)\z/) { | ||
| 103 | $type = 'number'; | ||
| 104 | } else { | ||
| 105 | $type = 'string'; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | if ($type eq 'number') { | ||
| 110 | if ($value =~ /^(-?)(0|[1-9][0-9]*)\z/) { | ||
| 111 | my ($neg, $val) = ($1, int($2)); | ||
| 112 | |||
| 113 | if ($val == 0) { | ||
| 114 | $value = 'beba'; | ||
| 115 | } else { | ||
| 116 | my @parts; | ||
| 117 | |||
| 118 | while ($val) { | ||
| 119 | my $p = $val % 35; | ||
| 120 | $val = int($val / 35); | ||
| 121 | |||
| 122 | push(@parts, $_word_mora[$p]); | ||
| 123 | } | ||
| 124 | |||
| 125 | $value = 'be'.join('', reverse @parts); | ||
| 126 | $value .= 'ub' if length $neg; | ||
| 127 | } | ||
| 128 | |||
| 129 | $type = 'string'; | ||
| 130 | } else { | ||
| 131 | croak 'Bad number: '.$value; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | if ($type eq 'string') { | ||
| 136 | if ($value =~ /[\s\.,\!\?]/) { | ||
| 137 | croak 'Multi word string passed. Consider using Lingua::famibeib::Text if this is what you wanted'; | ||
| 138 | } | ||
| 139 | |||
| 140 | if ($value =~ /^to./i) { | ||
| 141 | require Lingua::famibeib::ForeignWord; | ||
| 142 | return Lingua::famibeib::ForeignWord->new(string => $value, @opts); | ||
| 143 | } | ||
| 144 | |||
| 145 | $value = lc($value); | ||
| 146 | |||
| 147 | $value =~ s/^([bfklmst][aeiou])$/tu$1al/; | ||
| 148 | |||
| 149 | if ($value =~ /^[bfklmst][aeiou](?:[bfklmst][aeiou]|[aeiou][bfklmst])+\z/m) { | ||
| 150 | my ($stem, $modifiers) = $value =~ /^((?:[bfklmst][aeiou])+)([aeiou][bfklmst].*)?\z/; | ||
| 151 | |||
| 152 | if ($stem eq $value) { | ||
| 153 | # This is a stem | ||
| 154 | $self->{stem} = ':self'; | ||
| 155 | } else { | ||
| 156 | $self->{stem} = $stem; | ||
| 157 | } | ||
| 158 | |||
| 159 | if (defined($modifiers)) { | ||
| 160 | my %mod; | ||
| 161 | my $plural; | ||
| 162 | |||
| 163 | while (length($modifiers)) { | ||
| 164 | my $mod; | ||
| 165 | |||
| 166 | croak 'Invalid word: '.$value unless $modifiers =~ s/^([aeiou][bfklmst])//; | ||
| 167 | $mod = $1; | ||
| 168 | |||
| 169 | if ($mod eq 'ab') { | ||
| 170 | # plural | ||
| 171 | if ($modifiers =~ /^(?:[bfklmst][aeiou])*\z/) { | ||
| 172 | $plural = $mod.$modifiers; | ||
| 173 | $modifiers = ''; | ||
| 174 | } else { | ||
| 175 | croak 'Unsupported plural: -ab'.$modifiers; | ||
| 176 | } | ||
| 177 | } elsif ($mod eq 'af' || $mod eq 'ef') { | ||
| 178 | croak 'Modifiers af/ef not supported: '.$value; | ||
| 179 | } else { | ||
| 180 | $mod{$mod} //= 0; | ||
| 181 | $mod{$mod}++; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | $modifiers = ''; | ||
| 186 | $self->{modifiers} = {}; | ||
| 187 | foreach my $mod (sort keys %mod) { | ||
| 188 | $mod = $mod x $mod{$mod}; | ||
| 189 | $self->{modifiers}{$mod} = undef; | ||
| 190 | $modifiers .= $mod; | ||
| 191 | } | ||
| 192 | |||
| 193 | if (defined($plural) && length($plural)) { | ||
| 194 | $self->{modifiers}{$plural} = undef; | ||
| 195 | $modifiers .= $plural; | ||
| 196 | } | ||
| 197 | |||
| 198 | $value = $stem.$modifiers; | ||
| 199 | } | ||
| 200 | |||
| 201 | $self = $_registered_by_string{$value} // $self; | ||
| 202 | $self->{string} = $value; | ||
| 203 | } else { | ||
| 204 | croak 'Bad string: '.$value; | ||
| 205 | } | ||
| 206 | } else { | ||
| 207 | croak 'Bad type: '.$type; | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | return $self; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | sub combine { | ||
| 216 | my ($pkg, $word, @parts) = @_; | ||
| 217 | |||
| 218 | require Lingua::famibeib::Modifier; | ||
| 219 | |||
| 220 | if (ref($pkg)) { | ||
| 221 | unshift(@parts, $word); | ||
| 222 | $word = $pkg; | ||
| 223 | $pkg = __PACKAGE__; | ||
| 224 | } | ||
| 225 | |||
| 226 | $word = $pkg->new(from => $word); | ||
| 227 | |||
| 228 | $_ = Lingua::famibeib::Modifier->new(from => $_) foreach @parts; | ||
| 229 | |||
| 230 | unshift(@parts, $word->modifiers); | ||
| 231 | $word = $word->stem; | ||
| 232 | |||
| 233 | { | ||
| 234 | my $plural; | ||
| 235 | my %mod; | ||
| 236 | |||
| 237 | foreach my $mod (@parts) { | ||
| 238 | my $master_mora = $mod->master_mora; | ||
| 239 | |||
| 240 | if ($master_mora eq 'ab') { | ||
| 241 | $plural = $mod; | ||
| 242 | next; | ||
| 243 | } | ||
| 244 | |||
| 245 | if (defined(my $reset = $_modifier_resets{$master_mora})) { | ||
| 246 | delete $mod{$_} foreach @{$reset}; | ||
| 247 | } | ||
| 248 | |||
| 249 | $mod{$master_mora} = $mod; | ||
| 250 | } | ||
| 251 | |||
| 252 | @parts = map {$mod{$_}->as_string} sort keys %mod; | ||
| 253 | |||
| 254 | push(@parts, $plural->as_string) if defined $plural; | ||
| 255 | } | ||
| 256 | |||
| 257 | return $pkg->new(string => sprintf('%s%s', $word->as_string, join('', @parts))); | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | # NOTE: Implemented via Lingua::Generic::Interface::Word | ||
| 262 | |||
| 263 | |||
| 264 | sub as_number { | ||
| 265 | my ($self, $as) = @_; | ||
| 266 | |||
| 267 | $as //= 'int'; | ||
| 268 | |||
| 269 | if ($self->as_string =~ /^be((?:[bfklmst][aeiou])+)((?:ub)?)\z/) { | ||
| 270 | my ($mora, $neg) = ($1, $2); | ||
| 271 | |||
| 272 | if ($as eq 'int' && length($mora) <= 12) { | ||
| 273 | my $value = 0; | ||
| 274 | |||
| 275 | while ($mora =~ /([bfklmst][aeiou])/g) { | ||
| 276 | $value *= 35; | ||
| 277 | $value += $_word_mora{$1}; | ||
| 278 | } | ||
| 279 | |||
| 280 | return length($neg) ? -$value : $value; | ||
| 281 | } elsif ($as eq 'Math::BigInt' || $as eq 'Data::Identifier') { | ||
| 282 | require Math::BigInt; | ||
| 283 | |||
| 284 | my $value = Math::BigInt->bzero; | ||
| 285 | |||
| 286 | while ($mora =~ /([bfklmst][aeiou])/g) { | ||
| 287 | $value->bmul(35); | ||
| 288 | $value->badd($_word_mora{$1}); | ||
| 289 | } | ||
| 290 | |||
| 291 | $value = length($neg) ? $value->bneg : $value; | ||
| 292 | |||
| 293 | if ($as eq 'Data::Identifier') { | ||
| 294 | require Data::Identifier::Generate; | ||
| 295 | $value = Data::Identifier::Generate->integer($value); | ||
| 296 | } | ||
| 297 | |||
| 298 | return $value; | ||
| 299 | } else { | ||
| 300 | croak 'Invalid/Unsupported (as, value)-pair; value too large?'; | ||
| 301 | } | ||
| 302 | } elsif ($self->as_string =~ /^be/) { | ||
| 303 | croak 'Not a simple number'; | ||
| 304 | } else { | ||
| 305 | croak 'Not a number'; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | #@returns __PACKAGE__ | ||
| 311 | sub stem { | ||
| 312 | my ($self, @opts) = @_; | ||
| 313 | my $stem = $self->{stem}; | ||
| 314 | |||
| 315 | croak 'Stray options passed' if scalar @opts; | ||
| 316 | |||
| 317 | return $stem if ref $stem; | ||
| 318 | return $self if $stem eq ':self'; | ||
| 319 | return $self->{stem} = __PACKAGE__->new(string => $stem); | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | sub modifiers { | ||
| 324 | my ($self, @opts) = @_; | ||
| 325 | my $modifiers = $self->{modifiers}; | ||
| 326 | |||
| 327 | croak 'Stray options passed' if scalar @opts; | ||
| 328 | |||
| 329 | require Lingua::famibeib::Modifier; | ||
| 330 | |||
| 331 | return map {$modifiers->{$_} //= Lingua::famibeib::Modifier->new(string => $_)} keys %{$modifiers}; | ||
| 332 | } | ||
| 333 | |||
| 334 | |||
| 335 | #@returns Lingua::famibeib::Modifier | ||
| 336 | sub get_modifier_by_master_mora { | ||
| 337 | my ($self, $mora, %opts) = @_; | ||
| 338 | my $has_default = exists $opts{default}; | ||
| 339 | my $default = delete $opts{default}; | ||
| 340 | |||
| 341 | croak 'Stray options passed' if scalar keys %opts; | ||
| 342 | |||
| 343 | require Lingua::famibeib::Modifier; | ||
| 344 | |||
| 345 | $mora = Lingua::famibeib::Modifier->new(from => $mora)->master_mora; | ||
| 346 | |||
| 347 | foreach my $modifier ($self->modifiers) { | ||
| 348 | return $modifier if $mora eq $modifier->master_mora; | ||
| 349 | } | ||
| 350 | |||
| 351 | return $default if $has_default; | ||
| 352 | |||
| 353 | croak 'No such modifier'; | ||
| 354 | } | ||
| 355 | |||
| 356 | |||
| 357 | # NOTE: Implemented via Lingua::Generic::Interface::Word | ||
| 358 | |||
| 359 | |||
| 360 | sub cmp { | ||
| 361 | my ($self, $other, @opts) = @_; | ||
| 362 | |||
| 363 | croak 'Stray options passed' if scalar @opts; | ||
| 364 | |||
| 365 | return 1 if !defined($self) && !defined($other); | ||
| 366 | return undef unless defined($self) && defined($other); | ||
| 367 | |||
| 368 | $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)}; | ||
| 369 | $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)}; | ||
| 370 | |||
| 371 | { | ||
| 372 | my $stem_self = $self->stem; | ||
| 373 | my $stem_other = $other->stem; | ||
| 374 | |||
| 375 | if (!$self->eq($stem_self) || !$other->eq($stem_other)) { | ||
| 376 | my $res = $stem_self->cmp($stem_other); | ||
| 377 | |||
| 378 | return $res if $res != 0; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 382 | return $self->SUPER::cmp($other); | ||
| 383 | } | ||
| 384 | |||
| 385 | |||
| 386 | sub is_verb { | ||
| 387 | my ($self, @opts) = @_; | ||
| 388 | |||
| 389 | croak 'Stray options passed' if scalar @opts; | ||
| 390 | |||
| 391 | return !!$self->get_modifier_by_master_mora('ol', default => undef); | ||
| 392 | } | ||
| 393 | |||
| 394 | |||
| 395 | sub is_negative { | ||
| 396 | my ($self, @opts) = @_; | ||
| 397 | |||
| 398 | croak 'Stray options passed' if scalar @opts; | ||
| 399 | |||
| 400 | return !!$self->get_modifier_by_master_mora('ub', default => undef); | ||
| 401 | } | ||
| 402 | |||
| 403 | |||
| 404 | sub is_application { | ||
| 405 | my ($self, @opts) = @_; | ||
| 406 | |||
| 407 | croak 'Stray options passed' if scalar @opts; | ||
| 408 | |||
| 409 | return !!$self->get_modifier_by_master_mora('al', default => undef); | ||
| 410 | } | ||
| 411 | |||
| 412 | |||
| 413 | #@returns __PACKAGE__ | ||
| 414 | sub register { | ||
| 415 | my ($self) = @_; | ||
| 416 | |||
| 417 | $_registered_by_string{$self->as_string} //= $self; | ||
| 418 | $_registered_by_uuid{$self->as('uuid')} //= $self; | ||
| 419 | |||
| 420 | if (ref($self->{stem}) || $self->{stem} ne ':self') { | ||
| 421 | $self->stem->register; | ||
| 422 | } | ||
| 423 | |||
| 424 | return $self; | ||
| 425 | } | ||
| 426 | |||
| 427 | |||
| 428 | sub natural_language { | ||
| 429 | my ($self) = @_; | ||
| 430 | return state $natural_language = Data::Identifier->new(uuid => '1d668738-8aef-4cb4-a4ed-9368e872a93f', tagname => 'famibeib')->register; | ||
| 431 | } | ||
| 432 | |||
| 433 | |||
| 434 | #@returns Lingua::famibeib::Prefix | ||
| 435 | sub prefix { | ||
| 436 | my ($self, $size) = @_; | ||
| 437 | my $string = $self->as_string; | ||
| 438 | my $strlen = length($string) / 2; | ||
| 439 | my $prefix; | ||
| 440 | |||
| 441 | $size //= 1; | ||
| 442 | |||
| 443 | if ($size < 1) { | ||
| 444 | $size = $strlen + $size; | ||
| 445 | croak 'Bad length' unless $size > 0; | ||
| 446 | } | ||
| 447 | |||
| 448 | croak 'Bad length' if $size > $strlen; | ||
| 449 | |||
| 450 | require Lingua::famibeib::Prefix; | ||
| 451 | return Lingua::famibeib::Prefix->new(string => substr($string, 0, 2*$size)); | ||
| 452 | } | ||
| 453 | |||
| 454 | # ---- Private helpers ---- | ||
| 455 | |||
| 456 | sub as { | ||
| 457 | my ($self, $as, @opts) = @_; | ||
| 458 | my $id = $self->{id} //= do { | ||
| 459 | require Data::Identifier::Generate; | ||
| 460 | my $str = $self->as_string; | ||
| 461 | |||
| 462 | Data::Identifier::Generate->generic( | ||
| 463 | request => $str, | ||
| 464 | displayname => $str, | ||
| 465 | tagname => $str, | ||
| 466 | style => 'id-based', | ||
| 467 | namespace => '10ce38bf-6238-4ed7-96ef-98ea9642a4c6', | ||
| 468 | generator => _GENERATOR, | ||
| 469 | ) | ||
| 470 | }; | ||
| 471 | |||
| 472 | return $id if $as eq 'Data::Identifier' && scalar(@opts) == 0; | ||
| 473 | |||
| 474 | return $id->as($as, @opts); | ||
| 475 | } | ||
| 476 | |||
| 477 | sub ise { goto &Data::Identifier::Interface::Simple::ise } # overridden using tail-call | ||
| 478 | |||
| 479 | sub displayname { | ||
| 480 | my ($self, @opts) = @_; | ||
| 481 | return $self->as_string if scalar(@opts) == 0; | ||
| 482 | { # work around us not always having a Data::Identifier (see Lingua::famibeib::ForeignWord) | ||
| 483 | my %x = @opts; | ||
| 484 | delete $x{default}; | ||
| 485 | delete $x{no_defaults}; | ||
| 486 | return $self->as_string if scalar(keys %x) == 0; | ||
| 487 | } | ||
| 488 | return $self->as('Data::Identifier')->displayname(@opts); | ||
| 489 | } | ||
| 490 | |||
| 491 | 1; | ||
| 492 | |||
| 493 | __END__ |