| 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.07; | ||
| 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__ | ||
| 494 | |||
| 495 | =pod | ||
| 496 | |||
| 497 | =encoding UTF-8 | ||
| 498 | |||
| 499 | =head1 NAME | ||
| 500 | |||
| 501 | Lingua::famibeib::Word - module to interact with the famibeib words | ||
| 502 | |||
| 503 | =head1 VERSION | ||
| 504 | |||
| 505 | version v0.07 | ||
| 506 | |||
| 507 | =head1 SYNOPSIS | ||
| 508 | |||
| 509 | use Lingua::famibeib::Word; | ||
| 510 | |||
| 511 | my Lingua::famibeib::Word $word = Lingua::famibeib::Word->new(string => 'baba'); | ||
| 512 | |||
| 513 | my $str = $word->as_string; | ||
| 514 | |||
| 515 | This package is used to store individual famibeib words and query them about their properties. | ||
| 516 | |||
| 517 | This module inherits from | ||
| 518 | L<Lingua::Generic::Interface::Word> (since v0.07), | ||
| 519 | L<Data::Identifier::Interface::Simple> (since v0.01), | ||
| 520 | and L<Data::Identifier::Interface::Subobjects> (since v0.01). | ||
| 521 | Instances are overloaded so they will stringify to their string representation as per L</as_string>. | ||
| 522 | |||
| 523 | =head1 METHODS | ||
| 524 | |||
| 525 | =head2 new | ||
| 526 | |||
| 527 | my Lingua::famibeib::Word $word = Lingua::famibeib::Word->new($type => $value); | ||
| 528 | # e.g: | ||
| 529 | my Lingua::famibeib::Word $word = Lingua::famibeib::Word->new(string => $str); | ||
| 530 | |||
| 531 | (since v0.01) | ||
| 532 | |||
| 533 | Constructs a new word. | ||
| 534 | The word is normalised as part of this. | ||
| 535 | This method might deduplicate instances. | ||
| 536 | So not all instances might be new objects. | ||
| 537 | This is done to support working with large texts in an efficient manner. | ||
| 538 | |||
| 539 | Currently the following types (C<$type>) are supported: | ||
| 540 | |||
| 541 | =over | ||
| 542 | |||
| 543 | =item C<from> | ||
| 544 | |||
| 545 | (since v0.01) | ||
| 546 | |||
| 547 | Constructs a word from an object. | ||
| 548 | C<$value> should be a reference. | ||
| 549 | |||
| 550 | Currently references to the following types are supported: | ||
| 551 | L<Data::Identifier>, | ||
| 552 | L<Data::Identifier::Interface::Simple>, | ||
| 553 | L<Data::URIID::Base>, | ||
| 554 | and L<Lingua::famibeib::Word>. | ||
| 555 | More types might be supported. | ||
| 556 | |||
| 557 | If C<$value> is not a reference the value is parsed as per C<string> if it looks like a word string (experimental since v0.01). | ||
| 558 | |||
| 559 | =item C<string> | ||
| 560 | |||
| 561 | (since v0.01) | ||
| 562 | |||
| 563 | Constructs a word from it's string representation. | ||
| 564 | |||
| 565 | =item C<number> | ||
| 566 | |||
| 567 | (since v0.02) | ||
| 568 | |||
| 569 | Constructs a word from a number (must be an integer value). | ||
| 570 | |||
| 571 | =back | ||
| 572 | |||
| 573 | =head2 combine | ||
| 574 | |||
| 575 | my Lingua::famibeib::Word $combined = $word->combine(@modifiers); | ||
| 576 | # or: | ||
| 577 | my Lingua::famibeib::Word $combined = Lingua::famibeib::Word->combine($word, @modifiers); | ||
| 578 | |||
| 579 | (experimental since v0.02) | ||
| 580 | |||
| 581 | Combines a word with modifiers. Returns the resulting new word. | ||
| 582 | |||
| 583 | This method tries to build new valid words, resolving any logical conflicts. | ||
| 584 | In order to do so the last given modifier always wins. | ||
| 585 | |||
| 586 | =head2 as_string | ||
| 587 | |||
| 588 | my $str = $word->as_string; | ||
| 589 | |||
| 590 | (since v0.01) | ||
| 591 | |||
| 592 | Returns the string representation of the word. | ||
| 593 | |||
| 594 | See also: L<Lingua::Generic::Interface::Word/as_string>. | ||
| 595 | |||
| 596 | =head2 as_number | ||
| 597 | |||
| 598 | my $num = $word->as_number; | ||
| 599 | # or: | ||
| 600 | my $obj = $word->as_number($as); # since v0.04 | ||
| 601 | |||
| 602 | (since v0.02) | ||
| 603 | |||
| 604 | Returns the integer value of the word (or C<die> if none). | ||
| 605 | This works on simple numbers only. | ||
| 606 | Simple numbers are numbers with no modifiers (only a stem) or with the single modifier C<-ub> for negative. | ||
| 607 | |||
| 608 | To process more complex numbers (such as ordinals) process the modifiers individually and parse the value using | ||
| 609 | this method applied to the stem as returned by L</stem>. | ||
| 610 | |||
| 611 | Since v0.04 an object type can be requested. Currently supported are: | ||
| 612 | L<Math::BigInt>, | ||
| 613 | L<Data::Identifier>. | ||
| 614 | |||
| 615 | B<Note:> | ||
| 616 | If the value is too big to be represented safely by the selected type this method might C<die>. | ||
| 617 | |||
| 618 | =head2 stem | ||
| 619 | |||
| 620 | my Lingua::famibeib::Word $stem = $word->stem; | ||
| 621 | |||
| 622 | (since v0.01) | ||
| 623 | |||
| 624 | Returns the stem of a word. | ||
| 625 | If this word is already a stem it returns itself. | ||
| 626 | |||
| 627 | =head2 modifiers | ||
| 628 | |||
| 629 | my @modifiers = $word->modifiers; | ||
| 630 | |||
| 631 | (since v0.01) | ||
| 632 | |||
| 633 | Returns the list of the modifiers of the word. | ||
| 634 | |||
| 635 | B<Note:> | ||
| 636 | The list is not sorted in any way. | ||
| 637 | |||
| 638 | =head2 get_modifier_by_master_mora | ||
| 639 | |||
| 640 | my Lingua::famibeib::Modifier $modifier = $word->get_modifier_by_master_mora($mora [, %opts]); | ||
| 641 | |||
| 642 | (experimental since v0.01) | ||
| 643 | |||
| 644 | Returns a modifier based on a master mora. | ||
| 645 | See L<Lingua::famibeib::Modifier/master_mora> for details. | ||
| 646 | |||
| 647 | Optionally the following options might be passed: | ||
| 648 | |||
| 649 | =over | ||
| 650 | |||
| 651 | =item C<default> | ||
| 652 | |||
| 653 | The default value to return if the modifier is not used. | ||
| 654 | This can be set to C<undef> to switch the method from C<die>ing to returning C<undef> if no such modifier is part of the word. | ||
| 655 | |||
| 656 | =back | ||
| 657 | |||
| 658 | =head2 eq | ||
| 659 | |||
| 660 | my $bool = $word->eq($other); # $word must be non-undef | ||
| 661 | # or: | ||
| 662 | my $bool = Lingua::famibeib::Word::eq($word, $other); # $word can be undef | ||
| 663 | |||
| 664 | (since v0.01) | ||
| 665 | |||
| 666 | Compares two words to be equal. | ||
| 667 | |||
| 668 | If both words are C<undef> they are considered equal. | ||
| 669 | |||
| 670 | If C<$word> or C<$other> is not an instance of L<Lingua::famibeib::Word> or C<undef> | ||
| 671 | L</new> with the type C<from> is used. | ||
| 672 | |||
| 673 | The operators L<perlop/eq> and L<perlop/ne> are overloaded to this method. | ||
| 674 | |||
| 675 | See also: L<Lingua::Generic::Interface::Word/eq>. | ||
| 676 | |||
| 677 | =head2 cmp | ||
| 678 | |||
| 679 | my $val = $word->cmp($other); # $word must be non-undef | ||
| 680 | # or: | ||
| 681 | my $val = Lingua::famibeib::Word::cmp($word, $other); # $word can be undef | ||
| 682 | |||
| 683 | (experimental since v0.01) | ||
| 684 | |||
| 685 | Compares the words similar to C<cmp>. This method can be used to order words. | ||
| 686 | To check for them to be equal see L</eq>. | ||
| 687 | |||
| 688 | The parameters are parsed the same way as L</eq>. | ||
| 689 | |||
| 690 | The operator L<perlop/cmp> is overloaded to this method. | ||
| 691 | |||
| 692 | If this method is used for sorting the exact resulting order is not defined. However: | ||
| 693 | |||
| 694 | =over | ||
| 695 | |||
| 696 | =item * | ||
| 697 | |||
| 698 | If the words differ in stems they are sorted by stem first | ||
| 699 | |||
| 700 | =item * | ||
| 701 | |||
| 702 | The order is stable | ||
| 703 | |||
| 704 | =item * | ||
| 705 | |||
| 706 | The order is the same for C<$a-E<gt>cmp($b)> as for C<- $b-E<gt>cmp($a)>. | ||
| 707 | |||
| 708 | =back | ||
| 709 | |||
| 710 | See also: L<Lingua::Generic::Interface::Word/cmp>. | ||
| 711 | |||
| 712 | =head2 is_verb | ||
| 713 | |||
| 714 | my $bool = $word->is_verb; | ||
| 715 | |||
| 716 | (since v0.01) | ||
| 717 | |||
| 718 | Returns true-ish if the current word is a verb or false-ish if it is not. | ||
| 719 | |||
| 720 | =head2 is_negative | ||
| 721 | |||
| 722 | my $bool = $word->is_negative; | ||
| 723 | |||
| 724 | (since v0.01) | ||
| 725 | |||
| 726 | Returns true-ish if the current word is negative or false-ish if it is not. | ||
| 727 | |||
| 728 | =head2 is_application | ||
| 729 | |||
| 730 | my $bool = $word->is_application; | ||
| 731 | |||
| 732 | (since v0.01) | ||
| 733 | |||
| 734 | Returns true-ish if the current word is in application form or false-ish if it is not. | ||
| 735 | |||
| 736 | =head2 register | ||
| 737 | |||
| 738 | $word->register; | ||
| 739 | |||
| 740 | (since v0.01) | ||
| 741 | |||
| 742 | Registers the word with this module. | ||
| 743 | A registered word will be kept in memory indefinitely. | ||
| 744 | It is used for deduplication and some types of lookups. | ||
| 745 | |||
| 746 | This method will return C<$word>. | ||
| 747 | This can be used to build constants. | ||
| 748 | |||
| 749 | B<Note:> | ||
| 750 | Calling this multiple times on the same word is fine. | ||
| 751 | However, doing so might waste some time. | ||
| 752 | |||
| 753 | B<Note:> | ||
| 754 | If this word is not a stem itself it's stem is also registered. | ||
| 755 | |||
| 756 | B<Note:> | ||
| 757 | It is undefined (since v0.01) whether or not this will also register | ||
| 758 | the corresponding L<Data::Identifier>. | ||
| 759 | |||
| 760 | See also: L<Lingua::Generic::Interface::Word/register>. | ||
| 761 | |||
| 762 | =head2 natural_language | ||
| 763 | |||
| 764 | my Data::Identifier $natural_language = $word->natural_language; | ||
| 765 | |||
| 766 | (since v0.06) | ||
| 767 | |||
| 768 | Returns the natural language this word is in. | ||
| 769 | |||
| 770 | For famibeib words it will always return famibeib. | ||
| 771 | However this method might be useful together with other modules from the C<Lingua> namespace. | ||
| 772 | |||
| 773 | See also: L<Lingua::Generic::Interface::Word/natural_language>. | ||
| 774 | |||
| 775 | =head2 prefix | ||
| 776 | |||
| 777 | my Lingua::famibeib::Prefix $prefix = $word->prefix( [ $size ] ); | ||
| 778 | |||
| 779 | (since v0.06) | ||
| 780 | |||
| 781 | Returns the L<Lingua::famibeib::Prefix> of the word. | ||
| 782 | |||
| 783 | If C<$size> is given and positive it is the number of mora to include. | ||
| 784 | If C<$size> is given and negative or zero it is how many less mora to include. | ||
| 785 | |||
| 786 | If C<$size> is not given it defaults to one. | ||
| 787 | |||
| 788 | All values but one are experimental (as of v0.06). | ||
| 789 | |||
| 790 | =head1 AUTHOR | ||
| 791 | |||
| 792 | Philipp Schafft <lion@cpan.org> | ||
| 793 | |||
| 794 | =head1 COPYRIGHT AND LICENSE | ||
| 795 | |||
| 796 | This software is Copyright (c) 2025-2026 by Philipp Schafft <lion@cpan.org>. | ||
| 797 | |||
| 798 | This is free software, licensed under: | ||
| 799 | |||
| 800 | The Artistic License 2.0 (GPL Compatible) | ||
| 801 | |||
| 802 | =cut |