<commit_before>#!/usr/bin/ruby 

# Example Usage
# ./render.rb -t AODNParameterVocabulary.erb 


require 'erb'
require 'pg'
require 'optparse'


def map_query( conn, query, args, &code )
  # map a proc/block over postgres query result
  xs = []
  conn.exec( query, args ).each do |row|
    xs << code.call( row )
  end
  xs
end


class RDFBinding

  def initialize( conn, date, os)
    @conn = conn
    @date = date
    @os = os
  end

  # we are going to need a more general sql, to limit everything
  # to the general parameter scheme

  # change name sql_query_returning_subject ?

  def query_sql_subject( query, args )
    map_query( @conn, query, args) do |row|
      row['subject']
    end
  end

#   inline exapanded queries don't seem to run any faster
#
#   def query_rdf_objects( predicate, subject )
#     map_query( @conn, "select object from _rdf where predicate = $$#{predicate}$$ and subject = $$#{ subject }$$", nil) do |row|
#       row['object']
#     end
#   end
# 
#   def query_rdf_subjects( predicate, object )
#     map_query( @conn, "select subject from _rdf where predicate = $$#{predicate}$$ and object = $$#{object}$$", nil) do |row|
#       row['subject']
#     end
#   end

  def query_rdf_objects( predicate, subject )
    map_query( @conn, 'select object from _rdf where predicate = $1 and subject = $2',
    [predicate, subject]) do |row|
      row['object']
    end
  end

  def query_rdf_subjects( predicate, object )
    map_query( @conn, 'select subject from _rdf where predicate = $1 and object = $2',
    [predicate, object]) do |row|
      row['subject']
    end
  end

  def render( filename )
    # pass a filename here in place of an already opened stream
    # to simplify use when calling recursively from nested templates

    [ filename, "templates/#{filename}"].each do |path|
      if File.exists?( path)
        # puts "opening #{path}"
        result = ERB.new( File.read(path)).result(binding)
        # result = ERB.new(@template, nil, '>').result(binding)
        result = result.gsub /^[ \t]*$\n/, ''
        @os.puts result
        break
      end
    end
  end

end



options = {}

OptionParser.new do |opts|
  opts.banner = "Usage: example.rb [options]"
  opts.on('-t', '--templatefile NAME', 'templatefile') { |v| options[:template_file] = v }
  opts.on('-h', '--host NAME', 'host')          { |v| options[:host] = v }<commit_msg>tidy exception handling<commit_after>#!/usr/bin/ruby 

# Example Usage
# ./render.rb -t AODNParameterVocabulary.erb 


require 'erb'
require 'pg'
require 'optparse'


def map_query( conn, query, args, &code )
  # map a proc/block over postgres query result
  xs = []
  conn.exec( query, args ).each do |row|
    xs << code.call( row )
  end
  xs
end


class RDFBinding

  def initialize( conn, date, os)
    @conn = conn
    @date = date
    @os = os
  end

  def query_sql_subject( query, args )
    # supports composing general sql queries and returns the subject row
    map_query( @conn, query, args) do |row|
      row['subject']
    end
  end

#   inline exapanded queries don't seem to run any faster
#
#   def query_rdf_objects( predicate, subject )
#     map_query( @conn, "select object from _rdf where predicate = $$#{predicate}$$ and subject = $$#{ subject }$$", nil) do |row|
#       row['object']
#     end
#   end
# 
#   def query_rdf_subjects( predicate, object )
#     map_query( @conn, "select subject from _rdf where predicate = $$#{predicate}$$ and object = $$#{object}$$", nil) do |row|
#       row['subject']
#     end
#   end

  def query_rdf_objects( predicate, subject )
    map_query( @conn, 'select object from _rdf where predicate = $1 and subject = $2',
    [predicate, subject]) do |row|
      row['object']
    end
  end

  def query_rdf_subjects( predicate, object )
    map_query( @conn, 'select subject from _rdf where predicate = $1 and object = $2',
    [predicate, object]) do |row|
      row['subject']
    end
  end

  def render( filename )
    # pass a filename here in place of an already opened stream
    # to simplify use when calling recursively from nested templates

    processed = false
    [ filename, "templates/#{filename}"].each do |path|
      if File.exists?( path)
        # puts "opening #{path}"
        result = ERB.new( File.read(path)).result(binding)
        # result = ERB.new(@template, nil, '>').result(binding)
        result = result.gsub /^[ \t]*$\n/, ''
        @os.puts result
        processed = true
        break
      end
    end
    if !processed
      raise "Could not find template '#{filename}'!"
    end
  end

end



options = {}

OptionParser.new do |opts|
  opts.banner = "Usage: example.rb [options]"
  opts.on('-t', '--templatefile NAME', 'templatefile') { |v| options[:template_file] = v }
  opts.on('-h', '--host NAME', 'host')          { |v| options[:host] = v }
{'fraction_non_alphanumeric': 0.1340909090909091, 'fraction_numerical': 0.001652892561983471, 'mean_word_length': 2.8821170809943863, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '6142013', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1567, 'n_words': 517}