Looking for a misspelling script

i’m wanting to take a block of text and randomly replace letters to create a sort of “anti-spellchecker”. something that mispells random words within the text.

i found a snip of javascript that will do it per letter, i can retrack that down and post it for reference if needed. it also takes into account the letters that are close to each other on the keyboard to make it look more “authentic”.

why would i want to purposely mispell words? well, i like to create posts in an FB group where we make fun of conspiracies by making ridiculous stories, like “The Onion” but even more perposterous. to add to the authenticity i’d like to make it look like a frantic tin foil hat wearing keyboard warrior is on the rampage just plucking keys. plus it is actually really hard to purposely type bad i find. :wink:

one thing i’d love to find as a side note, is a way to scan text, capture a typo, and see how far it is from the intended key. that way i could copy the text of a “real” typo’d conspiracy, and see if the typos are intentional, scanned char is on the opposite side of the keyboard therefor it was probably intentional.

thanks in advance and any feedback would be great!

Seems simple enough. I’d build it for $30

Not sure this forum is the place to be doing payments for such things. Not to say that there should or should not be, but there’s certainly an ethos of trying to be helpful. In that vein, here’s something I’ve quickly put together that might do the sort of thing being asked for.

Here’s the “gibberish” function I created. It takes two parameters. the first is the string to convert to something more like gibberish. The second parameter is a way of specifying how often a character is mis-typed. For example if you pass in 1000, one in every thousand characters will be changed from the original. If you enter 10 it will be one in every ten characters, so you can specify just how badly typed something can be.

//Pass in a string where every 1 in so many characters is accidentally mis-typed
function gibberish(p_strInput, p_intOneIn)
{
	//Characters that can be mis-typed
	const SHIFTABLE = "abcdefghijklmnopqrstuvwxyz";

	//JSON describing what each shiftable character can be shifted to
	const jsonGibber = {};
	jsonGibber.a = "qwsz";
	jsonGibber.b = "vghn";
	jsonGibber.c = "xdfv";
	jsonGibber.d = "serfxc";
	jsonGibber.e = "wsdr";
	jsonGibber.f = "drtgvc";
	jsonGibber.g = "ftyhbv";
	jsonGibber.h = "gyujnb";
	jsonGibber.i = "ujko";
	jsonGibber.j = "huikmn";
	jsonGibber.k = "mjiol";
	jsonGibber.l = "pok";
	jsonGibber.m = "njk";
	jsonGibber.n = "bhjm";
	jsonGibber.o = "iklp";
	jsonGibber.p = "ol";
	jsonGibber.q = "aw";
	jsonGibber.r = "edft";
	jsonGibber.s = "awedxz";
	jsonGibber.t = "rfgy";
	jsonGibber.u = "yhji";
	jsonGibber.v = "cfgb";
	jsonGibber.w = "qase";
	jsonGibber.x = "zsdc";
	jsonGibber.y = "tghu";
	jsonGibber.z = "asx";
	let strReturn = "";

	//Process every character in the text passed in
	for (var intCounter = 0; intCounter < p_strInput.length; intCounter++)
	{
		//For convenience, let's put the character in a variable
		let strChar = p_strInput[intCounter];

		//Is this character to be shifted to another character?
		if(Math.floor(Math.random() * p_intOneIn) + 1 == 1)
		{
			//Is the character we're intending to shift in our shiftable list?
			if(SHIFTABLE.includes(strChar))
			{
				//Get a random new character
				//let newChar = randCharFromString(jsonGibber[strChar]);
				let newChar = jsonGibber[strChar][Math.floor(Math.random() * jsonGibber[strChar].length)]
				//If the old character is upper case, make the new character upper case
				if (strChar === strChar.toUpperCase()) newChar = newChar.toUpperCase();
				//Replace the character
				strChar = newChar;
			}
		}
		strReturn += strChar;
	}

	//Return the updated string
	return strReturn;
}

First of all, I’d just based the mistyping on a standard alphabet. No accents or additions, no symbols. No numbers. These could of course be added in with a little additional effort.

When a character is mistyped, it looks up a character that is nearby. I just based the mistypings on the keys on my Apple QWERTY keyboard where there is reasonable overlap with a side of that character’s key. It doesn’t take into account any other keyboard types, statistical occurrences of common mis-typing or mis-spelling. It is just an equally likely slip in any direction on the qwerty keyboard that would result in another letter.

You could tailor the shifts to suit, and apply biases simply by repeating letters in the strings. For example, I believe it is much more likely that you would accidentally type an “a” or “d” instead of an “s” than a “w”, “e”, “z” or “x”. I could increase the likelihood of that simply by amending the ‘gibber’ string like this, making them five times more likely to occur.

jsonGibber.s = "aaaaawedddddxz";

I would suggest that with a bit of research and a bit of tinkering, this could be tailored to whatever language and alphabetic additions you like.

Here’s an example that calls the function with a few lines of typing practice (covering all the letters in the alphabet), where every 1 in 20 characters (at random) should be shifted. The result is put out to Scriptable’s console, but you could opt to pull in and update the clipboard for example.

console.log(gibberish(`The quick brown fox jumped over the lazy dog.
Jackdaws love my big sphinx of quartz.
How vexingly quick daft zebras jump.
Pack my box with five dozen liquor jugs.
The five boxing wizards jump quickly.`, 20));

Maybe that is something like what you were after?

on first glance it looks like it’s what i’m looking to do, let me digest it and get back later. i just wanted to say thank you for the reply.

i’m surprised that my one semester of javascript might have really taught me something. there’s no way i could have written it or conceived it. my pseudo code was way more complicated, so much that it prompted me to write the solicitation. which i agree was bad form and tacky. i just assumed it would be much more complex and was way out of my league. i edited the title and the post a little to clean it up and not encourage such behavior. i’m sorry, coffee makes people do spontaneous things ya know. :wink:

thanks again and i’ll report back!

forgive my stupidity, how do i pass text into it?

Look at the code example at the end.

gibberish is the function doing the work of making the text worse.

The first parameter (p_strInput) for this function is the text to make worse.

The second parameter (p_intOneIn) is the “one in N” factor.

That whole function and it’s parameters is being passed to the console object’s log function as it’s “write this text out to the log” parameter.

Please note that functions are a fundamental building block in JavaScript.

i actually get the code and remember the syntax structure, i’m suprised. i’m hacking at it and reading. :wink: