Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Last active October 13, 2015 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainysmurf/16d1ed8613b137ffe24d to your computer and use it in GitHub Desktop.
Save brainysmurf/16d1ed8613b137ffe24d to your computer and use it in GitHub Desktop.

"Blankifier" allows teachers to quickly create a document where every "N" word is replaced with a "blank".

To get started, copy and paste the below into a Google Doc's script editor. Copy and paste text into the document, then click on Blanker -> Blanify

Features:

  • Decide how many words to skip before deciding to blank
  • Automatically skips words <= 3 words
// gl "global logger" set to true in order to get enable logging
var gl = false;
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Blanker')
.addItem('Blanify', 'clicked_on_blankify')
.addToUi();
ui.alert(
'How this works:',
'You type or paste in any text, and I will blank out every x number of words you specify. Click on "Blanker" -> "Blankify"',
ui.ButtonSet.OK);
}
/*
tokenize
@param s: string
@return an array of strings
*/
function tokenize(s){
s = s.replace(/(^\s*)|(\s*$)/gi,""); //exclude start and end white-space
s = s.replace(/[ ]{2,}/gi," "); //2 or more space to 1
return s.split(' ');
}
/*
blankify
@param txt: string
@param every: integer. How many words to skip over
@return string
*/
function blankify(txt, every) {
(gl || log) && Logger.log(txt + every);
var tokens = tokenize(txt); // tokens gives us an array with each element representing a word
var tally = 0;
var output = []; // array that will be put back onto the document
for (var i = 0; i < tokens.length; i++) {
var word = tokens[i];
tally += 1;
//(gl || log) && Logger.log(tally);
// don't count if it's a small word
if (word.length <= 3) {
(gl || log) && Logger.log(' <=3: ' + word);
output.push(word);
continue;
}
if (tally >= every) {
(gl || log) && Logger.log('blankify: '+word);
output.push("***");
tally = 0;
} else {
(gl || log) && Logger.log(' : ' + word);
output.push(word)
}
}
return output.join(" "); // this will convert the array into a string where a space separates each element
}
function test_blankify() {
var log = false;
var test_material = "I wonder what happens now.\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
var output = blankify(test_material, 10);
update_document(output);
}
/*
update_document
@param txt: array of words
*/
function update_document(txt) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.clear();
body.appendParagraph(txt);
}
/*
clicked_on_blankify
Called when user clicks on the menu
*/
function clicked_on_blankify() {
var log = true;
(gl || log) && Logger.log('clicked_on_blankify');
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var ui = DocumentApp.getUi();
var result = ui.prompt(
'Blank out every _ word',
'Please enter the number to skip (words <= 3 are not counted at all)',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var text = result.getResponseText();
// When user clicks okay, go through the document text, adding to the a new array (output)
if (button == ui.Button.OK) {
var every = parseInt(text);
var output = blankify(body.getText(), every);
update_document(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment