<commit_before>(function ($, CIESubjects) {
  function pregQuote (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: booeyOH
    // +   improved by: <NAME> (http://magnetiq.com)
    // +   improved by: <NAME> (http://kevin.vanzonneveld.net)
    // +   bugfixed by: <NAME>
    return (str + '').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}=!<>\|:])/g, '\\$1')
  }
  function getTypeString (type) {
    switch (type) {
      case 'qp': return 'question paper'
      case 'ms': return 'marking scheme'
      case 'in': return 'insert'
      default: return type
    }
  }
  function paperName (doc) {
    return doc.subject + ' ' + doc.time + ' paper ' + doc.paper + (doc.variant !== 0 ? doc.variant : '')
  }
  var queryBox = $('.queryBox')
  queryBox.focus()
  var resultList = $('.resultList')

  var lastText = ''
  queryBox.on('keydown keyup change', function (evt) {
    setTimeout(function () {
      var text = queryBox.val()
      if (text === lastText) {
        return
      }
      lastText = text

      processQuery(text.trim())
    }, 0)
  })

  function clearNonDirect () {
    $('.ftr').remove()
    $('.ppl').remove()
  }
  function processQuery (query) {
    resultList.html('')
    if (query === '') {
      resultList.append($('<div class="prompt">Search for something...</div>'))
      $('.help').css({display: ''})
      clearNonDirect()
      return
    }
    $('.help').css({display: 'none'})
    if (query.match(/^\d{2,4}$/)) {
      var results = 0
      for (var i = 0; i < CIESubjects.length; i++) {
        (function (subj) {
          if (subj.id.substr(0, query.length) === query) {
            var subjElem = $('<div class="subject"></div>')
            subjElem.append($('<span class="level"></span>').text(subj.level))
            subjElem.append($('<span class="id"></span>').text(subj.id))
            subjElem.append(': ')
            subjElem.append($('<span class="name"></span>').text(subj.name))
            resultList.append(subjElem)
            subjElem.click(function () {
              queryBox.val(subj.id + ' ')
              queryBox.focus()
              processQuery(subj.id)
            })
            results++
          }
        })(CIESubjects[i])
        if (results >= 5) {
          clearNonDirect()
          return
        }
      }
    } else {
      var results = 0
      for (var i = 0; i < CIESubjects.length; i ++) {
        (function (subj) {
          if (subj.name.toLowerCase().indexOf(query.toLowerCase()) >= 0) {
            var subjElem = $('<div class="subject"></div>')
            subjElem.append($('<span class="level"></span>').text(subj.level))
            subjElem.append($('<span class="name"></span>').text(subj.name))
            subjElem.append(': ')
            subjElem.append($('<span class="id"></span>').text(subj.id))
            resultList.append(subjElem)
            subjElem.click(function () {
              queryBox.val(subj.id + ' ')
              queryBox.focus()
              processQuery(subj.id)
            })
            results++
          }
        })(CIESubjects[i])
        if (results >= 5) {
          clearNonDirect()
          return
        }
      }
    }

    $('.ppl').css({opacity: 0.5})
    var ppResult = $('<div class="ppl"></div>')
    function fetchPP (subj, time, paper, variant, type) {
      var queryBody = {}
      if (subj) {
        queryBody.subject = subj
      }
      if (time) {
        queryBody.time = time
      }
      if (paper) {
        queryBody.paper = paper
      }
      if (variant) {
        queryBody.variant = variant
      }
      if (type) {
        queryBody.type = type
      }
      $.ajax({
        url: '/search/pp/',
        method: 'get',
        cache: true,
        dataType: 'json',
        data: queryBody,
        success: function (data, s, jqx) {
          if (queryBox.val().trim() !== query) {
            return
          }
          $('.ppl').remove()

          var ppMaps = {}
          for (var i = 0; i < data.length; i++) {
            (function (doc) {
              var pName = paperName(doc)
              var rs
              if (!ppMaps[pName]) {
                rs = $('<div class="pp"></div>')
                rs.append($('<span class="name"></span>').text(pName))
                ppMaps[pName] = rs
                ppResult.append(rs)
              } else {
                rs = ppMaps[pName]
              }
              rs.append($('<span class="type"></span>').text(getTypeString(doc.type)).click(function () {
                window.open('https://file.schsrch.xyz/' + doc._id)
              }))
            })(data[i])
          }

          $('.pplContainer').append(ppResult)
        }
      })
    }

    if (query.match(/^\d{4}$/)) {
      fetchPP(query, null, null, null, null)
    }
    var match
    if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})$/))) {
      fetchPP(match[1], match[2])
    } else if ((match = query.match(/^(\d{4})[_ ](\d)$/))) {
      fetchPP(match[1], null, match[2], null, null)
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]+)$/))) {
      fetchPP(match[1], null, null, null, match[2])
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ]([a-z]+)$/))) {
      fetchPP(match[1], match[2], null, null, match[3])
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ](\d)$/))) {
      fetchPP(match[1], match[2], match[3], null, null)
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ](\d)(\d)$/))) {
      fetchPP(match[1], match[2], match[3], match[4], null)
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ]([a-z]+)[_ ](\d)(\d)$/))) {
      fetchPP(match[1], match[2], match[4], match[5], match[3])
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ]([a-z]+)[_ ](\d)$/))) {
      fetchPP(match[1], match[2], match[4], null, match[3])
    } else {
      $('.ppl').remove()
    }

    $('.ftr').css({opacity: 0.5})
    var fullTextResults = $('<div class="ftr"></div>')
    setTimeout(function () {
      if (queryBox.val().trim() !== query) {
        return
      }
      $.ajax({
        url: '/search/fullText/' + encodeURIComponent(query) + '/',
        method: 'get',
        cache: true,
        dataType: 'json',
        success: function (data, s, jqx) {
          if (queryBox.val().trim() !== query) {
            return
          }
          $('.ftr').remove()
<commit_msg>accept XXXX xXX paper XX<commit_after>(function ($, CIESubjects) {
  function pregQuote (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: booeyOH
    // +   improved by: <NAME> (http://magnetiq.com)
    // +   improved by: <NAME> (http://kevin.vanzonneveld.net)
    // +   bugfixed by: <NAME>
    return (str + '').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}=!<>\|:])/g, '\\$1')
  }
  function getTypeString (type) {
    switch (type) {
      case 'qp': return 'question paper'
      case 'ms': return 'marking scheme'
      case 'in': return 'insert'
      default: return type
    }
  }
  function paperName (doc) {
    return doc.subject + ' ' + doc.time + ' paper ' + doc.paper + (doc.variant !== 0 ? doc.variant : '')
  }
  var queryBox = $('.queryBox')
  queryBox.focus()
  var resultList = $('.resultList')

  var lastText = ''
  queryBox.on('keydown keyup change', function (evt) {
    setTimeout(function () {
      var text = queryBox.val()
      if (text === lastText) {
        return
      }
      lastText = text

      processQuery(text.trim())
    }, 0)
  })

  function clearNonDirect () {
    $('.ftr').remove()
    $('.ppl').remove()
  }
  function processQuery (query) {
    resultList.html('')
    if (query === '') {
      resultList.append($('<div class="prompt">Search for something...</div>'))
      $('.help').css({display: ''})
      clearNonDirect()
      return
    }
    $('.help').css({display: 'none'})
    if (query.match(/^\d{2,4}$/)) {
      var results = 0
      for (var i = 0; i < CIESubjects.length; i++) {
        (function (subj) {
          if (subj.id.substr(0, query.length) === query) {
            var subjElem = $('<div class="subject"></div>')
            subjElem.append($('<span class="level"></span>').text(subj.level))
            subjElem.append($('<span class="id"></span>').text(subj.id))
            subjElem.append(': ')
            subjElem.append($('<span class="name"></span>').text(subj.name))
            resultList.append(subjElem)
            subjElem.click(function () {
              queryBox.val(subj.id + ' ')
              queryBox.focus()
              processQuery(subj.id)
            })
            results++
          }
        })(CIESubjects[i])
        if (results >= 5) {
          clearNonDirect()
          return
        }
      }
    } else {
      var results = 0
      for (var i = 0; i < CIESubjects.length; i ++) {
        (function (subj) {
          if (subj.name.toLowerCase().indexOf(query.toLowerCase()) >= 0) {
            var subjElem = $('<div class="subject"></div>')
            subjElem.append($('<span class="level"></span>').text(subj.level))
            subjElem.append($('<span class="name"></span>').text(subj.name))
            subjElem.append(': ')
            subjElem.append($('<span class="id"></span>').text(subj.id))
            resultList.append(subjElem)
            subjElem.click(function () {
              queryBox.val(subj.id + ' ')
              queryBox.focus()
              processQuery(subj.id)
            })
            results++
          }
        })(CIESubjects[i])
        if (results >= 5) {
          clearNonDirect()
          return
        }
      }
    }

    $('.ppl').css({opacity: 0.5})
    var ppResult = $('<div class="ppl"></div>')
    function fetchPP (subj, time, paper, variant, type) {
      var queryBody = {}
      if (subj) {
        queryBody.subject = subj
      }
      if (time) {
        queryBody.time = time
      }
      if (paper) {
        queryBody.paper = paper
      }
      if (variant) {
        queryBody.variant = variant
      }
      if (type) {
        queryBody.type = type
      }
      $.ajax({
        url: '/search/pp/',
        method: 'get',
        cache: true,
        dataType: 'json',
        data: queryBody,
        success: function (data, s, jqx) {
          if (queryBox.val().trim() !== query) {
            return
          }
          $('.ppl').remove()

          var ppMaps = {}
          for (var i = 0; i < data.length; i++) {
            (function (doc) {
              var pName = paperName(doc)
              var rs
              if (!ppMaps[pName]) {
                rs = $('<div class="pp"></div>')
                rs.append($('<span class="name"></span>').text(pName))
                ppMaps[pName] = rs
                ppResult.append(rs)
              } else {
                rs = ppMaps[pName]
              }
              rs.append($('<span class="type"></span>').text(getTypeString(doc.type)).click(function () {
                window.open('https://file.schsrch.xyz/' + doc._id)
              }))
            })(data[i])
          }

          $('.pplContainer').append(ppResult)
        }
      })
    }

    if (query.match(/^\d{4}$/)) {
      fetchPP(query, null, null, null, null)
    }
    var match
    if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})$/))) {
      fetchPP(match[1], match[2])
    } else if ((match = query.match(/^(\d{4})[_ ](\d)$/))) {
      fetchPP(match[1], null, match[2], null, null)
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]+)$/))) {
      fetchPP(match[1], null, null, null, match[2])
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ]([a-z]+)$/))) {
      fetchPP(match[1], match[2], null, null, match[3])
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ]*(paper[_ ]*)?(\d)$/))) {
      fetchPP(match[1], match[2], match[4], null, null)
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ]*(paper[_ ]*)?(\d)(\d)$/))) {
      fetchPP(match[1], match[2], match[4], match[5], null)
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ]([a-z]+)[_ ](\d)(\d)$/))) {
      fetchPP(match[1], match[2], match[4], match[5], match[3])
    } else if ((match = query.match(/^(\d{4})[_ ]([a-z]\d{2})[_ ]([a-z]+)[_ ](\d)$/))) {
      fetchPP(match[1], match[2], match[4], null, match[3])
    } else {
      $('.ppl').remove()
    }

    $('.ftr').css({opacity: 0.5})
    var fullTextResults = $('<div class="ftr"></div>')
    setTimeout(function () {
      if (queryBox.val().trim() !== query) {
        return
      }
      $.ajax({
        url: '/search/fullText/' + encodeURIComponent(query) + '/',
        method: 'get',
        cache: true,
        dataType: 'json',
        success: function (data, s, jqx) {
          if (queryBox.val().trim() !== query) {
            return
          }
          $('.ftr').remove()

{'fraction_non_alphanumeric': 0.2147483417869684, 'fraction_numerical': 0.009051892313694888, 'mean_word_length': 1.8664728248713933, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '1326547', 'n_tokens_mistral': 4373, 'n_tokens_neox': 4318, 'n_words': 802}