Home > Back-end >  How to find the first incorrect word in a document attempting to use Standard Pilish?
How to find the first incorrect word in a document attempting to use Standard Pilish?

Time:01-27

I am creating a GDocs Apps Script to check my document to see if it is in Standard Pilish. I'd like to have the checker return both the position of the first error and the word that is incorrect. I can get the position of the first error in pi, but since the word list does not necessarily perfectly reflect that positioning.

For example, I used a modified quote from Peter Walder: "Two mathematicians accommodatingly promenade to tavern, quest everything". The error lies in the 8th word in the list, but the 10th position in pi.

This is the script I've landed at, and I originally tried to just words[positionOne] before realizing my counting error.

function pilishTranslate() {
  var doc = DocumentApp.getActiveDocument();
  var text = doc.getBody().getText();
  
  // Remove quotation marks from the text
  text = text.replace(/\"/g, "");
  
  // Replace all non-letter characters with white space
  text = text.replace(/[^a-zA-Z\s]/g, " ");
  
  // Split the text into an array of words
  var words = text.split(/\s /);
  
  // Count word length
  var wordLengths = words.map(function(word) {
  return word.length;
  });
  // Change 10 character counts to 0
  wordLengths = wordLengths.map(function(length) {
  return length === 10 ? 0 : length;
  });
  // Join character counts into single string
  var wordLengthsString = wordLengths.join('');

  // Create variable for pi
  var decimal15 = '314159265358979'

  // Find common prefix of strings a and b.
  var prefix = function(a,b){
    return a && a[0] === b[0] ? a[0]   prefix(a.slice(1), b.slice(1)) : '';
  };
  // Find index of first difference.
  var diff = function(a,b){
    return a===b ? -1 : prefix(a,b).length;
  };
  // actual test case
  var tests = [
    [wordLengthsString,decimal15],
  ];
  // find first position of error
  var positionOne = tests.map(test => diff(test[0], test[1]))
  console.log(positionOne);
}

CodePudding user response:

function checkPilish(text) {
  // Remove quotation marks from the text
  text = text.replace(/\"/g, "");

  // Replace all non-letter characters with white space
  text = text.replace(/[^a-zA-Z\s]/g, " ");

  // Split the text into an array of words
  var words = text.split(/\s /);

  // Create variable for pi
  var decimal15 = '314159265358979'
  let pi_index = 0;
  
  // Loop over words
  for (let i = 0; i < words.length; i  ) {
    // convert word length to Standard Pilish digits
    let length_str = String(words[i].length);
    if (length_str == '10') {
      word_length = '0';
    }
    // check if this matches the current position in pi
    if (decimal15.substr(pi_index, length_str.length) != length_str) {
      return [i 1, words[i]];
    }
    pi_index  = length_str.length;
  }
  return [];
}

console.log(checkPilish("Two mathematicians accommodatingly promenade to tavern, quest everything"));
console.log(checkPilish("Two mathematicians accommodatingly promenade to tavern, quest all"));

  • Related