<commit_before>/** Based on http://www.codeproject.com/KB/recipes/Generate_UPS_Check_Digit.aspx
 */
/*jslint continue: true, devel: true, evil: true, indent: 2, nomen: true, plusplus: true, regexp: true, rhino: true, sloppy: true, sub: true, unparam: true, vars: true, white: true */
/*global _, HostAdapter, hostAdapter */
function calculate_check_digit(trk) {
  var runningtotal = 0;

  for (var i = 0; i < trk.length; i++) {
    var c = trk.charCodeAt(i) - 48;

    if ((i & 1) == 1)  {        
      if ((c >= 0) && (c < 9)) {
        runningtotal += (2 * c);            
      } else  {          
        runningtotal += c;
      }
    } else {        
       if ((c >= 0) && (c < 9)) {
         runningtotal += c;
       } else {
         // Indicates alpha value 
         runningtotal += ((2 * c) - (9 * (c / 5)));
       }
    }
  }

  var x = (runningtotal % 10);
  if (x == 0)  {
   return 0;
  } else {
   return (10 - x);
  }
}

function recognize(s, context) {
  var startIndex = s.indexOf("1Z");
  
  if (startIndex < 0) {
    return null;
  }
  
  s = s.substring(startIndex);
  
  if (s.length < 18) {
    return null;
  }
  
  s = s.substring(0, 18);
  
  var recognitionLevel = 0.0;
  var extraInfo = { 
    valid : false 
  };
   
  function makeReturnValue()
  { 
    
    var result = extraInfo;
    result.matchedText = s;
    result.recognitionLevel = recognitionLevel;
    
    return {
      'com.solveforall.recognition.business.UPSTrackingNumber' : [result]
    };    
  }
    
  extraInfo.accountNumber = s.substring(2, 8);

  recognitionLevel = 0.25;
    
  var code = s.substring(8, 10);
  
  if (code == "01")
  {
    extraInfo.serviceType = 'Next Day Air';        
  }
  else if (code == '02')
  {
    extraInfo.serviceType = 'Second Day Air';        
  }
  else if (code == '03')
  {
    extraInfo.serviceType = 'Ground';    
  }
  else
  {
    return makeReturnValue();
  }

  extraInfo.invoiceNumber = s.substring(10, 15);
  
  extraInfo.packageNumber = s.substring(15, 17);  
    
  if (calculate_check_digit(s.substring(2, 17)) !=  s.charAt(17))
  {
    return makeReturnValue();
  }
  
  recognitionLevel = 1.0;
  extraInfo.valid = true;
  
  return makeReturnValue();
}
<commit_msg>Fix UPS tracking number recognizer which never worked<commit_after>/*jslint continue: true, evil: false, indent: 2, nomen: true, plusplus: true, regexp: false, rhino: true, sloppy: true, sub: true, unparam: true, vars: true, white: true */
/** Based on http://www.codeproject.com/KB/recipes/Generate_UPS_Check_Digit.aspx
 */
function calculate_check_digit(trk) {
  var runningtotal = 0;

  for (var i = 0; i < trk.length; i++) {
    var c = trk.charCodeAt(i) - 48;

    if ((i & 1) === 1)  {        
      if ((c >= 0) && (c < 10)) {
        runningtotal += 2 * c;            
      } else  {                 
        runningtotal += ((c - 15) % 10) * 2;
      }
    } else {        
       if ((c >= 0) && (c < 10)) {
         runningtotal += c;
       } else {
         runningtotal += (c - 15) % 10;
       }
    }
  }

  var x = (runningtotal % 10);
      
  if (x === 0)  {
   return '0';
  }
  return '' + (10 - x);  
}

function recognize(s, context) {
  var startIndex = s.indexOf("1Z");
  
  if (startIndex < 0) {
    return null;
  }
  
  s = s.substring(startIndex);
  
  if (s.length < 18) {
    return null;
  }
  
  s = s.substring(0, 18);
  
  var recognitionLevel = 0.0;
  var extraInfo = { 
    valid : false 
  };
   
  function makeReturnValue() {     
    var result = extraInfo;
    result.matchedText = s;
    result.recognitionLevel = recognitionLevel;
    
    return {
      'com.solveforall.recognition.business.UPSTrackingNumber' : [result]
    };    
  }
    
  extraInfo.accountNumber = s.substring(2, 8);

  recognitionLevel = 0.25;
    
  var code = s.substring(8, 10);
  
  if (code == "01") {
    extraInfo.serviceType = 'Next Day Air';        
  } else if (code == '02') {
    extraInfo.serviceType = 'Second Day Air';        
  } else if (code == '03') {
    extraInfo.serviceType = 'Ground';    
  } else {
    return makeReturnValue();
  }

  extraInfo.invoiceNumber = s.substring(10, 15);
  
  extraInfo.packageNumber = s.substring(15, 17);  
    
  var check_digit = calculate_check_digit(s.substring(2, 17));
  extraInfo.validCheckDigit = check_digit;
  
  if (check_digit !==  s.charAt(17)) {
    return makeReturnValue();
  }
  
  recognitionLevel = 1.0;
  extraInfo.valid = true;
  
  return makeReturnValue();
}

{'fraction_non_alphanumeric': 0.1478338543992854, 'fraction_numerical': 0.02813756141134435, 'mean_word_length': 2.1387526278906797, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3213166', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1589, 'n_words': 377}