Last active
April 18, 2016 20:33
-
-
Save steverobbins/ac00861aa11ec78268e54de1b0a6255e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('THRESHOLD', 0.1); | |
// [address 1, address 2, should trigger fraud?] | |
$tests = [ | |
// Addresses are exactly the same | |
[ | |
[ | |
'street' => '16555 Von Karman Ave', | |
'city' => 'Irvine', | |
'region' => 'CA', | |
'postcode' => '92606', | |
'country' => 'US', | |
], | |
[ | |
'street' => '16555 Von Karman Ave', | |
'city' => 'Irvine', | |
'region' => 'CA', | |
'postcode' => '92606', | |
'country' => 'US', | |
], | |
true | |
], | |
// Addresses the same except for city/state | |
[ | |
[ | |
'street' => '16555 Von Karman Ave', | |
'city' => 'Irvine', | |
'region' => 'CA', | |
'postcode' => '92606', | |
'country' => 'US', | |
], | |
[ | |
'street' => '16555 Von Karman Ave', | |
'city' => 'Irvine', | |
'region' => 'NY', | |
'postcode' => '88888', | |
'country' => 'US', | |
], | |
false | |
], | |
// One door down in street number | |
[ | |
[ | |
'street' => '16555 Von Karman Ave', | |
'city' => 'Irvine', | |
'region' => 'CA', | |
'postcode' => '92606', | |
'country' => 'US', | |
], | |
[ | |
'street' => '16556 Von Karman Ave', | |
'city' => 'Irvine', | |
'region' => 'CA', | |
'postcode' => '92606', | |
'country' => 'US', | |
], | |
true | |
], | |
// Same city, different streets | |
[ | |
[ | |
'street' => '16555 Von Karman Ave', | |
'city' => 'Irvine', | |
'region' => 'CA', | |
'postcode' => '92606', | |
'country' => 'US', | |
], | |
[ | |
'street' => '71 Technology Dr', | |
'city' => 'Irvine', | |
'region' => 'CA', | |
'postcode' => '92618', | |
'country' => 'US', | |
], | |
false | |
], | |
// Different street/city | |
[ | |
[ | |
'street' => '16555 Von Karman Ave', | |
'city' => 'Irvine', | |
'region' => 'CA', | |
'postcode' => '92606', | |
'country' => 'US', | |
], | |
[ | |
'street' => '2595 E Imperial Hwy', | |
'city' => 'Brea', | |
'region' => 'CA', | |
'postcode' => '92821', | |
'country' => 'US', | |
], | |
false | |
], | |
// Saurabh's example | |
[ | |
[ | |
'street' => '294 Acme', | |
'city' => 'Oakland', | |
'region' => 'CA', | |
'postcode' => '94615', | |
'country' => 'US', | |
], | |
[ | |
'street' => '29 Clarewood Lane', | |
'city' => 'Oakland', | |
'region' => 'CA', | |
'postcode' => '94615', | |
'country' => 'US', | |
], | |
false | |
], | |
]; | |
function isFraud($address1, $address2) | |
{ | |
$a1 = strtolower(implode(' ', [ | |
$address1['street'], | |
$address1['city'], | |
$address1['region'], | |
$address1['postcode'], | |
$address1['country'], | |
])); | |
$a2 = strtolower(implode(' ', [ | |
$address2['street'], | |
$address2['city'], | |
$address2['region'], | |
$address2['postcode'], | |
$address2['country'], | |
])); | |
$lev = levenshtein($a1, $a2); | |
//var_dump($lev / strlen($a1)); | |
return [$lev / strlen($a1), $lev / strlen($a1) <= THRESHOLD]; | |
} | |
?><html> | |
<head> | |
<title>Affiliate Address Fraud Tests</title> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous"> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="page-header"> | |
<h1>Affiliate Address Fraud Tests</h1> | |
<h4>Threshold: <?php echo THRESHOLD ?></h4> | |
</div> | |
<table class="table"> | |
<thead> | |
<tr> | |
<th>Test #</th> | |
<th>Customer's Address</th> | |
<th>Affiliate's Address</th> | |
<th>Sameness</th> | |
<th>Expected</th> | |
<th>Received</th> | |
<th>Status</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach ($tests as $i => $test): ?> | |
<tr> | |
<td><?php echo $i + 1 ?></td> | |
<td> | |
<?php echo $test[0]['street'] ?><br /> | |
<?php echo $test[0]['city'] ?>, <?php echo $test[0]['region'] ?> <?php echo $test[0]['postcode'] ?> <?php echo $test[0]['country'] ?> | |
</td> | |
<td> | |
<?php echo $test[1]['street'] ?><br /> | |
<?php echo $test[1]['city'] ?>, <?php echo $test[1]['region'] ?> <?php echo $test[1]['postcode'] ?> <?php echo $test[1]['country'] ?> | |
</td> | |
<?php list($num, $result) = $result = isFraud($test[0], $test[1]) ?> | |
<td><?php echo round((1 - $num) * 100, 2) ?>%</td> | |
<td><?php echo $test[2] ? 'Fraudulent' : 'Valid' ?></td> | |
<td><?php echo $result ? 'Fraudulent' : 'Valid' ?></td> | |
<td> | |
<?php if ($result === $test[2]): ?> | |
<span class="label label-success">Pass</span> | |
<?php else: ?> | |
<span class="label label-danger">Fail</span> | |
<?php endif ?> | |
</td> | |
</tr> | |
<?php endforeach ?> | |
</tbody> | |
</table> | |
</div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment