
=head1 NAME

randomPerson.pl - Load macros for getting a random person with corresponding pronouns.

=head1 SYNOPSIS

    loadMacros('randomPerson.pl');

=head1 DESCRIPTION

C<randomPerson.pl> provides a C<randomPerson> function that generates
a random name that comes with corresponding pronouns and verb conjugation.
The default subject pronouns are 'he', 'she', and 'they'.

This idea evolved from the C<PCCmacros.pl> C<RandomName> subroutine,
extending it to cover pronouns and verb conjugation.

The first names (aka given names) were generated by randomly selecting 200
names from the United States Social Security Administration's top 1000 list
(actually 1000 per gender) from 2005.  The additional names associated with
they/them pronouns were taken from a
L<wikipedia list of public trans people|https://en.wikipedia.org/wiki/List_of_transgender_people>
who use they pronouns.  The last names (aka surnames) were generated by
randomly selecting 100 names from a list of top 1000 surnames in the United
States in 2020, according to babynames.com, which used the 2020 US Census.
That list was still lacking in representation of Asian, South Asian, and
African surnames, so 12 popular names from these regions were added.

=head2 Usage

First load the C<randomPerson> macro with

    loadMacros('randomPerson.pl');

and then call the randomPerson subroutine

    $p = randomPerson();

The variable C<$p> is now a C<Person> object with methods to access the names,
pronouns and verb conjugation.  It can be used within a problem as

    BEGIN_PGML
    [$p] travels 1.5 miles to school.
    After school, [$p->they] [$p->go] to work.
    [$p->Their] dog greets [$p->them] when [$p->they] [$p->verb('get')] home.
    The books on the table are [$p->theirs].
    END_PGML

Depending on the he/she/they pronoun, the methods C<they>, C<them>, C<their> and
C<theirs> (with or without capitalization) will select the correct subject, object,
possession, possesive forms of the pronoun.  Also, note that C<go> is one of a
few special verbs with irregular conjugation.

If you would like multiple people to be randomly choosen with unique names, use

    @persons = randomPerson(n => 4);

generates an array of 4 unique C<Person> objects.

Additionally, you can specify your own list of names and pronouns to draw from.
For example

    $p1 = randomPerson(names => [['Bart' => 'he'], ['Lisa' => 'she']]);

or without the pronouns, which will be assigned randomly from he/she/they:

    $p2 = randomPerson(names => ['Bart', 'Lisa']);

To use pronouns beyond 'he', 'she', and 'they', first add the pronoun and its
related forms like this:

    $Person::PRONOUNS{'it'} = { possessive => 'its', possession => 'its', object => 'it' };

If a new pronoun is added, verb conjugation for that pronoun will match 'he'/'she'
verb conjugation, not 'they' verb conjugation.


=cut

loadMacros('PGbasicmacros.pl', 'PGauxiliaryFunctions.pl');

@namespronouns = (
	[ Adeline    => 'she' ],
	[ Aiden      => 'they' ],
	[ Alan       => 'he' ],
	[ Alana      => 'she' ],
	[ Alayna     => 'she' ],
	[ Alec       => 'they' ],
	[ Alessandro => 'he' ],
	[ Alex       => 'he' ],
	[ Alexa      => 'she' ],
	[ Alexia     => 'she' ],
	[ Alia       => 'she' ],
	[ Allison    => 'she' ],
	[ Allyson    => 'she' ],
	[ Alyson     => 'she' ],
	[ Alyssa     => 'she' ],
	[ Amelia     => 'she' ],
	[ Amira      => 'she' ],
	[ Amiyah     => 'she' ],
	[ Annika     => 'she' ],
	[ Aracely    => 'she' ],
	[ Arnav      => 'he' ],
	[ Arturo     => 'he' ],
	[ Aryana     => 'she' ],
	[ Ashanti    => 'she' ],
	[ Autumn     => 'she' ],
	[ Averie     => 'she' ],
	[ Bradley    => 'he' ],
	[ Brendan    => 'he' ],
	[ Broderick  => 'he' ],
	[ Caleb      => 'he' ],
	[ Cameron    => 'he' ],
	[ Camilla    => 'she' ],
	[ Candice    => 'they' ],
	[ Carissa    => 'she' ],
	[ Cecelia    => 'she' ],
	[ Chad       => 'he' ],
	[ Chaim      => 'he' ],
	[ Charlie    => 'she' ],
	[ Clayton    => 'he' ],
	[ Clinton    => 'he' ],
	[ Cohen      => 'he' ],
	[ Colin      => 'he' ],
	[ Colt       => 'he' ],
	[ Conor      => 'he' ],
	[ Cruz       => 'he' ],
	[ Dakota     => 'she' ],
	[ Dana       => 'she' ],
	[ Dane       => 'he' ],
	[ Dangelo    => 'he' ],
	[ Danica     => 'she' ],
	[ Darion     => 'he' ],
	[ Darrius    => 'he' ],
	[ Dayanara   => 'she' ],
	[ Deborah    => 'she' ],
	[ Delilah    => 'she' ],
	[ Don        => 'they' ],
	[ Donald     => 'he' ],
	[ Donavan    => 'he' ],
	[ Dorian     => 'they' ],
	[ Draven     => 'he' ],
	[ Duncan     => 'he' ],
	[ Dylan      => 'they' ],
	[ Edgar      => 'he' ],
	[ El         => 'they' ],
	[ Elaine     => 'she' ],
	[ Elias      => 'he' ],
	[ Eliot      => 'they' ],
	[ Elliott    => 'he' ],
	[ Elsie      => 'she' ],
	[ Emanuel    => 'he' ],
	[ Emilie     => 'she' ],
	[ Esteban    => 'he' ],
	[ Evelyn     => 'she' ],
	[ Everett    => 'he' ],
	[ Ezekiel    => 'he' ],
	[ Ezra       => 'they' ],
	[ Fabian     => 'he' ],
	[ Fabiola    => 'she' ],
	[ Fatima     => 'she' ],
	[ Felicity   => 'she' ],
	[ Finn       => 'they' ],
	[ Fox        => 'they' ],
	[ Frances    => 'she' ],
	[ Freddy     => 'he' ],
	[ Gabriel    => 'he' ],
	[ Garcia     => 'they' ],
	[ Garrison   => 'he' ],
	[ Geoffrey   => 'he' ],
	[ Gilberto   => 'he' ],
	[ Gonzalo    => 'he' ],
	[ Gregory    => 'he' ],
	[ Greyson    => 'they' ],
	[ Guillermo  => 'he' ],
	[ Hailey     => 'she' ],
	[ Harley     => 'he' ],
	[ Haven      => 'she' ],
	[ Heath      => 'he' ],
	[ Hugo       => 'he' ],
	[ Humberto   => 'he' ],
	[ Ignacio    => 'he' ],
	[ Indya      => 'they' ],
	[ Isiah      => 'he' ],
	[ Ismael     => 'he' ],
	[ Jadyn      => 'he' ],
	[ Jaidyn     => 'he' ],
	[ Jailyn     => 'she' ],
	[ Jamison    => 'he' ],
	[ Jayda      => 'she' ],
	[ Jayleen    => 'she' ],
	[ Jazlyn     => 'she' ],
	[ Jenifer    => 'she' ],
	[ Jenny      => 'she' ],
	[ Jessica    => 'she' ],
	[ Jolette    => 'she' ],
	[ Jonathan   => 'he' ],
	[ Josephine  => 'she' ],
	[ Josh       => 'he' ],
	[ Josiah     => 'he' ],
	[ Jovan      => 'he' ],
	[ Jovanni    => 'he' ],
	[ Julien     => 'he' ],
	[ Juliet     => 'she' ],
	[ Kaeden     => 'he' ],
	[ Kailey     => 'she' ],
	[ Kamden     => 'he' ],
	[ Kara       => 'she' ],
	[ Karley     => 'she' ],
	[ Katarina   => 'she' ],
	[ Katlyn     => 'she' ],
	[ Kaylen     => 'she' ],
	[ Kaylin     => 'she' ],
	[ Kelsey     => 'she' ],
	[ Kelvin     => 'he' ],
	[ Kiana      => 'she' ],
	[ Kiersten   => 'she' ],
	[ Lachlan    => 'they' ],
	[ Lamar      => 'he' ],
	[ Laney      => 'she' ],
	[ Lauren     => 'she' ],
	[ Layshia    => 'they' ],
	[ Leroy      => 'he' ],
	[ Lisa       => 'she' ],
	[ Lizette    => 'she' ],
	[ Lucian     => 'he' ],
	[ Mackenzie  => 'she' ],
	[ Makayla    => 'she' ],
	[ Makena     => 'she' ],
	[ Malik      => 'he' ],
	[ Mariana    => 'she' ],
	[ Marisol    => 'she' ],
	[ Marshall   => 'he' ],
	[ Marvin     => 'he' ],
	[ Mary       => 'she' ],
	[ Mateo      => 'he' ],
	[ Matteo     => 'he' ],
	[ Matty      => 'they' ],
	[ Max        => 'they' ],
	[ Mckenzie   => 'she' ],
	[ Meghan     => 'she' ],
	[ Melany     => 'she' ],
	[ Melvin     => 'he' ],
	[ Mercedes   => 'she' ],
	[ Messiah    => 'he' ],
	[ Miguel     => 'he' ],
	[ Mikaela    => 'she' ],
	[ Mohammad   => 'he' ],
	[ Molly      => 'she' ],
	[ Myla       => 'she' ],
	[ Nayeli     => 'she' ],
	[ Neil       => 'he' ],
	[ Orlando    => 'he' ],
	[ Pamela     => 'she' ],
	[ Paola      => 'she' ],
	[ Payton     => 'she' ],
	[ Phoenix    => 'he' ],
	[ Precious   => 'she' ],
	[ Prince     => 'he' ],
	[ Rae        => 'they' ],
	[ Ray        => 'they' ],
	[ Rebeca     => 'she' ],
	[ Reese      => 'she' ],
	[ Reginald   => 'he' ],
	[ Reina      => 'she' ],
	[ Renee      => 'she' ],
	[ Rhiannon   => 'she' ],
	[ Rhys       => 'he' ],
	[ Ricky      => 'he' ],
	[ Riley      => 'he' ],
	[ Rocky      => 'he' ],
	[ Rodney     => 'he' ],
	[ Rogelio    => 'he' ],
	[ Roland     => 'he' ],
	[ Rosemary   => 'she' ],
	[ Ryleigh    => 'she' ],
	[ Sammy      => 'he' ],
	[ Selah      => 'she' ],
	[ Serenity   => 'she' ],
	[ Seth       => 'he' ],
	[ Shannon    => 'she' ],
	[ Shayla     => 'she' ],
	[ Sincere    => 'he' ],
	[ Skye       => 'she' ],
	[ Taliyah    => 'she' ],
	[ Taniya     => 'she' ],
	[ Taryn      => 'she' ],
	[ Tommy      => 'they' ],
	[ Tony       => 'he' ],
	[ Trevon     => 'he' ],
	[ Trinity    => 'she' ],
	[ Triston    => 'he' ],
	[ Tucker     => 'he' ],
	[ Ty         => 'he' ],
	[ Tyler      => 'they' ],
	[ Tyrese     => 'he' ],
	[ Valentina  => 'she' ],
	[ Xzavier    => 'he' ],
	[ Yair       => 'he' ],
	[ Yuliana    => 'she' ],
	[ Zane       => 'he' ],
	[ Zoey       => 'she' ]
);

@lastnames = qw(
	Arroyo       Barajas   Barron     Bates      Baxter    Bender    Benitez    Berg
	Black        Boyd      Bradshaw   Bush       Campbell  Carr      Carson     Case
	Christensen  Cline     Cole       Davenport  Davidson  Decker    Delgado    Dudley
	Dunlap       Farrell   Faulkner   Fletcher   Fox       Friedman  Glass      Gomez
	Gonzalez     Harding   Henderson  Hernandez  Hoang     Hosseini  Howe       Huff
	Huffman      Jones     Jordan     Kane       Keith     Kim       Kumari     LeBlanc
	Lee          Lindsey   Little     Love       Lu        Magana    Manning    Marks
	Massey       Maynard   McCoy      Melendez   Meyers    Mills     Moore      Morris
	Morse        Morton    Nielsen    O'Connor   Orr       Osman     Patel      Pearson
	Petersen     Pham      Pitts      Pope       Portillo  Pruitt    Pugh       Quintero
	Raymond      Reynolds  Rollins    Ross       Schwartz  Scott     Simmons    Simon
	Singh        Sloan     Solomon    Stafford   Stevens   Stone     Stuart     Swanson
	Tanaka       Tanner    Tate       Thomas     Traore    Trujillo  Velasquez  Velez
	Vu           Warner    Warren     Webb       Yakubu    Yang      Zakaria    Zamora
);

=head1 FUNCTIONS

=head2 randomPerson

Returns a person as a Person object from a list in the macro.

=head3 Examples and Options

=over

=item * No arguments returns a single random person

Example

    randomPerson()

=item * C<S<< n => k >>> returns an array of k Person objects with unique names.

Example

    randomPerson(n=>5)

returns an array of 5 Person objects with unique names.

=item * C<S<< names => arrayref >>> returns either a single Person or array of Persons
from the given names

If the arrayref is in the form of C<S<< [['name1' => 'pronoun1'],['name2' => 'pronoun2'], ...] >>> then
a person selected randomly from the given list is returned.

Example:

    $a = randomPerson(names => [[ Bart => 'he' ], [ Lisa => 'she' ], [ Matty => 'they' ]]);

Alternatively, each person name/pronoun can be set as an hashref.
For example the above can be written:

    $a = randomPerson(names => [
        { name => 'Bart', pronoun => 'he' },
        { name => 'Lisa', pronoun => 'she' },
        { name => 'Matty', pronoun => 'they' }
    ]);

If the pronoun is missing using either arrayrefs or hashrefs, then a pronoun is determined randomly.
Each of the following are legal

    $p1 = randomPerson( names => ['Larry', 'Moe', 'Curly']);
    $p2 = randomPerson( names => ['Larry', [ Moe => 'he' ], 'Curly']);
    $p3 = randomPerson( names => [{ name => 'Larry'}, { name => 'Moe' }, { name => 'Curly' }]);
    $p4 = randomPerson( names => [{ name => 'Larry'}, { name => 'Moe', pronoun => 'he' }, { name => 'Curly' }]);

And the option C<n> can be used to return an array with that number of persons.

    @p = randomPerson(n => 2, names => [[ Bart => 'he' ], [ Lisa => 'she' ], [ Matty => 'they' ]]);

=back

=cut

