<commit_before>-- some miscellaneous helper functions

local M = {}

function M.fill_template(template, dict)
  local function adjust_cond(s)
    return string.gsub(tostring(s),"^{\n?",""):gsub("\n?}$","")
  end
  local function conditional(test,yes,no)
    local cond = dict[test]
    if cond and not (type(cond) == "table" and #cond == 0) then
      return adjust_cond(yes)
    else -- count 0-length array as false
      return (no == nil and "") or adjust_cond(no)
    end
  end
  local function adjust_for(s)
    return adjust_cond(s):gsub("%b[]$","")
  end
  local function forloop(var,ary,contents)
    if not (dict[ary] and type(dict[ary]) == "table") then
      return ""
    end
    local items = dict[ary]
    local cont = adjust_for(contents)
    local result = ""
    local between = contents:match("%b[]}$")<commit_msg>Added comment.<commit_after>-- some miscellaneous helper functions

local M = {}

-- Templates recognize the following constructs:
-- * ${var} - gets filled with the value of dict[var]
-- * $[if foo]{yes}{no} - yes if foo is true and not an empty array.
--   The {no} part may be omitted.
-- * $[for x in foo]{blah ${x}[, ]} - prints blah ${...} for every
--   value of foo, interposing ", ".  The interposed part may be
--   omitted.
function M.fill_template(template, dict)
  local function adjust_cond(s)
    return string.gsub(tostring(s),"^{\n?",""):gsub("\n?}$","")
  end
  local function conditional(test,yes,no)
    local cond = dict[test]
    if cond and not (type(cond) == "table" and #cond == 0) then
      return adjust_cond(yes)
    else -- count 0-length array as false
      return (no == nil and "") or adjust_cond(no)
    end
  end
  local function adjust_for(s)
    return adjust_cond(s):gsub("%b[]$","")
  end
  local function forloop(var,ary,contents)
    if not (dict[ary] and type(dict[ary]) == "table") then
      return ""
    end
    local items = dict[ary]
    local cont = adjust_for(contents)
    local result = ""
    local between = contents:match("%b[]}$")
{'fraction_non_alphanumeric': 0.15495049504950495, 'fraction_numerical': 0.0019801980198019802, 'mean_word_length': 3.346236559139785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2310140', 'n_tokens_mistral': 661, 'n_tokens_neox': 649, 'n_words': 204}