Is it possible to spell/grammar check player text in sugarcube?

Nah, when I first started with it, a lot of it went over my head too. I just skimmed though it, at first, so I could understand what was possible. Then, whenever I needed to do anything specific, I’d look it up again, read through the examples, and try stuff out until I understood it. I pretty much had the documentation open all the time when I first started.

The thing is, nobody is born knowing these things, so it’s all practice, read the documentation, practice, Google for help, practice, look at other people’s code, practice, oh, and did I mention practice?

Anyways, hopefully some of my stuff helps you figure things out a bit quicker than I did. :slight_smile:

2 Likes

Yeah true! Basically wherever the documentation is example heavy is where I’m more likely to understand hehe. Thanks so much for all your help!

Those hiding and showing links are so cool.

I think I can find the length easy enough by splitting the input variable into an array, but …
stupid question: How do I actually find the number of errors that the spellcheck found (so as to forward the player or not)?

In the setup.wordUpdate() function there’s a for() loop which checks each word. Within that loop, look for the if (words[i] !== suggestion) { ... } code. The code within that if() gets triggered each time an error is found. So, if you create a variable and set it to zero just before the for() loop (such as by doing var counter = 0;), then increment that variable within that if() statement (by doing counter += 1;), then that will count up the number of errors.

Then, after the for() loop you can use the variable (counter) to tell how many errors there were, and you can use words.length to tell how many words were typed. Using that information you can determine whether to show or hide the link.

So, the code around that for() loop might look something like this:

		// Add unrecognized words and suggestions to listbox.
		var counter = 0;
		for (var i = 0; i < words.length; i++) {
			if (setup.isProperty(wordSuggestions, words[i])) {
				suggestion = wordSuggestions[words[i]];
			} else {
				suggestion = find_similar(words[i], 0.7)[0][0];
				wordSuggestions[words[i]] = suggestion;
			}
			opt = document.createElement("option");
			opt.value = words[i];
			if (suggestion === undefined) {
				opt.text = words[i] + " (???)";  // No suggestion.
			} else {
				opt.text = words[i] + " (" + suggestion + "?)";
			}
			if (words[i] !== suggestion) {
				counter += 1;
				listbox.add(opt);  // Adds info to listbox.
			}
		}
		if ((words.length >= 10) && ((counter / words.length) <= 0.1)) {
			$("#hidtxt").css("display", "");  // Show the link.
		} else {
			$("#hidtxt").css("display", "none");  // Hide the link.
		}

And that would show the link after 10 or more words were typed in, with 10% or less errors (i.e. 1 error per 10 words).

You can modify that code to work however you please.

Hope that helps! :slight_smile:

1 Like

THIS IS AMAZING!!!

I just put it into the Javascript as per your instructions and it worked like a charm!

I’ve book marked this page too so I can refer back to it later when I’m doing things in Javascript. You explained it really clearly. Thankyou so so much!