sub randomPerson {
	my %options = (n => 1, @_);
	my @persons;

	# If the names are passed in.
	if ($options{names}) {
		for my $p (@{ $options{names} }) {
			if (ref $p eq 'ARRAY') {
				push(@persons, [ $p->[0], $p->[1] // list_random('he', 'she', 'they') ]);
			} elsif (ref $p eq 'HASH') {
				push(@persons, [ $p->{name}, $p->{pronoun} // list_random('he', 'she', 'they') ]);
			} else {
				push(@persons, [ $p, list_random('he', 'she', 'they') ]);
			}
		}
	} else {
		# select the names from the global @namespronouns array above.
		@persons = @namespronouns;
	}

	my @names = map { Person->new(name => $_->[0], pronoun => $_->[1]) } random_subset($options{n}, \@persons);
	return wantarray ? @names : $names[0];
}

=head2 randomLastName

This returns a random last name based on popular last names in the United States.  Example

    $p = randomLastName();

Note it is just a string, and doesn't have the pronouns that the Person object does.

If an array is requested and a number is passed in the form C<S<< n => k >>> (which defaults to 1),
then an array of k unique last names is returned.

    @lastnames = randomLastName(n => 4);

generates 4 unique last names.

=cut

sub randomLastName {
	my %options = (n => 1, @_);
	my @names   = random_subset($options{n}, @lastnames);
	return wantarray ? @names : $names[0];
}

=head1 CONSTRUCTOR (Person object)

This makes a Person object to handle name and pronouns of a Person.

Make a Person with

    Person->new(name => 'Roger', pronoun => 'he')

for example. This is used by the C<randomPerson> method which returns a blessed Person object
which can be used to write a problem with a random name with pronouns and verb conjugation.

=cut

package Person;
our @ISA = ("Value::String");

our %PRONOUNS = (
	he   => { possessive => 'his',   possession => 'his',    object => 'him' },
	she  => { possessive => 'her',   possession => 'hers',   object => 'her' },
	they => { possessive => 'their', possession => 'theirs', object => 'them' },
);

sub new {
	my ($class, %opts) = @_;
	die "The field 'pronoun' must be passed in." unless defined($opts{pronoun});

	die 'The acceptable pronouns are:'
		. join(', ', main::lex_sort(keys %PRONOUNS))
		. ". You passed in '$opts{pronoun}'"
		if !defined($PRONOUNS{ $opts{pronoun} });
	die 'The field "name" must be passed in' unless defined($opts{name});
	my $self = {
		name    => $opts{name},
		pronoun => $opts{pronoun}
	};
	bless $self, $class;
	return $self;
}

sub string {
	return shift->{name};
}

sub TeX {
	return '\text{' . shift->{name} . '}';
}

=head1 METHODS (Person object)

=head2 name

This returns the name of the person.

    $p = new Person(name => 'Roger', pronoun => 'he');
    $p->name;

returns the name 'Roger'.

=cut

sub name {
	return shift->{name};
}

=head2 subject

=head2 they

These return the subject pronoun in lowercase. The second option is syntactic sugar.

    $p->subject
    $p->they

returns one of (he, she, they)

=cut

sub subject {
	return shift->{pronoun};
}

sub they {
	return shift->subject();
}

=head2 Subject

=head2 They

These return the subject pronoun, capitalized. The second option is syntactic sugar.

    $p->Subject
    $p->They

returns one of (He, She, They)

=cut

sub Subject {
	return ucfirst(shift->{pronoun});
}

sub They {
	return shift->Subject();
}

=head2 possessive

=head2 their

These return the possessive adjective in lowercase. The second option is syntactic sugar.

    $p->possessive
    $p->their

returns one of (his, her, their)

=cut

sub possessive {
	my $p = shift->{pronoun};
	return $PRONOUNS{$p}{possessive};
}

sub their {
	return shift->possessive();
}

=head2 Possessive

=head2 Their

These return the possessive adjective, capitalized. The second option is syntactic sugar.

    $p->Possessive
    $p->Their

returns one of (His, Her, Their)

=cut

sub Possessive {
	return ucfirst(shift->possessive);
}

sub Their {
	return shift->Possessive();
}

=head2 possession

=head2 theirs

These return the possessive pronoun in lowercase. The second option is syntactic sugar.

    $p->possession
    $p->theirs

returns one of (his, hers, theirs)

=cut

sub possession {
	my $p = shift->{pronoun};
	return $PRONOUNS{$p}{possession};
}

sub theirs {
	return shift->possession();
}

=head2 Possession

=head2 Theirs

These return the possessive pronoun, capitalized. The second option is syntactic sugar.

    $p->Possession
    %p->Theirs

returns one of (His, Hers, Theirs)

=cut

sub Possession {
	return ucfirst(shift->possession);
}

sub Theirs {
	return shift->Possession();
}

=head2 object

=head2 them

These return the object pronoun in lowercase. The second option is syntactic sugar.

    $p->object
    $p->them

returns one of (him, her, them)

=cut

sub object {
	my $p = shift->{pronoun};
	return $PRONOUNS{$p}{object};
}

sub them {
	return shift->object();
}

=head2 Object

=head2 Them

These return the object pronoun, capitalized. The second option is syntactic sugar.

    $p->Object
    $p->Them

returns (Him, Her, Them)

=cut

sub Object {
	return ucfirst(shift->object);
}

sub Them {
	return shift->Object();
}

=head2 verb/Verb

Returns the correct conjugation of the verb. If only one argument is passed in, it should
be a regular verb in the third person plural conjugation (the "they" version). For example:

    $p1 = new Person(name => 'Roger', pronoun => 'he');
    $p1->verb('find');

returns 'finds'. All that happens is an 's' or an 'es' is appended, or 'y'->'ies', according
to some simple English conjugation rules.

    $p2 = new Person(name => 'Max', pronoun => 'they');
    $p2->verb('find')

returns 'find'.  The verb can be called directly as a method as well.

If a second argument is passed, it should be the third person singular conjugation
of the same verb (the "he/she" version). For example:

    $p1 = new Person(name => 'Roger', pronoun => 'he');
    $p1->verb('fly', 'flies');

returns 'flies'

    $p2 = new Person(name => 'Max', pronoun => 'they');
    $p2->verb('fly', 'flies');

returns 'fly'

Captilization can be done by capitalizing the verb. For example:

    $p2 = new Person(name => 'Max', pronoun => 'they');
    $p2->Verb('Say');

returns 'Say'.

=cut

sub verb {
	my ($self, $plur, $sing) = @_;
	if (defined($sing)) {
		return ($self->{pronoun} eq 'they' ? $plur : $sing);
	} elsif ($self->can($plur) && !(grep { $_ eq lc($plur) } qw/object subject name/)) {
		return $self->$plur;
	} elsif ($plur =~ /(s|ch|sh)$/) {
		return ($self->{pronoun} eq 'they' ? $plur : $plur . 'es');
	} elsif ($plur =~ /[^aeou]y$/) {
		$sing = $plur;
		$sing =~ s/y$/ies/;
		return ($self->{pronoun} eq 'they' ? $plur : $sing);
	} else {
		return ($self->{pronoun} eq 'they' ? $plur : $plur . 's');
	}
}

=head2 do/Do

Returns the correct conjugation of to do with captilization. For example

    $p->do;

or

    $p->Do;

=cut

sub do {
	return shift->verb('do', 'does');
}

sub Do {
	return shift->verb('Do', 'Does');
}

=head2 are/Are

Returns the correct conjugation of to be with captilization.  For example

    $p->are;

or

    $p->Are;

=cut

sub are {
	return shift->verb('are', 'is');
}

sub Are {
	return shift->verb('Are', 'Is');
}

=head2 go/Go

Returns the correct conjugation of to go with captilization.  For example

    $p->go;

or

    $p->Go;

=cut

sub go {
	return shift->verb('go', 'goes');
}

sub Go {
	return shift->verb('Go', 'Goes');
}

=head2 have/Have

Returns the correct conjugation of to have with captilization.  For example

    $p->have;

or

    $p->Have;

=cut

sub have {
	return shift->verb('have', 'has');
}

sub Have {
	return shift->verb('Have', 'Has');
}

=head2 were/Were

Returns the correct conjugation of past tense of to be with captilization.  For example

    $p->were;

or

    $p->Were;

=cut

sub were {
	return shift->verb('were', 'was');
}

sub Were {
	return shift->verb('Were', 'Was');
}

=head2 theyre/Theyre

Returns the correct contraction for the pronoun with to be, with captilization.  For example

    $p->theyre;

or

    $p->Theyre;

=cut

sub theyre {
	my $self = shift;
	return $self->they . "'" . $self->verb('re', 's');
}

sub Theyre {
	my $self = shift;
	return $self->They . "'" . $self->verb('re', 's');
}

=head2 theyve/Theyve

Returns the correct contraction for the pronoun with to have, with captilization.  For example

    $p->theyve;

or

    $p->Theyve;

=cut

sub theyve {
	my $self = shift;
	return $self->they . "'" . $self->verb('ve', 's');
}

sub Theyve {
	my $self = shift;
	return $self->They . "'" . $self->verb('ve', 's');
}

1;
