| 1 | # Copyright (c) 2026 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module to interact with famibeib word prefixes | ||
| 6 | |||
| 7 | |||
| 8 | package Lingua::famibeib::Prefix; | ||
| 9 | |||
| 10 | use v5.16; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use Data::Identifier; | ||
| 16 | |||
| 17 | our $VERSION = v0.07; | ||
| 18 | |||
| 19 | use parent qw(Data::Identifier::Interface::Simple Data::Identifier::Interface::Subobjects); | ||
| 20 | |||
| 21 | use constant { | ||
| 22 | _GENERATOR => Data::Identifier->new(uuid => '6574c0af-1389-4db7-96ac-3087b6202bd2')->register, | ||
| 23 | }; | ||
| 24 | |||
| 25 | use overload ( | ||
| 26 | '""' => \&as_string, | ||
| 27 | 'eq' => sub { $_[0]->eq($_[1]) }, | ||
| 28 | 'ne' => sub { !$_[0]->eq($_[1]) }, | ||
| 29 | 'cmp' => sub { $_[0]->cmp($_[1]) }, | ||
| 30 | ); | ||
| 31 | |||
| 32 | my %_registered_by_string; | ||
| 33 | my %_registered_by_uuid; | ||
| 34 | |||
| 35 | # Preregister most common prefixes: | ||
| 36 | __PACKAGE__->new(string => $_)->register foreach qw(ba be fa fe fi fo fu ta to tu); | ||
| 37 | |||
| 38 | |||
| 39 | sub new { | ||
| 40 | my ($pkg, $type, $value, @opts) = @_; | ||
| 41 | my $self = bless {}, $pkg; | ||
| 42 | |||
| 43 | croak 'Stray options passed' if scalar @opts; | ||
| 44 | croak 'No type given' unless defined $type; | ||
| 45 | croak 'No value given' unless defined $value; | ||
| 46 | |||
| 47 | if ($type eq 'from') { | ||
| 48 | if (ref $value) { | ||
| 49 | if ($value->isa(__PACKAGE__)) { | ||
| 50 | # TODO: handle this when @opts are non-empty | ||
| 51 | return $value; | ||
| 52 | } elsif ($value->isa('Data::Identifier') || $value->isa('Data::Identifier::Interface::Simple') || $value->isa('Data::URIID::Base')) { | ||
| 53 | my $id = $value->as('Data::Identifier'); | ||
| 54 | my $generator = eval {$id->generator}; | ||
| 55 | my $request; | ||
| 56 | |||
| 57 | if (defined($generator) && $generator->eq(_GENERATOR) && defined($request = $id->request(default => undef, no_defaults => 1))) { | ||
| 58 | $type = 'string'; | ||
| 59 | $value = $request; | ||
| 60 | } else { | ||
| 61 | if (defined(my $o = $_registered_by_uuid{$id->uuid})) { | ||
| 62 | if (scalar @opts) { | ||
| 63 | $type = 'string'; | ||
| 64 | $value = $o->as_string; | ||
| 65 | $self = $o; | ||
| 66 | } else { | ||
| 67 | return $o; | ||
| 68 | } | ||
| 69 | } else { | ||
| 70 | croak 'Unknown prefix (did you register it or a dictionary?): '.$value; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } else { | ||
| 75 | $type = 'string'; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | if ($type eq 'string') { | ||
| 80 | if ($value =~ /[\s\.,\!\?]/) { | ||
| 81 | croak 'Multi prefix string passed. Consider using Lingua::famibeib::Text if this is what you wanted'; | ||
| 82 | } | ||
| 83 | |||
| 84 | $value = lc($value); | ||
| 85 | |||
| 86 | if ($value =~ /^[bfklmst][aeiou](?:[bfklmst][aeiou])*\z/m) { | ||
| 87 | $self = $_registered_by_string{$value} // $self; | ||
| 88 | $self->{string} = $value; | ||
| 89 | } else { | ||
| 90 | croak 'Bad string: '.$value; | ||
| 91 | } | ||
| 92 | } else { | ||
| 93 | croak 'Bad type: '.$type; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | return $self; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | sub as_string { | ||
| 102 | my ($self) = @_; | ||
| 103 | return $self->{string}; | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub eq { | ||
| 108 | my ($self, $other, @opts) = @_; | ||
| 109 | |||
| 110 | croak 'Stray options passed' if scalar @opts; | ||
| 111 | |||
| 112 | return 1 if !defined($self) && !defined($other); | ||
| 113 | return undef unless defined($self) && defined($other); | ||
| 114 | |||
| 115 | $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)}; | ||
| 116 | $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)}; | ||
| 117 | |||
| 118 | return $self->as_string eq $other->as_string; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | sub cmp { | ||
| 123 | my ($self, $other, @opts) = @_; | ||
| 124 | |||
| 125 | croak 'Stray options passed' if scalar @opts; | ||
| 126 | |||
| 127 | return 1 if !defined($self) && !defined($other); | ||
| 128 | return undef unless defined($self) && defined($other); | ||
| 129 | |||
| 130 | $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)}; | ||
| 131 | $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)}; | ||
| 132 | |||
| 133 | { | ||
| 134 | my $str_self = $self->as_string; | ||
| 135 | my $str_other = $other->as_string; | ||
| 136 | |||
| 137 | return $str_self cmp $str_other; | ||
| 138 | } | ||
| 139 | |||
| 140 | croak 'BUG!'; | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | #@returns __PACKAGE__ | ||
| 145 | sub register { | ||
| 146 | my ($self) = @_; | ||
| 147 | my Data::Identifier $id = $self->as('Data::Identifier'); | ||
| 148 | my $string = $self->as_string; | ||
| 149 | |||
| 150 | $_registered_by_string{$string} //= $self; | ||
| 151 | $_registered_by_uuid{$id->uuid} //= $self; | ||
| 152 | |||
| 153 | if (length($string) == 2) { | ||
| 154 | # If it's the root prefix, also register the Identifier. | ||
| 155 | $id->register; | ||
| 156 | } | ||
| 157 | |||
| 158 | return $self; | ||
| 159 | } | ||
| 160 | # ---- Private helpers ---- | ||
| 161 | |||
| 162 | sub as { | ||
| 163 | my ($self, $as, @opts) = @_; | ||
| 164 | |||
| 165 | my $id = $self->{id} //= do { | ||
| 166 | require Data::Identifier::Generate; | ||
| 167 | my $str = $self->as_string; | ||
| 168 | |||
| 169 | Data::Identifier::Generate->generic( | ||
| 170 | request => $str, | ||
| 171 | displayname => $str, | ||
| 172 | tagname => $str, | ||
| 173 | style => 'id-based', | ||
| 174 | namespace => 'e75ce34a-a8a6-4f6c-aef1-6dad279118e0', | ||
| 175 | generator => _GENERATOR, | ||
| 176 | ) | ||
| 177 | }; | ||
| 178 | |||
| 179 | return $id if $as eq 'Data::Identifier' && scalar(@opts) == 0; | ||
| 180 | |||
| 181 | return $id->as($as, @opts); | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | __END__ | ||
| 187 | |||
| 188 | =pod | ||
| 189 | |||
| 190 | =encoding UTF-8 | ||
| 191 | |||
| 192 | =head1 NAME | ||
| 193 | |||
| 194 | Lingua::famibeib::Prefix - module to interact with famibeib word prefixes | ||
| 195 | |||
| 196 | =head1 VERSION | ||
| 197 | |||
| 198 | version v0.07 | ||
| 199 | |||
| 200 | =head1 SYNOPSIS | ||
| 201 | |||
| 202 | use Lingua::famibeib::Prefix; | ||
| 203 | |||
| 204 | (since v0.06) | ||
| 205 | |||
| 206 | This package is used to store famibeib word prefixes and query them about their properties. | ||
| 207 | |||
| 208 | This module inherits from L<Data::Identifier::Interface::Simple>, and L<Data::Identifier::Interface::Subobjects>. | ||
| 209 | Instances are overloaded so they will stringify to their string representation as per L</as_string>. | ||
| 210 | |||
| 211 | =head1 METHODS | ||
| 212 | |||
| 213 | =head2 new | ||
| 214 | |||
| 215 | my Lingua::famibeib::Prefix $prefix = Lingua::famibeib::Prefix->new($type => $value); | ||
| 216 | # e.g: | ||
| 217 | my Lingua::famibeib::Prefix $prefix = Lingua::famibeib::Prefix->new(string => $str); | ||
| 218 | |||
| 219 | (since v0.06) | ||
| 220 | |||
| 221 | Constructs a new prefix. | ||
| 222 | The prefix is normalised as part of this. | ||
| 223 | This method might deduplicate instances. | ||
| 224 | So not all instances might be new objects. | ||
| 225 | |||
| 226 | Currently the following types (C<$type>) are supported: | ||
| 227 | |||
| 228 | =over | ||
| 229 | |||
| 230 | =item C<from> | ||
| 231 | |||
| 232 | (since v0.06) | ||
| 233 | |||
| 234 | Constructs a prefix from an object. | ||
| 235 | C<$value> should be a reference. | ||
| 236 | |||
| 237 | Currently references to the following types are supported: | ||
| 238 | L<Data::Identifier>, | ||
| 239 | L<Data::Identifier::Interface::Simple>, | ||
| 240 | L<Data::URIID::Base>, | ||
| 241 | and L<Lingua::famibeib::Prefix>. | ||
| 242 | More types might be supported. | ||
| 243 | |||
| 244 | If C<$value> is not a reference the value is parsed as per C<string> if it looks like a prefix string (experimental since v0.06). | ||
| 245 | |||
| 246 | =item C<string> | ||
| 247 | |||
| 248 | (since v0.06) | ||
| 249 | |||
| 250 | Constructs a prefix from it's string representation. | ||
| 251 | |||
| 252 | =back | ||
| 253 | |||
| 254 | =head2 as_string | ||
| 255 | |||
| 256 | my $str = $prefix->as_string; | ||
| 257 | |||
| 258 | (since v0.06) | ||
| 259 | |||
| 260 | Returns the string representation of the prefix. | ||
| 261 | |||
| 262 | =head2 eq | ||
| 263 | |||
| 264 | my $bool = $prefix->eq($other); # $prefix must be non-undef | ||
| 265 | # or: | ||
| 266 | my $bool = Lingua::famibeib::Prefix::eq($prefix, $other); # $prefix can be undef | ||
| 267 | |||
| 268 | (since v0.06) | ||
| 269 | |||
| 270 | Compares two prefixes to be equal. | ||
| 271 | |||
| 272 | If both prefixes are C<undef> they are considered equal. | ||
| 273 | |||
| 274 | If C<$prefix> or C<$other> is not an instance of L<Lingua::famibeib::Prefix> or C<undef> | ||
| 275 | L</new> with the type C<from> is used. | ||
| 276 | |||
| 277 | The operators L<perlop/eq> and L<perlop/ne> are overloaded to this method. | ||
| 278 | |||
| 279 | =head2 cmp | ||
| 280 | |||
| 281 | my $val = $prefix->cmp($other); # $prefix must be non-undef | ||
| 282 | # or: | ||
| 283 | my $val = Lingua::famibeib::Prefix::cmp($prefix, $other); # $prefix can be undef | ||
| 284 | |||
| 285 | (experimental since v0.06) | ||
| 286 | |||
| 287 | Compares the prefixes similar to C<cmp>. This method can be used to order prefixes. | ||
| 288 | To check for them to be equal see L</eq>. | ||
| 289 | |||
| 290 | The parameters are parsed the same way as L</eq>. | ||
| 291 | |||
| 292 | The operator L<perlop/cmp> is overloaded to this method. | ||
| 293 | |||
| 294 | If this method is used for sorting the exact resulting order is not defined. However: | ||
| 295 | |||
| 296 | =over | ||
| 297 | |||
| 298 | =item * | ||
| 299 | |||
| 300 | The order is stable | ||
| 301 | |||
| 302 | =item * | ||
| 303 | |||
| 304 | The order is the same for C<$a-E<gt>cmp($b)> as for C<- $b-E<gt>cmp($a)>. | ||
| 305 | |||
| 306 | =back | ||
| 307 | |||
| 308 | =head2 register | ||
| 309 | |||
| 310 | $prefix->register; | ||
| 311 | |||
| 312 | (since v0.06) | ||
| 313 | |||
| 314 | Registers the prefix with this module. | ||
| 315 | A registered prefix will be kept in memory indefinitely. | ||
| 316 | It is used for deduplication and some types of lookups. | ||
| 317 | |||
| 318 | This method will return C<$prefix>. | ||
| 319 | This can be used to build constants. | ||
| 320 | |||
| 321 | B<Note:> | ||
| 322 | Calling this multiple times on the same prefix is fine. | ||
| 323 | However, doing so might waste some time. | ||
| 324 | |||
| 325 | B<Note:> | ||
| 326 | It is undefined (since v0.06) whether or not this will also register | ||
| 327 | the corresponding L<Data::Identifier>. | ||
| 328 | |||
| 329 | =head1 AUTHOR | ||
| 330 | |||
| 331 | Philipp Schafft <lion@cpan.org> | ||
| 332 | |||
| 333 | =head1 COPYRIGHT AND LICENSE | ||
| 334 | |||
| 335 | This software is Copyright (c) 2025-2026 by Philipp Schafft <lion@cpan.org>. | ||
| 336 | |||
| 337 | This is free software, licensed under: | ||
| 338 | |||
| 339 | The Artistic License 2.0 (GPL Compatible) | ||
| 340 | |||
| 341 | =cut |