Brett
05-26-2006, 04:55 PM
OK, I took up the challenge just to show that AMS can handle the same sorts of stuff as PHP. Look at the attached project. The functions are in the Globals. The code that triggers it is in the On Click event.
For those of you who don't want to download: (NOTE: I know the code could be cleaned up a lot - I wanted to do it quickly)
Global functions:
table_keyboard = {}
-- top row
table_keyboard['1'] = {'2', 'q'};
table_keyboard['2'] = {'1', 'q', 'w', '3'};
table_keyboard['3'] = {'2', 'w', 'e', '4'};
table_keyboard['4'] = {'3', 'e', 'r', '5'};
table_keyboard['5'] = {'4', 'r', 't', '6'};
table_keyboard['6'] = {'5', 't', 'y', '7'};
table_keyboard['7'] = {'6', 'y', 'u', '8'};
table_keyboard['8'] = {'7', 'u', 'i', '9'};
table_keyboard['9'] = {'8', 'i', 'o', '0'};
table_keyboard['0'] = {'9', 'o', 'p', '-'};
table_keyboard['-'] = {'0', 'p'};
-- 2nd from top
table_keyboard['q'] = {'1', '2', 'w', 'a'};
table_keyboard['w'] = {'q', 'a', 's', 'e', '3', '2'};
table_keyboard['e'] = {'w', 's', 'd', 'r', '4', '3'};
table_keyboard['r'] = {'e', 'd', 'f', 't', '5', '4'};
table_keyboard['t'] = {'r', 'f', 'g', 'y', '6', '5'};
table_keyboard['y'] = {'t', 'g', 'h', 'u', '7', '6'};
table_keyboard['u'] = {'y', 'h', 'j', 'i', '8', '7'};
table_keyboard['i'] = {'u', 'j', 'k', 'o', '9', '8'};
table_keyboard['o'] = {'i', 'k', 'l', 'p', '0', '9'};
table_keyboard['p'] = {'o', 'l', '-', '0'};
-- home row
table_keyboard['a'] = {'z', 's' , 'w', 'q'};
table_keyboard['s'] = {'a', 'z', 'x', 'd', 'e', 'w'};
table_keyboard['d'] = {'s', 'x', 'c', 'f', 'r', 'e'};
table_keyboard['f'] = {'d', 'c', 'v', 'g', 't', 'r'};
table_keyboard['g'] = {'f', 'v', 'b', 'h', 'y', 't'};
table_keyboard['h'] = {'g', 'b', 'n', 'j', 'u', 'y'};
table_keyboard['j'] = {'h', 'n', 'm', 'k', 'i', 'u'};
table_keyboard['k'] = {'j', 'm', 'l', 'o', 'i'};
table_keyboard['l'] = {'k', 'p', 'o'};
-- bottom row
table_keyboard['z'] = {'x', 's', 'a'};
table_keyboard['x'] = {'z', 'c', 'd', 's'};
table_keyboard['c'] = {'x', 'v', 'f', 'd'};
table_keyboard['v'] = {'c', 'b', 'g', 'f'};
table_keyboard['b'] = {'v', 'n', 'h', 'g'};
table_keyboard['n'] = {'b', 'm', 'j', 'h'};
table_keyboard['m'] = {'n', 'k', 'j'};
function StringToTable(inputstring)
local tblReturn = {};
local length = String.Length(inputstring);
for i = 1, length do
tblReturn[i] = String.Mid(inputstring,i,1);
end
return tblReturn;
end
function TableToString(tableinput)
local strReturn = "";
for i, letter in tableinput do
strReturn = strReturn..letter;
end
return strReturn;
end
-- accepts a string
-- returns array of likely single "wrong key" typos
-- arrays contain only characters that are valid domain names
function getWrongKeyTypos( word )
word = String.Lower(word);
typos = {};
typos_index = 1;
length = String.Length( word );
-- check each character
for i = 1, length do
-- if character has replacements then create all replacements
char = String.Mid(word,i,1);
if( table_keyboard[char] ) then
for index, letter in table_keyboard[char] do
scratch = String.Left(word,i-1);
scratch = scratch..letter;
scratch = scratch..String.Right(word,length-i);
typos[typos_index] = scratch;
typos_index = typos_index + 1;
end
end -- if
end -- for
return typos;
end
-- accepts a string
-- returns array of likely single missed character typos
-- arrays contain only characters that are valid domain names
function getMissedCharTypos( word )
word = String.Lower(word);
typos = {};
typos_index = 1;
length = String.Length( word );
-- check each character
for i = 1, length do
local tempWord = String.Left(word,i-1);
tempWord = tempWord..String.Right(word,length-i);
typos[typos_index] = tempWord;
typos_index = typos_index + 1;
end
return typos;
end
-- accepts a string
-- returns array of likely transposed character typos
-- arrays contain only characters that are valid domain names
function getTransposedCharTypos( word )
word = String.Lower(word);
typos = {};
typos_index = 1;
length = String.Length( word );
-- check each character
for i = 1, length do
if( i ~= length )then
local tblWord = StringToTable(word);
local tempChar = tblWord[i];
tblWord[i] = tblWord[i+1];
tblWord[i+1] = tempChar;
local tempWord = TableToString(tblWord);
typos[typos_index] = tempWord;
typos_index = typos_index + 1;
end
end
return typos;
end
-- accepts a string
-- returns array of likely double entered character typos
-- arrays contain only characters that are valid domain names
function getDoubleCharTypos( word )
word = String.Lower(word);
typos = {};
typos_index = 1;
length = String.Length( word );
-- check each character
for i = 1, length do
local char = String.Mid(word,i,1);
local tempWord = String.Left(word,i);
tempWord = tempWord..char;
tempWord = tempWord..String.Right(word,length-i);
typos[typos_index] = tempWord;
typos_index = typos_index + 1;
end
return typos;
end
In the On Click of the button:
Input.SetText("Output","");
local strOutput = "";
local strTheWord = Input.GetText("Word");
-- Wrong Keystrike
strOutput = strOutput.."Wrong Keystrike:\r\n\r\n";
tblTypos = getWrongKeyTypos(strTheWord);
if(tblTypos)then
for i,word in tblTypos do
strOutput = strOutput..word;
strOutput = strOutput.."\r\n";
end
end
-- Missed Characters
strOutput = strOutput.."\r\nMissed Characters:\r\n\r\n";
tblTypos = getMissedCharTypos(strTheWord);
if(tblTypos)then
for i,word in tblTypos do
strOutput = strOutput..word;
strOutput = strOutput.."\r\n";
end
end
-- Single Transposed
strOutput = strOutput.."\r\nSingle Transposed Character:\r\n\r\n";
tblTypos = getTransposedCharTypos(strTheWord);
if(tblTypos)then
for i,word in tblTypos do
strOutput = strOutput..word;
strOutput = strOutput.."\r\n";
end
end
-- Double Character
strOutput = strOutput.."\r\nDouble Character:\r\n\r\n";
tblTypos = getDoubleCharTypos(strTheWord);
if(tblTypos)then
for i,word in tblTypos do
strOutput = strOutput..word;
strOutput = strOutput.."\r\n";
end
end
Input.SetText("Output",strOutput);
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.