famibeib/Modifier.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 278 CC 34 SCAR 52.1
CC
34
Cov
0%
new
420.0
eq
42.0
cmp
42.0
CRAP 183.1
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 word modifiers
6  
7  
8 package Lingua::famibeib::Modifier;
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(Data::Identifier::Interface::Simple Data::Identifier::Interface::Subobjects);
19  
20 use constant {
21 _GENERATOR => Data::Identifier->new(uuid => '306baa6e-e672-4327-a6b4-ba1d3de89a1e')->register,
22 };
23  
24 use overload (
25 '""' => sub { $_[0]->as_string },
26 'eq' => sub { $_[0]->eq($_[1]) },
27 'ne' => sub { !$_[0]->eq($_[1]) },
28 'cmp' => sub { $_[0]->cmp($_[1]) },
29 );
30  
31 my %_modifiers = (
32 (map {$_ => {max_repeat => 0, allow_word => undef}} qw(ab eb ib ub af ef ak ek ok uk al il ol am em im)),
33 );
34  
35 $_modifiers{$_}{max_repeat} = 1 foreach qw(eb ib);
36 $_modifiers{ab}{allow_word} = 1;
37  
38 my %_registered_by_string;
39 my %_registered_by_uuid;
40  
41 foreach my $key (keys %_modifiers) {
42 my $d = $_modifiers{$key};
43 __PACKAGE__->new(string => $key)->register;
44 }
45  
46  
47 sub new {
48 my ($pkg, $type, $value, @opts) = @_;
49 my $self = bless {}, $pkg;
50  
51 croak 'Stray options passed' if scalar @opts;
52 croak 'No type given' unless defined $type;
53 croak 'No value given' unless defined $value;
54  
55 if ($type eq 'from') {
56 if (ref $value) {
57 if ($value->isa(__PACKAGE__)) {
58 # TODO: handle this when @opts are non-empty
59 return $value;
60 } elsif ($value->isa('Data::Identifier') || $value->isa('Data::Identifier::Interface::Simple') || $value->isa('Data::URIID::Base')) {
61 my $id = $value->as('Data::Identifier');
62 if (eval {$id->generator->eq(_GENERATOR)} && defined(my $request = $id->request(default => undef, no_defaults => 1))) {
63 $type = 'string';
64 $value = $request;
65 } else {
66 if (defined(my $o = $_registered_by_uuid{$id->uuid})) {
67 if (scalar @opts) {
68 $type = 'string';
69 $value = $o->as_string;
70 $self = $o;
71 } else {
72 return $o;
73 }
74 } else {
75 croak 'Unknown word (did you register it or a dictionary?): '.$value;
76 }
77 }
78 }
79 } else {
80 $type = 'string';
81 }
82 }
83  
84 if ($type eq 'string') {
85 my ($first, $rest);
86 my $d;
87  
88 $value =~ s/^-//;
89 $value = lc($value);
90  
91 if (defined(my $o = $_registered_by_string{$value})) {
92 return $o;
93 }
94  
95 ($first, $rest) = $value =~ /^(..)(.*)$/;
96 $d = $_modifiers{$first};
97  
98 croak 'Invalid modifier: Unknown mora: '.$first unless defined $d;
99  
100 if (defined($rest) && length($rest)) {
101 if ($rest =~ /^[aeiou][bfklmst]/) {
102 my $l = length($rest);
103  
104 croak 'Bad string: '.$value if $l & 1;
105  
106 $l /= 2;
107  
108 croak 'Bad repeat count' if $l > $d->{max_repeat};
109 croak 'Bad repeat pattern' if $rest ne ($first x $l);
110 } elsif ($rest =~ /^(?:[bfklmst][aeiou])+\z/ && $d->{allow_word}) {
111 # no-op
112 } else {
113 croak 'Bad string: '.$value;
114 }
115 }
116  
117 $self->{string} = $value;
118 } else {
119 croak 'Bad type: '.$type;
120 }
121  
122 return $self;
123 }
124  
125  
126 sub as_string {
127 my ($self, @opts) = @_;
128  
129 croak 'Stray options passed' if scalar @opts;
130  
131 return $self->{string};
132 }
133  
134  
135 sub master_mora {
136 my ($self, @opts) = @_;
137  
138 croak 'Stray options passed' if scalar @opts;
139  
140 return substr($self->{string}, 0, 2);
141 }
142  
143  
144 sub eq {
145 my ($self, $other, @opts) = @_;
146  
147 croak 'Stray options passed' if scalar @opts;
148  
149 return 1 if !defined($self) && !defined($other);
150 return undef unless defined($self) && defined($other);
151  
152 $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)};
153 $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)};
154  
155 return $self->as_string eq $other->as_string;
156 }
157  
158  
159 sub cmp {
160 my ($self, $other, @opts) = @_;
161  
162 croak 'Stray options passed' if scalar @opts;
163  
164 return 1 if !defined($self) && !defined($other);
165 return undef unless defined($self) && defined($other);
166  
167 $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)};
168 $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)};
169  
170 return $self->as_string cmp $other->as_string;
171 }
172  
173  
174 #@returns __PACKAGE__
175 sub register {
176 my ($self) = @_;
177  
178 $_registered_by_string{$self->as_string} //= $self;
179 $_registered_by_uuid{$self->as('uuid')} //= $self;
180  
181 $self->as('Data::Identifier')->register;
182  
183 return $self;
184 }
185  
186 # ---- Private helpers ----
187  
188 sub as {
189 my ($self, $as, @opts) = @_;
190 my $id = $self->{id} //= do {
191 require Data::Identifier::Generate;
192 my $str = $self->as_string;
193  
194 Data::Identifier::Generate->generic(
195 request => $str,
196 displayname => $str,
197 tagname => $str,
198 style => 'id-based',
199 namespace => '5c2b24f0-e0d9-4746-bd72-0d07061d0dd7',
200 generator => _GENERATOR,
201 )
202 };
203  
204 return $id if $as eq 'Data::Identifier' && scalar(@opts) == 0;
205  
206 return $id->as($as, @opts);
207 }
208  
209 sub displayname {
210 my ($self, @opts) = @_;
211 return $self->as_string if scalar(@opts) == 0;
212 return $self->as('Data::Identifier')->displayname(@opts);
213 }
214  
215 1;
216  
217 __END__
218  
219 =pod
220  
221 =encoding UTF-8
222  
223 =head1 NAME
224  
225 Lingua::famibeib::Modifier - module to interact with the famibeib word modifiers
226  
227 =head1 VERSION
228  
229 version v0.07
230  
231 =head1 SYNOPSIS
232  
233 use Lingua::famibeib::Modifier;
234  
235 This package represents a modifier that is applied to a word.
236 See also L<Lingua::famibeib::Word>.
237  
238 This module inherits from L<Data::Identifier::Interface::Simple>, and L<Data::Identifier::Interface::Subobjects>.
239 Instances are overloaded so they will stringify to their string representation as per L</as_string>.
240  
241 =head1 METHODS
242  
243 =head2 new
244  
245 my Lingua::famibeib::Modifier $modifier = Lingua::famibeib::Modifier->new($type => $value);
246 # e.g.:
247 my Lingua::famibeib::Modifier $modifier = Lingua::famibeib::Modifier->new(string => 'ik');
248  
249 (since v0.01)
250  
251 Creates a new modifier instance.
252 A modifier is the part of a word that is not it's stem.
253  
254 Currently the following types (C<$type>) are supported:
255  
256 =over
257  
258 =item C<from>
259  
260 Constructs a word from an object.
261 C<$value> should be a reference.
262  
263 Currently references to the following types are supported:
264 L<Data::Identifier>,
265 L<Data::Identifier::Interface::Simple>,
266 L<Data::URIID::Base>,
267 and L<Lingua::famibeib::Modifier>.
268 More types might be supported.
269 If C<$value> is not a reference the value is parsed as per C<string>.
270  
271 =item C<string>
272  
273 Constructs a modifier from a string.
274  
275 =back
276  
277 =head2 as_string
278  
279 my $str = $modifier->as_string;
280  
281 (since v0.01)
282  
283 Returns the current modifier as a string.
284  
285 =head2 master_mora
286  
287 my $str = $modifier->master_mora;
288  
289 (experimental since v0.01)
290  
291 Returns the master mora from the modifier.
292 This is the first mora of the modifier.
293 It provides the type of the modifier.
294  
295 =head2 eq
296  
297 my $bool = $modifier->eq($other); # $modifier must be non-undef
298 # or:
299 my $bool = Lingua::famibeib::Modifier::eq($modifier, $other); # $modifier can be undef
300  
301 (since v0.01)
302  
303 Compares two modifier to be equal.
304  
305 If both modifier are C<undef> they are considered equal.
306  
307 If C<$modifier> or C<$other> is not an instance of L<Lingua::famibeib::Modifier> or C<undef>
308 L</new> with the type C<from> is used.
309  
310 The operators L<perlop/eq> and L<perlop/ne> are overloaded to this method.
311  
312 =head2 cmp
313  
314 my $val = $modifier->cmp($other); # $modifier must be non-undef
315 # or:
316 my $val = Lingua::famibeib::Modifier::cmp($modifier, $other); # $modifier can be undef
317  
318 (experimental since v0.01)
319  
320 Compares the modifier similar to C<cmp>. This method can be used to order modifier.
321 To check for them to be equal see L</eq>.
322  
323 The parameters are parsed the same way as L</eq>.
324  
325 The operator L<perlop/cmp> is overloaded to this method.
326  
327 If this method is used for sorting the exact resulting order is not defined. However:
328  
329 =over
330  
331 =item *
332  
333 The order is stable
334  
335 =item *
336  
337 The order is the same for C<$a-E<gt>cmp($b)> as for C<- $b-E<gt>cmp($a)>.
338  
339 =back
340  
341 =head2 register
342  
343 $modifier->register;
344  
345 (since v0.01)
346  
347 Registers the modifier with this module.
348 A registered modifier will be kept in memory indefinitely.
349 It is used for deduplication and some types of lookups.
350  
351 This method will return C<$modifier>.
352 This can be used to build constants.
353  
354 B<Note:>
355 Calling this multiple times on the same modifier is fine.
356 However, doing so might waste some time.
357  
358 B<Note:>
359 Base and common modifiers are already registered by this module.
360 Hence it is hardly needed to call this method at all.
361  
362 B<Note:>
363 This method also registers the corresponding L<Data::Identifier> instance (since v0.02).
364  
365 =head1 AUTHOR
366  
367 Philipp Schafft <lion@cpan.org>
368  
369 =head1 COPYRIGHT AND LICENSE
370  
371 This software is Copyright (c) 2025-2026 by Philipp Schafft <lion@cpan.org>.
372  
373 This is free software, licensed under:
374  
375 The Artistic License 2.0 (GPL Compatible)
376  
377 =cut