| 1 | # Copyright (c) 2026 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module to generate proper famibeib names | ||
| 6 | |||
| 7 | |||
| 8 | package Lingua::famibeib::NameGenerator; | ||
| 9 | |||
| 10 | use v5.16; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use Digest::SHA3; | ||
| 16 | use Lingua::famibeib::Word; | ||
| 17 | |||
| 18 | our $VERSION = v0.07; | ||
| 19 | |||
| 20 | my @_word_mora = qw( | ||
| 21 | ba be bi bo bu | ||
| 22 | fa fe fi fo fu | ||
| 23 | ka ke ki ko ku | ||
| 24 | la le li lo lu | ||
| 25 | ma me mi mo mu | ||
| 26 | sa se si so su | ||
| 27 | ta te ti to tu | ||
| 28 | ); | ||
| 29 | |||
| 30 | my %_static_length_probabilities = ( | ||
| 31 | 1 => 0.01, | ||
| 32 | 2 => 0.10, | ||
| 33 | 3 => 0.80, | ||
| 34 | 4 => 0.40, | ||
| 35 | 5 => 0.10, | ||
| 36 | ); | ||
| 37 | |||
| 38 | my %_static_key_factors = ( | ||
| 39 | (map {$_ => 0.5} qw(species family tribe)), | ||
| 40 | (map {$_ => 1.0} qw(email realname nickname username name user tag)), | ||
| 41 | ); | ||
| 42 | |||
| 43 | |||
| 44 | #@returns Lingua::famibeib::Word | ||
| 45 | sub generate { | ||
| 46 | my ($pkg, %opts) = @_; | ||
| 47 | my $digest = Digest::SHA3->new(384); | ||
| 48 | my $minlen = delete($opts{minlength}) // $opts{length} // 3; | ||
| 49 | my $maxlen = delete($opts{maxlength}) // $opts{length} // 6; | ||
| 50 | my $counter = $opts{counter} // 0; | ||
| 51 | my $retry = $opts{retry}; | ||
| 52 | my $last = $opts{last}; | ||
| 53 | my @rands; | ||
| 54 | my %letters; | ||
| 55 | my %strings; | ||
| 56 | my %mora = map {$_ => 0} @_word_mora; | ||
| 57 | my $length; | ||
| 58 | |||
| 59 | $counter++; | ||
| 60 | $counter = 1/($counter**.5); | ||
| 61 | |||
| 62 | delete $opts{length}; | ||
| 63 | |||
| 64 | if (delete $opts{random}) { | ||
| 65 | my $random = $pkg->_random; | ||
| 66 | $digest->add(sprintf(':random%u*=%s', length($random), $random)); | ||
| 67 | } | ||
| 68 | |||
| 69 | foreach my $key (qw(seed retry counter last sex_or_gender species family tribe email realname nickname username name subjecttype user tag)) { | ||
| 70 | my $v = delete($opts{$key}) // next; | ||
| 71 | my $ref = ref($v) // ''; | ||
| 72 | my $displayname; | ||
| 73 | my $str; | ||
| 74 | |||
| 75 | if ($ref) { | ||
| 76 | if ($v->isa('Data::Identifier') || $v->isa('Data::Identifier::Interface::Simple')) { | ||
| 77 | $str = $v->ise; | ||
| 78 | $displayname = $v->displayname(default => undef, no_defaults => 1); | ||
| 79 | } else { | ||
| 80 | croak 'Unsupported object: '.$v; | ||
| 81 | } | ||
| 82 | } else { | ||
| 83 | $str = $v; | ||
| 84 | $displayname = $v; | ||
| 85 | } | ||
| 86 | |||
| 87 | $str = sprintf(':%s%u%s=%s', $key, length($str), $ref, $str); | ||
| 88 | $digest->add($str); | ||
| 89 | #say $str; | ||
| 90 | |||
| 91 | if (defined(my $factor = $_static_key_factors{$key})) { | ||
| 92 | $factor *= $counter; | ||
| 93 | if (defined($displayname) && length($displayname)) { | ||
| 94 | $displayname =~ s/\@.*\z//; | ||
| 95 | |||
| 96 | $displayname = lc($displayname); | ||
| 97 | |||
| 98 | unless (exists $strings{$factor.'-'.$displayname}) { | ||
| 99 | $strings{$factor.'-'.$displayname} = undef; | ||
| 100 | |||
| 101 | foreach my $c (split //, $displayname) { # we use lc() not fc() as famibeib in normal form is lower-case. | ||
| 102 | $letters{$c} //= 0; | ||
| 103 | $letters{$c} += $factor; | ||
| 104 | } | ||
| 105 | |||
| 106 | foreach my $mora (@_word_mora) { | ||
| 107 | my $c = () = $displayname =~ /\Q$mora/g; | ||
| 108 | $mora{$mora} += $factor*$c; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | croak 'Stray options passed' if scalar keys %opts; | ||
| 116 | croak 'Bad length' if $minlen > $maxlen || $minlen < 2 || $maxlen > 9; | ||
| 117 | # Correct given the prefix. | ||
| 118 | $minlen--; | ||
| 119 | $maxlen--; | ||
| 120 | |||
| 121 | @rands = unpack('C*', $digest->digest); | ||
| 122 | #say '[', join(', ', @rands), ']'; | ||
| 123 | |||
| 124 | foreach my $mora (@_word_mora) { | ||
| 125 | my ($x, $y) = $mora =~ /^(.)(.)\z/; | ||
| 126 | |||
| 127 | $mora{$mora} = (((($letters{$x} // 0)**2 + ($letters{$y} // 0)**2)**.25)/2) + sqrt($mora{$mora}/8) + (shift(@rands)/256); | ||
| 128 | } | ||
| 129 | |||
| 130 | { | ||
| 131 | my %lens = map {$_ => (shift(@rands)/256) * ($_static_length_probabilities{$_} // 0.01)} $minlen .. $maxlen; | ||
| 132 | $length = int((sort {$lens{$a} <=> $lens{$b} || $a <=> $b} keys %lens)[-1]); | ||
| 133 | } | ||
| 134 | |||
| 135 | #use Data::Dumper; | ||
| 136 | #warn Dumper(\@rands, \%mora); | ||
| 137 | |||
| 138 | { | ||
| 139 | my $res = 'ta'; | ||
| 140 | |||
| 141 | for (; $length; $length--) { | ||
| 142 | my $m = (sort {$mora{$a} <=> $mora{$b} || $a cmp $b} keys %mora)[-1]; | ||
| 143 | $res .= $m; | ||
| 144 | $mora{$m} *= 0.8; | ||
| 145 | $mora{$m} -= 0.3; | ||
| 146 | } | ||
| 147 | |||
| 148 | #warn Dumper(\@rands, \%mora); | ||
| 149 | $res = Lingua::famibeib::Word->new(string => $res); | ||
| 150 | |||
| 151 | if (defined $last) { | ||
| 152 | $last = Lingua::famibeib::Word->new(from => $last); # enforce a valid word | ||
| 153 | if ($last->eq($res)) { | ||
| 154 | # Same as last, we need to retry: | ||
| 155 | croak 'Double collision error. You broke the universe. Please retry with a new big-bang.' if $retry; | ||
| 156 | return __SUB__->(@_, retry => ($retry // 0) + 1); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | return $res; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | # ---- Private helpers ---- | ||
| 165 | |||
| 166 | sub _random { | ||
| 167 | state $prefix = sprintf('%s.%s', $$, $^T); | ||
| 168 | state $count = 0; | ||
| 169 | |||
| 170 | return sprintf('%s/%u/%u/%s', $prefix, $count++, time(), rand()); | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | __END__ | ||
| 176 | |||
| 177 | =pod | ||
| 178 | |||
| 179 | =encoding UTF-8 | ||
| 180 | |||
| 181 | =head1 NAME | ||
| 182 | |||
| 183 | Lingua::famibeib::NameGenerator - module to generate proper famibeib names | ||
| 184 | |||
| 185 | =head1 VERSION | ||
| 186 | |||
| 187 | version v0.07 | ||
| 188 | |||
| 189 | =head1 SYNOPSIS | ||
| 190 | |||
| 191 | use Lingua::famibeib::NameGenerator; | ||
| 192 | |||
| 193 | my Lingua::famibeib::Word $name = Lingua::famibeib::NameGenerator->generate(...); | ||
| 194 | |||
| 195 | (since v0.04) | ||
| 196 | |||
| 197 | This package is used to generate proper famibeib names (C<ta->). | ||
| 198 | |||
| 199 | =head1 METHODS | ||
| 200 | |||
| 201 | =head2 generate | ||
| 202 | |||
| 203 | my Lingua::famibeib::Word $name = Lingua::famibeib::NameGenerator->generate(%opts); | ||
| 204 | |||
| 205 | Generates a proper name based on the passed options. | ||
| 206 | |||
| 207 | This method tries to match the output to the input as best as it can. | ||
| 208 | Hence it is slower than just a random string generator. | ||
| 209 | |||
| 210 | Unless C<random> (see below) is passed, the output is also fully stable for any given version of this module. | ||
| 211 | This means, that if you call this method again with the same inputs it will result in the same output (as long as the same version is used). | ||
| 212 | |||
| 213 | It returns the name as a L<Lingua::famibeib::Word>. | ||
| 214 | |||
| 215 | The following (all optional) options are supported: | ||
| 216 | |||
| 217 | =over | ||
| 218 | |||
| 219 | =item C<counter> | ||
| 220 | |||
| 221 | The counter for the given request (if any). | ||
| 222 | This can be used together with C<last> to break loops of repeating names when generating multiple names. | ||
| 223 | |||
| 224 | Generally one would set this to 0 for the first request, with C<last> being C<undef>. | ||
| 225 | For the second request this would be 1, and C<last> would be the result of the first call and so on. | ||
| 226 | |||
| 227 | =item C<email> | ||
| 228 | |||
| 229 | An e-mail address of the person. | ||
| 230 | |||
| 231 | =item C<family> | ||
| 232 | |||
| 233 | The name of the family of the person. | ||
| 234 | Can be a plain name, or a L<Lingua::famibeib::Word> or L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 235 | |||
| 236 | =item C<last> | ||
| 237 | |||
| 238 | The result of the last call to this method. | ||
| 239 | This can be used to generate more results for the same inputs to allow the user to select one. | ||
| 240 | |||
| 241 | See also C<counter>. | ||
| 242 | |||
| 243 | =item C<length> | ||
| 244 | |||
| 245 | The length in mora to generate. | ||
| 246 | This value is the default for C<maxlength>, and C<minlength>. | ||
| 247 | |||
| 248 | =item C<maxlength> | ||
| 249 | |||
| 250 | The maximum length in mora to use for generated names. | ||
| 251 | |||
| 252 | =item C<minlength> | ||
| 253 | |||
| 254 | The minimum length in mora to use for generated names. | ||
| 255 | |||
| 256 | =item C<name> | ||
| 257 | |||
| 258 | A name for the entity in any other language. | ||
| 259 | This can be used if none of the more specific options fit: C<email>, C<nickname>, C<realname>, C<username>. | ||
| 260 | Can be a plain name, or a L<Lingua::famibeib::Word> or L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 261 | |||
| 262 | =item C<nickname> | ||
| 263 | |||
| 264 | A nick name, any name the entity is called by. | ||
| 265 | Can be a plain name, or a L<Lingua::famibeib::Word> or L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 266 | |||
| 267 | =item C<random> | ||
| 268 | |||
| 269 | If set to a true-ish value this method will add some (low quality) random value to the generator. | ||
| 270 | This is useful when no information about the entity is known. | ||
| 271 | This also means that the results will not be the same for two calls with the same parameters. | ||
| 272 | |||
| 273 | See also C<seed>. | ||
| 274 | |||
| 275 | =item C<realname> | ||
| 276 | |||
| 277 | The real name (legal name) of a entity. | ||
| 278 | Can be a plain name, or a L<Lingua::famibeib::Word> or L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 279 | |||
| 280 | =item C<seed> | ||
| 281 | |||
| 282 | A seed that is added to the generator. It can be used as an alternatives to C<random> when a result not bound to the entity is wanted | ||
| 283 | (or no such data is available), but still a deterministic result is wanted. | ||
| 284 | |||
| 285 | If this is a floating point value it is converted to a string in some way. | ||
| 286 | Different versions of this module and/or of perl, or the machine this runs on can result in different results. | ||
| 287 | |||
| 288 | =item C<sex_or_gender> | ||
| 289 | |||
| 290 | The sex or gender of the entity. | ||
| 291 | Should be a L<Lingua::famibeib::Word> or L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 292 | This method might try to match sexes/genders internally. | ||
| 293 | Note that L<Data::TagDB::Tag> implements L<Data::Identifier::Interface::Simple> and can therefore be used. | ||
| 294 | |||
| 295 | =item C<species> | ||
| 296 | |||
| 297 | The species of the entity. | ||
| 298 | Should be a L<Lingua::famibeib::Word> or L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 299 | This method might try to match species internally. | ||
| 300 | Note that L<Data::TagDB::Tag> implements L<Data::Identifier::Interface::Simple> and can therefore be used. | ||
| 301 | |||
| 302 | =item C<subjecttype> | ||
| 303 | |||
| 304 | The primary subject-type (C<has-type>) of the entity. | ||
| 305 | This should be a L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 306 | Note that L<Data::TagDB::Tag> implements L<Data::Identifier::Interface::Simple> and can therefore be used. | ||
| 307 | |||
| 308 | =item C<tag> | ||
| 309 | |||
| 310 | The tag of the entity. | ||
| 311 | This should be a L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 312 | Note that L<Data::TagDB::Tag> implements L<Data::Identifier::Interface::Simple> and can therefore be used. | ||
| 313 | |||
| 314 | =item C<tribe> | ||
| 315 | |||
| 316 | The tribe of the entity. Takes the same values as C<family>. | ||
| 317 | |||
| 318 | =item C<user> | ||
| 319 | |||
| 320 | The user object for this entity. | ||
| 321 | This should be a L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 322 | Note that L<Data::TagDB::Tag> implements L<Data::Identifier::Interface::Simple> and can therefore be used. | ||
| 323 | |||
| 324 | =item C<username> | ||
| 325 | |||
| 326 | The username for this entity. | ||
| 327 | Can be a plain name, or a L<Lingua::famibeib::Word> or L<Data::Identifier> or any L<Data::Identifier::Interface::Simple>. | ||
| 328 | Note that L<Data::TagDB::Tag> implements L<Data::Identifier::Interface::Simple> and can therefore be used. | ||
| 329 | |||
| 330 | =back | ||
| 331 | |||
| 332 | =head1 AUTHOR | ||
| 333 | |||
| 334 | Philipp Schafft <lion@cpan.org> | ||
| 335 | |||
| 336 | =head1 COPYRIGHT AND LICENSE | ||
| 337 | |||
| 338 | This software is Copyright (c) 2025-2026 by Philipp Schafft <lion@cpan.org>. | ||
| 339 | |||
| 340 | This is free software, licensed under: | ||
| 341 | |||
| 342 | The Artistic License 2.0 (GPL Compatible) | ||
| 343 | |||
| 344 | =cut |