Hi fellow developers! I am currently trying to help an automatic gap deletion to my own version of the multianswer/cloze question type. It's working more or less as expected but in my script which sets a SHORTANSWER "gap" (in fact a SHORTANSWER sub-question) every nth word (at n intervals) I would like my script to skip capitalised words (as is the norm in such cloze tests). The following script does skip capitalised words, but... it also skips the next n words. I know this is due to the "continue" instruction in my script line 17. Any idea how I could modify my script to achieve the result I want?
Interval for gaps set at every 5th word. Original text example:
Once upon a time, many hundreds of years ago, there lived a boy named Dick. He was an orphan and had little in the way of comfort,...
With my script I'm getting this result:
Once upon a time, {1:SA:=many} hundreds of years ago, {1:SA:=there} lived a boy named Dick. He was an orphan {1:SA:=and} had little in the {1:SA:=way} of comfort,...
But I would like to get this result:
Once upon a time, {1:SA:=many} hundreds of years ago, {1:SA:=there} lived a boy named Dick. {1:SA:=He} was an orphan and {1:SA:=had} little in the way {1:SA:=of} comfort,...
and here's the script: (interval == 0)
1 for (let i = 0; i < paragraphs.length; i++) {
2
let paraText = $(paragraphs[i]).text();
3 let words =
paraText.split(' ');
4 // Loop through the words and enclose
every 5th or 9th word in SHORTANSWER marker.
5 for (let index =
0; index < words.length; index++) {
6 if ((index + 1) %
interval === 0) {
7 // Separate the word from any trailing
punctuation
8 let word = words[index];
9
let punctuation = '';
10 if (/[.,!?;:]+$/.test(word))
{
11 punctuation = word.slice(-1); // Get the
punctuation mark
12 word = word.slice(0, -1); // Remove
the punctuation from the word
13 }
14 //
Check if the word starts with a capital letter
15 if (word
&& word[0] === word[0].toUpperCase() &&
/[A-Za-z]/.test(word[0])) {
16 // If the word starts with
a capital letter, skip the gapping transformation
17
continue;
18 }
19 // Enclose the word in
SHORTANSWER (SA) brackets, then add back the punctuation
20
words[index] = `{1:SA:=${word}}${punctuation}`;
21 }
22
}
23 // Join the words back into a single string
24
let gappedText = words.join(' ');
25 if (gappedText !==
'') {
26 $(paragraphs[i]).text(gappedText);
27
}
28 }
N.B. I did try ChatGPT for help but it keeps suggesting alternative scripts which do not work. 😡