<commit_before>google.load("visualization", "1", {packages:["corechart"]})
re = RegExp(/^\d+/mg)
last_data = null
last_width = null

configs = [
    { title: "0-100, by 10", range: 100, interval: 10 }
    { title: "0-100, by 5",  range: 100,  interval: 5 }
    { title: "0-100, by 25 [slots]",  buckets:[0,25,50,75,100] }
    { title: "0-10 [slots]",  buckets:[0..10] }
    { title: "0-5 [slots]",  buckets:[0..5] }
]

active_config = configs[0]


populate_select = (config_id, config_list)->
    select = $("#"+config_id)
    select.append("<option value=\"#{i}\">#{cfg.title}</option>") for cfg,i in config_list

calc = (force)->
    data = document.getElementById("data").value
    if !force and data == last_data and last_width == window.innerWidth
        return

    last_data = data
    last_width = window.innerWidth
    ratings_raw = data.match(re)
    if ratings_raw?
        ratings = _.map(ratings_raw, (v)->parseInt(v))
    else
        ratings = []

    graph(ratings)

stratify = (rankings)->
    if active_config.range?
        range = active_config.range
        interval = active_config.interval
        num_buckets = range / interval
        labels = ("#{x - (if x > interval then (interval-1) else interval)}-#{x}" for x in [interval..range] by interval)
    else
        num_buckets = active_config.buckets.length
        labels = ("#{x}" for x in active_config.buckets)

    values = Array.apply(null, new Array(num_buckets)).map(Number.prototype.valueOf,0)
    _.each rankings, (v)->
        if range?
            b = if v > 0 then Math.floor((v-1)/interval) else 0
        else
            b = v
        values[b] += 1

    merge = _.zip(labels, values)
    merge.unshift(["Ranking", "Count"])
    return merge

graph = (rankings)->
    merge = stratify(rankings, 100, 20)
    data = google.visualization.arrayToDataTable(merge)

    options =
          hAxis: {title: 'Rating'}
          vAxis: {title: 'Number of ratings'}
          legend: { position: "none" }

    chart = new google.visualization.ColumnChart(document.getElementById('graph'))
    chart.draw(data, options)

config_chg = ()->
    sel = document.getElementById("config_list")
    active_config = configs[sel.selectedIndex]
    calc(true)

populate_select("config_list", configs)
$("#config_list").change(config_chg)
setInterval(calc, 1000)<commit_msg>Bucket handling is bloated but works<commit_after>google.load("visualization", "1", {packages:["corechart"]})
re = RegExp(/^\d+/mg)
last_data = null
last_width = null

configs = [
    { title: "0-100, by 10", range: 100, interval: 10 }
    { title: "0-100, by 5",  range: 100,  interval: 5 }
    { title: "0-100, by 25 [slots]",  buckets:[0,25,50,75,100] }
    { title: "0-10 [slots]",  buckets:[0..10] }
    { title: "0-5 [slots]", buckets:[0..5] }
]

active_config = configs[0]

prepare_configs = (configs)->
    _.each configs, (cfg)->
        if(cfg.range? and cfg.buckets?) or (cfg.range? and not cfg.interval?)
            throw("Error in " + cfg.title)
        if cfg.buckets?
            cfg.continuous = (_.last(cfg.buckets) - _.first(cfg.buckets)) == cfg.buckets.length - 1
        if cfg.buckets and not cfg.continuous
            cfg.inv_buckets = []
            for v, i in cfg.buckets
                cfg.inv_buckets[v] = i

        console.log(cfg)

populate_select = (config_id, config_list)->
    select = $("#"+config_id)
    select.append("<option value=\"#{i}\">#{cfg.title}</option>") for cfg,i in config_list

calc = (force)->
    data = document.getElementById("data").value
    if !force and data == last_data and last_width == window.innerWidth
        return

    last_data = data
    last_width = window.innerWidth
    ratings_raw = data.match(re)
    if ratings_raw?
        ratings = _.map(ratings_raw, (v)->parseInt(v))
    else
        ratings = []

    graph(ratings)

stratify = (rankings)->
    if active_config.range?
        range = active_config.range
        interval = active_config.interval
        num_buckets = range / interval
        labels = ("#{x - (if x > interval then (interval-1) else interval)}-#{x}" for x in [interval..range] by interval)
    else
        num_buckets = active_config.buckets.length
        labels = ("#{x}" for x in active_config.buckets)

    values = Array.apply(null, new Array(num_buckets)).map(Number.prototype.valueOf,0)
    _.each rankings, (v)->
        if range?
            b = if v > 0 then Math.floor((v-1)/interval) else 0
        else
            if active_config.continuous
                b = v
            else
                b = active_config.inv_buckets[v]
        values[b] += 1

    merge = _.zip(labels, values)
    merge.unshift(["Ranking", "Count"])
    return merge

graph = (rankings)->
    merge = stratify(rankings, 100, 20)
    data = google.visualization.arrayToDataTable(merge)

    options =
          hAxis: {title: 'Rating'}
          vAxis: {title: 'Number of ratings'}
          legend: { position: "none" }

    chart = new google.visualization.ColumnChart(document.getElementById('graph'))
    chart.draw(data, options)

config_chg = ()->
    sel = document.getElementById("config_list")
    active_config = configs[sel.selectedIndex]
    calc(true)

prepare_configs(configs)
populate_select("config_list", configs)
$("#config_list").change(config_chg)
setInterval(calc, 1000)
{'fraction_non_alphanumeric': 0.14840525328330206, 'fraction_numerical': 0.023827392120075046, 'mean_word_length': 2.797008547008547, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, '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': '554484', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1738, 'n_words': 407}