Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Tuesday, 21 March 2017

Speak and Spell and Sniff

Everyone loves spelling tests right? No? Well at least parents and teachers love spelling tests? They must do - they always want to do them with their kids! If kids hate taking them, and adults hate setting them, that would make no sense!

What if we wrote a program to test your spellings? Well at least kids could practise, and us adults could do something more fun. No reason for us all to be bored!

Of course the problem is that you can't just print "spell pointless:" on the screen, as it pretty much gives the game away - hence the traditional spelling test where an adult reads out the list of words. Fortunately that's really easy in Sniff using the "speech" device (which should work "out of the box" on Mac and Windows. On Linux you need to install a speech synthesiser).

make words list of strings

make voice speech device
make message string

when loadWords
.delete all of words
.add "soldier" to words
.add "necessary" to words
.add "legend" to words
.add "northern" to words


We're going to need the speech device (which we're calling voice), and a list of words. Just to get started I've added a few good spelling word to the list.


make counter number
when start
.broadcast loadWords and wait

.set message to join "There are " join [length of words] " words"
.tell voice to "speak"
.wait 3 secs
.


To get us started, we just run the loadWords script, and to check its working we say how many words are in the test.

We could go through the list in order and just read them out, but to make it more fun(!) we'll mix them up.


.
.repeat until length of words=0
..set counter to pick random 1 to length of words
..set message to item counter of words
..tell voice to "speak"
..wait 1 secs
..ask "How do you spell that?" and wait
..if answer=message
...set message to "Correct"
...delete item counter of words
..else
...set message to "Sorry, Incorrect"
..tell voice to "speak"
..wait 3 secs

.
.stop all

This is more for practising than a proper test, so we pick a random word from the list, and say it. The user then has to type it, and if its correct we delete it from the list. If its incorrect we leave it in the list, so that you have to spell every word correctly to finish the program.

The program just loops until there are no more words to spell. Alternatively for a regular spelling test, we'd delete the word even if it was wrong, and keep a score (left as an exercise for the reader).

This all works pretty well, but its not ideal (depending on your love of spelling tests!) that it only knows four words, and that you have to edit the program to change them so instead we can load the words from a file:


make nativeFile device
make fileData string
make fileOK boolean
when loadFile
.delete all of words
.set fileData to "spellings.txt"
.tell nativeFile to "start read"
.repeat until not fileOK
..tell nativeFile to "read string"
..if fileOK
...add fileData to words
.tell nativeFile to "end read"

This just copies a list of words from the file spellings.txt. A quick search turned up a list of KS2 (age 7-11) spelling words and dropped them in the file. Unfortunately the program now starts by telling the there are 208 words in the test, which takes rather a long time to complete! To fix this I added a script to delete most of the words and leave a sensible number:


when deleteWords
.repeat until length of words=10
..delete item pick random 1 to length of words of words

This works, but because of the way random numbers work generates the same list off words every time. To get round that we need to start throwing away random numbers, so we get something closer to real randomness.

when start
.make dummy number
.forever
..set dummy to pick random 1 to 10

With this script in place, the beginning of our main script just needs to be tweaked:


when start
.broadcast loadFile and wait
.ask "Press Return to start" and wait
.broadcast deleteWords and wait
.set message to join "There are " join [length of words] " words"
.
.

Now it loads all of the words, and then asks to confirm you're ready. Because everyone will take a slightly different length of time to press return we'll get a truly random set of words.

It turns out writing code to set spelling tests is a lot more fun than doing spelling tests!

2 comments:

  1. As someone who is really interested in programming, but can't seem to get anywhere with it, this is really helpful. I would start away with Sniff immediately. My little brother is going to 'love' the spelling tests I'll create for him using this.

    ReplyDelete
  2. Thank you so much for writing and telling us about this program,it can prove to be useful for a lot of people.Keep it up!

    ReplyDelete