Skip to content

Instantly share code, notes, and snippets.

@luckyshot
Created January 28, 2015 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckyshot/ed3d3b661338499943e4 to your computer and use it in GitHub Desktop.
Save luckyshot/ed3d3b661338499943e4 to your computer and use it in GitHub Desktop.
Detects the gender (male or female) of a name
<?php
/**
* returns positive if male, negative if female
*/
function gender( $s )
{
$gender = 0; // female - , + male
$letters = array(
'a' => -5.652604738,
'b' => 0.4848823163,
'c' => 0.5988183031,
'd' => 1.710755277,
'e' => -1.615090236,
'f' => 0.6095456877,
'g' => 0.4637426344,
'h' => 0.1492751921,
'i' => -2.95725719,
'j' => 0.3618602821,
'k' => 0.4110798047,
'l' => -1.93138848,
'm' => -0.2297501411,
'n' => 0.6760146965,
'o' => 3.312554183,
'p' => 0.005272049275,
'q' => 0.03719266012,
'r' => 2.029755446,
's' => -0.06039585511,
't' => 0.4342335742,
'u' => 0.8660267662,
'v' => 0.3174885737,
'w' => 0.5574388945,
'x' => 0.1022798153,
'y' => -0.5130651266,
'z' => -0.1686643889,
);
$chars = count_chars( strtolower( $s ), 1 );
foreach ($chars as $char => $times)
{
$gender = $gender + ($letters[ chr($char) ] * $times);
}
$gender = $gender + $letters[ substr($s, -1, 1) ] * 5;
return $gender;
}
echo gender('John');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment