<commit_before>require 'redditkit'
require 'pry'

Header_regex = "^(\d+)?[dD](\d+)(.*)"
Line_regex = "^(\d+)(\s*-+\s*\d+)?(.*)"

class Tables
  def initialize(text)
    @text = text
    @tables = []
    parse
  end
end

class Table
  def initialize(text)
    @text = text
    @die = nil
    @header = ""
    @outcomes = []
    @is_inline = false
    parse
  end

  def parse
    lines = @text.split("\n")
    head = lines.shift
    head = head.gsub(/[[:punct:]]/, '')
    head_match = /#{Header_regex}/.match(head.strip)
    if head_match then
      @die = head_match[2].to_i
      @header = head_match[3]
    end
    lines.each { |l|
      l = l.gsub(/[[:punct:]]/, '')
      ti = /#{Line_regex}/.match(l.strip)
      if ti then
        @outcomes << TableItem.new(ti.to_s)
      end
    }
  end
end

class TableItem
  def initialize(text)
    @text = text
    @inline_table = nil
    @outcome = ""
    @weight = 0
    parse
  end

  def parse
    main_regex = /#{Line_regex}/.match(@text)
    binding.pry
    if not main_regex then
      return
    end
   @outcome = main_regex[3]

    if not main_regex[2] then
      @weight = 1
    # else
    #   start = main_regex[1].to_i
    #   stop = main_regex[2].to_i
    #   @weight = stop - start + 1
    # rescue
    #   @weight = 1
    end
    #if /[dD]\d+/.match(@outcome)
    
  end
end


File.open('barbarians', 'r') { |f|
  a = f.read()
  tb = Table.new(a)
  binding.pry
}<commit_msg>WIP: Fixed regex issues, separated tables<commit_after>require 'redditkit'
require 'pry'

Header_regex = /^(\d+)?[dD](\d+)(.*)/
Line_regex = /^(\d+)(\s*-+\s*\d+)?(.*)/

class Tables
  def initialize(text)
    @text = text
    @tables = []
    parse
  end

  def parse
    indicies = []
    lines = @text.split("\n")
    lines.reject! {|l| l.nil? || l.strip.empty?}
    lines.map.with_index { |line, i|
      line = line.gsub(/[[:punct:]]/, '')
      line_match = Header_regex.match(line.strip)
      if line_match then
        indicies << i
      end
    }
    return 0 unless indicies.length != 0

    table_text = []
    (0..indicies.length-1).each { |i|
      table_text << "#{lines[indicies[i]..indicies[i+1]]}\n"
      binding.pry
    }
    table_text << "#{lines[indicies[-1]..indicies.length]}\n"
    binding.pry
  end
end

class Table
  def initialize(text)
    @text = text
    @die = nil
    @header = ""
    @outcomes = []
    @is_inline = false
    parse
  end

  def parse
    lines = @text.split("\n")
    head = lines.shift
    head = head.gsub(/[[:punct:]]/, '')
    head = head.gsub(/[[:space:]]|\t/, '')
    head_match = Header_regex.match(head.strip)
    if head_match then
      @die = head_match[2].to_i
      @header = head_match[3]
    end
    lines.each { |l|
      l = l.gsub(/[[:punct:]]/, '')
      ti = Line_regex.match(l.strip)
      if ti then
        @outcomes << TableItem.new(ti.to_s)
      end
    }
  end
end

class TableItem
  def initialize(text)
    @text = text
    @inline_table = nil
    @outcome = ""
    @weight = 0
    parse
  end

  def parse
    main_regex = Line_regex.match(@text)
    binding.pry
    if not main_regex then
      return
    end
   @outcome = main_regex[3]

    if not main_regex[2] then
      @weight = 1
    # else
    #   start = main_regex[1].to_i
    #   stop = main_regex[2].to_i
    #   @weight = stop - start + 1
    # rescue
    #   @weight = 1
    end
    #if /[dD]\d+/.match(@outcome)

  end
end




File.open('barbarians', 'r') { |f|
  a = f.read()
  tb = Tables.new(a)
  binding.pry
}
{'fraction_non_alphanumeric': 0.16676201372997712, 'fraction_numerical': 0.007437070938215103, 'mean_word_length': 2.141958670260557, 'pattern_counts': {'":': 0, '<': 13, '<?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': '4562373', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1347, 'n_words': 286}