Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1431926
NamesGenerator.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
NamesGenerator.php
View Options
<?php
namespace
Eris\Generator
;
use
Eris\Generator
;
use
Eris\Generators
;
use
Eris\Random\RandomRange
;
function
names
()
{
return
Generators
::
names
();
}
/**
* @template-implements Generator<string>
*/
class
NamesGenerator
implements
Generator
{
private
$list
;
/**
* @link http://data.bfontaine.net/names/firstnames.txt
*/
public
static
function
defaultDataSet
()
{
return
new
self
(
array_map
(
function
(
$line
)
{
return
trim
(
$line
,
"
\n
"
);
},
file
(
__DIR__
.
"/first_names.txt"
)
)
);
}
public
function
__construct
(
array
$list
)
{
$this
->
list
=
$list
;
}
public
function
__invoke
(
$size
,
RandomRange
$rand
)
{
$candidateNames
=
$this
->
filterDataSet
(
$this
->
lengthLessThanOrEqualTo
(
$size
)
);
if
(!
$candidateNames
)
{
return
GeneratedValueSingle
::
fromJustValue
(
''
,
'names'
);
}
$index
=
$rand
->
rand
(
0
,
count
(
$candidateNames
)
-
1
);
return
GeneratedValueSingle
::
fromJustValue
(
$candidateNames
[
$index
],
'names'
);
}
public
function
shrink
(
GeneratedValue
$value
)
{
$candidateNames
=
$this
->
filterDataSet
(
$this
->
lengthSlightlyLessThan
(
strlen
(
$value
->
unbox
()))
);
if
(!
$candidateNames
)
{
return
$value
;
}
$distances
=
$this
->
distancesBy
(
$value
->
unbox
(),
$candidateNames
);
return
GeneratedValueSingle
::
fromJustValue
(
$this
->
minimumDistanceName
(
$distances
),
'names'
);
}
private
function
filterDataSet
(
callable
$predicate
)
{
return
array_values
(
array_filter
(
$this
->
list
,
$predicate
));
}
private
function
lengthLessThanOrEqualTo
(
$size
)
{
return
function
(
$name
)
use
(
$size
)
{
return
strlen
(
$name
)
<=
$size
;
};
}
private
function
lengthSlightlyLessThan
(
$size
)
{
$lowerLength
=
$size
-
1
;
return
function
(
$name
)
use
(
$lowerLength
)
{
return
strlen
(
$name
)
===
$lowerLength
;
};
}
private
function
distancesBy
(
$value
,
array
$candidateNames
)
{
$distances
=
[];
foreach
(
$candidateNames
as
$name
)
{
$distances
[
$name
]
=
levenshtein
(
$value
,
$name
);
}
return
$distances
;
}
private
function
minimumDistanceName
(
$distances
)
{
$minimumDistance
=
min
(
$distances
);
$candidatesWithEqualDistance
=
array_filter
(
$distances
,
function
(
$distance
)
use
(
$minimumDistance
)
{
return
$distance
==
$minimumDistance
;
}
);
return
array_keys
(
$candidatesWithEqualDistance
)[
0
];
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 21:14 (1 d, 13 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
92/42/8e0eb531c78e2dfd8b7f1e8e8fa5
Default Alt Text
NamesGenerator.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment