不知道這是不是你的意思,但是如果你用下面的方式構建你的用戶名腳本(我用PHP作爲語言),你可以通過添加更高模糊因子的選項來擴展它,同時保持代碼的可讀性:
echo findName('FLINTSTONE', 'FRED');
function findName($last, $first) {
for ($fuzzFactor = 0; ; $fuzzFactor++) {
$candidates = fuzzNames($last, $first, $fuzzFactor);
if (empty($candidates)) {
// exhausted
return "sorry, I'm out of options!";
}
foreach ($candidates as $candidate) {
if (isUnique($candidate)) {
return $candidate;
}
}
}
}
function fuzzNames($last, $first, $fuzzFactor) {
switch ($fuzzFactor) {
case 0:
// no fuzz, return first choice
return array(substr($last, 0, 3) . $first[0]);
case 1:
// replace the third letter of the last name
// by the fourth/fifth/.../last letter (FLNF, FLTF, ... , FLEF)
$candidates = array();
for ($i = 3; $i < strlen($last); $i++) {
$candidates[] = substr($last, 0, 2) . $last[$i] . $first[0];
}
return $candidates;
case 2:
// replace the second and third letter of the last name
// by their follow-ups (FINF, FITF, ... , FNEF)
$candidates = array();
for ($i = 2; $i < strlen($last) - 1; $i++) {
for ($j = $i + 1; $j < strlen($last); $j++) {
$candidates[] = $last[0] . $last[$i] . $last[$j] . $first[0];
}
}
return $candidates;
default:
return array();
}
}