How to Create A Spell Checker in Qualtrics

Albert Su
Albert Su
May 18, 2020 · 6 min read
Image for post
Image for post

Your Problem:

  • You have a survey with text entries whose values are used in a later portion of the same survey.

The Process:

Setting Up DiffLib

In order to get accurately spelled versions of misspelled words, we will be using the difflib package. The main tool that we will be using in the package is the getClosestMatches() method, which allows us to find the closest spelling from a list of words by splitting the misspelled word into substrings.

  1. Download this txt file onto your computer. Rename the file to “difflib.txt”.
Image for post
Image for post
If you are having trouble downloading the image, click on the raw button and save it to your computer using control/cmd + s

2. Upload your difflib.txt file into your Files Library in qualtrics. (Click Library at the top right of your screen → Click Files Library → Click Upload a New File)

3. Copy the link of the difflib.txt file that you have just uploaded. (Click Show All → Click the Settings Wheel of the difflib.txt file → Click View File → Right Click View → Click Copy Link Address)

Image for post
Image for post
Copy the link of the file here to add it to your survey.

4. Add the link of the difflib.txt file into the header of your survey. (Click Look & Feel at the middle left of your screen → Click Back to Old Editor → Click the Advanced tab → Paste the header with the following format:

<script type=“text/javascript” src=“Replace with your link”> </script>

5. Click the save button to save your changes.

You have now added the difflib library to your survey.

Creating the Spell Checker:

The following instructions set up a spell checker that replaces one or more text answers with the corrected version in the database. This works by creating an embedded variable that holds the value of the corrected word.

  1. Add the embedded variable to your survey flow. (Click survey flow → Click Add a New Element Here → Click Embedded Data) Rename the field to the variable that you are trying to spell-check. Click Save Flow when you have added a embedded data for each of the fields you want spell checked.
Image for post
Image for post
I am using Fruit1 as the example embedded data name.

2. Create the question you want the spell checker to work on. Make sure the question type is a text response. For this example, we will be using Text Entry.

Image for post
Image for post
The sample question is “What is your favorite fruit”?

3. Add the Javascript that will be used to save a spell checked version of the response into the embedded variable. (Click the Setting Wheel for the question → Add JavaScript → Add the following code in order to the addOnUnload() function)

Image for post
Image for post

4. Add the variable that will temporarily hold the uncorrected version of the response.

var var1 = this.getChoiceValue(1);

5. Add your dictionary that the spell checker will use to correct words to. In this case, my examples uses fruits, so this would be the start of a potential dictionary if the answers relate to fruits.

var keywordlist = ["Apple", "Apricot","Avocado","Banana","Bilberry", "Blackberry","Blackcurrant","Blueberry","Boysenberry","Currant"];

6. Set your correction threshold. This is a decimal between 0 and 1 that will determine the sensitivity of your spell checker. The higher the threshold, the more likely it is for the spell checker to make a correction instead of leaving the word alone.

var correctionthreshold = 0.6;
if (var1.length <= 4){
correctionthreshold = 0.5;
}

7. Use difflib to generate a spell checked version of the response. This either corrects var1(the response) or leaves it alone, depending on the correctionthreshold.

var correctedChoice = difflib.getCloseMatches(var1, keywordlist, 1, correctionthreshold);
if (correctedChoice.length > 0){
var1 = correctedChoice;
}

8. Save the response to the embedded variable you set earlier.

Qualtrics.SurveyEngine.setEmbeddedData('Fruit1',  var1);

This is what the Edit Question JavaScript should look like after completing the above steps.

Image for post
Image for post
Remember to save after finishing the JavaScript!

You have now added a spell checker to your Qualtrics Survey. The response value is “Fruit1” or whatever you set your embedded variable to. This can be found in your survey data after completion.

Using Spell Checked Answers in the Same Survey:

Problem: You are planning to change future questions based on their responses to previous text questions. However, the previous text questions may be incorrect.

Before doing the following, make sure you have completed the section titled “Creating the Spell Checker:” Put all of the code we are using into the addOnReady() function of the question where you want to use the correct response.

  1. To get the value of the corrected word, grab the data from the embedded value that we set earlier.
var fruit1 = Qualtrics.SurveyEngine.getEmbeddedData('Fruit1');

At this point, fruit1 is the value of the corrected response that we have set earlier and can be used in a variety of ways.

The following code is a guide to incorporate it into a menu of items for the user to choose from.

  1. Create the question where the corrected text will be used. This can be of any format.
Image for post
Image for post
Use the Create a New Question Button

2. Create your Question Text variable. This will be displayed at the top of the question in place of “Click to write the question text”

var QuestionText = "Here are the following choices: ";

3. Create your options variable. This is a list of options for the user to choose from.

var answers = ['Banana', 'Apple', 'Pear', 'Peach'];

4. Reformat your embedded variable to fit the options variable. For example, the following code makes sure that the fruit1 variable starts off with a capital letter, just like the answers.

var indict = false;
fruit1 = fruit1.toString();
for(var i = 0; i < answers.length; i++){
if(answers[i].valueOf().toLowerCase() ==
fruit1.valueOf().toLowerCase()){
indict = true;
}
}

5. Add your fruit1 variable to the list of options if it does not yet exist.

if(indict == false){
fruit1 = fruit1.charAt(0).toUpperCase() + fruit1.substring(1);
answers.push(fruit1);
}

6. Add the shuffle method to your code to shuffle the order of your answers.

function shuffle(arra1) {
var ctr = arra1.length, temp, index;
while (ctr > 0) {
index = Math.floor(Math.random() * ctr);
ctr--;
temp = arra1[ctr];
arra1[ctr] = arra1[index];
arra1[index] = temp;
}
return arra1;
}

Shuffle your answers to reduce bias.

answers = shuffle(answers);
for(var i = 0; i < answers.length; i++){
QuestionText = QuestionText.concat(answers[i] + ", ");
}

7. Add the Question Text to the Question.

jQuery("label[class='QuestionText BorderColor']").text(QuestionText);

This is what the javascript for the question should look like.

Image for post
Image for post
Remember to click save!

Congratulations, you have just added a spell checker to your survey!

If you have any questions or problems, please comment down below and send me an email at albert_su@berkeley.edu!

Here is a link to a .qsf file of what you just implemented!

The Startup

Medium's largest active publication, followed by +775K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store