Spell Check Script

874 views
Skip to first unread message

Brenda Ledwith

unread,
Aug 29, 2016, 6:10:58 AM8/29/16
to AdWords Scripts Forum
Hi,

I'm trying to test out an AdWords script that uses the Bing Spell Check API as per this article - http://searchengineland.com/spell-check-new-expanded-text-ads-adwords-script-256914

I've got my key from as required and have created the script similar to how it appears in the article. I have made an ad campaign that is currently paused, with a few ads in it, half with spelling errors to test if the script works.

I preview the script and it comes back with no logger output, and no data is getting pulled into my Google Drive which should be occurring as part of the script.

I'm thinking that perhaps I'm not customising the script enough. Can you please look it over and give me an idea of what I would need to adjust? I have removed my key here, but it is usually inserted on line 5. It's probably something quite simple that I'm doing wrong, but I have zero experiences with scripts and nobody to help. Thanks!

function main() {
function BingSpellChecker(config) {
  this.BASE_URL = 'https://api.cognitive.microsoft.com/bing/v5.0/spellcheck';
  this.CACHE_FILE_NAME = 'spellcheck_cache.json';
  this.key = config.key;
  this.toIgnore = config.toIgnore;
  this.cache = null;
  this.previousText = null;
  this.previousResult = null;
  this.delay = (config.delay) ? config.delay : 60000/7;
  this.timeOfLastCall = null;
  this.hitQuota = false;
 
  // Given a set of options, this function calls the API to check the spelling
  // options:
  //   options.text : the text to check
  //   options.mode : the mode to use, defaults to 'proof'
  // returns a list of misspelled words, or empty list if everything is good.
  this.checkSpelling = function(options) {
    if(this.toIgnore) {
      options.text = options.text.replace(new RegExp(this.toIgnore.join('|'),'gi'), '');
    }
    options.text = options.text.replace(/{.+}/gi, '');
    options.text = options.text.replace(/[^a-z ]/gi, '').trim();
   
    if(options.text.trim()) {
      if(options.text == this.previousText) {
        Logger.log('INFO: Using previous response.');
        return this.previousResult;
      }
      if(this.cache) {
        var words = options.text.split(/ +/);
        for(var i in words) {
          Logger.log('INFO: checking cache: '+words[i]);
          if(this.cache.incorrect[words[i]]) {
            Logger.log('INFO: Using cached response.');
            return [{"offset":1,"token":words[i],"type":"cacheHit","suggestions":[]}];
          }
        }
      }
      var url = this.BASE_URL;
      var config = {
        method : 'POST',
        headers : {
          'Ocp-Apim-Subscription-Key' : this.key,
          'Content-Type' : 'application/x-www-form-urlencoded'
        },
        payload : 'Text='+encodeURIComponent(options.text),
        muteHttpExceptions : true
      };
      if(options && options.mode) {
        url += '?mode='+options.mode;
      } else {
        url += '?mode=proof';
      }
      if(this.timeOfLastCall) {
        var now = Date.now();
        if(now - this.timeOfLastCall < this.delay) {
          Logger.log(Utilities.formatString('INFO: Sleeping for %s milliseconds',
                                            this.delay - (now - this.timeOfLastCall)));
          Utilities.sleep(this.delay - (now - this.timeOfLastCall));
        }
      }
      var resp = UrlFetchApp.fetch(url, config);
      this.timeOfLastCall = Date.now();
      if(resp.getResponseCode() != 200) {
        if(resp.getResponseCode() == 403) {
          this.hitQuota = true;
        }
        throw JSON.parse(resp.getContentText()).message;
      } else {
        var jsonResp = JSON.parse(resp.getContentText());
        this.previousText = options.text;
        this.previousResult = jsonResp.flaggedTokens;
        for(var i in jsonResp.flaggedTokens) {
          this.cache.incorrect[jsonResp.flaggedTokens[i].token] = true;
        }
        return jsonResp.flaggedTokens;
      }
    } else {
      return [];
    }
  };
 
  // Returns true if there are spelling mistakes in the text toCheck
  // toCheck : the phrase to spellcheck
  // returns true if there are words misspelled, false otherwise.
  this.hasSpellingIssues = function(toCheck) {
    var issues = this.checkSpelling({ text : toCheck });
    return (issues.length > 0);
  };
 
  // Loads the list of misspelled words from Google Drive.
  // set config.enableCache to true to enable.
  this.loadCache = function() {
    var fileIter = DriveApp.getFilesByName(this.CACHE_FILE_NAME);
    if(fileIter.hasNext()) {
      this.cache = JSON.parse(fileIter.next().getBlob().getDataAsString());
    } else {
      this.cache = { incorrect : {} };
    }
  }
 
  if(config.enableCache) {
    this.loadCache();
  }
 
  // Called when you are finished with everything to store the data back to Google Drive
  this.saveCache = function() {
    var fileIter = DriveApp.getFilesByName(this.CACHE_FILE_NAME);
    if(fileIter.hasNext()) {
      fileIter.next().setContent(JSON.stringify(this.cache));
    } else {
      DriveApp.createFile(this.CACHE_FILE_NAME, JSON.stringify(this.cache));
    }
  }
}
}

Tyler Sidell (AdWords Scripts Team)

unread,
Aug 29, 2016, 11:44:41 AM8/29/16
to AdWords Scripts Forum
HI Brenda,

Thanks for providing the script information.  Are you receiving any errors when you try to run this script?  Would you mind providing us with your CID (reply privately to the author) with the name of your script so that we can give it a try to see if we get any results returned?

As this is a third party script (not written by the AdWords Scripts team), we would suggest reaching out to the creator of the script as well.  There may also be some bugs and issues but since we don't provide updates for this script, our support on these third party scripts may be minimal.

Regards,
Tyler Sidell
AdWords Scripts Team

Brenda Ledwith

unread,
Aug 29, 2016, 9:12:10 PM8/29/16
to AdWords Scripts Forum
Thanks Tyler,

For anybody else following this thread, I am not experiencing any errors when running up the script - it's not picking up anything when it definitely should be. I've privately sent you my CID and script name so looking forward to hearing if you can pick anything up.

I will also contact the author of the script and see if he replies. Thanks!

Andrea Donatsch

unread,
May 2, 2017, 9:18:17 AM5/2/17
to AdWords Scripts Forum
Hey Tyler & Brenda

I want to use the same script but I get the same output.. Did you manage to solve it?

Thanks!

Joy Liu

unread,
Jul 3, 2020, 2:46:25 PM7/3/20
to Google Ads Scripts Forum
I am trying to use the same script and it does the labelling for me, but it labels ad copies with no typos as Spelling issues. 

Joy

Google Ads Scripts Forum Advisor

unread,
Jul 5, 2020, 9:40:21 PM7/5/20
to adwords...@googlegroups.com
Hi Joy,

Thanks for the reply.

However, I am afraid that I would not be able to provide support to this concern as it is related to a third party script which out of our scope. The script is also using external entities that our team is not familiar with. With this, I would suggest reaching out to the author of the third party script using this link to get support to your concern.

Regards,
Ejay
Google Ads Scripts Team

ref:_00D1U1174p._5004Q21j3Nu:ref
Reply all
Reply to author
Forward
0 new messages