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

I’m using Sugarcube 2.35. I’m wanting to get players to input large blocks of text as a creative writing exercise (I’m a teacher). I’m wondering if it’s possible to spell/grammar check the string that players input via the textbox?

Thanks so much for your time!

I’m not a Twine user, but I know that browsers nowadays typically have spellcheck turned on by default–usually the passive kind where they red-underline any unrecognized words. If you search for “spell” in the settings for Firefox or Chrome, for example, you’ll see that this is turned on (or can be turned on, if it has been turned off).

Hope this helps!

1 Like

It should be noted that this web-browser feature often makes use of the Operating System’s own spell-checker functionality (which may require third-party software like a Word Processing application to be installed), so if that OS doesn’t include such functionality then possibly neither will the web-browser.

1 Like

This difficulty in spellchecking is one of the reasons I decided to use Tweego+SugarCube instead of Twine. Since Tweego’s .tw files are just plain text files instead of windows in a GUI, I am able to use whatever combination of text editor and spellchecker I like. In my case, that happens to be emacs + aspell, but there are many others, of course.

1 Like

Thanks for the ideas!

Sorry just to clarify, I’m wanting feedback for the player (within the game) not the author (though that would be nice haha).

So as a creative writing tool, I’m wanting something to detect how many mistakes Jimmy has made in writing a paragraph, and then give him some in game consequences… If he just spams nonsense he should fail the test…Does that make sense?

You’d have to hook up some sort of javascript spell-checking library. And a quick search isn’t turning up anything that looks like it would be particularly easy to just drop in and use… :frowning:

1 Like

Ok thanks, I guess I’ll mark this the answer then if no one else thinks of something in the next day or two.

Thanks for your time guys.

If you have the Grammarly plugin installed, it should check spelling and grammar so long as the game runs in a browser window. I haven’t tried it individually though.

1 Like

Yeah I have Grammarly. It doesn’t seem to work tbh, but still, I need a solution within the game itself - I can’t expect players to all have Grammarly installed etc. if you know what I mean.

1 Like

I don’t know about grammar checking, but there’s the Open Source Spell Checker that you could use for spell checking. You can test it online here.

1 Like

Well, that’s disappointing. It works in AXMA 6.1 for the online editor - the Grammarly icon shows up and I can click to open the text in Grammarly with correction suggestions. However, I don’t think it would offer it in the released game, even if showing on a webpage. I could have sworn I got underlined text once while playing an old-school Twine 1 game online.

1 Like

Yeah don’t take my word for it though, because I’m a total noob and I’ve only installed Grammarly recently, so I may have settings wrong etc! :confused:

Here’s a quick example I wrote showing how Open Source Spell Checker could be used in Twine/SugarCube: Spellcheck Sample

You can download the source files from here: Spellcheck.zip

If you import the “Spellcheck_Sample.html” into Twine (and edit the path in the JavaScript section) you can see how that code works, play around with it, and hopefully get it to do what you want.

Note that, while I included an improved word list with my code (dictionary source), you may still need to add more words to it if you have any words you expect to use which aren’t already in that dictionary. Also, keep in mind that if, for example, they misspell “they’re” as “there” or “their”, this won’t catch that, since those are all actual words. It also ignores capitalization, so proper names, like “Paris”, will not be marked as incorrect if they’re written in lowercase.

Hope that helps! :slight_smile:

2 Likes

Wow thanks so much! I’ll take a look at that right now. It looks amazing!!!

EDIT** ok yep that looks really sweet. Thankyou!
Would it be possible to not allow a player to progress to another passage if they have a certain number of errors that need fixing? I don’t know if that’s possible, but if it is, it would be a great way of stopping players just spamming words :stuck_out_tongue:

FYI, I wasn’t happy with the speed of the spellchecker, so I sped it up. It should work a bit faster now. Just download it again to get the updated code.

As far as preventing the player from progressing if there are too many errors, you could put the link to the next passage within a <span> and toggle the visibility of that span as necessary using a little jQuery code. For example:

<span id="hidtxt">[[Link]]</span>
<<button "Toggle link visibility">>
	<<run $("#hidtxt").toggle()>>
<</button>>
<<button "Hide link">>
	<<run $("#hidtxt").css("display", "none")>>
<</button>>
<<button "Show link">>
	<<run $("#hidtxt").css("display", "")>>
<</button>>

That shows how you can toggle, hide, or display things by using their ID. Just apply something like that to your own links, based on the length of text and the number of errors, and that should help.

Enjoy! :slight_smile:

P.S. If you want to be really mean, you could play a sound whenever they try to paste some text. :grin:

1 Like

Wow that is so cool! Thanks a million!

I’ll download again and have a play.

Ooh yes I do want to be mean haha. :laughing:
I didn’t know you could detect pasting!?

Yeah, some elements, like the <textarea> element, have a paste event. In fact, the code is already using that event to detect changes to the text.

If you look in the <<script>> section of the Start passage, you’ll see this code:

$(event.content).find("#textarea-varname").on("change input paste", null, "varname", setup.checkWords);

What that does is trigger the setup.checkWords() function (found in the JavaScript section) whenever the <<textarea>> text is changed, has input, or something is pasted there.

If you wanted some other code to only trigger on pasted text, you could add another line after that one like this:

$(event.content).find("#textarea-varname").on("paste", function (event) {
	/* Replace this line with your own JavaScript code, and it will be triggered when text is pasted. */
});

Now, whatever code you put within that function will trigger whenever they try to paste text into that <<textarea>>. (Change varname in that code if your <<textarea>> uses a different variable than $varName.)

If you need to call SugarCube macros from within that JavaScript you can use:

$.wiki("SugarCube code goes here");

(See the SugarCube documentation here for details.)

Have fun! :smiley:

1 Like

Wow this is so cool. And incredibly helpful!

I was pulling my hair out last night (I don’t know Javascript but I’m getting decentish with sugarcube macros), and I was literally saying to myself, “If only there was a way for me to set sugarcube variables etc. within the awesome JavaScripts that people (like you) post online!”
Looks like that wiki thing is the answer! THANK YOU! This will save me hours and hours of looking up Javascript things that I don’t really understand :rofl:

Well, if you need to access SugarCube story variables from JavaScript you can use State.variables.variableName (to access $variableName), or for temporary variables, use State.temporary.variableName (to access _variableName).

I’d definitely recommend at least skimming through the SugarCube documentation so you’re aware of methods like those.

Also, if it helps, I have a collection of other Twine/SugarCube sample code I’ve written that you might want to take a look at.

Enjoy! :slight_smile:

1 Like

Ah ok! I tested the State.variables and it seems to be working as you said! Thanks so much.

Yeah tbh I’m checking the SugarCube documentation all the time, but still a lot of it goes over my head. I think programmers don’t realise just how much they take for granted and how simple it needs to be for noobs like me! :sweat_smile:

Yeah thank you so much - I have been perusing your sample code. It looks so rad. I particularly love how you’ve set it out with examples etc. Really nice job!